Compare commits

..
733 Commits
Author SHA1 Message Date
Wu Clan 123a44aed0 Ignore AI Buddy downloaded static files (#1262) 2026-09-20 11:01:09 +08:00
Wu Clan 9ec613eddc Rename task scheduler enabled field to status (#1261) 2026-09-20 10:48:48 +08:00
Wu Clan 8d9a79dee4 Reuse Redis key strings in local variables (#1259) 2026-09-16 17:55:50 +08:00
Wu Clan 935c49487e Fix scheduler background task garbage collection (#1257) 2026-09-16 16:37:20 +08:00
Wu Clan 54a438c762 Fix snowflake initialization blocking the event loop (#1256) 2026-09-16 16:36:37 +08:00
Wu Clan 7ab3bb81dd Fix distributed reload lock timeout handling (#1255) 2026-09-16 16:36:04 +08:00
Wu Clan 2054c5bdc4 Fix rate limiter Redis bucket cache lock contention (#1254) 2026-09-16 16:35:46 +08:00
Wu Clan 110a9c2e0c Refactor token session index and online status (#1253)
* Optimize Redis connection pool and batch operations

* Refactor token session index and online status
2026-09-16 16:31:24 +08:00
Wu Clan c5c043052c Optimize Redis connection pool and batch operations (#1252) 2026-09-16 16:28:35 +08:00
Wu Clan c5f6362e67 Update test token fixture to logout after use (#1251) 2026-09-16 10:47:06 +08:00
Wu Clan cd0d1f3ab8 Update code generator import order (#1250) 2026-09-16 10:47:01 +08:00
Wu Clan d47534ad8b Update cursor pagination page size params (#1249) 2026-09-16 10:46:57 +08:00
Wu Clan 315f8a55cc Update admin and task naming conventions (#1246) 2026-09-07 10:42:15 +08:00
Wu Clan 5a658c44f8 Update oauth2 plugin README and service docstring (#1245) 2026-09-07 10:36:42 +08:00
Wu Clan 5ae7afaf29 Update email plugin README configuration (#1244) 2026-09-07 10:36:25 +08:00
Wu Clan ae6d165e98 Update code generator plugin naming to CodeGen prefix (#1243) 2026-09-07 10:35:39 +08:00
Wu Clan e5aa1ccea7 Update code generator plugin menu SQL paths (#1242) 2026-09-06 23:01:29 +08:00
Wu Clan ee553f8b67 Update frontend plugin installer suffix handling (#1241) 2026-09-06 22:30:37 +08:00
jerryliu44 a38bd0b4a6 Fix correct multi-login permission check (#1240) 2026-08-28 10:19:29 +08:00
Wu Clan b58cca4788 Add claude.uy sponsor to the README and CLI (#1238) 2026-08-21 18:17:57 +08:00
Wu Clan 6149d0472f Update the changelog for v1.15.1 (#1236) 2026-08-16 16:47:36 +08:00
Wu Clan a0b3f022fe Update the version number to 1.15.1 (#1235) 2026-08-16 16:25:56 +08:00
Wu Clan 69001f2745 Bump ruff version for RUF201 rule (#1234) 2026-08-16 16:22:22 +08:00
Wu Clan ec6e1cdfd1 Optimize config of multiple database sources (#1233) 2026-08-16 15:55:12 +08:00
Wu Clan 7568275126 Add multiple database data source config (#1232)
* Add multiple database data source config

* Fix comments

* Fix deploy env
2026-08-14 17:44:45 +08:00
Wu Clan 67a6a58e57 Bump dependencies and pre-commits (#1231) 2026-08-13 11:09:42 +08:00
Wu Clan 8e5c379853 Bump dependencies and pre-commits (#1228)
* Bump dependencies and pre-commits

* Fix lint
2026-07-21 15:19:20 +08:00
Wu Clan 2dffe8f6a8 Update the changelog for v1.15.0 (#1226) 2026-07-10 18:01:52 +08:00
Wu Clan c802f67a5a Update the version number to 1.15.0 (#1225) 2026-07-10 17:58:49 +08:00
Wu Clan 1916261795 Update plugin config format validation (#1224) 2026-07-10 17:55:24 +08:00
Wu Clan 325016adc8 Update xdb database file to release (#1223) 2026-07-10 09:53:36 +08:00
Wu Clan 09fd380c3a Refactor plugin dynamic config loading (#1222) 2026-07-09 17:20:01 +08:00
IAseven e6292747bd Fix duplicate log reporting in OTEL (#1221) 2026-07-08 15:57:43 +08:00
Wu Clan a96b2cfad1 Update document link addresses in the CLI (#1220) 2026-07-08 13:55:22 +08:00
Wu Clan a0a1bde88c Update capability plugin config validation (#1219) 2026-07-08 11:48:51 +08:00
9251a5d5fd Add support for capability plugins (#1217)
* feat(plugin): support capability plugins

Add capability plugin metadata and parsing so plugins without app/api sections can participate in dependency ordering and hooks without being routed as app plugins.

Also rebuild missing plugin status cache from the normalized plugin config when a route dependency checks plugin state.

* refactor code

---------

Co-authored-by: linwu@onemt.com.cn <linwu@onemt.com.cn>
Co-authored-by: Wu Clan <jianhengwu0407@gmail.com>
2026-07-08 11:19:20 +08:00
Wu Clan e3d2c68592 Bump dependencies and pre-commits (#1218) 2026-07-06 14:46:29 +08:00
Wu Clan 4d6970c6cc Add is superuser protocol to ctx (#1216) 2026-07-03 13:40:58 +08:00
Wu Clan d4f78d6448 Fix JWT auth dependency error propagation (#1215) 2026-07-02 16:20:26 +08:00
Wu Clan efec3308df Add underlying security config for JWT (#1214) 2026-07-02 14:42:02 +08:00
Wu Clan 0441907b00 Fix redis rate limiter bucket routing (#1212)
* Fix redis rate limiter bucket routing

* Add rate limiter function docstrings

* Optimize Redis rate limiter time source
2026-06-29 14:06:57 +08:00
Wu Clan 08ee3878c1 Refactor cache key and prefix APIs (#1210) 2026-06-25 14:19:37 +08:00
Wu Clan c7e686588e Add AI dynamic configuration menu (#1209) 2026-06-24 11:42:53 +08:00
Wu Clan 495046c978 Fix redis pubsub socket timeout (#1206) 2026-06-20 23:58:29 +08:00
Wu Clan ffaa1282cc Bump dependencies and pre-commits (#1205) 2026-06-19 22:47:33 +08:00
Wu Clan 15dda9ab6e Refactor AI response regeneration 2026-06-16 15:10:38 +08:00
Wu Clan 814068a9e6 Add plugin deps command to the CLI (#1200)
* Add plugin deps command to the CLI

* Add dependency sync command to the CLI

* Fix plugin dependency index fallback
2026-06-16 11:25:53 +08:00
IAseven 39cdfdfb00 Fix user salt column LargeBinary length (#1197) 2026-06-03 17:11:32 +08:00
Wu Clan 4b730099cd Optimize plugin core and add otel hooks (#1196) 2026-06-01 18:15:59 +08:00
Wu Clan 974972b490 Add celery explicit idempotent protection (#1195) 2026-06-01 18:15:31 +08:00
Wu Clan c9b4678148 Update the changelog for v1.14.0 (#1194) 2026-05-30 16:04:53 +08:00
Wu Clan d20bc91465 Update the version number to 1.14.0 (#1193) 2026-05-30 16:02:03 +08:00
Wu Clan bc8d1ab9e3 Fix task scheduler unpack fields (#1192) 2026-05-30 15:54:12 +08:00
Wu Clan a5e9a0e805 Refactor physical deletion to logical deletion (#1191)
* Refactor physical deletion to logical deletion

* Update the usage of unique indexes

* Update scheduler update and delete

* Fix lint

* Add some missing logic

* Optimize codegen get by id
2026-05-30 15:02:00 +08:00
Wu Clan 1897b3198e Optimize the usage of setex for redis (#1190) 2026-05-29 11:14:47 +08:00
Wu Clan 6b9c7d81de Bump dependencies and pre-commits (#1189) 2026-05-29 11:00:32 +08:00
Wu Clan e9b83d3c93 Optimize Grafana observability metrics and config (#1188)
* Optimize Grafana observability metrics and config

* Update docker images
2026-05-29 09:41:50 +08:00
Wu Clan 0f0e94c9b1 Add opera log task cancel to lifespan (#1187) 2026-05-28 11:44:09 +08:00
Wu Clan ba0f30cde1 Optimize operation log truncation handling (#1186) 2026-05-28 11:13:37 +08:00
Wu Clan 9f3673d812 Optimize user session logging and deletion (#1185) 2026-05-28 10:25:43 +08:00
Wu Clan ef9a6b554c Fix cache invalidation decorator usage (#1182) 2026-05-26 16:14:47 +08:00
Wu Clan dbfdc6762e Optimize SQL script execution in the CLI (#1181) 2026-05-25 11:06:06 +08:00
Wu Clan 2e86d79150 Optimize some global variable definitions (#1178) 2026-05-15 11:55:22 +08:00
Wu Clan 5352f98c13 Optimize database operations within loops (#1177)
* Optimize database operations within loops

* Optimize opera log
2026-05-14 11:38:38 +08:00
Wu Clan e32b4232c5 Update xdb dependency and database file (#1171) 2026-05-07 13:42:31 +08:00
Wu Clan 0731f38702 Update the changelog for v1.13.4 (#1170) 2026-04-28 18:07:19 +08:00
Wu Clan 7ebcdda26f Update the version number to 1.13.4 (#1169) 2026-04-28 18:03:05 +08:00
Wu Clan f32f7e56a2 Bump dependencies and pre-commits (#1168) 2026-04-28 17:57:14 +08:00
Wu Clan 330639d975 Fix typing warnings for return methods (#1167) 2026-04-28 16:42:23 +08:00
Wu Clan 0fa5468483 Fix multi level cache storage logic (#1166) 2026-04-27 17:54:09 +08:00
Wu Clan bfe4aa91ce Add uninstall plugin operation checks (#1165)
* Add uninstall plugin operation checks

* Fix lint
2026-04-27 17:36:00 +08:00
Wu Clan 5d85bc98d2 Optimize the core code ordering of plugins (#1164) 2026-04-27 09:51:18 +08:00
Wu Clan 8f90f764b6 Fix the add plugin CLI command config (#1163) 2026-04-26 23:18:40 +08:00
Wu Clan 49d12f7825 Add the snowflake ID enabled config (#1162) 2026-04-26 21:56:28 +08:00
Wu Clan b60c9ab17c Fix Grafana tempo and observability config (#1161)
* Add database and queue observability

* Update prometheus

* Delete the wrong changes

* Fix Grafana tempo and observability config
2026-04-26 13:09:47 +08:00
Wu Clan 576b2fc5d0 Add database and queue observability (#1160)
* Add database and queue observability

* Update prometheus

* Delete the wrong changes
2026-04-26 13:07:03 +08:00
Wu Clan 35a368ae04 Update Grafana dashboard and datasource config (#1159) 2026-04-21 18:02:37 +08:00
Wu Clan 776482c5d6 Update plugin README content format (#1158)
* Update plugin README content format

* Fix lint
2026-04-19 21:36:57 +08:00
Toby WongandWu Clan 92f182cd09 Add plugin depends on and lifecycle ordering (#1153)
* feat: add priority support to LifespanManager.register

* feat: support lifespan stages and plugin dependencies

* Optimize implementations

* Fix dependency resolution

* Fix the depends on type

---------

Co-authored-by: Wu Clan <jianhengwu0407@gmail.com>
2026-04-17 16:54:48 +08:00
Wu Clan 0428a2b07d Bump dependencies and pre-commits (#1156) 2026-04-14 23:58:51 +08:00
Wu Clan 9f193c7e7c Fix mysql enum data type mismatch (#1155) 2026-04-14 23:03:15 +08:00
Wu Clan 4e0ca6a214 Remove cleanup operations after plugin install (#1154)
* Remove cleanup operations after plugin install

* Fix lint
2026-04-13 15:09:45 +08:00
IAseven 45842d917a Fix app pytest client fixture scope (#1152)
* 🐞 fix:修复测试模块client scope=module时异常

* 🌈 style:

* 🌈 style:
2026-04-12 23:43:37 +08:00
Wu Clan 8719c63424 Update the architecture description in README (#1150) 2026-04-08 21:53:10 +08:00
Wu Clan d71db7d58d Update the changelog for v1.13.3 (#1149) 2026-04-08 21:05:52 +08:00
Wu Clan 02b5e670cc Update the version number to 1.13.3 (#1148) 2026-04-08 20:45:21 +08:00
Wu Clan 41f53d7225 Bump dependencies and pre-commits (#1147) 2026-04-08 20:44:59 +08:00
YuWorm b71179cea9 Add plugin system hook function mechanism (#1142)
* Enhance plugin system with lifespan registry, setup hooks, and patching utilities

* fix: remove registrar.py unused-import

* Optimize implementations

* Update function docs

* update replace_middleware func docs

* Recover deleted comments
2026-04-08 10:35:02 +08:00
Wu Clan 41a64bf64c Add superpowers to gitignore file (#1146) 2026-04-08 10:34:06 +08:00
Wu Clan afacc84da3 Fix custom email model return type (#1145) 2026-04-08 10:33:46 +08:00
Wu Clan 3bbe460beb Update custom reload filter call rules (#1144) 2026-04-08 09:10:19 +08:00
Wu Clan 131bfcf063 Add the installation frontend plugin CLI (#1141) 2026-04-03 09:51:19 +08:00
Wu Clan 283d762141 Refactor plugin dependency cache and check (#1140) 2026-04-02 17:00:20 +08:00
Wu Clan bd9737cc6e Fix the return of registered task results (#1139) 2026-04-02 13:56:29 +08:00
Wu Clan 6d381dba74 Fix current superuser permission validation (#1138) 2026-04-02 12:38:21 +08:00
Wu Clan 7e6856a5e0 Fix plugin dependencies for redis instance (#1133) 2026-03-30 14:37:31 +08:00
Wu Clan c96181bd08 Update plugin config and dependency cache (#1130)
* Update plugin config and dependency cache

* Fix deps check
2026-03-27 23:50:58 +08:00
Wu Clan 7a934fee32 Update the plugin requirements check (#1129) 2026-03-27 22:42:30 +08:00
Wu Clan 5bec0e083b Add custom cursor pagination support (#1128)
* Add custom cursor pagination support

* Update fastapi pagination dependency
2026-03-25 20:46:22 +08:00
Wu Clan a97dab1716 Update plugins and monitoring permissions (#1126) 2026-03-24 15:59:22 +08:00
Wu Clan 5ff7f4bc69 Fix code generation menu sql scripts (#1127) 2026-03-24 15:59:06 +08:00
Wu Clan 94f643c587 Fix the logic related to the status field (#1125) 2026-03-24 11:43:09 +08:00
Wu Clan c98ea57a94 Update the changelog for v1.13.2 (#1124) 2026-03-23 16:58:54 +08:00
Wu Clan a58ca7f156 Update the version number to 1.13.2 (#1123) 2026-03-23 16:56:42 +08:00
Wu Clan c08f64ae75 Add sql scripts for OAuth2 plugin (#1121)
* Add sql scripts for OAuth2 plugin

* Fix lint
2026-03-23 16:46:58 +08:00
Wu Clan 48352563ef Update plugin config and sql validate (#1122)
* Update plugin config and sql validate

* Fix lint
2026-03-23 16:44:44 +08:00
Wu Clan f87d8f43c8 Fix zip plugin installation and unzip (#1120)
* Fix zip plugin installation and unzip

* FIx lint
2026-03-22 20:49:53 +08:00
Wu Clan 568e3ca66b Fix plugin status change cache missing (#1118) 2026-03-21 23:34:40 +08:00
Wu Clan a9acc31a78 Fix online api user id acquisition (#1116) 2026-03-19 12:12:20 +08:00
Wu Clan 2866fc45b1 Fix typos in code and comments (#1115) 2026-03-19 11:50:43 +08:00
Wu Clan dcb68698e5 Fix user menu permission code update (#1114) 2026-03-19 11:50:20 +08:00
Wu Clan 06f9751c84 Fix rbac permission checking logic (#1113) 2026-03-19 11:50:04 +08:00
Wu Clan 23975442d8 Update plugin dynamic import check (#1112)
* Update plugin dynamic import check

* Update cli dynamic import
2026-03-19 11:49:33 +08:00
Wu Clan 577a13b271 Update refresh token geo-checksum response (#1111) 2026-03-19 11:48:42 +08:00
Wu Clan 05bb98dada Update user id context setting location (#1110)
* Update user id context setting location

* Update jwt middleware

* Fix lint
2026-03-19 11:48:11 +08:00
Wu Clan 4abe09dbc3 Replace the uvicorn runner with granian (#1105) 2026-03-09 21:59:00 +08:00
Wu Clan 2bedeef26c Fix log type check and default formatter (#1104) 2026-03-09 21:58:27 +08:00
Wu Clan 8efcc394cc Update the changelog for v1.13.1 (#1103) 2026-03-08 18:50:31 +08:00
Wu Clan 8fbd2c1ede Update the version number to 1.13.1 (#1102) 2026-03-08 18:48:26 +08:00
Wu Clan 19de14ffa6 Bump dependencies and pre-commits (#1100) 2026-03-08 13:12:56 +08:00
Wu Clan 2876f57ea4 Update utc zoneinfo to datetime timezone (#1099) 2026-03-07 14:23:10 +08:00
Wu Clan e9ed1292f0 Update project name symbol in multiple files (#1097) 2026-03-05 18:37:16 +08:00
Wu Clan 62ece2f289 Fix menu SQL in mysql SQL scripts (#1096) 2026-03-05 18:36:55 +08:00
Wu Clan 0bd41a9e67 Optimize the CLI command console output style (#1094)
* Optimize the CLI command console output style

* Update output functions

* Update destroy sql style
2026-03-05 18:36:44 +08:00
SmallGarbageandWu Clan e8ac9e848c Update the upload file type validation logic (#1091)
* Refactor file type validation logic

Refactor file type checks to allow valid formats and raise errors for unsupported types.

* Fix lint

---------

Co-authored-by: Wu Clan <jianhengwu0407@gmail.com>
2026-02-28 22:23:23 +08:00
Wu Clan 9698dde9c2 Add destroy SQL scripts for multiple plugins (#1093) 2026-02-28 22:23:07 +08:00
Wu Clan 39c3366e0a Refactor the menu SQL definition in SQL scripts (#1092)
* Refactor the menu SQL definition in SQL scripts

* Fix know issues
2026-02-28 20:08:27 +08:00
Wu Clan 6432a66732 Add remove plugin and formatting code CLI (#1088) 2026-02-25 21:22:49 +08:00
Wu Clan 1aa448796b Fix uninstall plugin requirements command (#1087) 2026-02-25 18:07:11 +08:00
Wu Clan db8fff0431 Fix query sorting based on model sort column (#1083) 2026-02-25 11:21:08 +08:00
Wu Clan 28113a4d52 Remove the sponsorship from the readme (#1085) 2026-02-24 21:11:48 +08:00
Wu Clan d843dfa1c7 Update SQL scripts related to data permissions (#1084) 2026-02-24 14:15:55 +08:00
Wu Clan 9e817db278 Add data permission rule value template variable (#1081)
* Add data permission rule value template variable

* Improved models and columns
2026-02-23 18:12:02 +08:00
Wu Clan 0dfe74d1a3 Add redis client native OTEL observability (#1082) 2026-02-23 15:15:59 +08:00
Wu Clan eb9399e14f Fix task issues and update crontab expressions (#1080) 2026-02-16 14:37:56 +08:00
Wu Clan 0c9eafbbb9 Update the changelog for v1.13.0 (#1078) 2026-02-14 13:13:27 +08:00
Wu Clan b412dceb9e Update the version number to 1.13.0 (#1079)
* Update the version number to 1.13.0

* Update changelog
2026-02-14 12:48:53 +08:00
Wu Clan 1de4201b52 Fix missing parent class init in base exception (#1077) 2026-02-13 15:08:58 +08:00
Wu Clan bcdaf3f3c9 Bump dependencies and pre-commits (#1076) 2026-02-13 14:44:57 +08:00
Wu Clan 565800d8cf Fix dynamic config cache serialization (#1075)
* Fix dynamic config cache serialization

* Fix lint
2026-02-13 14:30:38 +08:00
Wu Clan 966779bcd7 Refactor the interface rate limiter implementation (#1072)
* Refactor the interface rate limiter implementation

* Update deps
2026-02-11 17:27:46 +08:00
Wu Clan 3f06baffed Add cache key chaining syntax support (#1069) 2026-02-07 14:34:16 +08:00
Wu Clan 6de3eee4e0 Add version check and update workflows (#1068)
* Add version check and update workflows

* Fix lint
2026-02-07 13:14:45 +08:00
Wu Clan e227eb1768 Add multi level cache for dynamic config (#1067) 2026-02-06 15:19:05 +08:00
MicLon b6875a3c37 Fix captcha and user validation order logic error (#1065) 2026-02-06 14:08:07 +08:00
yzbf c521d2ef36 Add log mount to docker compose (#1063)
* Expose container logs via Docker Compose

* Simplify Docker logs mount

* Update comments
2026-02-06 14:02:40 +08:00
Wu Clan 4d457b2215 Fix multi level cache key build and usage (#1066)
* Fix multi level cache key build and usage

* Update build cache key
2026-02-06 13:42:13 +08:00
Wu Clan 95aebe1ceb Add tombi and plugin JSON schema config (#1062)
* Add tombi and plugin JSON schema config

* Add tombi lint
2026-02-05 14:47:20 +08:00
Wu Clan f5af411339 Add tombi toml and bump pre-commits (#1061) 2026-02-04 23:37:38 +08:00
Wu Clan d1fd5b6694 Update alembic and ruff config to pyproject.toml (#1060)
* Update alembic and ruff config to pyproject.toml

* Fix lint
2026-02-04 23:30:05 +08:00
Wu Clan ab1f60120a Optimize the serialization of join query results (#1058)
* Optimize the serialization of join query results

* Update some naming
2026-02-04 20:20:20 +08:00
SoulEater 220de51aad Fix relational insertion error for empty lists (#1056) 2026-02-03 21:29:53 +08:00
Wu Clan 646a0ec2fc Add granian and celery metrics collection (#1057)
* Add granian and celery metrics collection

* Update dashboards

* Add asyncio instrument

* Fix opera log finally
2026-02-03 20:44:05 +08:00
Wu Clan 6f1c27786d Add multi level caching and optimize caching (#1054)
* Add multi level caching and optimize caching

* optimize current caching

* improved decorators

* improved local

* add pub/sub

* fix serialize

* Improve pub and sub

* Add comment

* Fix lru_cache maxsize

* Fix lint

* Fix config warmup
2026-02-02 17:18:49 +08:00
Wu Clan 093788acea Update redis and server health monitor data (#1051)
* Update redis and server health monitor data

* Update uptime
2026-01-30 16:45:22 +08:00
Wu Clan 4bab3c2cb0 Update task path resolution and docker variables (#1050) 2026-01-30 10:55:39 +08:00
Wu Clan b82f9e8c1c Add the alembic related operation CLI (#1049) 2026-01-29 14:50:14 +08:00
Wu Clan 89f8fe141e Update plugin and code generation subprocess output (#1048) 2026-01-29 13:02:04 +08:00
Wu Clan ac19f8480f Update the plugin git url pattern (#1047) 2026-01-27 17:06:43 +08:00
Wu Clan d397a0d985 Fix code generation app route injection (#1042) 2026-01-26 21:27:30 +08:00
Wu Clan eb27b5bfed Add data validator for plugin config (#1041) 2026-01-26 21:17:18 +08:00
Wu Clan 08674ab95f Update plugins and adapt to new features (#1040)
* Update plugins and adapt to new features

* Update plugin versions
2026-01-25 20:23:08 +08:00
Wu Clan fbdc3f2dc5 Add code generation preview mode CLI command (#1036) 2026-01-25 15:17:04 +08:00
Wu Clan 19d8a2964f Remove deprecated parameter for code generation business (#1035)
* Remove deprecated parameter for code generation business

* Update doc_comment

* Fix doc_comment
2026-01-25 15:16:07 +08:00
Wu Clan a496932138 Add injection app routes to code generation (#1034)
* Add injection app routes to code generation

* Update get table sqls

* Update get table sqls

* Fix lint

* Fix sql scripts syntax
2026-01-25 15:14:01 +08:00
Wu Clan a6da0bfd41 Refactor code generation and plugin hot reloading (#1032)
* Refactor code generation and plugin hot reloading

* Improve implementation

* Update plugin config loader
2026-01-23 17:08:51 +08:00
Wu Clan ced2b8d269 Allow to add config in plugin toml (#1033) 2026-01-23 16:42:24 +08:00
Wu Clan f2b246089d Add router jinja template for code generation (#1031) 2026-01-23 10:39:47 +08:00
Wu Clan 0aa63ed6e6 Fix custom filename in code generation (#1030) 2026-01-22 16:33:37 +08:00
Wu Clan c8ebd1e9f6 Bump dependencies and pre-commits (#1029) 2026-01-22 15:55:33 +08:00
Wu Clan 68d962f2f8 Update the i18n file storage directory (#1027) 2026-01-21 12:03:40 +08:00
Wu Clan 1a6aba6105 Optimize code generation data processing (#1020)
* Optimize code generation data processing

* Add SQL script generation

* Update code gen table scripts

* Update types

* Fix model jinja and type conversion
2026-01-20 18:27:42 +08:00
Wu Clan b2785dd46f Fix snowflake type primary key field serializer (#1024) 2026-01-18 23:43:33 +08:00
Wu Clan dc73f09c55 Optimize i18n language file directory structure (#1023) 2026-01-18 16:16:37 +08:00
Wu Clan b4d984cb4e Optimize dynamic config loading implementation (#1022) 2026-01-18 16:01:59 +08:00
Wu Clan 4d19cd2f5a Rename locale to locales to avoid library conflict (#1021) 2026-01-18 15:01:43 +08:00
Wu Clan d9d0c839ed Fix database engine in auto init CLI command (#1018) 2026-01-16 13:41:14 +08:00
Wu Clan ca51c2f8c5 Fix install plugin dependencies in docker container (#1017) 2026-01-16 12:40:56 +08:00
Wu Clan 88b46c1b72 Fix database calls in init CLI command (#1016) 2026-01-15 20:03:53 +08:00
Wu Clan 383620c899 Update the database and Redis for easier scaling (#1015)
* Update the database and Redis for easier scaling

* Restore plugin redis variable naming
2026-01-15 16:54:43 +08:00
Wu Clan f876162456 Update the JWT for easier scaling (#1011)
* Update the JWT for easier scaling

* Fix comment
2026-01-14 23:52:29 +08:00
Wu Clan 362a559236 Update the changelog for v1.12.3 (#1009) 2026-01-13 19:50:54 +08:00
Wu Clan 8ae1a43581 Update i18n language storage and loading (#1008) 2026-01-13 19:10:41 +08:00
Wu Clan ee849f0854 Update the plugin dependency install method (#1007) 2026-01-13 16:14:41 +08:00
Wu Clan 5c9a27cc16 Update nickname generation when create user (#1004)
* Update nickname generation when create user

* Fix lint

* Update code style
2026-01-12 19:03:02 +08:00
Wu Clan 1b68854b84 Update code generation part file naming (#1002) 2026-01-11 18:03:20 +08:00
Wu Clan b73585ebd2 Optimize definitions of multiple utility functions (#1001)
* Optimize definitions of multiple utility functions

* Update plugin tools
2026-01-11 16:57:52 +08:00
Wu Clan 28a6228556 Update redis and server monitor implementations (#1000)
* Update redis and server monitor implementations

* Fix server disk information
2026-01-11 13:21:37 +08:00
Wu Clan dfce2ca094 Remove the opera log desensitization asynchronous (#999) 2026-01-10 18:30:16 +08:00
shj366 326bdf9a17 Fix opera log non-json data overload (#998)
* fix: skip non-json body logging for multipart uploads

* Update truncation implementation
2026-01-10 18:23:02 +08:00
Wu Clan cbe4e5ebe0 Update login log request header column length (#996) 2026-01-08 12:23:37 +08:00
Wu Clan 2d666e375f Update the changelog for v1.12.2 (#995) 2026-01-07 12:23:47 +08:00
Wu Clan ff40c41549 Remove Linux Do OAuth2 login (#994) 2026-01-07 12:21:21 +08:00
Wu Clan 7619670521 Fix the key of the refresh token removed (#993) 2026-01-07 12:09:10 +08:00
Wu Clan 85ba942f35 Fix SQL scripts error in config plugin (#991) 2026-01-06 14:30:49 +08:00
Wu Clan 88f3173f8f Fix operation log queue status management (#989)
* Fix operation log queue status management

* Fix lint
2026-01-06 10:53:23 +08:00
Wu Clan fd87dfb25a Remove invalid configs of operation log (#988) 2026-01-05 14:09:24 +08:00
Wu Clan ad7f725e4c Simplify the desensitization of operation log data (#987) 2026-01-05 12:16:51 +08:00
Wu Clan 2ef8810376 Fix environment variable file auto init (#985) 2026-01-04 12:20:23 +08:00
Wu Clan f0e6ed9985 Update the changelog for v1.12.1 (#983) 2025-12-31 15:36:25 +08:00
Wu Clan 809879997c Update git and docker ignore files (#981) 2025-12-30 12:20:33 +08:00
Wu Clan b153b7ac18 Add observability instrument for httpx request (#980) 2025-12-30 12:16:59 +08:00
Wu Clan 308ccf71ae Bump dependencies and pre-commits (#979) 2025-12-29 15:47:46 +08:00
Wu Clan c8f6d9da72 Add the auto init project CLI command (#978) 2025-12-28 14:12:31 +08:00
Wu Clan 3f2c3e44ba Optimize login log database session calls (#977) 2025-12-25 18:12:43 +08:00
Wu Clan b5e2a9e591 Fix case where the user agent was empty (#976) 2025-12-25 12:03:57 +08:00
Wu Clan cdabe91092 Add OTEL semantic specification metrics config (#973) 2025-12-24 10:49:25 +08:00
Wu Clan 69a59993e3 Add observability instrument for redis client (#972) 2025-12-20 19:25:34 +08:00
Wu Clan b6defc671e Fix i18n target language error when concurrent (#970) 2025-12-20 19:25:05 +08:00
Wu Clan 8cbea57a8a Add an independent contribution document (#971) 2025-12-19 15:20:34 +08:00
Wu Clan 7845e1ff49 Fix support for special character passwords (#968)
* Fix special character password support in celery

* Fix celery amqp and socketio password

* Remove non-essential changes
2025-12-19 14:47:26 +08:00
Wu Clan 2b76b64e85 Add code generation and notice SQL scripts (#966) 2025-12-16 19:55:01 +08:00
Wu Clan dd3165a082 Rename the pre start script to migrate (#965) 2025-12-16 19:54:12 +08:00
Wu Clan 407d12760b Update Grafana security and user default config (#964) 2025-12-16 14:39:21 +08:00
Wu Clan f8b244c3a9 Update the changelog for v1.12.0 (#963) 2025-12-15 17:11:29 +08:00
Wu Clan dfc715aad8 Update the version number to 1.12.0 (#962) 2025-12-15 17:07:06 +08:00
Wu Clan 4bc5ba53e6 Add the Grafana observability suite (#961)
* Add the Grafana observability suite

* Update configs

* Update docker script directory structure

* Fix the otel trace id

* Add grafana ini

* Fix some configs and loguru integration

* Add the celery grafana

* Update Grafana dashboards

* Update configs

* Fix issues with the panel

* Update grafana configs

* Update grafana dashboards

* Optimized panel styles

* Add sqlalchemy traces

* Fix the CORS

* Update the grafana query and config

* Update grafana status is off by default
2025-12-15 17:06:43 +08:00
曾果 6b4fd93e5f Fix the IP address in the request log (#959) 2025-12-15 10:07:43 +08:00
Wu Clan 0c5956f828 Fix CLI command for code generation (#956) 2025-12-07 15:14:52 +08:00
Wu Clan 122d17ab57 Update the init project database CLI to subcommand (#954) 2025-12-05 17:23:44 +08:00
Wu Clan 4fb14e480a Add CLI init project database support (#952)
* Add CLI init project database support

* Update get sql script

* Update init CLI contexts
2025-12-05 17:03:01 +08:00
Wu Clan ea4ac6ff41 Optimize the coupling of code generation CLI (#951) 2025-12-05 14:52:58 +08:00
Wu Clan bb5bcdf430 Add the database primary key mode config (#953)
* Add the database primary key mode config

* Update auto to autoincrement
2025-12-05 14:52:12 +08:00
Wu Clan aad9afa9bb Optimize the coupling of user social plugin (#950) 2025-12-04 11:56:37 +08:00
Wu Clan a0e6e12d85 Update pre-commit to prek in pre-commit script (#949) 2025-12-04 11:56:00 +08:00
Wu Clan 5d680ff93f Optimize data permission logic and usage (#947)
* Optimize data permission rules and usage

* Update get data permission models

* Update date permission filter

* Optimize the target model logic

* Upgrade dependencies to use latest features

* Remove model warnings

* Fix the latest feature issues

* Fix the sqlalchemy Table class import

* Fix the sqlalchemy Table class compatibility
2025-12-03 18:44:18 +08:00
Wu Clan 866b0e6ba4 Fix super value in update user permissions (#948) 2025-12-03 18:41:26 +08:00
Wu Clan 5a49d20c9b Update celery related docker container independence (#943)
* Update celery related docker container independence

* Fix lint

* Fix nginx conf
2025-11-28 18:33:32 +08:00
Wu Clan 7641d5993a Update the changelog for v1.11.2 (#942) 2025-11-28 12:31:05 +08:00
Wu Clan 07d66137cb Update the version number to 1.11.2 (#940) 2025-11-28 12:28:52 +08:00
Wu Clan f918dcd9c2 Update the fba run CLI command output (#941) 2025-11-28 12:28:18 +08:00
Wu Clan ed4e312cb1 Update code generation interface definitions (#939)
* Update code generation interface definitions

* Update more

* Resume irrelevant updates
2025-11-27 19:20:44 +08:00
Wu Clan 0b539f868e Update task application interface definitions (#938)
* Update the task execution interface definition

* Update more
2025-11-27 19:20:03 +08:00
Wu Clan 8899832de9 Update the files interface filename to file (#937) 2025-11-27 16:26:04 +08:00
Wu Clan d844aa323a Bump dependencies and pre-commits (#936) 2025-11-26 11:14:41 +08:00
Wu Clan ef640b0b5c Fix get column types in code generation (#935) 2025-11-25 18:07:09 +08:00
Wu Clan 4d43c6a3ea Update the i18n language file init location (#934) 2025-11-25 18:06:34 +08:00
Wu Clan 407c848145 Optimize the use of some LRU caches (#932) 2025-11-25 16:48:55 +08:00
Wu Clan d58260a0c0 Add env reqs for plugin install and uninstall (#929) 2025-11-21 17:18:27 +08:00
Dylan 16bcaf963d Add distributed deployment support for snowflake ID (#927)
* feat: Add support distributed deployment for Snowflake

* Update the algorithm implementation

* Remove duplicate codes and update error messages
2025-11-19 12:48:24 +08:00
Wu Clan 551dc51c52 Restore captcha uuid naming in the login params (#928) 2025-11-18 19:58:37 +08:00
Wu Clan 0729061438 Fix typos in data rule sql scripts (#926) 2025-11-18 19:33:24 +08:00
Wu Clan 636d867544 Update the changelog for v1.11.1 (#923) 2025-11-16 21:49:29 +08:00
Wu Clan cf9e5dc4f4 Update user and login security configs (#922)
* Update user and login security configs

* Optimize some code definitions

* Update config comments

* Update the captcha check

* Update the config plugin sql scripts

* Add user password history model to init

* Fix some logic errors

* Add last_password_changed_time to user sql

* Fix user update password

* Fix the dynamic config check

* Update the user sql style
2025-11-16 21:23:36 +08:00
linrong 2c0acb1103 Fix the user list query serialization (#921) 2025-11-16 16:04:22 +08:00
Wu Clan 425bc202c2 Add user social binding and unbinding (#919)
* Add user social binding and unbinding

* Add oauth2 state to binding

* Update oauth2 state

* Fix imports

* Update some interface definitions

* Update the authorization type security
2025-11-15 17:03:30 +08:00
Wu Clan 3b24dca935 Fix missing table in alembic migration (#920) 2025-11-14 17:55:15 +08:00
Wu Clan 98db5f4984 Update the changelog for v1.11.0 (#917) 2025-11-12 13:09:47 +08:00
Wu Clan 2b56168ad0 Refactor foreign keys and relationships to pure logic (#901)
* Refactor foreign keys and relationships to pure logic

* Revert of some changes

* More revert

* Update the user paginate

* Update user create and update

* Update dept select and delete

* Rename the join query functions

* Update select_join_serialize doc and README

* Fix typo in README

* Update the user delete

* Update the user social

* Update the dict plugin crud

* Update the dict plugin version

* Bump dependencies and pre-commits

* Update the code generator plugin crud

* Update the menu crud

* Update the role crud

* Update the data scope and rule crud

* Restore get_paginated to get_select

* Update the code generator plugin version

* Add the py version in pre-commit

* Remove the plugin include parameter config

* Add more cache cleaning TODO

* Rename get_with_relation to get_join

* Add the user cache clear

* Fix known compatibility issues

* Update the version number to 1.11.0

* Fix lint

* Optimize select_join_serialize logic

* Delete cache cleanup comments

* Update the oauth2 plugin version

* Fix user-role table cleanup when user update
2025-11-12 13:06:26 +08:00
Wu Clan b9255815e1 Update the changelog for v1.10.4 (#916) 2025-11-12 12:01:57 +08:00
Wu Clan 93e2f0f5f9 Fix dept and menu table subqueries (#914)
* Fix dept and menu table subqueries

* Fix user cache cleanup when updating data scope
2025-11-11 17:31:59 +08:00
Wu Clan 437c026da9 Fix user cache cleanup when updating data scope (#915) 2025-11-11 17:30:43 +08:00
Wu Clan 316079045f Add user social unbinding account interface (#913) 2025-11-11 17:06:11 +08:00
Wu Clan 5ae9f8b288 Fix the user menu sidebar parsing (#912) 2025-11-11 17:05:43 +08:00
Wu Clan bef65e1baa Fix create and delete department validations (#911) 2025-11-11 17:05:24 +08:00
Wu Clan 87a3cd8a9c Fix user cache cleanup when operating data rules (#910) 2025-11-11 17:05:08 +08:00
Wu Clan be7b742891 Fix typo in the data permission prompt (#909) 2025-11-11 17:04:25 +08:00
Wu Clan 1f98797a60 Update the version number to 1.10.4 (#907) 2025-11-10 23:51:28 +08:00
Wu Clan 8505e32757 Add dept validation to user updates (#906) 2025-11-10 23:50:38 +08:00
Wu Clan 91297d1ccd Fix response status codes in the request logs (#905) 2025-11-08 16:40:16 +08:00
Wu Clan 0ea213edfd Optimize the data permission condition build (#904) 2025-11-08 16:35:15 +08:00
Wu Clan 292f5d7e48 Optimize the request params of the service layer (#903) 2025-11-08 16:24:19 +08:00
Wu Clan ef871f3b5c Add the user social independent enum file (#902) 2025-11-08 16:21:51 +08:00
Wu Clan 6b17da59f8 Update the time column type in the task (#900) 2025-11-06 21:04:51 +08:00
Wu Clan 86d580edd8 Optimize redis batch get and delete operations (#899)
* Optimize redis batch get and delete operations

* Fix typo
2025-11-04 20:59:45 +08:00
Wu Clan cdbe37de7d Update the interface timing accuracy in log (#897)
* Update the interface timing accuracy in log

* Update log request finish
2025-11-04 20:59:00 +08:00
Wu Clan f46974ddd9 Bump fastapi oauth2 from 0.0.1 to 0.0.2 (#896) 2025-11-02 19:42:51 +08:00
Wu Clan a14d4243d9 Update the changelog for v1.10.3 (#895) 2025-10-30 19:28:00 +08:00
Wu Clan 90588ebae8 Add the celery rabbitmq vhost config (#894) 2025-10-30 19:24:46 +08:00
Wu Clan 00bc02f49c Fix the venv pip availability in Linux (#893)
* Fix the venv pip availability in Linux

* Remove pip install timeout
2025-10-30 18:06:58 +08:00
Wu Clan 7e9ce2e691 Fix celery compatibility with psycopg version (#892) 2025-10-30 13:32:12 +08:00
Wu Clan 81ec0f0bd9 Fix import in code generation api template (#891) 2025-10-30 11:14:05 +08:00
Wu Clan 37f9716c4f Bump dependencies to the latest version (#890)
* Bump dependencies to the latest version

* Bump fastapi to the latest version

* Fix lint
2025-10-30 11:07:40 +08:00
Wu Clan 4540cd9338 Update the length style of the model columns (#883)
* Update the length style of the model columns

* Fix opera log table username column length

* Fix login log table username column length
2025-10-23 15:00:28 +08:00
Wu Clan 3c39d2f2f3 Add Google OAuth2 callback to opera log exclusion (#882) 2025-10-22 18:58:36 +08:00
Wu Clan 1e01ffc3d9 Fix the OAuth2 link acquisition in HTTPS (#881)
* Fix the OAuth2 link acquisition in HTTPS

* Update OAUTH2_BACKEND_URL to OAUTH2_BACKEND_BASE_URL
2025-10-22 18:52:40 +08:00
Wu Clan f4b1c43b55 Update the serializer of httpurl type (#879)
* Update the serializer of httpurl type

* Fix lint
2025-10-22 17:30:13 +08:00
Wu Clan 02254938b4 Update the length of the user email column (#878) 2025-10-22 15:58:50 +08:00
Wu Clan 054ff0cab2 Fix the httpurl type compatibility with postgresql (#877)
* Fix the httpurl type compatibility with postgresql

* Add noqa comments
2025-10-22 15:58:42 +08:00
Wu Clan 4f898a0264 Update the container naming in docker scripts (#876) 2025-10-22 15:33:10 +08:00
Wu Clan 70d90c2c01 Add port for fba sever in docker compose (#875) 2025-10-22 15:32:56 +08:00
Wu Clan 964143b30d Fix docker default database env variables (#874) 2025-10-22 14:01:18 +08:00
Wu Clan ce9f59e7d6 Update the changelog for v1.10.2 (#873) 2025-10-21 15:56:46 +08:00
Wu Clan 1f958fc42a Bump fastapi to remove warning for python 3.14 (#872) 2025-10-21 15:45:05 +08:00
Wu Clan e6e53296a8 Optimize sqlalchemy types to simplify compatibility (#870)
* Optimize sqlalchemy types to simplify compatibility

* Update TimeZone type comment
2025-10-20 17:41:00 +08:00
Wu Clan f533de8abd Bump rtoml and uvicorn to support python 3.14 (#871) 2025-10-20 12:42:28 +08:00
Wu Clan adc6427aab Update the changelog for v1.10.1 (#869) 2025-10-18 18:29:30 +08:00
Wu Clan d034b78b48 Bump dependencies to the latest version (#868) 2025-10-18 18:14:43 +08:00
Wu Clan 6deda906de Update the release changelog workflow (#867) 2025-10-18 18:08:29 +08:00
Wu Clan a063105de8 Fix the volume in the docker compose script (#865)
* Fix the volume in the docker compose script

* Fix the db comment
2025-10-18 15:57:29 +08:00
Wu Clan 02b4215742 Fix tasks related to deleting db logs (#863) 2025-10-18 15:49:55 +08:00
Wu Clan 56b6e2113f Update the changelog for v1.10.0 (#864) 2025-10-18 15:44:17 +08:00
Wu Clan 4fcc78eb94 Update the changelog for v1.9.0 (#862) 2025-10-18 15:41:28 +08:00
Wu Clan b28a4d7673 Update the primary database to postgresql (#829)
* Update the primary database to postgresql

* Delete the prints

* Fix lint

* Update models in code generation

* Delete print

* Fix the salt column type
2025-10-17 23:03:17 +08:00
Wu Clan 27383e4ff5 Fix ctx in validation exception handler (#861)
* Fix ctx in validation exception handler

* Fix lint
2025-10-17 16:21:28 +08:00
Wu Clan bf16f8916d Update the changelog for v1.9.0 (#856) 2025-10-17 13:05:31 +08:00
Wu Clan 75a62ac6aa Fix dict plugin pgsql init script missing (#857)
* Fix dict plugin pgsql init script missing

* Fix lint
2025-10-17 13:05:10 +08:00
Wu Clan bbc4aaa020 Fix the api ninja in code generation (#858) 2025-10-17 13:04:54 +08:00
Wu Clan 89e5d02fe2 Update unique judgment logic for dict data (#859) 2025-10-17 13:04:35 +08:00
Wu Clan a93973af06 Fix the import table cli command (#860) 2025-10-17 13:02:28 +08:00
Wu Clan 15a9527866 Refactor the service layer db session call method (#850)
* Refactor the service layer db session call method

* Fix lint

* Update paging interfaces

* Fix some errors
2025-10-16 12:49:56 +08:00
Wu Clan 9147d35c8c Fix safely of access ctx in exception handlers (#855) 2025-10-16 12:45:01 +08:00
Wu Clan 6a065797f5 Update request state usage to context variable (#853)
* Update request state usage to context variable

* Update context to custom ctx

* Fix the exception interception in opera log

* restore elapsed
2025-10-16 10:23:39 +08:00
Wu Clan dd08775c85 Fix the superuser verify missing auth (#854) 2025-10-14 17:55:54 +08:00
Wu Clan 0cbe635ed8 Update superuser verify to dependency injection (#852)
* Update superuser verify to dependency injection

* Update sql scripts
2025-10-14 17:38:47 +08:00
Wu Clan e923f13e83 Bump fastapi and pydantic to the latest (#851) 2025-10-14 15:18:35 +08:00
Wu Clan 889da744ef Update the changelog for v1.8.3 (#849) 2025-10-13 17:31:23 +08:00
Wu Clan 74df1d1ce2 Bump sqlalchemy crud plus from 0.12.0 to 0.12.1 (#848) 2025-10-13 10:45:49 +08:00
Wu Clan 54e09b68c7 Bump fastapi and sqlalchemy to support python 3.14 (#847) 2025-10-11 13:19:10 +08:00
Wu Clan 5762834744 Update the ruff rules and format the code (#846)
* Update the ruff rules and format the code

* Update the per-file-ignores

* Update the ci

* Update rules

* Fix codes

* Fix pagination

* Update rules
2025-10-10 19:02:49 +08:00
Wu Clan 51354593d0 Bump dependencies and pre-commits (#845) 2025-10-09 13:26:56 +08:00
Wu Clan 8751425e2b Fix SQL script syntax error in config plugin (#843) 2025-09-27 12:33:58 +08:00
Wu Clan 921f05fa19 Fix the pgsql script in dict plugin (#842) 2025-09-27 12:17:45 +08:00
Wu Clan 5754c5449d Update the changelog for v1.8.2 (#841) 2025-09-26 13:32:05 +08:00
Wu Clan 2f535e3510 Update the version number to 1.8.2 (#840) 2025-09-26 13:28:41 +08:00
Wu Clan c25e447530 Update docker and deployment script comments (#839)
* Update docker and deployment script comments

* Update celery stdout logfile conf
2025-09-26 13:18:34 +08:00
Wu Clan 13359b736c Fix the phone filter in the user list (#838) 2025-09-26 12:08:53 +08:00
Wu Clan 500f20d91c Add extension parameters to paging data (#835) 2025-09-26 12:03:41 +08:00
Wu Clan d4de42fb46 Add the refresh token to demo mode exclusion (#834) 2025-09-26 12:03:27 +08:00
Wu Clan 3f0537a727 Simplify the code generation CLI parameters (#837) 2025-09-26 12:03:08 +08:00
Wu Clan d1cdb9e462 Fix the get model object method return logic (#832)
* Fix the get model object method return

* Update the error trigger method

* Delete the error log
2025-09-22 20:51:32 +08:00
Wu Clan 6c029b9be7 Fix code generation template missing Decimal (#830)
* Fix code generation template missing Decimal

* Update template rendering

* Update version number

* Fix lint
2025-09-22 10:46:13 +08:00
Wu Clan df12a0c701 Update the user agent in opera log model (#831) 2025-09-22 10:42:26 +08:00
Wu Clan 0be5685d9f Add version number output to the startup CLI (#820) 2025-09-17 11:11:50 +08:00
Wu Clan 3efca8af37 Update the dict plugin table structure (#817)
* Update the dict plugin table structure

* Update dict sql

* FIx the dict plugin sql
2025-09-15 09:04:30 +08:00
Wu Clan 82b2f808ac Fix the summary in opera log middleware (#816) 2025-09-14 13:22:24 +08:00
Wu Clan f417374f3e Update the login captcha verify logic (#815) 2025-09-12 21:02:21 +08:00
Wu Clan 237534c63e Update the startup progress information display (#812) 2025-09-12 15:44:17 +08:00
morty zhao b525124fdf Fix special character password support in alembic (#811) 2025-09-12 11:07:55 +08:00
Wu Clan 5217c95451 Fix summary default in opera log middleware (#809) 2025-09-11 18:21:40 +08:00
B ea973cac90 Fix pgsql syntax error in code generation (#808)
* Update title field in OperaLogSchemaBase to allow None values

* Update response type in get_all_tables to allow None values in dictionary

* Fix code generation error in PostgreSQL

* Update opera_log.py
2025-09-11 17:53:15 +08:00
Wu Clan 1bedda0e66 Update the offline location information parse (#807) 2025-09-11 14:49:09 +08:00
B c1087fa51d Fix the subprocess ensurepip got stuck in Linux (#806)
* Fix ensurepip_install

* fix code format
2025-09-10 19:42:13 +08:00
Wu Clan cfddf44e7e Update the swagger docs version to dynamic (#805) 2025-09-09 19:36:33 +08:00
Wu Clan 085b32e921 Update the changelog for v1.8.1 (#804) 2025-09-09 19:19:30 +08:00
Wu Clan 2099c33aad Fix alembic migration file missing import (#803) 2025-09-09 17:21:23 +08:00
yzbf b809497aba Fix the subprocess check call failed in windows (#802)
* fix: 修复Windows下subprocess调用失败的问题

* fix: 修复代码格式问题
2025-09-09 16:21:51 +08:00
Wu Clan a6fe8c0e8d Fix console import in main module (#800) 2025-09-07 19:33:55 +08:00
Wu Clan 629cf7ce45 Optimize the code generation interaction logic (#799) 2025-09-06 19:24:02 +08:00
Wu Clan fad6c05d41 Add the code generator to the CLI (#798)
* Add the code generator to the CLI

* Optimize run_await implementation

* Optimize interactions
2025-09-05 23:43:05 +08:00
Wu Clan 77d59d3453 Add schema for the tree data (#796) 2025-09-04 16:38:53 +08:00
Wu Clan 931d6b281a Fix the psycopg dependency naming (#795) 2025-09-04 11:54:39 +08:00
Wu Clan d929f0dbdc Fix sql script syntax error in config plugin (#793) 2025-09-03 19:57:11 +08:00
Wu Clan 6d1b47903e Add the dict type query all interface (#794) 2025-09-03 19:56:56 +08:00
Wu Clan 8420ccf190 Update the notice model and pagination (#792) 2025-09-02 15:51:07 +08:00
Wu Clan 1e38e1918b Bump dependencies and pre-commits (#791) 2025-09-02 12:45:34 +08:00
IAseven 52e78152a1 Fix the tzinfo comparison in the timezone type (#787) 2025-08-29 10:30:42 +08:00
Wu Clan dcb07c60f2 Add the Google OAuth2 login (#790) 2025-08-28 22:10:22 +08:00
Wu Clan 33c9f78cf0 Update the handling of CORS 500 status code (#789) 2025-08-28 15:36:06 +08:00
Wu Clan 37b1033697 Update the opera log exception message record (#788) 2025-08-28 15:01:01 +08:00
Wu Clan 0aa4ebc635 Update the model datetime column type to custom (#786)
* Update the model datetime column type to custom

* Update the schema datetime filed json encoder
2025-08-27 20:07:35 +08:00
language 30d4107670 Fix CLI openapi url in production environment (#785) 2025-08-26 09:39:30 +08:00
Wu Clan dcf235db78 Fix code generation template params and filename (#784) 2025-08-22 13:20:30 +08:00
Wu Clan 1dfcd7ae3c Update the celery task result table creation logic (#783)
* Update the celery task result table creation logic

* Disable beat_sync_every config

* Update the prepared comment
2025-08-21 23:46:52 +08:00
wuyao4 2b28244099 Fix the plugin model object detection logic (#782) 2025-08-21 18:17:50 +08:00
Wu Clan f71071f905 Fix the celery beat distributed lock timeout (#779)
* Fix the celery beat distributed lock timeout

* Fix redis client not close after plugin parse

* Optimize code layout and lock extend
2025-08-21 13:50:57 +08:00
Wu Clan 34c9c398ca Fix redis client not close after plugin parse (#780) 2025-08-21 10:58:53 +08:00
Wu Clan f099777dd3 Fix the docker compose env configuration file (#778)
* Fix the docker compose env configuration file

* Fix CELERY_RABBITMQ_HOST value
2025-08-19 13:21:11 +08:00
Wu Clan c95be82cb9 Update the env parameter value in env file (#777) 2025-08-19 12:50:08 +08:00
Wu Clan 00ec039095 Add test data SQL script for config plugin (#776) 2025-08-19 12:38:22 +08:00
Wu Clan 4d087355b4 Optimize email sending config update logic (#775) 2025-08-19 12:19:31 +08:00
Wu Clan d57fabcbb4 Add bulk update interface for config plugin (#774)
* Add bulk update interface for config plugin

* Remove email sending update
2025-08-19 12:18:38 +08:00
yanlingsishao 3a32522e2b Optimize the celery crontab expression validation (#773) 2025-08-18 22:19:59 +08:00
Wu Clanandwu-clan 22f8850bf7 Update the changelog for v1.8.0 (#772)
This commit was created by changelog-from-release in 'Release changelog' CI workflow

Co-authored-by: wu-clan <wu-clan@users.noreply.github.com>
2025-08-15 20:09:28 +08:00
Wu Clan eeb709c6aa Update the version number to 1.8.0 (#771) 2025-08-15 20:06:21 +08:00
Wu Clan a6bbf2971d Update the menu title in SQL scripts (#770) 2025-08-15 19:58:46 +08:00
Dylan cd48bb4210 Add i18n support for response message (#753)
* feat: i18n support

* Optimize i18n

* Update the locale in the code

* Update the zh-CN file

* Update the en-US file

* Update the reload filter

* Update locale success plugin value

* Fix lint

* Update pydantic error message translation

* Update to minimal implementation

* Fix minimal missing code
2025-08-15 19:57:10 +08:00
Wu Clan 4500dd0128 Add a standalone email sending plugin (#769) 2025-08-13 18:09:42 +08:00
Wu Clan 2b6d8222ad Update the content layout of the config file (#768) 2025-08-13 18:07:02 +08:00
Wu Clan 1b47ab7e83 Optimize the timezone datetime return encoder (#767)
* Optimize the timezone datetime return encoder

* Update the default datetime conversion
2025-08-13 11:16:58 +08:00
Wu Clan bd804e0a38 Update the description for the run file (#766) 2025-08-12 23:28:43 +08:00
Wu Clan 8e8af2032a Optimize naming and preview in code generation (#764) 2025-08-12 16:42:25 +08:00
IAseven e09062eb39 Optimize the opera log storage logic through queue (#750)
*  feat: 操作日志中间件添加批量插入功能

* Delete GEMINI.md

* 🌈 style: 修复格式化错误

* 🐞 fix: 通过asyncio.wait_for兼容py3.10中asyncio.timeout不存在

* 🦄 refactor: 重新组织操作日志批量插入代码逻辑

* 优化代码实现

* 恢复默认配置

* 恢复默认 .gitignore 文件

* 更新队列批处理逻辑
2025-08-07 17:36:10 +08:00
Wu Clan 8c00492e44 Update the naming of table creation function (#760) 2025-08-07 16:57:57 +08:00
Wu Clan 0237d4c7b1 Update log output config and format (#759) 2025-08-07 16:46:56 +08:00
Wu Clan fe3a3b4e86 Optimize the data sort logic of tree nodes (#758) 2025-08-06 18:23:09 +08:00
Wu Clan 65ec721a1c Add business pagination in the code generator (#757) 2025-08-05 21:04:27 +08:00
Wu Clan 4eb76ad6ea Update the opera log desensitization method (#756)
* Update the opera log desensitization method

* Update request args function return
2025-08-05 18:38:49 +08:00
Wu Clan 1f8687155a Fix message format in validation exception handler (#755) 2025-08-05 18:11:02 +08:00
Wu Clan 8591d4e592 Refactor task routes and add control routes (#749) 2025-08-04 13:18:07 +08:00
Wu Clan dedf4e7bae Refactor code generation files and routes (#748)
* Refactor code generation files and routes

* Fix lint
2025-08-04 13:16:41 +08:00
Wu Clan 1e4aa88487 Fix the kwargs params of schedule task (#747) 2025-08-04 13:15:48 +08:00
Wu Clan 0dd745b7a7 Add schedule task demo that contains params (#746) 2025-07-31 18:29:54 +08:00
Wu Clan 6b2402f212 Add some interfaces for user profiles (#745) 2025-07-31 16:46:02 +08:00
Wu Clan 24a487eeea Simplify the plugin status update logic (#744) 2025-07-30 11:31:37 +08:00
Wu Clan 4f574189c7 Fix the error trigger when model auto import (#743) 2025-07-29 22:52:34 +08:00
Wu Clan b559a74cea Add update support for user email and phone (#742) 2025-07-29 22:52:08 +08:00
Wu Clan 83dcdbe59d Update the OAuth2 login password policy (#741)
* Update the OAuth2 login password policy

* Update the crud pwd

* Update the reset pwd service
2025-07-29 22:51:31 +08:00
Wu Clan d64f7c2911 Fix the opera log field encryption (#739) 2025-07-25 19:23:36 +08:00
Wu Clan 53e64bce37 Add auth whitelist regular expression config (#738) 2025-07-24 21:34:24 +08:00
Wu Clan 00a781357b Fix celery CLI option to required (#737) 2025-07-24 21:32:57 +08:00
Wu Clan d7f87ed0ad Update the default cache period for userinfo (#734) 2025-07-21 21:31:55 +08:00
Wu Clan bda9b1d463 Add distributed lock for scheduled task (#732)
* Add distributed lock for scheduled task

* Add the task to extend lock

* Fix the close
2025-07-21 12:29:12 +08:00
Wu Clan e0a106ec51 Simplify task crontab expression validation (#733) 2025-07-18 21:11:54 +08:00
Wu Clan 016361bd68 Update the changelog for v1.7.0 (#729) 2025-07-16 13:40:55 +08:00
Wu Clan f2d3c39425 Fix login and operation log clearing (#728) 2025-07-16 13:34:37 +08:00
Wu Clan e45d2d6fe7 Add CLI support for startup celery services (#724)
* Add CLI support for startup celery services

* update the cli file
2025-07-16 12:28:57 +08:00
Wu Clan 4ddf84fa22 Bump granian from 2.4.0 to 2.4.2 (#727) 2025-07-16 12:28:02 +08:00
Wu Clan 326a1883e8 Fix the parsing of execution task params (#725)
* Fix the parsing of execution task params

* Enhanced checksums
2025-07-16 12:10:33 +08:00
Wu Clan 9ff36d4498 Delete the default value of schema enum data (#723) 2025-07-15 10:43:58 +08:00
Wu Clan 98ef07ad32 Simplify celery task crontab config (#722) 2025-07-15 00:28:10 +08:00
Wu Clan 802b0d456a Optimize celery integrations and events (#721) 2025-07-14 19:17:24 +08:00
Wu Clan 6767f0e2e6 Update the celery task comment and name (#720) 2025-07-11 21:08:56 +08:00
Wu Clan d72a05c965 Fix the celery task scheduler query (#719) 2025-07-11 21:08:44 +08:00
Wu Clan ce3be1db8e Add support for celery dynamic tasks (#715)
* Add support for celery dynamic tasks

* Update the celery conf

* Update the celery task tables name

* Refactor the celery task-related interfaces

* Optimize auto-discovery tasks

* Remove redundant config

* Refine the business codes

* Optimize crontab validation returns

* Update dependencies in pyproject toml

* Fix some bugs

* Update dependencies

* Update the version to 1.7.0

* Fix update and delete event
2025-07-11 07:54:33 +08:00
Wu Clan e84ef04f15 Update the CLI startup service mode (#718) 2025-07-10 20:33:50 +08:00
Wu Clan adee3a2177 Simplify user permission database queries (#717) 2025-07-09 20:44:32 +08:00
Wu Clan 526e0aab9a Optimize the analysis of get plugins (#716)
* Optimize the analysis of get plugins

* update var
2025-07-08 12:20:13 +08:00
Wu Clan 2bbbbe764a Update the log output default style (#714)
* Update the log output default style

* Update the log file compression

* fix line

* Add log summary
2025-07-06 14:32:36 +08:00
Wu Clan ef5e921c08 Update the middleware logging accuracy (#713)
* Update the middleware logging accuracy

* Update the log style
2025-07-05 19:52:37 +08:00
Wu Clan a2fa59285a Update the granian env to command params (#712) 2025-07-04 19:46:40 +08:00
Wu Clan 494942e87e Add CLI support for execute sql scripts (#711)
* Add CLI support for execute sql scripts

* Update the arg helps
2025-07-02 20:19:29 +08:00
Wu Clan aa2b76673f Update the reload excludes for CLI run (#709) 2025-07-02 18:16:43 +08:00
Wu Clan 099880dd1c Update the refresh token verify mechanism (#710)
* Remove the logout interface auth dependency

* Update the refresh token check
2025-07-02 18:08:28 +08:00
Wu Clan d906a103af Fix the code generation delete schema template (#708) 2025-07-01 22:28:37 +08:00
Wu Clan 4ed49d2d13 Replace gunicorn deployment to granian (#705)
* Replace gunicorn deployment to granian

* Update env comments

* Fix the granian env
2025-07-01 21:33:49 +08:00
Wu Clan 7b5ae4696f Fix the code generation schema template (#706) 2025-07-01 18:18:47 +08:00
Wu Clan 54ea301152 Update the CLI to be executed async (#704) 2025-07-01 18:18:29 +08:00
Wu Clan 69c27232ac Update the changelog for v1.6.0 (#703) 2025-06-30 17:02:55 +08:00
Wu Clan f36dcb3f5c Update the version number to 1.6.0 (#702) 2025-06-30 17:00:19 +08:00
Wu Clan 63d088d62c Update the Dockerfile to adapt the CLI (#701) 2025-06-30 16:53:16 +08:00
Wu Clan a461f78224 Optimize the installation of plugin dependencies (#700)
* Optimize the installation of plugin dependencies

* Remove enumerate

* Update main params
2025-06-30 09:29:35 +08:00
Wu Clan c306432708 Update the help for CLI run worker (#699) 2025-06-29 20:06:45 +08:00
Wu Clan 97f778cc90 Add CLI support for plugin install (#698)
* Add CLI support for plugin install

* Fix some usage errors

* Update prompt information
2025-06-29 17:41:12 +08:00
Wu Clan 88695ac6ad Add custom CLI for service startup (#697)
* Add custom CLI for service startup

* Remove redundant scripts

* Fix lint
2025-06-28 14:56:19 +08:00
Wu Clan 3b04329b04 Add the test user to SQL scripts (#696) 2025-06-27 18:43:42 +08:00
Wu Clan bd4acf8121 Update the extension plugin config (#695) 2025-06-27 18:14:25 +08:00
Wu Clan b9a9b1efe6 Update the SQL to adapt frontend plugin (#694) 2025-06-26 21:58:35 +08:00
Wu Clan c050f58ee9 Fix the OAuth2 redirect route names (#693) 2025-06-26 21:50:22 +08:00
Wu Clan d47375ae41 Optimize dict create and update logic (#691)
* Optimize dict create and update logic

* Update version
2025-06-25 10:41:09 +08:00
Wu Clan 0602c6144d Update the changelog for v1.5.2 (#690) 2025-06-24 17:40:09 +08:00
Wu Clan c84f0658fb Update the dict pagination query parameters (#689)
* Update the dict pagination query parameters

* Update the dict pagination query parameters

* Update version
2025-06-24 17:34:18 +08:00
Wu Clan a2902bd23a Add built-in plugin missing files (#688) 2025-06-24 17:34:06 +08:00
Wu Clan 69a9b90711 Optimize the zip plug-in file name parsing (#687) 2025-06-24 17:33:57 +08:00
Wu Clan b96402e11d Simplify custom response status codes (#686) 2025-06-24 10:42:15 +08:00
Wu Clan 0bc6f6a719 Update the init test data for SQL scripts (#685)
* Update the init test data for SQL scripts

* Update plugin sql scripts

* fix snowflake id
2025-06-24 10:15:23 +08:00
Wu Clan ebd65c8246 Update dict data label column config (#684) 2025-06-23 22:33:34 +08:00
Wu Clan 234bf708b3 Fix the code with outdated system config (#683) 2025-06-23 22:23:06 +08:00
Dylan 6d5e741d94 Optimize api with semantic HTTP status codes (#681) 2025-06-23 22:18:17 +08:00
Wu Clan f9bfe8f510 Add dictionary type and datas queries (#679) 2025-06-23 10:58:55 +08:00
Wu Clan 408c866dda Update cache cleanup for logout interface (#678)
* Update cache cleanup for logout interface

* Add dependencies of interface
2025-06-22 23:41:48 +08:00
Wu Clan bac41a46f8 Optimize token detection and caching logic (#677) 2025-06-21 20:18:13 +08:00
Wu Clan 8638c26db1 Add the snowflake ID sql script (#675) 2025-06-20 20:31:44 +08:00
Wu Clan 319ba13df1 Optimize routes to better align with RESTful (#673)
* Optimize routes to better align with RESTful

* Add codes endpoint description

* Update jinja templates

* fix typo

* fix sql
2025-06-19 11:06:34 +08:00
Wu Clan 0d1f05d307 Fix some error class import (#672) 2025-06-17 15:30:35 +08:00
Wu Clan e6608d18ce Update the changelog for v1.5.1 (#671) 2025-06-16 21:38:13 +08:00
Dylan 7afd8415cd Add support for snowflake ID primary key (#670)
* fix: 修复PostgreSQL SQL语法错误,将反引号替换为双引号

* feat: 新增雪花算法ID实现

* 优化雪花算法和主键类型

* 修复错误引用

* 添加雪花详情链接

* feat: add snowflake ID parser method

* 修复独立执行异常

* 更新系统时间错误类
2025-06-16 13:34:27 +08:00
Dylan 845f2f0ff8 Fix the postgresql sql script syntax error (#669) 2025-06-15 16:38:48 +08:00
Wu Clan 11d7792c0f Bump sqlalchemy crud plus version to 1.10.0 (#668)
* Bump sqlalchemy crud plus version to 1.10.0

* Update filter style

* Update filters style
2025-06-14 12:35:19 +08:00
Wu Clan 6883ec34c6 Fix the sidebar menu type filtering (#667) 2025-06-13 16:50:53 +08:00
Wu Clan 4c45e7ff27 Update the changelog for v1.5.0 (#664) 2025-06-09 21:36:54 +08:00
Wu Clan 5621daea98 Update the version to 1.5.0 (#663) 2025-06-09 21:32:12 +08:00
Wu Clan 71d168dfea Add data permission in init test sql (#662) 2025-06-09 21:00:49 +08:00
Wu Clan 2d4365189d Update menu sort in init test sql (#661) 2025-06-09 20:27:56 +08:00
Wu Clan 0c28a46e60 Update auth failed default status code (#660)
* Update auth failed default status code

* update init sql
2025-06-09 19:58:06 +08:00
Wu Clan a4c7dae99c Update the plugin download interface permission (#659) 2025-06-09 19:10:48 +08:00
Wu Clan bfda0a226c Update code generate interface permission (#658) 2025-06-09 19:10:40 +08:00
Wu Clan c9bbb105a3 Update data scope and rule to m2m (#657) 2025-06-09 19:10:15 +08:00
Wu Clan b415dc8912 Add OAuth2 user to auto bind a role (#656) 2025-06-08 20:18:24 +08:00
Wu Clan 05b74d71a3 Simplify OAuth2 model and optimize auth service (#655) 2025-06-08 16:51:19 +08:00
Wu Clan 10c0c691a7 Update user email and phone operation logic (#654) 2025-06-08 16:43:51 +08:00
Wu Clan 0bc3786fc6 Update OAuth2 callback interface return (#653) 2025-06-06 20:35:25 +08:00
Wu Clan 8e041ccb1b Update the changelog for v1.4.3 (#651) 2025-06-02 20:43:12 +08:00
Wu Clan a82e9b7b9d Fix data permission condition for filter data (#650) 2025-06-01 00:51:31 +08:00
Wu Clan 7bab8cc636 Update default value for role filter scopes (#649) 2025-05-31 23:53:47 +08:00
Wu Clan 7659b63d88 Add data permission condition for filter data (#648) 2025-05-31 23:40:59 +08:00
Wu Clan 33ebc5723f Add get all data scope rules interface (#647) 2025-05-31 23:09:26 +08:00
Wu Clan 86d9efd785 Update the server startup time to string (#646) 2025-05-31 17:26:07 +08:00
Wu Clan 57ae7f03a6 Refactor routes to better align with RESTful (#645) 2025-05-31 17:01:44 +08:00
Wu Clan 6ee3983846 Fix the filter of query all menus (#642) 2025-05-30 22:48:30 +08:00
Wu Clan f05ed5cdda Fix the menu delete interface arg description (#641) 2025-05-30 18:58:33 +08:00
Wu Clan 187dfa5344 Fix the role update business variables (#640) 2025-05-30 18:56:50 +08:00
Wu Clan 918c8a9d33 Update the changelog for v1.4.2 (#639) 2025-05-29 18:12:45 +08:00
Wu Clan ccfe35ea85 Simplify the user info update business (#638) 2025-05-29 18:09:50 +08:00
yzbf 4a118c166a Fix overwriting existing init files in code generator (#637) 2025-05-28 20:17:18 +08:00
Huyuwei 5f3823cb6d Fix auto-increment id for postgres init data (#632)
* fix: Reset auto-increment values for all tables based on max id

* refactor: Remove auto-increment reset for mysql

* remove unused code
2025-05-28 13:01:44 +08:00
Wu Clan 19ef52da9b Optimize install and build of plugin zip (#636)
* Optimiz install and build of plugin zip

* fix import
2025-05-27 11:46:17 +08:00
Wu Clan 39c3339a8d Optimize role-related data processing performance (#635) 2025-05-26 20:04:13 +08:00
Wu Clan 9f52629dcb Update the captcha invalidation error class (#634) 2025-05-26 19:16:56 +08:00
Wu Clan 1cddd6e2d5 Update non-linked sidebar support (#633)
* Update non-linked sidebar support

* fix typing
2025-05-26 15:52:45 +08:00
Wu Clan ed10e6ff47 Update the changelog for v1.4.1 (#630) 2025-05-25 14:19:41 +08:00
Wu Clan 48574fb50a Update the token decode for logout interface (#629) 2025-05-25 14:16:36 +08:00
Wu Clan ee7fe3087d Update the token check for logout interface (#625) 2025-05-23 22:05:26 +08:00
Wu Clan 270ac3d941 Refactor the system token to online users (#624) 2025-05-23 20:18:47 +08:00
Wu Clan 2f76988076 Add the deepwiki badge to README (#623) 2025-05-23 12:09:04 +08:00
Wu Clan b275481742 Update the menu path and type columns (#622)
* Update the menu path and type columns

* Update build vben5 tree

* Update data
2025-05-22 21:22:24 +08:00
Wu Clan f3f1ceb595 Update the changelog for v1.4.0 (#621) 2025-05-22 14:41:03 +08:00
Wu Clan bcc764142b Update the OAuth2 module to plugin (#620)
* Update the OAuth2 module to plugin

* update create user social param
2025-05-22 14:36:23 +08:00
Wu Clan 9b0d9f8296 Update uv installation in docker deploy (#619) 2025-05-22 14:30:53 +08:00
Wu Clan 63ba7646c1 Update the build plugin api params (#615)
* Update the build plugin api params

* update seek

* fix lint
2025-05-20 19:45:22 +08:00
Wu Clan 1a7734185f Fix non-asyncio nested async IO (#610)
* Fix non-asyncio nested async IO

* Add redis init ping
2025-05-20 18:11:11 +08:00
Wu Clan 8541059d23 Update uninstall and build plugin api method (#614) 2025-05-20 18:10:29 +08:00
Wu Clan 61edc81333 Fix the plugin status update logic (#613) 2025-05-20 17:50:07 +08:00
Wu Clan 8998687adb Fix the task result schema param type (#611) 2025-05-20 15:10:15 +08:00
Wu Clan 85b0294da9 Update the new plugin status to changed (#607)
* Fix cache update after plugin uninstall

* Update new to changed
2025-05-17 09:21:11 +08:00
Wu Clan a8ecb4ae3b Add new plugin status check interface (#606) 2025-05-17 09:06:06 +08:00
Wu Clan a87ff74b56 Update the changelog for v1.3.0 (#605) 2025-05-16 14:16:59 +08:00
Wu Clan 3c5682988d Update the casbin RBAC module path (#604) 2025-05-16 11:49:18 +08:00
Wu Clan 1d32154498 Optimize the zip plugin install logic (#603) 2025-05-15 23:40:33 +08:00
Wu Clan f4290ddb8c Fix the fastapi cli startup event loop (#602) 2025-05-15 22:54:25 +08:00
Wu Clan 1e54b0ba42 Add plugin info config and interfaces (#601)
* Add plugin info config and interfaces

* Remove test data
2025-05-15 20:50:24 +08:00
Wu Clan 27b9cf4c64 Simplify apps and plugins config method (#600) 2025-05-13 13:16:06 +08:00
Wu Clan 87f29e52a1 Update the changelog for v1.2.0 (#598) 2025-05-05 19:46:41 +08:00
Wu Clan 16a0f9646d Update the SQL script for creat tables (#597) 2025-04-28 19:39:33 +08:00
Wu Clan 032364e48e Refactor the data rule to scope rule (#596)
* WIP

* update codes

* update codes

* update filter_data_permission

* Fix schema

* Fix issues
2025-04-28 18:15:52 +08:00
Wu Clan bc5d142920 Optimize the server information retrieval (#595)
* Optimize the server information retrieval

* Update import

* Fix typing
2025-04-28 15:41:53 +08:00
Wu Clan 9b3425d801 Update the default RBAC solution (#593) 2025-04-27 14:54:39 +08:00
Wu Clan 5842127c2b Update code generator table columns (#590)
* Update code generator table columns

* Update func doc
2025-04-24 20:27:08 +08:00
Wu Clan 193eba3c58 Update the changelog for v1.1.2 (#589) 2025-04-23 23:43:56 +08:00
Wu Clan 338e50e255 Update the routing style of the task app (#588)
* Update the routing style of the task app

* Fix prefix
2025-04-23 19:33:36 +08:00
Wu Clan 65500d7a53 Bump fastapi pagination from 0.12.34 to 0.13.0 (#587) 2025-04-23 13:04:09 +08:00
Wu Clan 0833cd5443 Optimize the userinfo cache cleaning logic (#586) 2025-04-23 12:40:27 +08:00
Wu Clan e8c844ead7 Fix cache cleanup when updating role menu (#585) 2025-04-21 17:58:01 +08:00
Wu Clan 1d5f2dc5f0 Fix the condition to query menu by title (#584) 2025-04-21 17:57:12 +08:00
Wu Clanandwu-clan 9a36154564 Update the changelog for v1.1.1 (#583)
This commit was created by changelog-from-release in 'Release changelog' CI workflow

Co-authored-by: wu-clan <wu-clan@users.noreply.github.com>
2025-04-18 23:19:01 +08:00
Wu Clan 5f8ae74ea5 Fix list query in the dict plugin (#582) 2025-04-18 23:16:03 +08:00
Wu Clan da8e7c1b01 Fix the plugin system route injection (#581) 2025-04-18 23:09:25 +08:00
Wu Clan f513b98f15 Update the changelog for v1.1.0 (#580) 2025-04-17 17:54:52 +08:00
Wu Clan 80cef6dde7 Update code generator file and table naming (#579)
* Update code generator file and table naming

* Update column table comment
2025-04-17 17:47:20 +08:00
Huyuwei 2d504f281e Fix avatar url type of update avatar (#575) 2025-04-17 15:02:19 +08:00
Wu Clan fc34c98f0a Update the code generator to plugin (#578)
* Update the code generator to plugin

* Fix get all tables return type
2025-04-17 11:06:43 +08:00
Wu Clan 1cc020efdd Update unique columns in dict models (#577) 2025-04-17 11:06:29 +08:00
Wu Clan bb33e99e33 Update casbin RBAC verify to dynamic import (#576) 2025-04-17 11:06:15 +08:00
Wu Clan 6c70b34ae6 Optimize the file structure of code generator (#574) 2025-04-15 19:54:18 +08:00
Wu Clan 960d9f6215 Update the default value for some functions (#573)
* Update the default value for some functions

* Fix lint
2025-04-14 14:22:05 +08:00
Wu Clan 085f08ec3b Update the changelog for v1.0.5 (#572) 2025-04-09 11:55:49 +08:00
Wu Clan 2a8722f56e Fix the log rule in gitignore (#571) 2025-04-09 11:53:55 +08:00
Wu Clan 4372b9bbc2 Bump fastapi oauth2 from 0.0.1a2 to 0.0.1 (#570) 2025-04-09 11:49:04 +08:00
Wu Clan db81319dc4 Fix the failure hook of celery task (#569) 2025-04-08 12:09:29 +08:00
Wu Clan 644f7a0413 Fix the login password verification (#568)
* Fix the login password verification

* Update the check criteria
2025-04-07 11:04:39 +08:00
Wu Clan b93ff19ebe Update the number of pagination le (#565) 2025-04-03 19:32:26 +08:00
Wu Clan 4c007cb47d Update custom validation error messages (#566) 2025-04-03 19:32:11 +08:00
Wu Clan c9e9a45288 Update the vben5 tree data structure (#564) 2025-04-02 23:18:55 +08:00
Wu Clan d44a5491f5 Update menu and add vben5 compatibility (#563)
* Update menu and add vben5 compatibility

* Update sidebar api description
2025-04-02 22:20:19 +08:00
Wu Clan 289cf5b974 Update dict data and type to plugin (#562) 2025-04-02 14:04:06 +08:00
Wu Clan e207622a70 Update the system config to plugin (#561) 2025-04-02 13:43:24 +08:00
Wu Clan 38d9d9fda5 Add python 3.13 to GitHub ci (#560) 2025-03-28 14:22:45 +08:00
Wu Clan 996895e775 Bump dependencies and pre-commits (#559) 2025-03-28 14:22:20 +08:00
Wu Clan 0675e53f2a Update the changelog for v1.0.4 (#558) 2025-03-28 10:15:11 +08:00
Wu Clan e492cec7d7 Optimize codes and comments with cursor (#550) 2025-03-28 10:08:32 +08:00
PoetryL 92fe1e7554 Fix the query for the sub department (#557) 2025-03-27 23:30:12 +08:00
Wu Clan 7b080600f6 Update volumes of redis in docker compose (#556) 2025-03-26 18:58:06 +08:00
Wu Clan 71f9667bc0 Fix the celery env in docker compose (#555) 2025-03-26 18:34:41 +08:00
Thank CAT 2bf548f8b9 Fix filters for opera log query list (#554) 2025-03-25 09:19:28 +08:00
Wu Clan 15fd4545aa Optimize schemas with model relationships (#552) 2025-03-20 18:46:13 +08:00
Wu Clan 7f6c4ad6da Fix return schema of the config api (#551) 2025-03-20 16:27:44 +08:00
Wu Clan 2be12b4ea4 Fix async install plugin dependencies for windows (#549) 2025-03-17 23:39:10 +08:00
Wu Clan baebdd9870 Optimize dependencies to reduce package size (#548) 2025-03-17 18:41:58 +08:00
Wu Clan d1648bb709 Update git and docker ignore files (#547)
* Update git and docker ignore files

* add __pycache__ dir

* add .python-version
2025-03-17 13:20:21 +08:00
Wu Clan a4a394a18d Optimize the dynamic import of data models (#546) 2025-03-16 15:13:09 +08:00
Wu Clan 951fdc902a Optimize the return of relationship interfaces (#545)
* Optimize the return of relationship interfaces

* Fix gen business detail schema
2025-03-13 18:30:05 +08:00
Wu Clan 18ffa06d70 Updated the backend contribution guide (#544) 2025-03-12 18:15:10 +08:00
Wu Clan d28f152d78 Update the changelog for v1.0.3 (#543) 2025-03-12 14:23:38 +08:00
Wu Clan 1e3e9ea84a Update docker scripts and nginx conf (#542) 2025-03-11 23:26:53 +08:00
Huyuwei 5b42e59d65 Add Aliyun mirror to PyPI index in pyproject.toml (#541)
* Add Aliyun mirror to PyPI index in pyproject.toml

* Update uv.lock

* fix end of files

* Remove UV_INDEX environment variable from Dockerfile
2025-03-11 16:48:30 +08:00
Huyuwei 1882467554 Fix Dockerfile mounts for dependency installation (#540) 2025-03-10 15:34:11 +08:00
Huyuwei f1cd48e9d0 Fix typos in Dockerfile comments (#539) 2025-03-10 15:33:42 +08:00
Wu Clan d1fd84617b Refactor toml and dependencies file dir (#538)
* Refactor toml and dependencies file dir

* fix ci
2025-03-09 23:38:20 +08:00
Wu Clan 03fcf82b26 Update docker scripts in backend README (#537) 2025-03-04 18:34:07 +08:00
Wu Clan f75e1bf3df Update the changelog for v1.0.2 (#536) 2025-03-01 16:01:41 +08:00
Wu Clan 554a6f59bd Add plugin related interfaces (#535)
* Add plugin related interfaces

* Update interface auth

* Add build plugin zip service
2025-03-01 15:58:41 +08:00
Wu Clan 68d41cf028 Add log module root and output levels (#534) 2025-02-27 15:38:41 +08:00
Wu Clan 5defeca590 Fix celery async task worker pool (#533)
* Fix celery async task worker pool

* Update celery start scripts

* fix lint
2025-02-27 15:38:10 +08:00
Wu Clan 4d94418124 Update the changelog for v1.0.1 (#532) 2025-02-26 15:53:25 +08:00
Huyuwei 6c258f056a Optimize Dockerfile for faster builds (#526) 2025-02-26 14:45:48 +08:00
Wu Clan 87af1f1319 Fix the sql script for init data (#530) 2025-02-25 21:42:03 +08:00
Wu Clan 3f9badd4b9 Add sqlalchemy connection pool config (#529) 2025-02-25 15:46:10 +08:00
Wu Clan 06de770c4d Add async attrs for sqla mapped base (#528) 2025-02-25 15:43:35 +08:00
Huyuwei b5948441ac Add missing volume config for docker deploy (#525) 2025-02-25 15:43:13 +08:00
Wu Clan acbab69f12 Update the changelog for v1.0.0 (#524) 2025-02-24 21:39:49 +08:00
Wu Clan 1f8ac4953e Add changelog ci for github release 2025-02-24 21:17:56 +08:00
Wu Clan 1d981ec159 Update the built-in features in README (#519) 2025-02-24 12:48:46 +08:00
Wu Clan b0f675f697 Delete the model redundancy level field (#518) 2025-02-24 12:30:08 +08:00
Wu Clan 6d44f807a7 Update loguru and deploy log config (#517)
* Update loguru config

* Update config

* fix log level

* Update deploy log config
2025-02-24 12:17:05 +08:00
Wu Clan 9602e28429 Add local file upload interfaces (#489)
* Add the alibaba cloud oss plugin

* Update oss plugin struct

* Add local upload
2025-02-21 20:18:35 +08:00
Wu Clan 440f324a3d Update the logic for create new token (#516) 2025-02-17 16:56:45 +08:00
Wu Clan 5e7a4e1e74 Update casbin rbac verify to plugin (#513)
* Update casbin rbac verify to plugin

* Update requirements

* move sys api to casbin plugin

* Update plugin toml and parse

* Fix plugin router inject

* Update api prefix to apis
2025-02-17 13:41:11 +08:00
Wu Clan 4d13424b30 Add plugin requirements auto install functions (#514) 2025-02-15 19:58:49 +08:00
Wu Clan 860c96d0ee Delete the threads in gunicorn config (#512) 2025-02-14 13:22:34 +08:00
Wu Clan d9985f9d4d Add plugin system and notice plugin (#503)
* Update system notice to plugin

* Add plugin model alembic support

* update plugin conf

* add plugin route injection

* update plugin route inject

* fix and optimize plugin router inject
2025-02-13 21:19:49 +08:00
Wu Clan 1e0b04006b Fix OAuth2 service user last login time (#510) 2025-02-11 18:10:38 +08:00
Wu Clan d419343939 Fix OAuth2 service user last login time (#509) 2025-02-11 17:57:05 +08:00
Wu Clan eb3fd55321 Fix OAuth2 service register user args (#508) 2025-02-11 17:48:25 +08:00
Wu Clan 4aa4301abd Fix the user pagination api return schema (#507)
* Fix the user pagination api return schema

* lint
2025-02-11 14:28:02 +08:00
Wu Clan 1d1a917580 Fix fastapi config variable naming and type (#506) 2025-02-08 11:47:45 +08:00
Wu Clan 0255283577 Fix the return datetime data encoder (#505) 2025-02-07 12:19:32 +08:00
Wu Clan 2df1f0f358 Bump dependencies and pre-commits (#504) 2025-02-07 12:09:25 +08:00
Wu Clan 52816dd4b4 Fix opera log of non-dict request body (#501) 2025-01-25 10:29:44 +08:00
Wu Clan 20e873b6e5 Fix casbin policy api return schema (#500) 2025-01-24 21:54:20 +08:00
Wu Clan ebe710fac2 Update menu field show to display (#498) 2025-01-23 22:46:05 +08:00
Wu Clan e588913d54 Fix current user info detail schema (#499) 2025-01-23 22:31:39 +08:00
Wu Clan 1c46e2480c Fix return schema of user me api (#497) 2025-01-23 21:52:16 +08:00
Wu Clan 3b65679fb0 Add token related interfaces (#495) 2025-01-23 13:12:46 +08:00
Wu Clan 7553ccf14d Update return schema of query interface (#492)
* Update return model of query interface

* Update tree data return type hints

* Update code generation and task return types

* Fix inconsistency between ORM query result and schema
2025-01-18 23:33:37 +08:00
qhp13654398483 cf693cdce6 Fix serialization when pagination is empty (#491)
* bug fix

* update codes
2025-01-18 20:47:31 +08:00
Wu Clan 3fe0023a0a Update redocs arg and url to redoc (#493) 2025-01-17 16:06:30 +08:00
Wu Clan 13cee674a7 Add response model include data schema (#490)
* Add response model include data schema

* Fix paging data usage

* optimize annotations
2025-01-17 12:39:24 +08:00
dividduang 64ee74bb38 Add system notice interface (#487) 2025-01-14 12:45:33 +08:00
Wu Clan 1e855dbb86 Optimize docker deploy settings and scripts (#486) 2025-01-13 17:33:54 +08:00
Wu Clan e0606d115c Update the route version define location (#485)
* Update the route version define location

* fix lint
2025-01-10 14:00:26 +08:00
Wu Clan 8b159536ce Fix user cache when updated user role (#483) 2025-01-03 18:53:26 +08:00
Wu Clan 2279ff7c58 Bump msgspec from 0.18.6 to 0.19.0 (#482) 2024-12-28 17:49:28 +08:00
Wu Clan 3f6c43e15d Update opera middleware request args parse (#481) 2024-12-27 18:27:05 +08:00
Wu Clan 6c1f68ec00 Update opera log cost time precision (#479) 2024-12-25 22:09:27 +08:00
Meepoljdx 78dad3021a Add postgresql database support (#475)
* feat: add postgresql db supports

* change: change mysql conn str create way

* fix: Modify the default alembic migration file to meet multi-database support

* Update settings and lint

* update models

* Simplify database config

* Simplify the get db method

* Update create db url

* Updated model type adaptation

* Update sql scripts

* Fix models type

* Adaptation to postgresql code generation

* Update README.md

* Fix alembic file template

* Update docker scripts
2024-12-25 22:04:04 +08:00
Wu Clan 4f59f459a0 Remove data scope in the role model (#478) 2024-12-24 17:29:17 +08:00
Wu Clan 58b24b62ac Fix login log parameter error in task (#476) 2024-12-13 16:40:48 +08:00
Wu Clan a9b9b58290 Fix and update alembic env and ini (#474) 2024-12-11 19:55:46 +08:00
Wu Clan f90b4667c3 Fix data rule expression column comment (#473) 2024-12-09 12:21:40 +08:00
Wu Clan 3b2b45d6a5 Optimized user auth for auth service (#472) 2024-12-07 14:03:30 +08:00
Wu Clan 8c04bcb981 Add the project logo to README (#469)
* Add the project logo to README

* update
2024-12-01 23:43:04 +08:00
Wu Clan 7c4e288fd6 Simplify data rule and remove type (#468)
* Simplify data rule and remove type

* fix lint

* fix user info cache
2024-11-27 14:09:57 +08:00
Wu Clan 1838b63b11 Fix schema type of user role rule (#467)
* Fix schema type of user role rule

* Fix sql scripts
2024-11-24 16:42:40 +08:00
dependabot[bot] 6d5d1a48f3 Bump tornado from 6.4.1 to 6.4.2 in /backend (#466)
Bumps [tornado](https://github.com/tornadoweb/tornado) from 6.4.1 to 6.4.2.
- [Changelog](https://github.com/tornadoweb/tornado/blob/v6.4.2/docs/releases.rst)
- [Commits](https://github.com/tornadoweb/tornado/compare/v6.4.1...v6.4.2)

---
updated-dependencies:
- dependency-name: tornado
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-11-23 21:35:05 +08:00
Wu Clan bd45022a59 Update role-based data permissions (#465)
* Add department data operation permissions

* Update sys models

* Add role department many-to-many relationship

* Format RBAC code

* update codes

* add update role depts api

* add comments

* Update the implementation

* update dept arg

* debug front

* Update the model file naming

* Refactor the data perms

* Add data permission rule

* Optimize the details

* Implement data filtering

* finish
2024-11-23 21:23:52 +08:00
Wu Clan b29a3cbfb7 Update user password encryption method (#463) 2024-11-18 14:07:05 +08:00
Wu Clan a15693cc26 Fix celery service functions error (#462) 2024-11-16 17:13:29 +08:00
Wu Clan 5e60d9c248 Update api body params to schema (#461) 2024-11-16 11:07:40 +08:00
Wu Clan 12d7ec8174 Update code generator API file structure (#460) 2024-11-15 17:03:35 +08:00
Wu Clan 601a6514a8 Update some service class invocations (#459) 2024-11-15 17:02:52 +08:00
Wu Clan fc5a0f9dfa Update the celery configuration and tasks (#458)
* Update the celery configuration and tasks

* fix message notifications
2024-11-15 14:29:02 +08:00
Wu Clan 576ed77d36 Fix code generation file missing (#457) 2024-11-13 19:26:17 +08:00
Wu Clan 06cbe56201 Fix typo in contribution description (#456) 2024-11-13 13:40:37 +08:00
Wu Clan 87eec419e6 Update multiple version dependency specifiers (#454) 2024-11-12 19:52:42 +08:00
Wu Clan 0e7cef5b6c Update system config to dynamic (#447)
* Update system config to be dynamic

* update the configuration interface

* custom parameters are allowed

* update apis

* add more code

* finish

* fix bugs
2024-11-10 07:30:05 +08:00
Wu Clan 5c0b107142 Update singleton pattern class typing (#452) 2024-11-08 12:16:13 +08:00
Wu Clan 0cfeeb4e45 Bump sqlalchemy crud plus version to 1.5.0 (#450) 2024-11-07 13:42:40 +08:00
Wu Clan aabb72138a Update the usage documentation in README (#449) 2024-11-07 11:30:00 +08:00
Wu Clan 0a1f0a90b2 Add dependency-groups by PEP 735 (#444) 2024-11-03 18:56:07 +08:00
Wu Clan 493c2b2545 Add asynchronous socketio application server (#437)
* Add asynchronous socketio application server

* fix uv lock

* Update cors

* fix socket app
2024-11-02 00:12:01 +08:00
Wu Clan 0810647c48 Update project manager pdm to uv (#440)
* Update project manager pdm to uv

* Fix ci venv

* Fix ci venv source
2024-10-12 18:09:41 +08:00
Wu Clan a3d079cfdf Optimize the serialize return of SQLA select (#436) 2024-10-10 12:11:05 +08:00
Wu Clan c22d1394e7 Update interactive link descriptions (#434) 2024-10-08 22:12:46 +08:00
Wu Clan 03add792a1 Bump redis from 5.0.1 to 5.1.0 (#433)
* Bump redis from 5.0.1 to 5.1.0

* fix requirements.txt
2024-09-28 15:05:18 +08:00
Wu Clan ab4495ce65 Optimize and normalize the code generator (#430) 2024-09-24 13:50:02 +08:00
Wu Clan 1d3b0e7d13 Update official documentation link to README (#429) 2024-09-22 15:56:27 +08:00
Wu Clan 36a458b957 Fix the OAuth2 service login log task (#428) 2024-09-22 12:54:31 +08:00
Wu Clan 23d9d17d9f Fix pydantic field and model validator (#427) 2024-09-21 17:59:52 +08:00
Wu Clan 8da993655b Add request state middleware (#426)
* Add request state middleware

* Fix linux do OAuth2 redirect uri
2024-09-21 17:57:27 +08:00
Wu Clan 6792bf9954 Fix the missing OAuth2 interface parameters (#425) 2024-09-20 20:12:13 +08:00
Wu Clan 6cf30b3c5d Update synchronous function calls in JWT (#423) 2024-09-18 22:24:05 +08:00
Wu Clan eb23d1b5f9 Update multi login sync update refresh tokens (#422) 2024-09-18 22:23:40 +08:00
Wu Clan 29ba40ef32 Update JWT errors class import (#421) 2024-09-13 12:02:20 +08:00
Wu Clan 02f65abf74 Fix for create new token cache delete (#420) 2024-09-13 12:01:49 +08:00
Wu Clan f5b7de6910 Optimize the internal implementation of serializers (#419) 2024-09-11 16:40:05 +08:00
Wu Clan ac8e4a0406 Fix the gen model template formatting (#416) 2024-09-10 16:26:23 +08:00
Wu Clan b6821612af Fix exception logging in opera log (#417) 2024-09-10 16:19:19 +08:00
Wu Clan 8783d74cc7 Fix log output and logging levels (#414)
* Fix log output and logging levels

* update record order
2024-09-09 23:14:56 +08:00
Wu Clan 5a52871e7e Optimize exception info opera log record (#413) 2024-09-09 22:52:05 +08:00
Wu Clan 90b2cca984 Bump pydantic from 2.8.1 to 2.9.1 (#412) 2024-09-09 20:46:10 +08:00
Wu Clan 9c37297858 Update the global exception log stack (#406)
* Update the global exception log stack

* del todo
2024-09-09 15:56:38 +08:00
Wu Clan 1f95a776f0 Add trace ID to exception handlers (#411) 2024-09-09 15:46:13 +08:00
Wu Clan b13410c7d1 Optimize the naming of setting params (#410) 2024-09-09 11:50:22 +08:00
Wu Clan 9aa2d7b593 Add request trace ID record (#409)
* Fix cookie expiration time zone

* Fix the loss of cross-domain cookies

* Add request trace ID record

* Fix the dependency lock file
2024-09-09 10:43:34 +08:00
Wu Clan 1d956cace4 Fix cookie expiration time zone (#408)
* Fix cookie expiration time zone

* Fix the loss of cross-domain cookies
2024-09-08 17:33:51 +08:00
Wu Clan 4f5c5c28e6 Fix server monitor io blocking (#404) 2024-09-05 22:46:31 +08:00
Wu Clan 24528e6050 Updated refresh token storage logic (#403) 2024-09-05 21:37:25 +08:00
Wu Clan 7853233ddb Update the interactive link address (#402) 2024-09-04 20:06:25 +08:00
Wu Clan 51f3011947 Update code generate comment column format (#399) 2024-09-03 14:40:22 +08:00
Wu Clan 87432a74c2 Fix the user permissions update services (#397) 2024-09-01 20:23:25 +08:00
Wu Clan e19ae09966 Fix OAuth2 user query conditions (#396) 2024-09-01 20:22:58 +08:00
Wu Clan 8765f833d2 Fix code generation to new features (#393) 2024-08-31 18:08:24 +08:00
Wu Clan c22202eac4 Update the README announcement to note (#390)
* Update the README announcement to note

* delete English README some content
2024-08-28 10:30:26 +08:00
Wu Clan a698373064 Bump sqlalchemy crud plus version to 1.3.0 (#388) 2024-08-25 23:02:13 +08:00
Wu Clan b43c08b5d2 Update redis cache prefix separator to : (#387)
* Update redis cache prefix separator to `:`

* Update refresh token redis prefix
2024-08-22 12:28:05 +08:00
Wu Clan 2002068d81 Fix logging when a login error occurs (#386) 2024-08-22 12:26:13 +08:00
六月andliuyue bb0d7f3f56 Fix arm system cpu frequency retrieval (#385)
* Fix: Disable CPU frequency retrieval on macOS systems with Apple Silicon (closes #384)

* Fix: Solve the single quote problem

---------

Co-authored-by: liuyue <yshan2028@gmail.com>
2024-08-21 21:37:26 +08:00
Wu Clan c743182211 Optimize serialization and jwt performance (#382)
* Optimize serialization and algorithm performance

* Fix get tree data to async

* Removed CPU-intensive multi-threaded execution

* Update pdm pre commit to 2.18.0

* Add jwt user info cache

* Update ci

* Fix ci

* Fix redis delete prefix

* Adapt to JWT user instance

* Update jwt middleware token parse
2024-08-17 09:26:16 +08:00
Wu Clan 22586dd947 Add directory tree and update app notes (#380) 2024-08-13 10:45:54 +08:00
Wu Clan f61dc4fc84 Update user and auth error message (#379) 2024-08-11 21:54:46 +08:00
Wu Clan 144d704a5d Fix code generation model create and update (#378) 2024-08-11 21:32:09 +08:00
Wu Clan f30a18844e Remove current menu status auth (#374) 2024-08-11 21:00:27 +08:00
Wu Clan 043a622e67 Update code generate download api auth (#376) 2024-08-04 19:43:03 +08:00
Wu Clan a602dff739 Update some code generation api and params (#373)
* Update some code generation api and params

* Update get business api query

* Add get business models api

* Fix get business models api serialize

* Add get generate code path api

* Update download method post to get

* Add get model detail api

* Fix crud model method

* Fix get model api return

* Fix update model service

* Update create update model service

* Add code generate sql

* Add code generate api permissions

* Update code gen path join
2024-08-04 15:24:09 +08:00
Wu Clan 863dfed4ec Add custom code template pathname config (#372) 2024-07-28 12:38:05 +08:00
Wu Clan ceb8fb1cb2 Update code generate business model datetime column (#371) 2024-07-28 11:58:46 +08:00
Wu Clan 21fed49d7b Update access log cost time style (#370) 2024-07-27 12:04:22 +08:00
Wu Clan c9be643e76 Update create new token function return type (#369)
* Update create new token function return type

* update args input
2024-07-27 12:03:33 +08:00
Wu Clan 7287093772 Update operation log middleware info reading (#368)
* Fix operation log middleware summary reading

* update ua info and ip info

* update opera middle functions

* move dataclasses.py

* fix lint
2024-07-27 11:00:36 +08:00
Wu Clan 2112523b4c Fix celery asynchronous task execution (#367) 2024-07-26 19:00:38 +08:00
Wu Clan 34506c3039 Add page to display dynamic configuration (#365)
* Add page to display dynamic configuration

* update config data and api

* add config route

* update config api route

* update config api route

* fix config schema

* update config services
2024-07-25 19:11:06 +08:00
Wu Clan f94c2d7948 Fix code auto-generated model creation (#364) 2024-07-23 17:58:23 +08:00
Wu Clan 294d7ae9e9 Update crud user staff field logic (#363) 2024-07-23 15:48:40 +08:00
Wu Clan a0d47733a9 Bump fast captcha version to 0.3.2 (#362) 2024-07-22 23:31:00 +08:00
Wu Clan 581bed2a33 Update oauth2 route naming and return (#361)
* Update oauth2 route naming and storage

* Update some arg naming

* Update oauth2 redirect uri

* adapts to the front-end oauth2
2024-07-22 13:47:43 +08:00
Wu Clan f9c4028c60 Update the docker-compose deployment script (#360)
* Update the docker-compose deployment script

* fix typo

* Update docker compose fba_ui conf

* Update the docker compose fba_ui conf

* Add docker compose image repository name

* Update docker compose fba_ui comment
2024-07-21 09:55:39 +08:00
Wu Clan 9752ce8e92 Fix alembic migration failure caused by model (#359) 2024-07-18 20:22:34 +08:00
Wu Clan 3cfc95cb02 Add code generator to create init files (#358) 2024-07-18 18:05:56 +08:00
Wu Clan 3d12c3270e Fix gen model and schema template formatting (#356)
* Fix gen model column default value

* Update gen templates and schema_name storage

* Fix lint
2024-07-17 17:06:55 +08:00
Wu Clan 812b8a0fb7 Update code generation model column type storage (#352) 2024-07-12 21:21:01 +08:00
Wu Clan e5262f0b32 Fix model template conditional syntax (#351)
* Fix model template conditional syntax

* Update README
2024-07-12 16:42:30 +08:00
Wu Clan 42ee5d7355 Add code generator README document (#349)
* Update and enable access log middleware

* Add code generator README document

* additional doc
2024-07-10 17:08:15 +08:00
Wu Clan 5a283b1c3c Update and enable access log middleware (#348) 2024-07-10 16:47:23 +08:00
Wu Clan f9ec9d5d56 Add code generator app (#318)
* Add code generator app

* Define the generator interfaces

* Update the name of the template files

* update lock hash

* update route and schemas

* update gen code structure

* add some gen codes

* upgrade SQLAlchemy to release

* update template util

* Fix lint error

* Add template rendering code

* Add gen model code

* Fix gen model table

* Update business model column naming

* Update any route

* Fix api ninja args

* Update gen business model

* Fix interface services logic

* Fix template render

* Add more code template

* Add model auto gen template

* Fix model type map style

* Update schema and model templates

* Update gen_model schema

* Fix model template if

* Update have_datetime_column field from gen_model to gen_business table

* Update the name of the zip

* Update model template vars

* Add download generate code

* Add preview code encoding

* Update service code template

* fix lint

* Update github auth tag

* Update requirements

* Add code generation write

* Update generate interface and zip conf

* Optimize templates details

* Update model relation column conf

* Add import tables module

* Add get all databases interface

* Fix get tables interface

* Bump pydantic to 2.8.1

* Upgrading lint dependencies

* Add import business and model implement

* Split import database execution code

* Update text SQL execution to prevent SQL injection

* Optimize schema name generation

* clean code

* Update create tables sql scripts

* Update import sql query conditions

* Add todo comments

* Mark business creation interface

* Update features in README
2024-07-10 12:12:29 +08:00
obrua 3b060c73b2 Refactor global log default handler (#347)
* 🦄 refactor: optimize log output

Unified log output using the loguru logging library

* 🌈 style: pre-commit fix

* 🌈 style: pre-commit fix

* Clean up and update some code

* Update default log output style
2024-07-09 18:40:25 +08:00
Wu Clan 85c16de32b Fix user social binding query (#344) 2024-07-08 16:27:26 +08:00
Wu Clan 9aa74afff1 Add Linux Do OAuth2 login (#343)
* Add Linux Do OAuth2 login

* Adapt to linux do oauth2 email field

* Fix get user email logic

* Fix lint
2024-07-07 17:39:54 +08:00
Wu Clan f21af0c3c3 Update app route definition rules (#341) 2024-07-02 09:29:22 +08:00
Wu Clan af101d57fe Update the tree structure traversal algorithm (#338) 2024-07-02 08:49:40 +08:00
Wu Clan 1ad5482a22 Restore the main startup of fastapi service (#336) 2024-06-16 14:09:14 +08:00
Wu Clan 805a5a4e96 Add test account to README (#330) 2024-06-08 23:47:35 +08:00
Wu Clan 08a8acb9ea Update the opera log middleware task (#326)
* Update the opera log middleware task

* Delete task await
2024-06-03 12:58:05 +08:00
Wu Clan dd474cdc08 Add interface fast response method (#327) 2024-06-03 12:46:05 +08:00
Wu Clan 84c16bc983 Delete the gzip middleware to improve performance (#325) 2024-06-03 12:45:42 +08:00
Wu Clan 2589ddcdf6 Upgrade and update fastapi service startup (#319) 2024-05-04 22:39:09 +08:00
Wu Clan eb8dfda8fa Update CRUDBase to sqlalchemy-crud-plus (#317)
* Update CRUDBase to dependent package

* Update sqla crud plus to 0.0.2
2024-04-27 23:07:40 +08:00
Wu Clan 9edb72d065 Fix missing data from alembic migration (#312) 2024-04-12 11:38:18 +08:00
Wu Clan 406116a230 Add backend scripts description (#309) 2024-03-31 19:48:58 +08:00
Wu Clan 8a165ec181 Fix English README.md update date (#308) 2024-03-22 18:24:03 +08:00
Wu Clan 5e438c685d Refactor the backend architecture (#299)
* define the basic architecture

* Update script and deployment file locations

* Update the route registration

* Fix CI download dependencies

* Updated ruff to 0.3.3

* Update app subdirectory naming

* Update the model import

* fix pre-commit pdm lock

* Update the service directory naming

* Add CRUD method documents

* Fix the issue of circular import

* Update the README document

* Update the SQL statement for create tables

* Update docker scripts and documentation

* Fix docker scripts

* Update the backend README.md

* Add the security folder and move the redis client

* Update the configuration item

* Fix environment configuration reads

* Update the default configuration

* Updated README description

* Updated the user registration API

* Fix test cases

* Update the celery configuration

* Update and fix celery configuration

* Updated the celery structure

* Update celery tasks and api

* Add celery flower

* Update the import style

* Update contributors
2024-03-22 18:16:15 +08:00
Wu Clan a98621b40d Update the README.md branch prompt (#302) 2024-03-21 12:13:00 +08:00
Wu Clan 01ee7deeff Prepare to lock the legacy branch (#301)
* Prepare to lock the legacy branch

* Update README.md

* Fix typo

* Fix the docker environment file
2024-03-21 10:56:12 +08:00
Wu Clan 3b64aeb97b Add OAuth 2.0 authorization login (#293)
* [WIP] Add OAuth 2.0 authorization login

* Add social user relationship table

* Update social user relationship table back_populates

* Add OAuth 2.0 related interface

* Automatically redirect authorization addresses

* Update OAuth2 authorization to GitHub

* Add implementation code

* fix the callback interface return

* fix typo

* fix the api return

* fix imports

* Fix logic for creating system users and social tables

* Fix user information storage

* Add OAuth2 source link

* remove unnecessary db refresh

* remove the front end docker-compose annotation
2024-03-15 13:46:43 +08:00
Wu Clan 594efb5e96 Add the repository star map (#296) 2024-03-15 10:41:07 +08:00
Wu Clan 8af4336428 Update interface file directory level (#295)
* Update interface file directory level

* fix ruff format
2024-02-25 20:48:25 +08:00
Wu Clan 115b6f0b30 Update the response status code in exception handlers (#292) 2024-02-20 10:40:21 +08:00
Wu Clan 2bad404913 Add the front-end docker-compose script (#283)
* Add the front-end docker-compose script

* Update the file content location

* fix typo

* Update the default nginx docker compose

* Add an online demo document
2024-02-03 20:41:41 +08:00
Wu Clan 7e633dbdd5 Add the pdm project manager (#282)
* Add the pdm project manager

* Update the interactive links

* fix typo
2024-02-01 17:37:29 +08:00
Wu Clan 2ae020e70a Restore the Github ci workflows (#281)
* Restore the Github ci workflows

* upgrade actions version
2024-01-31 10:37:41 +08:00
Wu Clan 94cbc629cd Fix custom validator exception serialization in dev mode (#278)
* Fix custom validator exception serialization in dev mode

* Fix formatting judgment conditions
2024-01-24 10:17:20 +08:00
Wu Clan 4f1d1c2094 Add custom email string type (#277) 2024-01-24 10:17:04 +08:00
Wu Clan cd96d2f5cd Update dao and service instantiation styles (#276)
* Update dao and service instantiation styles

* Fix auth_service use
2024-01-22 10:22:13 +08:00
Wu Clan 576160129a Update interface coding style (#275) 2024-01-20 19:58:19 +08:00
Wu Clan 754b9d9edc Fix dept and menu parent id update logic (#274) 2024-01-20 19:58:06 +08:00
Wu Clan b5c656f308 Update sponsor links and FUNDING (#273) 2024-01-18 23:06:42 +08:00
Wu Clan a41b1673e5 Update schemas naming style (#272) 2024-01-18 19:27:41 +08:00
Wu Clan a22aaa0a3e Attempt to optimize serialization performance (#266)
* Attempt to serialize performance optimization

* Add casbin service functions return type

* update comments
2024-01-11 20:15:03 +08:00
Wu Clan 5c7d6659cf Reconstruct RBAC authentication logic (#264)
* Reconstruct RBAC authentication logic

* fix typo

* Migrate casbin sqla Adapter to redis

* Delete casbin model conf file

* Add permission dependencies

* Add request permission depends on execution condition

* Update openapi authorization method

* Add request permission identity

* Add request permission dependency description

* Migrate casbin redis adapter to sqla

* Update menu model and add function

* Fix menu permission identification

* Update user partial interface permissions

* Update menu table SQL

* Add role menu permission description to README

* fix README typo

* Simplify permission dependency injection

* Fix menu authorization store

* Fix interface permission dependency order

* Update role menu permission flag

* Update the background permission logic of the interface
2024-01-08 09:35:47 +08:00
Wu Clan f39d11eb9d Fix use request.form() in middleware (#260)
* Fix request.form() in middleware

* resume global exception handling

* update the order in which data is processed

* Add demo interface for uploading files

* fix path params
2024-01-03 22:23:56 +08:00
Wu Clan 227221ff67 Clean up outdated pydantic dict methods (#262) 2024-01-02 22:05:20 +08:00
Wu Clan 27d229c250 Add the project status page to the README (#259) 2023-12-23 18:24:52 +08:00
Wu Clan 754498be0b Add pydantic-v2 migration reminder (#257)
* Add pydantic-v2 migration reminder

* Fix format rendering

* Beautify the date format

* Add celebratory content
2023-12-21 00:38:59 +08:00
Wu Clan 7bfc7908ab Migrate to pydantic-v2 (#246)
* update the schema to pydantic-v2

* upgrade the ruff version in pre-commit

* update pagination to p-v2

* update encoder to p-v2

* update response_schema to p-v2

* update exception_handler to p-v2

* update config to p-v2

* update crud base to p-v2

* Migrate the JSON Encoder to the official one to reduce maintenance costs

* updated readme description

* restore the bcrypt compatible version

* fix validation error log code return format

* Fix manual processing method for GetCurrentUserInfo

* Integrate pydantic validate exceptions and user exceptions

* Fix validation exception handler

* add custom open response code

* Add a stand-alone assertion error handler

* Restore todo deleted by v1 branch to v2

* Add email and phone number types
2023-12-20 23:56:37 +08:00
556 changed files with 43577 additions and 7850 deletions
+13
View File
@@ -0,0 +1,13 @@
__pycache__/
.git/
.github/
.idea/
.vscode/
.cursor/
venv/
.venv/
.ruff_cache/
.pytest_cache/
.claude/
.serena/
.logs/
+1
View File
@@ -0,0 +1 @@
custom: https://wu-clan.github.io/sponsor/
+25
View File
@@ -0,0 +1,25 @@
name: Release changelog
on:
workflow_run:
workflows: ['Check version']
types:
- completed
jobs:
changelog:
runs-on: ubuntu-latest
if: ${{ github.event.workflow_run.conclusion == 'success' }}
steps:
- name: Checkout code
uses: actions/checkout@v6
with:
ref: master
- uses: rhysd/changelog-from-release/action@v3
with:
file: CHANGELOG.md
pull_request: true
commit_summary_template: 'Update the changelog for %s'
args: -c
github_token: ${{ secrets.GH_TOKEN }}
+34
View File
@@ -0,0 +1,34 @@
name: ci
on:
push:
branches:
- master
pull_request:
jobs:
lint:
runs-on: ubuntu-latest
name: lint ${{ matrix.python-version }}
strategy:
matrix:
python-version: [ '3.10', '3.11', '3.12', '3.13', '3.14' ]
fail-fast: false
steps:
- uses: actions/checkout@v6
- name: Install uv
uses: astral-sh/setup-uv@v7
- name: Set up Python ${{ matrix.python-version }}
run: uv python install ${{ matrix.python-version }}
- name: Install dependencies
run: |
uv sync --only-group lint
- name: Run lint
run: |
source .venv/bin/activate
chmod 755 backend/scripts/lint.sh
./backend/scripts/lint.sh
+25
View File
@@ -0,0 +1,25 @@
name: Check version
on:
push:
tags:
- v*
jobs:
check-version:
name: check version
runs-on: ubuntu-latest
if: startsWith(github.ref, 'refs/tags/')
steps:
- uses: actions/checkout@v6
- name: Install uv
uses: astral-sh/setup-uv@v7
- name: Set up Python
run: uv python install 3.13
- name: Check the package version
uses: samuelcolvin/check-python-version@v5
with:
version_file_path: backend/__init__.py
+13 -6
View File
@@ -1,10 +1,17 @@
__pycache__/
.idea/
.env
.vscode/
.cursor/
.DS_Store
venv/
.mypy_cache/
backend/app/log/
backend/app/alembic/versions/
backend/app/static/media/
.venv/
.python-version
.ruff_cache/
.pytest_cache/
logs/
.claude/
.serena/
.agents/
.superpowers/
superpowers/
.codegraph/
.pi/
+27 -7
View File
@@ -1,19 +1,39 @@
default_language_version:
python: '>= 3.10'
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
rev: v6.0.0
hooks:
- id: check-added-large-files
- id: end-of-file-fixer
- id: requirements-txt-fixer
- id: check-json
- id: check-yaml
- id: check-toml
- repo: https://github.com/tombi-toml/tombi-pre-commit
rev: v1.3.0
hooks:
- id: tombi-lint
args: ["--offline"]
- id: tombi-format
args: ["--offline"]
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.1.6
rev: v0.16.3
hooks:
- id: ruff
- id: ruff-check
args:
- '--config'
- '.ruff.toml'
# https://github.com/astral-sh/ruff-pre-commit/issues/64
- '--fix'
- '--unsafe-fixes'
- id: ruff-format
- repo: https://github.com/astral-sh/uv-pre-commit
rev: 0.12.3
hooks:
- id: uv-lock
- id: uv-export
args:
- '-o'
- 'requirements.txt'
- '--no-hashes'
-42
View File
@@ -1,42 +0,0 @@
line-length = 120
target-version = "py310"
cache-dir = "./.ruff_cache"
[per-file-ignores]
"backend/app/api/v1/*.py" = ["TCH"]
"backend/app/models/*.py" = ["TCH003"]
"backend/app/**/__init__.py" = ["F401"]
"backend/app/tests/*.py" = ["E402"]
[lint]
select = [
"E",
"F",
"I",
"W505",
"PT018",
"SIM101",
"SIM114",
"PGH004",
"PLE1142",
"RUF100",
"F404",
"TCH",
"UP007"
]
[lint.flake8-pytest-style]
mark-parentheses = false
parametrize-names-type = "list"
parametrize-values-row-type = "list"
parametrize-values-type = "tuple"
[lint.flake8-unused-arguments]
ignore-variadic-names = true
[lint.isort]
lines-between-types = 1
order-by-type = true
[format]
quote-style = "single"
+153
View File
@@ -0,0 +1,153 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "FBA Plugin Manifest Schema",
"description": "JSON Schema for FastAPI Best Architecture plugin.toml files. See: https://fastapi-practices.github.io/fastapi_best_architecture_docs/plugin/dev.html",
"type": "object",
"required": ["plugin"],
"additionalProperties": false,
"properties": {
"plugin": {
"type": "object",
"description": "Plugin metadata",
"required": ["summary", "version", "description", "author", "tags"],
"additionalProperties": false,
"x-tombi-table-keys-order": "schema",
"properties": {
"icon": {
"type": "string",
"description": "Icon path (plugin repository icon path or URL)"
},
"summary": {
"type": "string",
"minLength": 1,
"maxLength": 100,
"description": "Brief summary (1-100 characters)"
},
"version": {
"type": "string",
"pattern": "^\\d+\\.\\d+\\.\\d+$",
"description": "Plugin version (semver format: x.y.z)"
},
"description": {
"type": "string",
"minLength": 1,
"maxLength": 500,
"description": "Detailed description (1-500 characters)"
},
"author": {
"type": "string",
"minLength": 1,
"maxLength": 50,
"description": "Plugin author (1-50 characters)"
},
"tags": {
"type": "array",
"minItems": 1,
"description": "Plugin tags for categorization",
"items": {
"type": "string",
"enum": ["ai", "mcp", "agent", "auth", "storage", "notification", "task", "payment", "other"]
},
"x-tombi-array-values-order": "ascending"
},
"database": {
"type": "array",
"minItems": 1,
"description": "Supported databases. Required for app-level and extend-level plugins, optional for capability-level plugins without models.",
"items": {
"type": "string",
"enum": ["mysql", "postgresql"]
},
"x-tombi-array-values-order": "ascending"
},
"depends_on": {
"type": "array",
"description": "List of plugin names this plugin depends on (used for startup order)",
"items": {
"type": "string",
"minLength": 1
},
"x-tombi-array-values-order": "ascending"
}
}
},
"app": {
"type": "object",
"description": "Application configuration. For app-level plugins: use 'router'. For extend-level plugins: use 'extend'.",
"additionalProperties": false,
"minProperties": 1,
"x-tombi-table-keys-order": "schema",
"properties": {
"extend": {
"type": "string",
"minLength": 1,
"description": "Parent app folder name (for extension-level plugins)"
},
"router": {
"type": "array",
"minItems": 1,
"description": "Router instances (for application-level plugins)",
"items": {
"type": "string",
"minLength": 1
},
"x-tombi-array-values-order": "version-sort"
}
}
},
"settings": {
"type": "object",
"description": "Plugin base configuration (hot-pluggable, uppercase keys only)",
"x-tombi-additional-key-label": "SETTING_NAME",
"x-tombi-table-keys-order": "ascending",
"propertyNames": {
"pattern": "^[A-Z][A-Z0-9_]*$"
},
"additionalProperties": {
"oneOf": [
{ "type": "string" },
{ "type": "number" },
{ "type": "boolean" },
{
"type": "array",
"items": { "type": "string" }
}
]
}
},
"api": {
"type": "object",
"description": "API endpoint configurations (for extension-level plugins). The key (e.g., 'xxx' in api.xxx) corresponds to the API filename without extension.",
"x-tombi-additional-key-label": "api_filename",
"x-tombi-table-keys-order": "ascending",
"minProperties": 1,
"propertyNames": {
"pattern": "^[a-zA-Z_][a-zA-Z0-9_]*$"
},
"additionalProperties": {
"type": "object",
"required": ["prefix", "tags"],
"additionalProperties": false,
"x-tombi-table-keys-order": {"properties": "schema"},
"properties": {
"prefix": {
"type": "string",
"minLength": 1,
"pattern": "^/[a-zA-Z0-9_/-]*$",
"description": "URL prefix for the API (must start with '/', allowed chars: a-z, A-Z, 0-9, _, -, /)"
},
"tags": {
"type": "string",
"minLength": 1,
"description": "OpenAPI tags for Swagger documentation"
}
}
}
}
}
}
+1651
View File
File diff suppressed because it is too large Load Diff
+60
View File
@@ -0,0 +1,60 @@
## Backend
1. Prerequisites
- Python >= 3.10
- Git
- [uv](https://docs.astral.sh/uv/getting-started/installation/)
- Fork this repository to your GitHub account
2. Installation and setup
Clone your forked repository:
```shell
git clone https://github.com/<your account>/fastapi_best_architecture.git
```
Go to the root directory of the project, open the terminal, and run the following command:
```sh
uv run fba init --auto
```
3. Checkout
Checkout a new branch and make your changes
```shell
git checkout -b your-new-feature-branch
```
4. Format and Lint
```shell
fba format
```
5. Commit and push
Commit your changes and push your branch to the GitHub.
6. PR
Create a PR via GitHub
## Scripts
> [!WARNING]
>
> The following script may not apply to the Windows platform
>
> It is recommended to execute under the backend directory, and chmod authorization may be required
- `migrate.sh`: Perform automatic database migration
- `scripts/format.sh`: Perform ruff format with preview
- `scripts/lint.sh`: Perform prek formatting
- `scripts/export.sh`: Execute uv export dependency package
+88
View File
@@ -0,0 +1,88 @@
# Select the image to build based on SERVER_TYPE, defaulting to fba_server, or docker-compose build args
ARG SERVER_TYPE=fba_server
# === Python environment from uv ===
FROM ghcr.io/astral-sh/uv:python3.10-trixie-slim AS builder
# Used for build Python packages
RUN sed -i 's/deb.debian.org/mirrors.ustc.edu.cn/g' /etc/apt/sources.list.d/debian.sources \
&& apt-get update \
&& apt-get install -y --no-install-recommends gcc make python3-dev \
&& rm -rf /var/lib/apt/lists/*
COPY . /fba
WORKDIR /fba
# Configure uv environment
ENV UV_COMPILE_BYTECODE=1 \
UV_NO_CACHE=1 \
UV_LINK_MODE=copy \
UV_PROJECT_ENVIRONMENT=/usr/local
# Install dependencies with cache
RUN --mount=type=cache,target=/root/.cache/uv \
--mount=type=bind,source=uv.lock,target=uv.lock \
--mount=type=bind,source=pyproject.toml,target=pyproject.toml \
uv sync --locked --no-default-groups --group server --no-install-project
# Preinstall plugin dependencies
RUN --mount=type=cache,target=/root/.cache/uv \
python -c "from backend.plugin.requirements import install_requirements; install_requirements(None)"
# === Runtime base server image ===
FROM ghcr.io/astral-sh/uv:python3.10-trixie-slim AS base_server
RUN sed -i 's/deb.debian.org/mirrors.ustc.edu.cn/g' /etc/apt/sources.list.d/debian.sources \
&& apt-get update \
&& apt-get install -y --no-install-recommends curl ca-certificates supervisor \
&& rm -rf /var/lib/apt/lists/*
COPY --from=builder /fba /fba
COPY --from=builder /usr/local /usr/local
COPY deploy/backend/supervisor/supervisord.conf /etc/supervisor/supervisord.conf
# === FastAPI server image ===
FROM base_server AS fba_server
COPY deploy/backend/supervisor/fba_server.conf /etc/supervisor/conf.d/
RUN mkdir -p /var/log/fba
EXPOSE 8001
CMD ["supervisord", "-c", "/etc/supervisor/supervisord.conf"]
# === Celery Worker image ===
FROM base_server AS fba_celery_worker
COPY deploy/backend/supervisor/fba_celery_worker.conf /etc/supervisor/conf.d/
RUN mkdir -p /var/log/fba
CMD ["supervisord", "-c", "/etc/supervisor/supervisord.conf"]
# === Celery Beat image ===
FROM base_server AS fba_celery_beat
COPY deploy/backend/supervisor/fba_celery_beat.conf /etc/supervisor/conf.d/
RUN mkdir -p /var/log/fba
CMD ["supervisord", "-c", "/etc/supervisor/supervisord.conf"]
# === Celery Flower image ===
FROM base_server AS fba_celery_flower
COPY deploy/backend/supervisor/fba_celery_flower.conf /etc/supervisor/conf.d/
RUN mkdir -p /var/log/fba
EXPOSE 8555
CMD ["supervisord", "-c", "/etc/supervisor/supervisord.conf"]
# Build image
FROM ${SERVER_TYPE}
+46 -204
View File
@@ -1,28 +1,35 @@
<div align="center">
<img alt="The logo includes the abstract combination of the three letters FBA, forming a lightning bolt that seems to spread out from the ground" width="320" src="https://wu-clan.github.io/picx-images-hosting/logo/fba.png">
# FastAPI Best Architecture
[![GitHub](https://img.shields.io/github/license/fastapi-practices/fastapi_best_architecture)](https://github.com/fastapi-practices/fastapi_best_architecture/blob/master/LICENSE)
[![Static Badge](https://img.shields.io/badge/python-3.10%2B-blue)](https://www.python.org/downloads/)
> [!TIP]
> You are viewing the pydantic-v1 branch, which is locked and no longer provides updates and fixes
Enterprise-level backend architecture solution
English | [简体中文](./README.zh-CN.md)
FastAPI framework based on the front-end and back-end separation of the middle and back-end solutions, follow
the [pseudo three-tier architecture](#pseudo-three-tier-architecture) design, support for **python3.10** and above
versions
[![GitHub](https://img.shields.io/github/license/fastapi-practices/fastapi_best_architecture)](https://github.com/fastapi-practices/fastapi_best_architecture/blob/master/LICENSE)
[![Python](https://img.shields.io/badge/python-3.10%2B-blue)](https://www.python.org/downloads/)
![MySQL](https://img.shields.io/badge/MySQL-8.0%2B-%2300758f)
![PostgreSQL](https://img.shields.io/badge/PostgreSQL-16.0%2B-%23336791)
![SQLAlchemy](https://img.shields.io/badge/SQLAlchemy-2.0-%23778877)
[![Pydantic v2](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/pydantic/pydantic/main/docs/badge/v2.json)](https://pydantic.dev)
[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)
[![uv](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/uv/main/assets/badge/v0.json)](https://github.com/astral-sh/uv)
![Docker](https://img.shields.io/badge/Docker-%232496ED?logo=docker&logoColor=white)
[![Discord](https://img.shields.io/badge/Discord-%235865F2.svg?logo=discord&logoColor=white)](https://discord.com/invite/yNN3wTbVAC)
![Discord](https://img.shields.io/discord/1185035164577972344)
[![Ask DeepWiki](https://deepwiki.com/badge.svg)](https://deepwiki.com/fastapi-practices/fastapi_best_architecture)
Its purpose is to allow you to use it directly as the infrastructure of your new project, this repository as a template
library open to any person or enterprise can be used for free!
</div>
**Continuously updated and maintained**
## Three-tier architecture
## Pseudo three-tier architecture
The MVC architecture is common in Python Web as a general design pattern, but the three-tier architecture is more
fascinating
The mvc architecture is a common design pattern in python web, but the three-tier architecture is even more fascinating.
In python web development, there is no common standard for the concept of three-tier architecture, so we'll call it a
pseudo three-tier architecture here
However, please note that we do not have a traditional multi-app (microservices) directory structure (django,
springBoot...). ), but a self-righteous directory structure that you can give it any twist if you don't like this model!
| workflow | java | fastapi_best_architecture |
|----------------|----------------|---------------------------|
@@ -32,171 +39,30 @@ pseudo three-tier architecture here
| data access | dao / mapper | crud |
| model | model / entity | model |
## Online preview
## Help
Unfortunately, we don't have the funds to provide an online preview, you can deploy by checking
out [local-development](#local-development), or directly using [Docker](#docker-deploy)
to deploy, or at [fastapi_best_architecture_ui](https://github.com/fastapi-practices/fastapi_best_architecture_ui)
See a preview of some of the screenshots
For more details, please check
the [official documentation](https://fastapi-practices.github.io/fastapi_best_architecture_docs/)
## Features
## Sponsors
- [x] Design with FastAPI PEP 593 Annotated Parameters
- [x] Global asynchronous design with async/await + asgiref
- [x] Follows Restful API specification
- [x] Global SQLAlchemy 2.0 syntax
- [x] Casbin RBAC access control model
- [x] Celery asynchronous tasks
- [x] JWT middleware whitelist authentication
- [x] Global customizable time zone time
- [x] Docker / Docker-compose deployment
- [x] Pytest Unit Testing
## Built-in features
1. [x] User management: system user role management, permission assignment
2. [x] Department Management: Configure the system organization (company, department, group...)
3. [x] Menu Management: Configuration of system menus, user menus, button privilege identification
4. [x] Role Management: Assign role menu privileges, assign role routing privileges
5. [x] Dictionary Management: Maintain common fixed data or parameters within the system.
6. [x] Operation Logs: logging and querying of normal and abnormal system operations.
7. [x] Login Authentication: graphical authentication code background authentication login
8. [x] Login Logs: Logging and querying of normal and abnormal user logins
9. [x] Service Monitoring: server hardware device information and status
10. [x] Scheduled tasks: automated tasks, asynchronous tasks, and function invocation are supported
11. [x] Interface Documentation: Automatically generate online interactive API interface documentation.
## Local development
* Python: 3.10+
* Mysql: 8.0+
* Redis: The latest stable version is recommended
* Nodejs: 14.0+
### BackEnd
1. Install dependencies
```shell
pip install -r requirements.txt
```
2. Create a database `fba`, choose utf8mb4 encoding
3. Install and start Redis
4. Create a `.env` file in the `backend/app/` directory
```shell
cd backend/app/
touch .env
```
5. Copy `.env.example` to `.env`
```shell
cp .env.example .env
```
6. Database migration [alembic](https://alembic.sqlalchemy.org/en/latest/tutorial.html)
```shell
cd backend/app/
# Generate migration file
alembic revision --autogenerate
# Execute the migration
alembic upgrade head
```
7. Start celery worker and beat
```shell
celery -A tasks worker --loglevel=INFO
# Optional, if you don't need to use the scheduled task
celery -A tasks beat --loglevel=INFO
```
8. Modify the configuration file as needed
9. Execute the `backend/app/main.py` file to start the service
10. Browser access: http://127.0.0.1:8000/api/v1/docs
---
### Front
Go to [fastapi_best_architecture_ui](https://github.com/fastapi-practices/fastapi_best_architecture_ui) for details
### Docker deploy
> [!WARNING]
> Default port conflict8000330663795672
>
> As a best practice, shut down on-premises services before deploymentmysqlredisrabbitmq...
1. Go to the directory where the ``docker-compose.yml`` file is located and create the environment variable
file ``.env``
```shell
cd deploy/docker-compose/
cp .env.server ../../backend/app/.env
# This command is optional
cp .env.docker .env
```
2. Modify the configuration file as needed
3. Execute the one-click boot command
```shell
docker-compose up -d --build
```
4. Wait for the command to complete automatically
5. Visit the browser: http://127.0.0.1:8000/api/v1/docs
## Test data
Initialize the test data using the `backend/sql/init_test_data.sql` file
## Development process
For reference only
### BackEnd
1. Define the database model (model) and remember to perform database migration for each change
2. Define the data validation model (schema)
3. Define routes (router) and views (api)
4. Define the business logic (service)
5. Write database operations (crud)
### Front
Go to [fastapi_best_architecture_ui](https://github.com/fastapi-practices/fastapi_best_architecture_ui) for details
## Test
Execute unittests via pytest
1. Create the test database `fba_test`, select utf8mb4 encoding
2. Using `backend/sql/create_tables.sql` file to create database tables
3. Initialize the test data using the `backend/sql/init_pytest_data.sql` file
4. Enter the app directory
```shell
cd backend/app/
```
5. Execute the test command
```shell
pytest -vs --disable-warnings
```
<div align="center">
<table>
<tr>
<td align="center">
<a href="https://claude.uy/home">
<img src="https://purple-sun-4f5a.wuyao1243.workers.dev/" alt="Claude.uy" width="400">
</a>
</td>
</tr>
</table>
</div>
## Contributors
<span style="margin: 0 5px;" ><a href="https://github.com/wu-clan" ><img src="https://images.weserv.nl/?url=avatars.githubusercontent.com/u/52145145?v=4&h=60&w=60&fit=cover&mask=circle&maxage=7d" /></a></span>
<span style="margin: 0 5px;" ><a href="https://github.com/downdawn" ><img src="https://images.weserv.nl/?url=avatars.githubusercontent.com/u/41266749?v=4&h=60&w=60&fit=cover&mask=circle&maxage=7d" /></a></span>
<a href="https://github.com/fastapi-practices/fastapi_best_architecture/graphs/contributors">
<img src="https://contrib.rocks/image?repo=fastapi-practices/fastapi_best_architecture"/>
</a>
## Special thanks
@@ -207,37 +73,13 @@ Execute unittests via pytest
- [Ruff](https://beta.ruff.rs/docs/)
- ...
## 互动
## Interactivity
有且仅有当前一个频道,请注意辨别真伪
<table>
<tr>
<td><a href="https://t.me/+ZlPhIFkPp7E4NGI1">直链跳转</a></td>
</tr>
<tr>
<td> Telegram </td>
</tr>
</table>
## Sponsor us
If this program has helped you, you can sponsor us with some coffee beans :coffee:
<table>
<tr>
<td><img src="https://github.com/wu-clan/image/blob/master/pay/weixin.jpg?raw=true" width="180px" alt="WeChat"/>
<td><img src="https://github.com/wu-clan/image/blob/master/pay/zfb.jpg?raw=true" width="180px" alt="Alipay"/>
<td><img src="https://github.com/wu-clan/image/blob/master/pay/ERC20.jpg?raw=true" width="180px" alt="0x40D5e2304b452256afD9CE2d3d5531dc8d293138"/>
</tr>
<tr>
<td align="center">WeChat Pay</td>
<td align="center">Ali Pay</td>
<td align="center">ERC20</td>
</tr>
</table>
[Discord](https://wu-clan.github.io/homepage/)
## License
This project is licensed under the terms of
This project is licensed by the terms of
the [MIT](https://github.com/fastapi-practices/fastapi_best_architecture/blob/master/LICENSE) license
[![Stargazers over time](https://starchart.cc/fastapi-practices/fastapi_best_architecture.svg?variant=adaptive)](https://starchart.cc/fastapi-practices/fastapi_best_architecture)
+42 -198
View File
@@ -1,24 +1,33 @@
# FastAPI 最佳架构
<div align="center">
[![GitHub](https://img.shields.io/github/license/fastapi-practices/fastapi_best_architecture)](https://github.com/fastapi-practices/fastapi_best_architecture/blob/master/LICENSE)
[![Static Badge](https://img.shields.io/badge/python-3.10%2B-blue)](https://www.python.org/downloads/)
<img alt="Logo 包含了 FBA 三个字母抽象结合,形成了一个类似从地面扩散投影上来的闪电" width="320" src="https://wu-clan.github.io/picx-images-hosting/logo/fba.png">
> [!TIP]
> 你正在查看 pydantic-v1 分支,此分支已被锁定,不再提供更新和修复
# FastAPI Best Architecture
企业级后端架构解决方案
简体中文 | [English](./README.md)
基于 FastAPI 框架的前后端分离中后台解决方案,遵循[伪三层架构](#伪三层架构)设计, 支持 **python3.10** 及以上版本
[![GitHub](https://img.shields.io/github/license/fastapi-practices/fastapi_best_architecture)](https://github.com/fastapi-practices/fastapi_best_architecture/blob/master/LICENSE)
[![Python](https://img.shields.io/badge/python-3.10%2B-blue)](https://www.python.org/downloads/)
![MySQL](https://img.shields.io/badge/MySQL-8.0%2B-%2300758f)
![PostgreSQL](https://img.shields.io/badge/PostgreSQL-16.0%2B-%23336791)
![SQLAlchemy](https://img.shields.io/badge/SQLAlchemy-2.0-%23778877)
[![Pydantic v2](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/pydantic/pydantic/main/docs/badge/v2.json)](https://pydantic.dev)
[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)
[![uv](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/uv/main/assets/badge/v0.json)](https://github.com/astral-sh/uv)
![Docker](https://img.shields.io/badge/Docker-%232496ED?logo=docker&logoColor=white)
[![Discord](https://img.shields.io/badge/Discord-%235865F2.svg?logo=discord&logoColor=white)](https://discord.com/invite/yNN3wTbVAC)
![Discord](https://img.shields.io/discord/1185035164577972344)
[![Ask DeepWiki](https://deepwiki.com/badge.svg)](https://deepwiki.com/fastapi-practices/fastapi_best_architecture)
它的目的是让你可以直接用它作为你新项目的基础架构,本仓库作为模板库公开,任何人或企业均可免费使用
</div>
**持续更新维护中**
## 三层架构
## 伪三层架构
mvc 架构作为常规设计模式,在 python web 中很常见,但是三层架构更令人着迷
mvc 架构作为常规设计模式,在 python web 中也很常见,但是三层架构更令人着迷
在 python web 开发中,三层架构的概念并没有通用标准,所以这里我们称之为伪三层架构
但请注意,我们并没有传统的多 app (微服务)目录结构(django、springBoot...),而是自以为是的目录结构,如果您不喜欢这种模式,可以对其进行任意改造!
| 工作流程 | java | fastapi_best_architecture |
|------|----------------|---------------------------|
@@ -28,170 +37,29 @@ mvc 架构作为常规设计模式,在 python web 中也很常见,但是三
| 数据访问 | dao / mapper | crud |
| 模型 | model / entity | model |
## 在线预览
## 帮助
遗憾的是,我们没有资金提供在线预览,您可以通过查看[本地开发](#本地开发)进行部署,或直接使用 [Docker](#docker-部署)
进行部署,或者在 [fastapi_best_architecture_ui](https://github.com/fastapi-practices/fastapi_best_architecture_ui)
查看部分截图预览
更多详情请查看 [官方文档](https://fastapi-practices.github.io/fastapi_best_architecture_docs/)
## 特征
## 赞助商
- [x] 使用 FastAPI PEP 593 Annotated 参数设计
- [x] async/await + asgiref 的全局异步设计
- [x] 遵循 Restful API 规范
- [x] 全局 SQLAlchemy 2.0 语法
- [x] Casbin RBAC 访问控制模型
- [x] Celery 异步任务
- [x] JWT 中间件白名单认证
- [x] 全局自定义时区时间
- [x] Docker / Docker-compose 部署
- [x] Pytest 单元测试
## 内置功能
1. [x] 用户管理:系统用户角色管理,权限分配
2. [x] 部门管理:配置系统组织机构(公司、部门、小组...)
3. [x] 菜单管理:配置系统菜单,用户菜单,按钮权限标识
4. [x] 角色管理:角色菜单权限分配,角色路由权限分配
5. [x] 字典管理:维护系统内部常用固定数据或参数
6. [x] 操作日志:系统正常操作和异常操作日志记录和查询
7. [x] 登录认证:图形验证码后台认证登录
8. [x] 登录日志:用户正常登录和异常登录的日志记录与查询
9. [x] 服务监控:服务器硬件设备信息与状态
10. [x] 定时任务:自动化任务,异步任务,支持函数调用
11. [x] 接口文档:自动生成在线交互式 API 接口文档
## 本地开发
* Python 3.10+
* Mysql 8.0+
* Redis 推荐最新稳定版
* Nodejs 14.0+
### 后端
1. 安装依赖项
```shell
pip install -r requirements.txt
```
2. 创建一个数据库 `fba`,选择 utf8mb4 编码
3. 安装并启动 Redis
4. 在 `backend/app/` 目录下创建一个 `.env` 文件
```shell
cd backend/app/
touch .env
```
5. 复制 `.env.example` 到 `.env`
```shell
cp .env.example .env
```
6. 数据库迁移 [alembic](https://alembic.sqlalchemy.org/en/latest/tutorial.html)
```shell
cd backend/app/
# 生成迁移文件
alembic revision --autogenerate
# 执行迁移
alembic upgrade head
```
7. 启动 celery worker 和 beat
```shell
celery -A tasks worker --loglevel=INFO
# 可选,如果您不需要使用计划任务
celery -A tasks beat --loglevel=INFO
```
8. 按需修改配置文件
9. 执行 `backend/app/main.py` 文件启动服务
10. 浏览器访问:http://127.0.0.1:8000/api/v1/docs
---
### 前端
跳转 [fastapi_best_architecture_ui](https://github.com/fastapi-practices/fastapi_best_architecture_ui) 查看详情
### Docker 部署
> [!WARNING]
> 默认端口冲突:8000330663795672
>
> 最佳做法是在部署之前关闭本地服务:mysqlredisrabbitmq...
1. 进入 `docker-compose.yml` 文件所在目录,创建环境变量文件`.env`
```shell
cd deploy/docker-compose/
cp .env.server ../../backend/app/.env
# 此命令为可选
cp .env.docker .env
```
2. 按需修改配置文件
3. 执行一键启动命令
```shell
docker-compose up -d --build
```
4. 等待命令自动完成
5. 浏览器访问:http://127.0.0.1:8000/api/v1/docs
## 测试数据
使用 `backend/sql/init_test_data.sql` 文件初始化测试数据
## 开发流程
仅供参考
### 后端:
1. 定义数据库模型(model),每次变化记得执行数据库迁移
2. 定义数据验证模型(schema)
3. 定义路由(router)和视图(api)
4. 定义业务逻辑(service
5. 编写数据库操作(crud
### 前端
跳转 [fastapi_best_architecture_ui](https://github.com/fastapi-practices/fastapi_best_architecture_ui) 查看详情
## 测试
通过 pytest 执行单元测试
1. 创建测试数据库 `fba_test`,选择 utf8mb4 编码
2. 使用 `backend/sql/create_tables.sql` 文件创建数据库表
3. 使用 `backend/sql/init_pytest_data.sql` 文件初始化测试数据
4. 进入app目录
```shell
cd backend/app/
```
5. 执行测试命令
```shell
pytest -vs --disable-warnings
```
<div align="center">
<table>
<tr>
<td align="center">
<a href="https://claude.uy/home">
<img src="https://purple-sun-4f5a.wuyao1243.workers.dev/" alt="Claude.uy" width="400">
</a>
</td>
</tr>
</table>
</div>
## 贡献者
<span style="margin: 0 5px;" ><a href="https://github.com/wu-clan" ><img src="https://images.weserv.nl/?url=avatars.githubusercontent.com/u/52145145?v=4&h=60&w=60&fit=cover&mask=circle&maxage=7d" /></a></span>
<span style="margin: 0 5px;" ><a href="https://github.com/downdawn" ><img src="https://images.weserv.nl/?url=avatars.githubusercontent.com/u/41266749?v=4&h=60&w=60&fit=cover&mask=circle&maxage=7d" /></a></span>
<a href="https://github.com/fastapi-practices/fastapi_best_architecture/graphs/contributors">
<img src="https://contrib.rocks/image?repo=fastapi-practices/fastapi_best_architecture"/>
</a>
## 特别鸣谢
@@ -204,34 +72,10 @@ mvc 架构作为常规设计模式,在 python web 中也很常见,但是三
## 互动
有且仅有当前一个频道,请注意辨别真伪
<table>
<tr>
<td><a href="https://t.me/+ZlPhIFkPp7E4NGI1">直链跳转</a></td>
</tr>
<tr>
<td> Telegram </td>
</tr>
</table>
## 赞助我们
如果此项目能够帮助到你,你可以赞助作者一些咖啡豆表示鼓励 :coffee:
<table>
<tr>
<td><img src="https://github.com/wu-clan/image/blob/master/pay/weixin.jpg?raw=true" width="180px" alt="Wechat"/>
<td><img src="https://github.com/wu-clan/image/blob/master/pay/zfb.jpg?raw=true" width="180px" alt="Alipay"/>
<td><img src="https://github.com/wu-clan/image/blob/master/pay/ERC20.jpg?raw=true" width="180px" alt="0x40D5e2304b452256afD9CE2d3d5531dc8d293138"/>
</tr>
<tr>
<td align="center">微信</td>
<td align="center">支付宝</td>
<td align="center">ERC20</td>
</tr>
</table>
[Discord](https://wu-clan.github.io/homepage/)
## 许可证
本项目根据 [MIT](https://github.com/fastapi-practices/fastapi_best_architecture/blob/master/LICENSE) 许可证的条款进行许可
本项目 [MIT](https://github.com/fastapi-practices/fastapi_best_architecture/blob/master/LICENSE) 许可证的条款进行许可
[![Stargazers over time](https://starchart.cc/fastapi-practices/fastapi_best_architecture.svg?variant=adaptive)](https://starchart.cc/fastapi-practices/fastapi_best_architecture)
-26
View File
@@ -1,26 +0,0 @@
FROM python:3.10-slim
WORKDIR /fba
COPY . .
RUN sed -i 's/deb.debian.org/mirrors.ustc.edu.cn/g' /etc/apt/sources.list.d/debian.sources \
&& sed -i 's|security.debian.org/debian-security|mirrors.ustc.edu.cn/debian-security|g' /etc/apt/sources.list.d/debian.sources
RUN apt-get update \
&& apt-get install -y --no-install-recommends gcc python3-dev \
&& rm -rf /var/lib/apt/lists/*
# 某些包可能存在同步不及时导致安装失败的情况,可更改为官方源:https://pypi.org/simple
RUN pip install --upgrade pip -i https://mirrors.aliyun.com/pypi/simple \
&& pip install --no-cache-dir -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple
ENV TZ = Asia/Shanghai
RUN mkdir -p /var/log/fastapi_server
COPY ./deploy/fastapi_server.conf /etc/supervisor/conf.d/
EXPOSE 8001
CMD ["uvicorn", "backend.app.main:app", "--host", "127.0.0.1", "--port", "8000"]
+34
View File
@@ -0,0 +1,34 @@
# Env
ENVIRONMENT='dev'
# Database
DATABASE_TYPE='postgresql'
DATABASE_HOST='127.0.0.1'
DATABASE_PORT=5432
DATABASE_USER='postgres'
DATABASE_PASSWORD='123456'
# 额外数据源
# DATABASE_SOURCES='{"read": "postgresql+asyncpg://user:password@host:5432/fba"}'
DATABASE_SOURCES='{}'
# Redis
REDIS_HOST='127.0.0.1'
REDIS_PORT=6379
REDIS_PASSWORD=''
REDIS_DATABASE=0
# Token
TOKEN_SECRET_KEY='1VkVF75nsNABBjK_7-qz7GtzNy3AMvktc9TCPwKczCk'
# [ App ] task
# Celery
CELERY_BROKER_REDIS_DATABASE=1
# Rabbitmq
CELERY_RABBITMQ_HOST='127.0.0.1'
CELERY_RABBITMQ_PORT=5672
CELERY_RABBITMQ_USERNAME='guest'
CELERY_RABBITMQ_PASSWORD='guest'
# [ Plugin ] oauth2
OAUTH2_GITHUB_CLIENT_ID='test'
OAUTH2_GITHUB_CLIENT_SECRET='test'
OAUTH2_GOOGLE_CLIENT_ID='test'
OAUTH2_GOOGLE_CLIENT_SECRET='test'
# [ Plugin ] email
EMAIL_USERNAME=''
EMAIL_PASSWORD=''
+7
View File
@@ -0,0 +1,7 @@
__pycache__/
.env
alembic/versions/
static/media/
static/ai_buddy/
*.log
celerybeat-schedule.*
+18
View File
@@ -0,0 +1,18 @@
# FastAPI Best Architecture - Backend
## Docker
1. Make sure you're at the root of the project
2. Run the following Docker command to build container:
```shell
docker build -f Dockerfile -t fba_backend_independent .
```
3. Start container
Native boot needs to change `127.0.0.1` in `.env` to `host.docker.internal`
```shell
docker run -d -p 8000:8000 --name fba_server fba_backend_independent
```
+17
View File
@@ -0,0 +1,17 @@
import sqlalchemy as sa
from backend.utils.dynamic_import import get_all_models
def _register_model_globals() -> None:
"""导入所有模型并注册到 backend 模块命名空间"""
for model_obj in get_all_models():
model_name = model_obj.name if isinstance(model_obj, sa.Table) else model_obj.__name__
if model_name not in globals():
globals()[model_name] = model_obj
_register_model_globals()
__version__ = '1.15.1'
+34
View File
@@ -0,0 +1,34 @@
# Logging configuration
[loggers]
keys = root,sqlalchemy,alembic
[handlers]
keys = console
[formatters]
keys = generic
[logger_root]
level = WARN
handlers = console
qualname =
[logger_sqlalchemy]
level = WARN
handlers =
qualname = sqlalchemy.engine
[logger_alembic]
level = INFO
handlers =
qualname = alembic
[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic
[formatter_generic]
format = %(levelname)-5.5s [%(name)s] %(message)s
datefmt = %H:%M:%S
+3
View File
@@ -0,0 +1,3 @@
Generic single-database configuration with an async dbapi.
If you add a new app to the framework, be sure to read the contents of the `env.py` file.
+118
View File
@@ -0,0 +1,118 @@
import asyncio
import os
from logging.config import fileConfig
from alembic import context
from sqlalchemy import pool
from sqlalchemy.engine import Connection
from sqlalchemy.ext.asyncio import async_engine_from_config
from backend.common.model import MappedBase
from backend.core import path_conf
from backend.core.path_conf import BASE_PATH
from backend.database.db import get_database_url
if not os.path.exists(path_conf.ALEMBIC_VERSION_DIR):
os.makedirs(path_conf.ALEMBIC_VERSION_DIR)
# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config
# Interpret the config file for Python logging.
# This line sets up loggers basically.
if config.config_file_name is not None:
fileConfig(BASE_PATH / config.config_file_name)
# add your model's MetaData object here
# for 'autogenerate' support
target_metadata = MappedBase.metadata
# other values from the config, defined by the needs of env.py,
# can be acquired:
# my_important_option = config.get_main_option("my_important_option")
# ... etc.
config.set_main_option(
'sqlalchemy.url',
get_database_url().render_as_string(hide_password=False).replace('%', '%%'),
)
def run_migrations_offline() -> None:
"""Run migrations in 'offline' mode.
This configures the context with just a URL
and not an Engine, though an Engine is acceptable
here as well. By skipping the Engine creation
we don't even need a DBAPI to be available.
Calls to context.execute() here emit the given string to the
script output.
"""
url = config.get_main_option('sqlalchemy.url')
context.configure(
url=url,
target_metadata=target_metadata,
literal_binds=True,
dialect_opts={'paramstyle': 'named'},
compare_type=True,
compare_server_default=True,
transaction_per_migration=True,
)
with context.begin_transaction():
context.run_migrations()
def do_run_migrations(connection: Connection) -> None:
def process_revision_directives(context, revision, directives) -> None: # ruff:ignore[missing-type-function-argument]
"""当迁移无变化时,不生成迁移记录"""
if config.cmd_opts.autogenerate:
script = directives[0]
if script.upgrade_ops.is_empty():
directives[:] = []
print('\nNo changes in model detected')
context.configure(
connection=connection,
target_metadata=target_metadata,
compare_type=True,
compare_server_default=True,
transaction_per_migration=True,
process_revision_directives=process_revision_directives,
)
with context.begin_transaction():
context.run_migrations()
async def run_async_migrations() -> None:
"""In this scenario we need to create an Engine
and associate a connection with the context.
"""
connectable = async_engine_from_config(
config.get_section(config.config_ini_section, {}),
prefix='sqlalchemy.',
poolclass=pool.NullPool,
)
async with connectable.connect() as connection:
await connection.run_sync(do_run_migrations)
await connectable.dispose()
def run_migrations_online() -> None:
"""Run migrations in 'online' mode."""
asyncio.run(run_async_migrations())
if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()
@@ -7,6 +7,7 @@ Create Date: ${create_date}
"""
from alembic import op
import sqlalchemy as sa
import backend.common.model
${imports if imports else ""}
# revision identifiers, used by Alembic.
-27
View File
@@ -1,27 +0,0 @@
# Env: dev、pro
ENVIRONMENT='dev'
# MySQL
DB_HOST='127.0.0.1'
DB_PORT=3306
DB_USER='root'
DB_PASSWORD='123456'
# Redis
REDIS_HOST='127.0.0.1'
REDIS_PORT=6379
REDIS_PASSWORD=''
REDIS_DATABASE=0
# Celery
CELERY_REDIS_HOST='127.0.0.1'
CELERY_REDIS_PORT=6379
CELERY_REDIS_PASSWORD=''
CELERY_BROKER_REDIS_DATABASE=1
CELERY_BACKEND_REDIS_DATABASE=2
# Rabbitmq
RABBITMQ_HOST='127.0.0.1'
RABBITMQ_PORT=5672
RABBITMQ_USERNAME='guest'
RABBITMQ_PASSWORD='guest'
# Token
TOKEN_SECRET_KEY='1VkVF75nsNABBjK_7-qz7GtzNy3AMvktc9TCPwKczCk'
# Opera Log
OPERA_LOG_ENCRYPT_SECRET_KEY='d77b25790a804c2b4a339dd0207941e4cefa5751935a33735bc73bb7071a005b'
-2
View File
@@ -1,2 +0,0 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
View File
View File
+14
View File
@@ -0,0 +1,14 @@
from fastapi import APIRouter
from backend.app.admin.api.v1.auth import router as auth_router
from backend.app.admin.api.v1.log import router as log_router
from backend.app.admin.api.v1.monitor import router as monitor_router
from backend.app.admin.api.v1.sys import router as sys_router
from backend.core.conf import settings
v1 = APIRouter(prefix=settings.FASTAPI_API_V1_PATH)
v1.include_router(auth_router)
v1.include_router(sys_router)
v1.include_router(log_router)
v1.include_router(monitor_router)
@@ -0,0 +1,9 @@
from fastapi import APIRouter
from backend.app.admin.api.v1.auth.auth import router as auth_router
from backend.app.admin.api.v1.auth.captcha import router as captcha_router
router = APIRouter(prefix='/auth')
router.include_router(auth_router, tags=['授权'])
router.include_router(captcha_router, tags=['验证码'])
+58
View File
@@ -0,0 +1,58 @@
from typing import Annotated
from fastapi import APIRouter, Depends, Request, Response
from fastapi.security import HTTPBasicCredentials
from pyrate_limiter import Duration, Rate
from starlette.background import BackgroundTasks
from backend.app.admin.schema.token import GetLoginToken, GetNewToken, GetSwaggerToken
from backend.app.admin.schema.user import AuthLoginParam
from backend.app.admin.service.auth_service import auth_service
from backend.common.response.response_schema import ResponseModel, ResponseSchemaModel, response_base
from backend.common.security.jwt import DependsJwtAuth
from backend.database.db import CurrentSession, CurrentSessionTransaction
from backend.utils.limiter import RateLimiter
router = APIRouter()
@router.post('/login/swagger', summary='swagger 调试专用', description='用于快捷获取 token 进行 swagger 认证')
async def login_swagger(
db: CurrentSessionTransaction, obj: Annotated[HTTPBasicCredentials, Depends()]
) -> GetSwaggerToken:
token, user = await auth_service.swagger_login(db=db, obj=obj)
return GetSwaggerToken(access_token=token, user=user) # type: ignore
@router.post(
'/login',
summary='用户登录',
description='json 格式登录, 仅支持在第三方api工具调试, 例如: postman',
dependencies=[Depends(RateLimiter(Rate(5, Duration.MINUTE)))],
)
async def login(
db: CurrentSessionTransaction,
response: Response,
obj: AuthLoginParam,
background_tasks: BackgroundTasks,
) -> ResponseSchemaModel[GetLoginToken]:
data = await auth_service.login(db=db, response=response, obj=obj, background_tasks=background_tasks)
return response_base.success(data=data)
@router.get('/codes', summary='获取所有授权码', description='适配 vben admin v5', dependencies=[DependsJwtAuth])
async def get_codes(db: CurrentSession, request: Request) -> ResponseSchemaModel[list[str]]:
codes = await auth_service.get_codes(db=db, request=request)
return response_base.success(data=codes)
@router.post('/refresh', summary='刷新 token')
async def refresh_token(db: CurrentSession, request: Request, response: Response) -> ResponseSchemaModel[GetNewToken]:
data = await auth_service.refresh_token(db=db, request=request, response=response)
return response_base.success(data=data)
@router.post('/logout', summary='用户登出')
async def logout(request: Request, response: Response) -> ResponseModel:
await auth_service.logout(request=request, response=response)
return response_base.success()
+39
View File
@@ -0,0 +1,39 @@
import uuid
from fast_captcha import img_captcha
from fastapi import APIRouter, Depends
from pyrate_limiter import Duration, Rate
from starlette.concurrency import run_in_threadpool
from backend.app.admin.schema.captcha import GetCaptchaDetail
from backend.common.response.response_schema import ResponseSchemaModel, response_base
from backend.core.conf import settings
from backend.database.db import CurrentSession
from backend.database.redis import redis_client
from backend.utils.dynamic_config import load_login_config
from backend.utils.limiter import RateLimiter
router = APIRouter()
@router.get(
'/captcha',
summary='获取登录验证码',
dependencies=[Depends(RateLimiter(Rate(5, Duration.SECOND * 30)))],
)
async def get_captcha(db: CurrentSession) -> ResponseSchemaModel[GetCaptchaDetail]:
await load_login_config(db)
img, code = await run_in_threadpool(img_captcha, img_byte='base64')
captcha_uuid = str(uuid.uuid4())
await redis_client.set(
f'{settings.LOGIN_CAPTCHA_REDIS_PREFIX}:{captcha_uuid}',
code,
ex=settings.LOGIN_CAPTCHA_EXPIRE_SECONDS,
)
data = GetCaptchaDetail(
is_enabled=settings.LOGIN_CAPTCHA_ENABLED,
expire_seconds=settings.LOGIN_CAPTCHA_EXPIRE_SECONDS,
uuid=captcha_uuid,
image=img,
)
return response_base.success(data=data)
+9
View File
@@ -0,0 +1,9 @@
from fastapi import APIRouter
from backend.app.admin.api.v1.log.login_log import router as login_log
from backend.app.admin.api.v1.log.opera_log import router as opera_log
router = APIRouter(prefix='/logs')
router.include_router(login_log, prefix='/login', tags=['登录日志'])
router.include_router(opera_log, prefix='/opera', tags=['操作日志'])
+61
View File
@@ -0,0 +1,61 @@
from typing import Annotated
from fastapi import APIRouter, Depends, Query
from backend.app.admin.schema.login_log import DeleteLoginLogParam, GetLoginLogDetail
from backend.app.admin.service.login_log_service import login_log_service
from backend.common.pagination import DependsPagination, PageData
from backend.common.response.response_schema import ResponseModel, ResponseSchemaModel, response_base
from backend.common.security.jwt import DependsJwtAuth
from backend.common.security.permission import RequestPermission
from backend.common.security.rbac import DependsRBAC
from backend.database.db import CurrentSession, CurrentSessionTransaction
router = APIRouter()
@router.get(
'',
summary='分页获取登录日志',
dependencies=[
DependsJwtAuth,
DependsPagination,
],
)
async def get_login_logs_paginated(
db: CurrentSession,
username: Annotated[str | None, Query(description='用户名')] = None,
status: Annotated[int | None, Query(description='状态')] = None,
ip: Annotated[str | None, Query(description='IP 地址')] = None,
) -> ResponseSchemaModel[PageData[GetLoginLogDetail]]:
page_data = await login_log_service.get_list(db=db, username=username, status=status, ip=ip)
return response_base.success(data=page_data)
@router.delete(
'',
summary='批量删除登录日志',
dependencies=[
Depends(RequestPermission('log:login:del')),
DependsRBAC,
],
)
async def delete_login_logs(db: CurrentSessionTransaction, obj: DeleteLoginLogParam) -> ResponseModel:
count = await login_log_service.delete(db=db, obj=obj)
if count > 0:
return response_base.success()
return response_base.fail()
@router.delete(
'/all',
summary='清空登录日志',
dependencies=[
Depends(RequestPermission('log:login:clear')),
DependsRBAC,
],
)
async def delete_all_login_logs(db: CurrentSessionTransaction) -> ResponseModel:
await login_log_service.delete_all(db=db)
return response_base.success()
+61
View File
@@ -0,0 +1,61 @@
from typing import Annotated
from fastapi import APIRouter, Depends, Query
from backend.app.admin.schema.opera_log import DeleteOperaLogParam, GetOperaLogDetail
from backend.app.admin.service.opera_log_service import opera_log_service
from backend.common.pagination import DependsPagination, PageData
from backend.common.response.response_schema import ResponseModel, ResponseSchemaModel, response_base
from backend.common.security.jwt import DependsJwtAuth
from backend.common.security.permission import RequestPermission
from backend.common.security.rbac import DependsRBAC
from backend.database.db import CurrentSession, CurrentSessionTransaction
router = APIRouter()
@router.get(
'',
summary='分页获取操作日志',
dependencies=[
DependsJwtAuth,
DependsPagination,
],
)
async def get_opera_logs_paginated(
db: CurrentSession,
username: Annotated[str | None, Query(description='用户名')] = None,
status: Annotated[int | None, Query(description='状态')] = None,
ip: Annotated[str | None, Query(description='IP 地址')] = None,
) -> ResponseSchemaModel[PageData[GetOperaLogDetail]]:
page_data = await opera_log_service.get_list(db=db, username=username, status=status, ip=ip)
return response_base.success(data=page_data)
@router.delete(
'',
summary='批量删除操作日志',
dependencies=[
Depends(RequestPermission('log:opera:del')),
DependsRBAC,
],
)
async def delete_opera_logs(db: CurrentSessionTransaction, obj: DeleteOperaLogParam) -> ResponseModel:
count = await opera_log_service.delete(db=db, obj=obj)
if count > 0:
return response_base.success()
return response_base.fail()
@router.delete(
'/all',
summary='清空操作日志',
dependencies=[
Depends(RequestPermission('log:opera:clear')),
DependsRBAC,
],
)
async def delete_all_opera_logs(db: CurrentSessionTransaction) -> ResponseModel:
await opera_log_service.delete_all(db=db)
return response_base.success()
@@ -0,0 +1,11 @@
from fastapi import APIRouter
from backend.app.admin.api.v1.monitor.online import router as token_router
from backend.app.admin.api.v1.monitor.redis import router as redis_router
from backend.app.admin.api.v1.monitor.server import router as server_router
router = APIRouter(prefix='/monitors')
router.include_router(redis_router, prefix='/redis', tags=['redis监控'])
router.include_router(server_router, prefix='/server', tags=['服务器监控'])
router.include_router(token_router, prefix='/sessions', tags=['会话监控'])
+119
View File
@@ -0,0 +1,119 @@
import json
from typing import TYPE_CHECKING, Annotated, Any
from fastapi import APIRouter, Path, Query
from backend.app.admin.schema.token import GetTokenDetail
from backend.common.enums import StatusType
from backend.common.exception import errors
from backend.common.response.response_schema import ResponseModel, ResponseSchemaModel, response_base
from backend.common.security.jwt import DependsSuperUser, jwt_decode
from backend.common.security.token import revoke_token
from backend.core.conf import settings
from backend.database.redis import redis_client
if TYPE_CHECKING:
from backend.common.dataclasses import TokenPayload
router = APIRouter()
@router.get('', summary='获取在线用户', dependencies=[DependsSuperUser])
async def get_sessions( # ruff:ignore[complex-structure]
username: Annotated[str | None, Query(description='用户名')] = None,
) -> ResponseSchemaModel[list[GetTokenDetail]]:
users_key = f'{settings.TOKEN_SESSION_REDIS_PREFIX}:users'
user_ids = list(await redis_client.smembers(users_key))
if not user_ids:
return response_base.success(data=[])
session_sets = await redis_client.smembers_many([
f'{settings.TOKEN_SESSION_REDIS_PREFIX}:{user_id}' for user_id in user_ids
])
session_refs: list[tuple[str, str]] = []
for user_id, members in zip(user_ids, session_sets, strict=True):
session_refs.extend((user_id, session_uuid) for session_uuid in members)
if not session_refs:
await redis_client.srem(users_key, *user_ids)
return response_base.success(data=[])
tokens = await redis_client.mget_batched([
f'{settings.TOKEN_REDIS_PREFIX}:{user_id}:{session_uuid}' for user_id, session_uuid in session_refs
])
token_payloads: list[TokenPayload] = []
live_user_ids: set[str] = set()
for (user_id, _session_uuid), token in zip(session_refs, tokens, strict=True):
if not token:
continue
try:
token_payloads.append(jwt_decode(token))
except errors.TokenError:
continue
live_user_ids.add(user_id)
stale_user_ids = [user_id for user_id in user_ids if user_id not in live_user_ids]
if stale_user_ids:
await redis_client.srem(users_key, *stale_user_ids)
if not token_payloads:
return response_base.success(data=[])
extra_infos = await redis_client.mget_batched([
f'{settings.TOKEN_EXTRA_INFO_REDIS_PREFIX}:{item.user_id}:{item.session_uuid}' for item in token_payloads
])
sid_sets = await redis_client.smembers_many([
f'{settings.TOKEN_ONLINE_REDIS_PREFIX}:session:{item.session_uuid}' for item in token_payloads
])
sid_list = [sid for members in sid_sets for sid in members]
sid_values = await redis_client.mget_batched([
f'{settings.TOKEN_ONLINE_REDIS_PREFIX}:sid:{sid}' for sid in sid_list
])
sid_session_map = dict(zip(sid_list, sid_values, strict=True))
online_sessions = {
item.session_uuid
for item, members in zip(token_payloads, sid_sets, strict=True)
if any(sid_session_map.get(sid) == item.session_uuid for sid in members)
}
data: list[GetTokenDetail] = []
for token_payload, extra_info in zip(token_payloads, extra_infos, strict=True):
info: dict[str, Any] = {}
if extra_info:
try:
parsed = json.loads(extra_info)
except (json.JSONDecodeError, TypeError):
parsed = None
if isinstance(parsed, dict):
info = parsed
if info.get('swagger') is not None:
continue
if username is not None and username != info.get('username'):
continue
data.append(
GetTokenDetail(
id=token_payload.user_id,
session_uuid=token_payload.session_uuid,
username=info.get('username', '未知'),
nickname=info.get('nickname', '未知'),
ip=info.get('ip', '未知'),
os=info.get('os', '未知'),
browser=info.get('browser', '未知'),
device=info.get('device', '未知'),
status=StatusType.enable if token_payload.session_uuid in online_sessions else StatusType.disable,
last_login_time=info.get('last_login_time', '未知'),
expire_time=token_payload.expire_time,
)
)
data.sort(key=lambda item: (item.id, item.session_uuid))
return response_base.success(data=data)
@router.delete(
'/{pk}',
summary='强制下线',
dependencies=[DependsSuperUser],
)
async def delete_session(
pk: Annotated[int, Path(description='用户 ID')],
session_uuid: Annotated[str, Query(description='会话 UUID')],
) -> ResponseModel:
await revoke_token(pk, session_uuid)
return response_base.success()
+42
View File
@@ -0,0 +1,42 @@
from fastapi import APIRouter
from backend.app.admin.schema.monitor import RedisCommandStat, RedisMonitorInfo, RedisServerInfo
from backend.common.response.response_schema import ResponseSchemaModel, response_base
from backend.common.security.jwt import DependsJwtAuth
from backend.database.redis import redis_client
from backend.utils.format import fmt_seconds
router = APIRouter()
@router.get('', summary='Redis 监控', dependencies=[DependsJwtAuth])
async def get_redis_info() -> ResponseSchemaModel[RedisMonitorInfo]:
info = await redis_client.info()
db_size = await redis_client.dbsize()
server_info = RedisServerInfo(
redis_version=str(info.get('redis_version', '')),
redis_mode=str(info.get('redis_mode', '')),
role=str(info.get('role', '')),
tcp_port=str(info.get('tcp_port', '')),
uptime=str(fmt_seconds(int(info.get('uptime_in_seconds', 0)))),
connected_clients=str(info.get('connected_clients', '')),
blocked_clients=str(info.get('blocked_clients', '')),
used_memory_human=str(info.get('used_memory_human', '')),
used_memory_rss_human=str(info.get('used_memory_rss_human', '')),
maxmemory_human=str(info.get('maxmemory_human', '0B')),
mem_fragmentation_ratio=str(info.get('mem_fragmentation_ratio', '0')),
instantaneous_ops_per_sec=str(info.get('instantaneous_ops_per_sec', '')),
total_commands_processed=str(info.get('total_commands_processed', '')),
rejected_connections=str(info.get('rejected_connections', '')),
keys_num=str(db_size),
)
command_stats = await redis_client.info('commandstats')
stats_list = []
for key, value in command_stats.items():
if isinstance(value, dict):
stats_list.append(RedisCommandStat(name=key.split('_')[-1], value=str(value.get('calls', '0'))))
data = RedisMonitorInfo(info=server_info, stats=stats_list)
return response_base.success(data=data)
+134
View File
@@ -0,0 +1,134 @@
import os
import platform
import socket
import sys
from datetime import datetime
from datetime import timezone as tz
import psutil
from fastapi import APIRouter
from starlette.concurrency import run_in_threadpool
from backend.app.admin.schema.monitor import (
CpuInfo,
DiskInfo,
MemInfo,
ServerMonitorInfo,
ServiceInfo,
SysInfo,
)
from backend.common.response.response_schema import ResponseSchemaModel, response_base
from backend.common.security.jwt import DependsSuperUser
from backend.utils.format import fmt_bytes, fmt_seconds
from backend.utils.timezone import timezone
router = APIRouter()
@router.get('', summary='Server 监控', dependencies=[DependsSuperUser])
async def get_server_info() -> ResponseSchemaModel[ServerMonitorInfo]: # ruff:ignore[complex-structure]
def get_all_info() -> ServerMonitorInfo: # ruff:ignore[complex-structure]
# CPU 信息
cpu_data = {
'physical_num': psutil.cpu_count(logical=False) or 0,
'logical_num': psutil.cpu_count(logical=True) or 0,
'max_freq': 0.0,
'min_freq': 0.0,
'current_freq': 0.0,
'usage': round(psutil.cpu_percent(interval=0.1), 2),
}
try:
if hasattr(psutil, 'cpu_freq'):
cpu_freq = psutil.cpu_freq()
if cpu_freq:
cpu_data.update({
'max_freq': round(cpu_freq.max, 2),
'min_freq': round(cpu_freq.min, 2),
'current_freq': round(cpu_freq.current, 2),
})
except Exception:
pass
cpu = CpuInfo(**cpu_data)
# 内存信息
mem = psutil.virtual_memory()
gb_factor = 1024**3
mem_info = MemInfo(
total=round(mem.total / gb_factor, 2),
used=round(mem.used / gb_factor, 2),
free=round(mem.available / gb_factor, 2),
usage=round(mem.percent, 2),
)
# 系统信息
hostname = socket.gethostname()
ip = '127.0.0.1'
try:
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
s.settimeout(0.5)
s.connect(('8.8.8.8', 80))
ip = s.getsockname()[0]
except (TimeoutError, socket.gaierror, OSError):
pass
sys_info = SysInfo(name=hostname, os=platform.system(), ip=ip, arch=platform.machine())
# 磁盘信息
disk_list = []
exclude_fstypes = {'overlay', 'overlay2', 'tmpfs', 'devtmpfs', 'shm', 'proc', 'sysfs', 'cgroup', 'cgroup2'}
seen_devices = set()
for partition in psutil.disk_partitions(all=False):
# 跳过虚拟文件系统
if partition.fstype.lower() in exclude_fstypes:
continue
# 跳过重复设备(同一设备的不同挂载点)
if partition.device in seen_devices:
continue
try:
usage = psutil.disk_usage(partition.mountpoint)
if usage:
seen_devices.add(partition.device)
disk_list.append(
DiskInfo(
dir=partition.mountpoint,
device=partition.device,
type=partition.fstype,
total=fmt_bytes(usage.total),
used=fmt_bytes(usage.used),
free=fmt_bytes(usage.free),
usage=f'{usage.percent:.2f}%',
)
)
except (PermissionError, OSError):
continue
# 服务信息
process = psutil.Process(os.getpid())
proc_mem = process.memory_info()
try:
create_time = datetime.fromtimestamp(process.create_time(), tz=tz.utc)
start_time = timezone.from_datetime(create_time)
except (psutil.NoSuchProcess, OSError):
start_time = timezone.now()
elapsed = fmt_seconds(round((timezone.now() - start_time).total_seconds()))
service = ServiceInfo(
name='Python3',
version=platform.python_version(),
home=sys.executable,
startup=timezone.to_str(start_time),
elapsed=elapsed,
cpu_usage=f'{process.cpu_percent(interval=0.1):.2f}%',
mem_vms=fmt_bytes(proc_mem.vms),
mem_rss=fmt_bytes(proc_mem.rss),
mem_free=fmt_bytes(proc_mem.vms - proc_mem.rss),
)
return ServerMonitorInfo(cpu=cpu, mem=mem_info, sys=sys_info, disk=disk_list, service=service)
data = await run_in_threadpool(get_all_info)
return response_base.success(data=data)
+21
View File
@@ -0,0 +1,21 @@
from fastapi import APIRouter
from backend.app.admin.api.v1.sys.data_rule import router as data_rule_router
from backend.app.admin.api.v1.sys.data_scope import router as data_scope_router
from backend.app.admin.api.v1.sys.dept import router as dept_router
from backend.app.admin.api.v1.sys.file import router as file_router
from backend.app.admin.api.v1.sys.menu import router as menu_router
from backend.app.admin.api.v1.sys.plugin import router as plugin_router
from backend.app.admin.api.v1.sys.role import router as role_router
from backend.app.admin.api.v1.sys.user import router as user_router
router = APIRouter(prefix='/sys')
router.include_router(dept_router, prefix='/depts', tags=['系统部门'])
router.include_router(menu_router, prefix='/menus', tags=['系统菜单'])
router.include_router(role_router, prefix='/roles', tags=['系统角色'])
router.include_router(user_router, prefix='/users', tags=['系统用户'])
router.include_router(data_rule_router, prefix='/data-rules', tags=['系统数据规则'])
router.include_router(data_scope_router, prefix='/data-scopes', tags=['系统数据范围'])
router.include_router(file_router, prefix='/files', tags=['系统文件'])
router.include_router(plugin_router, prefix='/plugins', tags=['系统插件'])
+119
View File
@@ -0,0 +1,119 @@
from typing import Annotated
from fastapi import APIRouter, Depends, Path, Query
from backend.app.admin.schema.data_rule import (
CreateDataRuleParam,
DeleteDataRuleParam,
GetDataRuleColumnDetail,
GetDataRuleDetail,
GetDataRuleTemplateVariableDetail,
UpdateDataRuleParam,
)
from backend.app.admin.service.data_rule_service import data_rule_service
from backend.common.pagination import DependsPagination, PageData
from backend.common.response.response_schema import ResponseModel, ResponseSchemaModel, response_base
from backend.common.security.jwt import DependsJwtAuth
from backend.common.security.permission import RequestPermission
from backend.common.security.rbac import DependsRBAC
from backend.database.db import CurrentSession, CurrentSessionTransaction
router = APIRouter()
@router.get('/models', summary='获取数据规则可用模型', dependencies=[DependsJwtAuth])
async def get_data_rule_models() -> ResponseSchemaModel[list[str]]:
models = await data_rule_service.get_models()
return response_base.success(data=models)
@router.get('/models/{model}/columns', summary='获取数据规则可用模型列', dependencies=[DependsJwtAuth])
async def get_data_rule_model_columns(
model: Annotated[str, Path(description='模型名称')],
) -> ResponseSchemaModel[list[GetDataRuleColumnDetail]]:
models = await data_rule_service.get_columns(model=model)
return response_base.success(data=models)
@router.get('/value-template-variables', summary='获取数据规则值可用模板变量', dependencies=[DependsJwtAuth])
async def get_data_rule_value_template_variables() -> ResponseSchemaModel[list[GetDataRuleTemplateVariableDetail]]:
variables = await data_rule_service.get_value_template_variables()
return response_base.success(data=variables)
@router.get('/all', summary='获取所有数据规则', dependencies=[DependsJwtAuth])
async def get_all_data_rules(db: CurrentSession) -> ResponseSchemaModel[list[GetDataRuleDetail]]:
data = await data_rule_service.get_all(db=db)
return response_base.success(data=data)
@router.get('/{pk}', summary='获取数据规则详情', dependencies=[DependsJwtAuth])
async def get_data_rule(
db: CurrentSession,
pk: Annotated[int, Path(description='数据规则 ID')],
) -> ResponseSchemaModel[GetDataRuleDetail]:
data = await data_rule_service.get(db=db, pk=pk)
return response_base.success(data=data)
@router.get(
'',
summary='分页获取所有数据规则',
dependencies=[
DependsJwtAuth,
DependsPagination,
],
)
async def get_data_rules_paginated(
db: CurrentSession,
name: Annotated[str | None, Query(description='规则名称')] = None,
) -> ResponseSchemaModel[PageData[GetDataRuleDetail]]:
page_data = await data_rule_service.get_list(db=db, name=name)
return response_base.success(data=page_data)
@router.post(
'',
summary='创建数据规则',
dependencies=[
Depends(RequestPermission('data:rule:add')),
DependsRBAC,
],
)
async def create_data_rule(db: CurrentSessionTransaction, obj: CreateDataRuleParam) -> ResponseModel:
await data_rule_service.create(db=db, obj=obj)
return response_base.success()
@router.put(
'/{pk}',
summary='更新数据规则',
dependencies=[
Depends(RequestPermission('data:rule:edit')),
DependsRBAC,
],
)
async def update_data_rule(
db: CurrentSessionTransaction,
pk: Annotated[int, Path(description='数据规则 ID')],
obj: UpdateDataRuleParam,
) -> ResponseModel:
count = await data_rule_service.update(db=db, pk=pk, obj=obj)
if count > 0:
return response_base.success()
return response_base.fail()
@router.delete(
'',
summary='批量删除数据规则',
dependencies=[
Depends(RequestPermission('data:rule:del')),
DependsRBAC,
],
)
async def delete_data_rules(db: CurrentSessionTransaction, obj: DeleteDataRuleParam) -> ResponseModel:
count = await data_rule_service.delete(db=db, obj=obj)
if count > 0:
return response_base.success()
return response_base.fail()
+128
View File
@@ -0,0 +1,128 @@
from typing import Annotated
from fastapi import APIRouter, Depends, Path, Query
from backend.app.admin.schema.data_scope import (
CreateDataScopeParam,
DeleteDataScopeParam,
GetDataScopeDetail,
GetDataScopeWithRelationDetail,
UpdateDataScopeParam,
UpdateDataScopeRuleParam,
)
from backend.app.admin.service.data_scope_service import data_scope_service
from backend.common.pagination import DependsPagination, PageData
from backend.common.response.response_schema import ResponseModel, ResponseSchemaModel, response_base
from backend.common.security.jwt import DependsJwtAuth
from backend.common.security.permission import RequestPermission
from backend.common.security.rbac import DependsRBAC
from backend.database.db import CurrentSession, CurrentSessionTransaction
router = APIRouter()
@router.get('/all', summary='获取所有数据范围', dependencies=[DependsJwtAuth])
async def get_all_data_scope(db: CurrentSession) -> ResponseSchemaModel[list[GetDataScopeDetail]]:
data = await data_scope_service.get_all(db=db)
return response_base.success(data=data)
@router.get('/{pk}', summary='获取数据范围详情', dependencies=[DependsJwtAuth])
async def get_data_scope(
db: CurrentSession,
pk: Annotated[int, Path(description='数据范围 ID')],
) -> ResponseSchemaModel[GetDataScopeDetail]:
data = await data_scope_service.get(db=db, pk=pk)
return response_base.success(data=data)
@router.get('/{pk}/rules', summary='获取数据范围所有规则', dependencies=[DependsJwtAuth])
async def get_data_scope_rules(
db: CurrentSession,
pk: Annotated[int, Path(description='数据范围 ID')],
) -> ResponseSchemaModel[GetDataScopeWithRelationDetail]:
data = await data_scope_service.get_rules(db=db, pk=pk)
return response_base.success(data=data)
@router.get(
'',
summary='分页获取所有数据范围',
dependencies=[
DependsJwtAuth,
DependsPagination,
],
)
async def get_data_scopes_paginated(
db: CurrentSession,
name: Annotated[str | None, Query(description='范围名称')] = None,
status: Annotated[int | None, Query(description='状态')] = None,
) -> ResponseSchemaModel[PageData[GetDataScopeDetail]]:
page_data = await data_scope_service.get_list(db=db, name=name, status=status)
return response_base.success(data=page_data)
@router.post(
'',
summary='创建数据范围',
dependencies=[
Depends(RequestPermission('data:scope:add')),
DependsRBAC,
],
)
async def create_data_scope(db: CurrentSessionTransaction, obj: CreateDataScopeParam) -> ResponseModel:
await data_scope_service.create(db=db, obj=obj)
return response_base.success()
@router.put(
'/{pk}',
summary='更新数据范围',
dependencies=[
Depends(RequestPermission('data:scope:edit')),
DependsRBAC,
],
)
async def update_data_scope(
db: CurrentSessionTransaction,
pk: Annotated[int, Path(description='数据范围 ID')],
obj: UpdateDataScopeParam,
) -> ResponseModel:
count = await data_scope_service.update(db=db, pk=pk, obj=obj)
if count > 0:
return response_base.success()
return response_base.fail()
@router.put(
'/{pk}/rules',
summary='更新数据范围规则',
dependencies=[
Depends(RequestPermission('data:scope:rule:edit')),
DependsRBAC,
],
)
async def update_data_scope_rules(
db: CurrentSessionTransaction,
pk: Annotated[int, Path(description='数据范围 ID')],
rule_ids: UpdateDataScopeRuleParam,
) -> ResponseModel:
count = await data_scope_service.update_data_scope_rule(db=db, pk=pk, rule_ids=rule_ids)
if count > 0:
return response_base.success()
return response_base.fail()
@router.delete(
'',
summary='批量删除数据范围',
dependencies=[
Depends(RequestPermission('data:scope:del')),
DependsRBAC,
],
)
async def delete_data_scopes(db: CurrentSessionTransaction, obj: DeleteDataScopeParam) -> ResponseModel:
count = await data_scope_service.delete(db=db, obj=obj)
if count > 0:
return response_base.success()
return response_base.fail()
+83
View File
@@ -0,0 +1,83 @@
from typing import Annotated
from fastapi import APIRouter, Depends, Path, Query
from sqlalchemy import ColumnElement
from backend.app.admin.model import Dept
from backend.app.admin.schema.dept import CreateDeptParam, GetDeptDetail, GetDeptTree, UpdateDeptParam
from backend.app.admin.service.dept_service import dept_service
from backend.common.response.response_schema import ResponseModel, ResponseSchemaModel, response_base
from backend.common.security.jwt import DependsJwtAuth
from backend.common.security.permission import DataPermissionFilter, RequestPermission
from backend.common.security.rbac import DependsRBAC
from backend.database.db import CurrentSession, CurrentSessionTransaction
router = APIRouter()
@router.get('/{pk}', summary='获取部门详情', dependencies=[DependsJwtAuth])
async def get_dept(
db: CurrentSession, pk: Annotated[int, Path(description='部门 ID')]
) -> ResponseSchemaModel[GetDeptDetail]:
data = await dept_service.get(db=db, pk=pk)
return response_base.success(data=data)
@router.get('', summary='获取部门树', dependencies=[DependsJwtAuth])
async def get_dept_tree(
db: CurrentSession,
data_filter: Annotated[ColumnElement[bool], Depends(DataPermissionFilter(Dept))],
name: Annotated[str | None, Query(description='部门名称')] = None,
leader: Annotated[str | None, Query(description='部门负责人')] = None,
phone: Annotated[str | None, Query(description='联系电话')] = None,
status: Annotated[int | None, Query(description='状态')] = None,
) -> ResponseSchemaModel[list[GetDeptTree]]:
dept = await dept_service.get_tree(
db=db, data_filter=data_filter, name=name, leader=leader, phone=phone, status=status
)
return response_base.success(data=dept)
@router.post(
'',
summary='创建部门',
dependencies=[
Depends(RequestPermission('sys:dept:add')),
DependsRBAC,
],
)
async def create_dept(db: CurrentSessionTransaction, obj: CreateDeptParam) -> ResponseModel:
await dept_service.create(db=db, obj=obj)
return response_base.success()
@router.put(
'/{pk}',
summary='更新部门',
dependencies=[
Depends(RequestPermission('sys:dept:edit')),
DependsRBAC,
],
)
async def update_dept(
db: CurrentSessionTransaction, pk: Annotated[int, Path(description='部门 ID')], obj: UpdateDeptParam
) -> ResponseModel:
count = await dept_service.update(db=db, pk=pk, obj=obj)
if count > 0:
return response_base.success()
return response_base.fail()
@router.delete(
'/{pk}',
summary='删除部门',
dependencies=[
Depends(RequestPermission('sys:dept:del')),
DependsRBAC,
],
)
async def delete_dept(db: CurrentSessionTransaction, pk: Annotated[int, Path(description='部门 ID')]) -> ResponseModel:
count = await dept_service.delete(db=db, pk=pk)
if count > 0:
return response_base.success()
return response_base.fail()
+25
View File
@@ -0,0 +1,25 @@
from typing import Annotated
from fastapi import APIRouter, Depends, File, UploadFile
from backend.common.dataclasses import UploadUrl
from backend.common.response.response_schema import ResponseSchemaModel, response_base
from backend.common.security.permission import RequestPermission
from backend.common.security.rbac import DependsRBAC
from backend.utils.file_ops import upload_file, upload_file_verify
router = APIRouter()
@router.post(
'/upload',
summary='本地文件上传',
dependencies=[
Depends(RequestPermission('sys:file:upload')),
DependsRBAC,
],
)
async def upload_files(file: Annotated[UploadFile, File()]) -> ResponseSchemaModel[UploadUrl]:
upload_file_verify(file)
filename = await upload_file(file)
return response_base.success(data={'url': f'/static/upload/{filename}'})
+82
View File
@@ -0,0 +1,82 @@
from typing import Annotated, Any
from fastapi import APIRouter, Depends, Path, Query, Request
from backend.app.admin.schema.menu import CreateMenuParam, GetMenuDetail, GetMenuTree, UpdateMenuParam
from backend.app.admin.service.menu_service import menu_service
from backend.common.response.response_schema import ResponseModel, ResponseSchemaModel, response_base
from backend.common.security.jwt import DependsJwtAuth
from backend.common.security.permission import RequestPermission
from backend.common.security.rbac import DependsRBAC
from backend.database.db import CurrentSession, CurrentSessionTransaction
router = APIRouter()
@router.get('/sidebar', summary='获取用户菜单侧边栏', description='已适配 vben admin v5', dependencies=[DependsJwtAuth])
async def get_user_sidebar(db: CurrentSession, request: Request) -> ResponseSchemaModel[list[dict[str, Any] | None]]:
menu = await menu_service.get_sidebar(db=db, request=request)
return response_base.success(data=menu)
@router.get('/{pk}', summary='获取菜单详情', dependencies=[DependsJwtAuth])
async def get_menu(
db: CurrentSession, pk: Annotated[int, Path(description='菜单 ID')]
) -> ResponseSchemaModel[GetMenuDetail]:
data = await menu_service.get(db=db, pk=pk)
return response_base.success(data=data)
@router.get('', summary='获取菜单树', dependencies=[DependsJwtAuth])
async def get_menu_tree(
db: CurrentSession,
title: Annotated[str | None, Query(description='菜单标题')] = None,
status: Annotated[int | None, Query(description='状体')] = None,
) -> ResponseSchemaModel[list[GetMenuTree]]:
menu = await menu_service.get_tree(db=db, title=title, status=status)
return response_base.success(data=menu)
@router.post(
'',
summary='创建菜单',
dependencies=[
Depends(RequestPermission('sys:menu:add')),
DependsRBAC,
],
)
async def create_menu(db: CurrentSessionTransaction, obj: CreateMenuParam) -> ResponseModel:
await menu_service.create(db=db, obj=obj)
return response_base.success()
@router.put(
'/{pk}',
summary='更新菜单',
dependencies=[
Depends(RequestPermission('sys:menu:edit')),
DependsRBAC,
],
)
async def update_menu(
db: CurrentSessionTransaction, pk: Annotated[int, Path(description='菜单 ID')], obj: UpdateMenuParam
) -> ResponseModel:
count = await menu_service.update(db=db, pk=pk, obj=obj)
if count > 0:
return response_base.success()
return response_base.fail()
@router.delete(
'/{pk}',
summary='删除菜单',
dependencies=[
Depends(RequestPermission('sys:menu:del')),
DependsRBAC,
],
)
async def delete_menu(db: CurrentSessionTransaction, pk: Annotated[int, Path(description='菜单 ID')]) -> ResponseModel:
count = await menu_service.delete(db=db, pk=pk)
if count > 0:
return response_base.success()
return response_base.fail()
+74
View File
@@ -0,0 +1,74 @@
from typing import Annotated, Any
from fastapi import APIRouter, File, Path, UploadFile
from fastapi.params import Query
from starlette.responses import StreamingResponse
from backend.app.admin.service.plugin_service import plugin_service
from backend.common.enums import PluginType
from backend.common.response.response_code import CustomResponse
from backend.common.response.response_schema import ResponseModel, ResponseSchemaModel, response_base
from backend.common.security.jwt import DependsSuperUser
router = APIRouter()
@router.get('', summary='获取所有插件', dependencies=[DependsSuperUser])
async def get_all_plugins() -> ResponseSchemaModel[list[dict[str, Any]]]:
plugins = await plugin_service.get_all()
return response_base.success(data=plugins)
@router.get('/changed', summary='是否存在插件变更', dependencies=[DependsSuperUser])
async def plugin_changed() -> ResponseSchemaModel[bool]:
plugins = await plugin_service.changed()
return response_base.success(data=bool(plugins))
@router.post(
'',
summary='安装插件',
description='使用插件 zip 压缩包或 git 仓库地址进行安装(仅开发环境)',
dependencies=[DependsSuperUser],
)
async def install_plugin(
type: Annotated[PluginType, Query(description='插件类型')],
file: Annotated[UploadFile | None, File()] = None,
repo_url: Annotated[str | None, Query(description='插件 git 仓库地址')] = None,
) -> ResponseModel:
plugin_name = await plugin_service.install(type=type, file=file, repo_url=repo_url)
return response_base.success(
res=CustomResponse(
code=200,
msg=f'插件 {plugin_name} 安装成功,请根据插件说明(README.md)进行相关配置并重启服务',
),
)
@router.delete(
'/{plugin}',
summary='卸载插件',
description='此操作会直接删除插件依赖,但不会直接删除插件,而是将插件移动到备份目录(仅开发环境)',
dependencies=[DependsSuperUser],
)
async def uninstall_plugin(plugin: Annotated[str, Path(description='插件名称')]) -> ResponseModel:
await plugin_service.uninstall(plugin=plugin)
return response_base.success(
res=CustomResponse(code=200, msg=f'插件 {plugin} 卸载成功,请根据插件说明(README.md)移除相关配置并重启服务'),
)
@router.put('/{plugin}/status', summary='更新插件状态', dependencies=[DependsSuperUser])
async def update_plugin_status(plugin: Annotated[str, Path(description='插件名称')]) -> ResponseModel:
await plugin_service.update_status(plugin=plugin)
return response_base.success()
@router.get('/{plugin}', summary='下载插件', dependencies=[DependsSuperUser])
async def download_plugin(plugin: Annotated[str, Path(description='插件名称')]) -> StreamingResponse:
bio = await plugin_service.build(plugin=plugin)
return StreamingResponse(
bio,
media_type='application/x-zip-compressed',
headers={'Content-Disposition': f'attachment; filename={plugin}.zip'},
)
+154
View File
@@ -0,0 +1,154 @@
from typing import Annotated
from fastapi import APIRouter, Depends, Path, Query
from backend.app.admin.schema.menu import GetMenuTree
from backend.app.admin.schema.role import (
CreateRoleParam,
DeleteRoleParam,
GetRoleDetail,
GetRoleWithRelationDetail,
UpdateRoleMenuParam,
UpdateRoleParam,
UpdateRoleScopeParam,
)
from backend.app.admin.service.role_service import role_service
from backend.common.pagination import DependsPagination, PageData
from backend.common.response.response_schema import ResponseModel, ResponseSchemaModel, response_base
from backend.common.security.jwt import DependsJwtAuth
from backend.common.security.permission import RequestPermission
from backend.common.security.rbac import DependsRBAC
from backend.database.db import CurrentSession, CurrentSessionTransaction
router = APIRouter()
@router.get('/all', summary='获取所有角色', dependencies=[DependsJwtAuth])
async def get_all_roles(db: CurrentSession) -> ResponseSchemaModel[list[GetRoleDetail]]:
data = await role_service.get_all(db=db)
return response_base.success(data=data)
@router.get('/{pk}/menus', summary='获取角色菜单树', dependencies=[DependsJwtAuth])
async def get_role_menu_tree(
db: CurrentSession,
pk: Annotated[int, Path(description='角色 ID')],
) -> ResponseSchemaModel[list[GetMenuTree] | None]:
menu = await role_service.get_menu_tree(db=db, pk=pk)
return response_base.success(data=menu)
@router.get('/{pk}/scopes', summary='获取角色所有数据范围', dependencies=[DependsJwtAuth])
async def get_role_scopes(
db: CurrentSession, pk: Annotated[int, Path(description='角色 ID')]
) -> ResponseSchemaModel[list[int]]:
rule = await role_service.get_scopes(db=db, pk=pk)
return response_base.success(data=rule)
@router.get('/{pk}', summary='获取角色详情', dependencies=[DependsJwtAuth])
async def get_role(
db: CurrentSession, pk: Annotated[int, Path(description='角色 ID')]
) -> ResponseSchemaModel[GetRoleWithRelationDetail]:
data = await role_service.get(db=db, pk=pk)
return response_base.success(data=data)
@router.get(
'',
summary='分页获取所有角色',
dependencies=[
DependsJwtAuth,
DependsPagination,
],
)
async def get_roles_paginated(
db: CurrentSession,
name: Annotated[str | None, Query(description='角色名称')] = None,
status: Annotated[int | None, Query(description='状态')] = None,
) -> ResponseSchemaModel[PageData[GetRoleDetail]]:
page_data = await role_service.get_list(db=db, name=name, status=status)
return response_base.success(data=page_data)
@router.post(
'',
summary='创建角色',
dependencies=[
Depends(RequestPermission('sys:role:add')),
DependsRBAC,
],
)
async def create_role(db: CurrentSessionTransaction, obj: CreateRoleParam) -> ResponseModel:
await role_service.create(db=db, obj=obj)
return response_base.success()
@router.put(
'/{pk}',
summary='更新角色',
dependencies=[
Depends(RequestPermission('sys:role:edit')),
DependsRBAC,
],
)
async def update_role(
db: CurrentSessionTransaction, pk: Annotated[int, Path(description='角色 ID')], obj: UpdateRoleParam
) -> ResponseModel:
count = await role_service.update(db=db, pk=pk, obj=obj)
if count > 0:
return response_base.success()
return response_base.fail()
@router.put(
'/{pk}/menus',
summary='更新角色菜单',
dependencies=[
Depends(RequestPermission('sys:role:menu:edit')),
DependsRBAC,
],
)
async def update_role_menus(
db: CurrentSessionTransaction,
pk: Annotated[int, Path(description='角色 ID')],
menu_ids: UpdateRoleMenuParam,
) -> ResponseModel:
count = await role_service.update_role_menu(db=db, pk=pk, menu_ids=menu_ids)
if count > 0:
return response_base.success()
return response_base.fail()
@router.put(
'/{pk}/scopes',
summary='更新角色数据范围',
dependencies=[
Depends(RequestPermission('sys:role:scope:edit')),
DependsRBAC,
],
)
async def update_role_scopes(
db: CurrentSessionTransaction,
pk: Annotated[int, Path(description='角色 ID')],
scope_ids: UpdateRoleScopeParam,
) -> ResponseModel:
count = await role_service.update_role_scope(db=db, pk=pk, scope_ids=scope_ids)
if count > 0:
return response_base.success()
return response_base.fail()
@router.delete(
'',
summary='批量删除角色',
dependencies=[
Depends(RequestPermission('sys:role:del')),
DependsRBAC,
],
)
async def delete_roles(db: CurrentSessionTransaction, obj: DeleteRoleParam) -> ResponseModel:
count = await role_service.delete(db=db, obj=obj)
if count > 0:
return response_base.success()
return response_base.fail()
+172
View File
@@ -0,0 +1,172 @@
from typing import Annotated
from fastapi import APIRouter, Body, Depends, Path, Query, Request
from backend.app.admin.schema.role import GetRoleDetail
from backend.app.admin.schema.user import (
AddUserParam,
GetCurrentUserInfoWithRelationDetail,
GetUserInfoWithRelationDetail,
ResetPasswordParam,
UpdateUserParam,
)
from backend.app.admin.service.user_service import user_service
from backend.common.enums import UserPermissionType
from backend.common.pagination import DependsPagination, PageData
from backend.common.response.response_schema import ResponseModel, ResponseSchemaModel, response_base
from backend.common.security.jwt import DependsJwtAuth, DependsSuperUser
from backend.common.security.permission import RequestPermission
from backend.common.security.rbac import DependsRBAC
from backend.database.db import CurrentSession, CurrentSessionTransaction
router = APIRouter()
@router.get('/me', summary='获取当前用户信息', dependencies=[DependsJwtAuth])
async def get_current_user(request: Request) -> ResponseSchemaModel[GetCurrentUserInfoWithRelationDetail]:
data = request.user.model_dump()
return response_base.success(data=data)
@router.get('/{pk}', summary='获取用户信息', dependencies=[DependsJwtAuth])
async def get_userinfo(
db: CurrentSession,
pk: Annotated[int, Path(description='用户 ID')],
) -> ResponseSchemaModel[GetUserInfoWithRelationDetail]:
data = await user_service.get_userinfo(db=db, pk=pk)
return response_base.success(data=data)
@router.get('/{pk}/roles', summary='获取用户所有角色', dependencies=[DependsJwtAuth])
async def get_user_roles(
db: CurrentSession, pk: Annotated[int, Path(description='用户 ID')]
) -> ResponseSchemaModel[list[GetRoleDetail]]:
data = await user_service.get_roles(db=db, pk=pk)
return response_base.success(data=data)
@router.get(
'',
summary='分页获取所有用户',
dependencies=[
DependsJwtAuth,
DependsPagination,
],
)
async def get_users_paginated(
db: CurrentSession,
dept: Annotated[int | None, Query(description='部门 ID')] = None,
username: Annotated[str | None, Query(description='用户名')] = None,
phone: Annotated[str | None, Query(description='手机号')] = None,
status: Annotated[int | None, Query(description='状态')] = None,
) -> ResponseSchemaModel[PageData[GetUserInfoWithRelationDetail]]:
page_data = await user_service.get_list(db=db, dept=dept, username=username, phone=phone, status=status)
return response_base.success(data=page_data)
@router.post('', summary='创建用户', dependencies=[DependsSuperUser])
async def create_user(
db: CurrentSessionTransaction, obj: AddUserParam
) -> ResponseSchemaModel[GetUserInfoWithRelationDetail]:
await user_service.create(db=db, obj=obj)
data = await user_service.get_userinfo(db=db, username=obj.username)
return response_base.success(data=data)
@router.put('/{pk}', summary='更新用户信息', dependencies=[DependsSuperUser])
async def update_user(
db: CurrentSessionTransaction,
pk: Annotated[int, Path(description='用户 ID')],
obj: UpdateUserParam,
) -> ResponseModel:
count = await user_service.update(db=db, pk=pk, obj=obj)
if count > 0:
return response_base.success()
return response_base.fail()
@router.put('/{pk}/permissions', summary='更新用户权限', dependencies=[DependsSuperUser])
async def update_user_permission(
db: CurrentSessionTransaction,
request: Request,
pk: Annotated[int, Path(description='用户 ID')],
type: Annotated[UserPermissionType, Query(description='权限类型')],
) -> ResponseModel:
count = await user_service.update_permission(db=db, request=request, pk=pk, type=type)
if count > 0:
return response_base.success()
return response_base.fail()
@router.put('/me/password', summary='更新当前用户密码', dependencies=[DependsJwtAuth])
async def update_user_password(
db: CurrentSessionTransaction, request: Request, obj: ResetPasswordParam
) -> ResponseModel:
count = await user_service.update_password(db=db, user_id=request.user.id, obj=obj)
if count > 0:
return response_base.success()
return response_base.fail()
@router.put('/{pk}/password', summary='重置用户密码', dependencies=[DependsSuperUser])
async def reset_user_password(
db: CurrentSessionTransaction,
pk: Annotated[int, Path(description='用户 ID')],
password: Annotated[str, Body(embed=True, description='新密码')],
) -> ResponseModel:
count = await user_service.reset_password(db=db, pk=pk, password=password)
if count > 0:
return response_base.success()
return response_base.fail()
@router.put('/me/nickname', summary='更新当前用户昵称', dependencies=[DependsJwtAuth])
async def update_user_nickname(
db: CurrentSessionTransaction,
request: Request,
nickname: Annotated[str, Body(embed=True, description='用户昵称')],
) -> ResponseModel:
count = await user_service.update_nickname(db=db, user_id=request.user.id, nickname=nickname)
if count > 0:
return response_base.success()
return response_base.fail()
@router.put('/me/avatar', summary='更新当前用户头像', dependencies=[DependsJwtAuth])
async def update_user_avatar(
db: CurrentSessionTransaction,
request: Request,
avatar: Annotated[str, Body(embed=True, description='用户头像地址')],
) -> ResponseModel:
count = await user_service.update_avatar(db=db, user_id=request.user.id, avatar=avatar)
if count > 0:
return response_base.success()
return response_base.fail()
@router.put('/me/email', summary='更新当前用户邮箱', dependencies=[DependsJwtAuth])
async def update_user_email(
db: CurrentSessionTransaction,
request: Request,
captcha: Annotated[str, Body(embed=True, description='邮箱验证码')],
email: Annotated[str, Body(embed=True, description='用户邮箱')],
) -> ResponseModel:
count = await user_service.update_email(db=db, user_id=request.user.id, captcha=captcha, email=email)
if count > 0:
return response_base.success()
return response_base.fail()
@router.delete(
path='/{pk}',
summary='删除用户',
dependencies=[
Depends(RequestPermission('sys:user:del')),
DependsRBAC,
],
)
async def delete_user(db: CurrentSessionTransaction, pk: Annotated[int, Path(description='用户 ID')]) -> ResponseModel:
count = await user_service.delete(db=db, pk=pk)
if count > 0:
return response_base.success()
return response_base.fail()
View File
+110
View File
@@ -0,0 +1,110 @@
from collections.abc import Sequence
from sqlalchemy import Select
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy_crud_plus import CRUDPlus
from backend.app.admin.model import DataRule
from backend.app.admin.schema.data_rule import CreateDataRuleParam, UpdateDataRuleParam
from backend.utils.timezone import timezone
class CRUDDataRule(CRUDPlus[DataRule]):
"""数据规则数据库操作类"""
async def get(self, db: AsyncSession, pk: int) -> DataRule | None:
"""
获取规则详情
:param db: 数据库会话
:param pk: 规则 ID
:return:
"""
return await self.select_model(db, pk, deleted=0)
async def get_select(self, name: str | None) -> Select:
"""
获取规则列表查询表达式
:param name: 规则名称
:return:
"""
filters = {'deleted': 0}
if name is not None:
filters['name__like'] = f'%{name}%'
return await self.select_order('id', **filters)
async def get_by_name(self, db: AsyncSession, name: str) -> DataRule | None:
"""
通过名称获取规则
:param db: 数据库会话
:param name: 规则名称
:return:
"""
return await self.select_model_by_column(db, name=name, deleted=0)
async def get_all(self, db: AsyncSession) -> Sequence[DataRule]:
"""
获取所有规则
:param db: 数据库会话
:return:
"""
return await self.select_models(db, deleted=0)
async def get_all_by_ids(self, db: AsyncSession, pks: list[int]) -> Sequence[DataRule]:
"""
通过 ID 列表批量获取数据规则
:param db: 数据库会话
:param pks: 规则 ID 列表
:return:
"""
return await self.select_models(db, id__in=pks, deleted=0)
async def create(self, db: AsyncSession, obj: CreateDataRuleParam) -> None:
"""
创建规则
:param db: 数据库会话
:param obj: 创建规则参数
:return:
"""
await self.create_model(db, obj)
async def update(self, db: AsyncSession, pk: int, obj: UpdateDataRuleParam) -> int:
"""
更新规则
:param db: 数据库会话
:param pk: 规则 ID
:param obj: 更新规则参数
:return:
"""
return await self.update_model_by_column(db, obj, id=pk, deleted=0)
async def delete(self, db: AsyncSession, pks: list[int]) -> int:
"""
批量删除规则
:param db: 数据库会话
:param pks: 规则 ID 列表
:return:
"""
return await self.delete_model_by_column(
db,
allow_multiple=True,
logical_deletion=True,
deleted_flag_column='deleted',
deleted_flag_value=self.model.id,
deleted_at_column='deleted_time',
deleted_at_factory=timezone.now(),
id__in=pks,
deleted=0,
)
data_rule_dao: CRUDDataRule = CRUDDataRule(DataRule)
+167
View File
@@ -0,0 +1,167 @@
from collections.abc import Sequence
from typing import Any
from sqlalchemy import Select, and_, delete, insert
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy_crud_plus import CRUDPlus, JoinConfig
from backend.app.admin.model import DataRule, DataScope, data_scope_rule
from backend.app.admin.schema.data_scope import (
CreateDataScopeParam,
CreateDataScopeRuleParam,
UpdateDataScopeParam,
UpdateDataScopeRuleParam,
)
from backend.utils.serializers import select_join_serialize
from backend.utils.timezone import timezone
class CRUDDataScope(CRUDPlus[DataScope]):
"""数据范围数据库操作类"""
async def get(self, db: AsyncSession, pk: int) -> DataScope | None:
"""
获取数据范围详情
:param db: 数据库会话
:param pk: 范围 ID
:return:
"""
return await self.select_model(db, pk, deleted=0)
async def get_by_name(self, db: AsyncSession, name: str) -> DataScope | None:
"""
通过名称获取数据范围
:param db: 数据库会话
:param name: 范围名称
:return:
"""
return await self.select_model_by_column(db, name=name, deleted=0)
async def get_join(self, db: AsyncSession, pk: int) -> Any:
"""
获取数据范围关联数据
:param db: 数据库会话
:param pk: 范围 ID
:return:
"""
result = await self.select_models(
db,
id=pk,
deleted=0,
join_conditions=[
JoinConfig(model=data_scope_rule, join_on=data_scope_rule.c.data_scope_id == self.model.id),
JoinConfig(
model=DataRule,
join_on=and_(DataRule.id == data_scope_rule.c.data_rule_id, DataRule.deleted == 0),
fill_result=True,
),
],
)
return select_join_serialize(result, relationships=['DataScope-m2m-DataRule:rules'])
async def get_all(self, db: AsyncSession) -> Sequence[DataScope]:
"""
获取所有数据范围
:param db: 数据库会话
:return:
"""
return await self.select_models(db, deleted=0)
async def get_all_by_ids(self, db: AsyncSession, pks: list[int]) -> Sequence[DataScope]:
"""
通过 ID 列表批量获取数据范围
:param db: 数据库会话
:param pks: 范围 ID 列表
:return:
"""
return await self.select_models(db, id__in=pks, deleted=0)
async def get_select(self, name: str | None, status: int | None) -> Select:
"""
获取数据范围列表查询表达式
:param name: 范围名称
:param status: 范围状态
:return:
"""
filters = {'deleted': 0}
if name is not None:
filters['name__like'] = f'%{name}%'
if status is not None:
filters['status'] = status
return await self.select_order('id', **filters)
async def create(self, db: AsyncSession, obj: CreateDataScopeParam) -> None:
"""
创建数据范围
:param db: 数据库会话
:param obj: 创建数据范围参数
:return:
"""
await self.create_model(db, obj)
async def update(self, db: AsyncSession, pk: int, obj: UpdateDataScopeParam) -> int:
"""
更新数据范围
:param db: 数据库会话
:param pk: 范围 ID
:param obj: 更新数据范围参数
:return:
"""
return await self.update_model_by_column(db, obj, id=pk, deleted=0)
@staticmethod
async def update_rules(db: AsyncSession, pk: int, rule_ids: UpdateDataScopeRuleParam) -> int:
"""
更新数据范围规则
:param db: 数据库会话
:param pk: 范围 ID
:param rule_ids: 数据规则 ID 列表
:return:
"""
data_scope_rule_stmt = delete(data_scope_rule).where(data_scope_rule.c.data_scope_id == pk)
await db.execute(data_scope_rule_stmt)
if rule_ids.rules:
data_scope_rule_data = [
CreateDataScopeRuleParam(data_scope_id=pk, data_rule_id=rule_id).model_dump()
for rule_id in rule_ids.rules
]
data_scope_rule_stmt = insert(data_scope_rule)
await db.execute(data_scope_rule_stmt, data_scope_rule_data)
return len(rule_ids.rules)
async def delete(self, db: AsyncSession, pks: list[int]) -> int:
"""
批量删除数据范围
:param db: 数据库会话
:param pks: 范围 ID 列表
:return:
"""
return await self.delete_model_by_column(
db,
allow_multiple=True,
logical_deletion=True,
deleted_flag_column='deleted',
deleted_flag_value=self.model.id,
deleted_at_column='deleted_time',
deleted_at_factory=timezone.now(),
id__in=pks,
deleted=0,
)
data_scope_dao: CRUDDataScope = CRUDDataScope(DataScope)
+143
View File
@@ -0,0 +1,143 @@
from collections.abc import Sequence
from typing import Any
from sqlalchemy import ColumnElement, and_
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy_crud_plus import CRUDPlus, JoinConfig
from backend.app.admin.model import Dept, User
from backend.app.admin.schema.dept import CreateDeptParam, UpdateDeptParam
from backend.utils.serializers import select_join_serialize
from backend.utils.timezone import timezone
class CRUDDept(CRUDPlus[Dept]):
"""部门数据库操作类"""
async def get(self, db: AsyncSession, dept_id: int) -> Dept | None:
"""
获取部门详情
:param db: 数据库会话
:param dept_id: 部门 ID
:return:
"""
return await self.select_model_by_column(db, id=dept_id, deleted=0)
async def get_by_name(self, db: AsyncSession, name: str) -> Dept | None:
"""
通过名称获取部门
:param db: 数据库会话
:param name: 部门名称
:return:
"""
return await self.select_model_by_column(db, name=name, deleted=0)
async def get_all(
self,
db: AsyncSession,
data_filter: ColumnElement[bool],
name: str | None,
leader: str | None,
phone: str | None,
status: int | None,
) -> Sequence[Dept]:
"""
获取所有部门
:param db: 数据库会话
:param data_filter: 请求用户
:param name: 部门名称
:param leader: 负责人
:param phone: 联系电话
:param status: 部门状态
:return:
"""
filters = {'deleted': 0}
if name is not None:
filters['name__like'] = f'%{name}%'
if leader is not None:
filters['leader__like'] = f'%{leader}%'
if phone is not None:
filters['phone__startswith'] = phone
if status is not None:
filters['status'] = status
return await self.select_models_order(db, 'sort', 'asc', data_filter, **filters)
async def create(self, db: AsyncSession, obj: CreateDeptParam) -> None:
"""
创建部门
:param db: 数据库会话
:param obj: 创建部门参数
:return:
"""
await self.create_model(db, obj)
async def update(self, db: AsyncSession, dept_id: int, obj: UpdateDeptParam) -> int:
"""
更新部门
:param db: 数据库会话
:param dept_id: 部门 ID
:param obj: 更新部门参数
:return:
"""
return await self.update_model_by_column(db, obj, id=dept_id, deleted=0)
async def delete(self, db: AsyncSession, dept_id: int) -> int:
"""
删除部门
:param db: 数据库会话
:param dept_id: 部门 ID
:return:
"""
return await self.delete_model_by_column(
db,
logical_deletion=True,
deleted_flag_column='deleted',
deleted_flag_value=self.model.id,
deleted_at_column='deleted_time',
deleted_at_factory=timezone.now(),
id=dept_id,
deleted=0,
)
async def get_join(self, db: AsyncSession, dept_id: int) -> Any | None:
"""
获取部门及关联数据
:param db: 数据库会话
:param dept_id: 部门 ID
:return:
"""
result = await self.select_model(
db,
dept_id,
deleted=0,
join_conditions=[
JoinConfig(
model=User,
join_on=and_(User.dept_id == self.model.id, User.deleted == 0),
fill_result=True,
)
],
)
return select_join_serialize(result, relationships=['Dept-o2m-User'])
async def get_children(self, db: AsyncSession, dept_id: int) -> Sequence[Dept | None]:
"""
获取子部门列表
:param db: 数据库会话
:param dept_id: 部门 ID
:return:
"""
return await self.select_models(db, parent_id=dept_id, deleted=0)
dept_dao: CRUDDept = CRUDDept(Dept)
+64
View File
@@ -0,0 +1,64 @@
from sqlalchemy import Select
from sqlalchemy import delete as sa_delete
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy_crud_plus import CRUDPlus
from backend.app.admin.model import LoginLog
from backend.app.admin.schema.login_log import CreateLoginLogParam
class CRUDLoginLog(CRUDPlus[LoginLog]):
"""登录日志数据库操作类"""
async def get_select(self, username: str | None, status: int | None, ip: str | None) -> Select:
"""
获取登录日志列表查询表达式
:param username: 用户名
:param status: 登录状态
:param ip: IP 地址
:return:
"""
filters = {}
if username is not None:
filters['username__like'] = f'%{username}%'
if status is not None:
filters['status'] = status
if ip is not None:
filters['ip__like'] = f'%{ip}%'
return await self.select_order('created_time', 'desc', **filters)
async def create(self, db: AsyncSession, obj: CreateLoginLogParam) -> None:
"""
创建登录日志
:param db: 数据库会话
:param obj: 创建登录日志参数
:return:
"""
await self.create_model(db, obj, commit=True)
async def delete(self, db: AsyncSession, pks: list[int]) -> int:
"""
批量删除登录日志
:param db: 数据库会话
:param pks: 登录日志 ID 列表
:return:
"""
return await self.delete_model_by_column(db, allow_multiple=True, id__in=pks)
@staticmethod
async def delete_all(db: AsyncSession) -> None:
"""
删除所有日志
:param db: 数据库会话
:return:
"""
await db.execute(sa_delete(LoginLog))
login_log_dao: CRUDLoginLog = CRUDLoginLog(LoginLog)
+132
View File
@@ -0,0 +1,132 @@
from collections.abc import Sequence
from sqlalchemy import delete
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy_crud_plus import CRUDPlus
from backend.app.admin.model import Menu, role_menu
from backend.app.admin.schema.menu import CreateMenuParam, UpdateMenuParam
from backend.utils.timezone import timezone
class CRUDMenu(CRUDPlus[Menu]):
"""菜单数据库操作类"""
async def get(self, db: AsyncSession, menu_id: int) -> Menu | None:
"""
获取菜单详情
:param db: 数据库会话
:param menu_id: 菜单 ID
:return:
"""
return await self.select_model(db, menu_id, deleted=0)
async def get_by_title(self, db: AsyncSession, title: str) -> Menu | None:
"""
通过标题获取菜单
:param db: 数据库会话
:param title: 菜单标题
:return:
"""
return await self.select_model_by_column(db, title=title, type__ne=2, deleted=0)
async def get_all(self, db: AsyncSession, title: str | None, status: int | None) -> Sequence[Menu]:
"""
获取菜单列表
:param db: 数据库会话
:param title: 菜单标题
:param status: 菜单状态
:return:
"""
filters = {'deleted': 0}
if title is not None:
filters['title__like'] = f'%{title}%'
if status is not None:
filters['status'] = status
return await self.select_models_order(db, 'sort', 'asc', **filters)
async def get_sidebar(self, db: AsyncSession, menu_ids: list[int] | None) -> Sequence[Menu]:
"""
获取用户的菜单侧边栏
:param db: 数据库会话
:param menu_ids: 菜单 ID 列表
:return:
"""
filters = {'type__in': [0, 1, 3, 4], 'deleted': 0}
if menu_ids:
filters['id__in'] = menu_ids
return await self.select_models_order(db, 'sort', 'asc', **filters)
async def get_all_by_ids(self, db: AsyncSession, menu_ids: list[int]) -> Sequence[Menu]:
"""
通过 ID 列表批量获取菜单
:param db: 数据库会话
:param menu_ids: 菜单 ID 列表
:return:
"""
return await self.select_models(db, id__in=menu_ids, deleted=0)
async def create(self, db: AsyncSession, obj: CreateMenuParam) -> None:
"""
创建菜单
:param db: 数据库会话
:param obj: 创建菜单参数
:return:
"""
await self.create_model(db, obj)
async def update(self, db: AsyncSession, menu_id: int, obj: UpdateMenuParam) -> int:
"""
更新菜单
:param db: 数据库会话
:param menu_id: 菜单 ID
:param obj: 更新菜单参数
:return:
"""
return await self.update_model_by_column(db, obj, id=menu_id, deleted=0)
async def delete(self, db: AsyncSession, menu_id: int) -> int:
"""
删除菜单
:param db: 数据库会话
:param menu_id: 菜单 ID
:return:
"""
role_menu_stmt = delete(role_menu).where(role_menu.c.menu_id == menu_id)
await db.execute(role_menu_stmt)
return await self.delete_model_by_column(
db,
logical_deletion=True,
deleted_flag_column='deleted',
deleted_flag_value=self.model.id,
deleted_at_column='deleted_time',
deleted_at_factory=timezone.now(),
id=menu_id,
deleted=0,
)
async def get_children(self, db: AsyncSession, menu_id: int) -> Sequence[Menu | None]:
"""
获取子菜单列表
:param db: 数据库会话
:param menu_id: 菜单 ID
:return:
"""
return await self.select_models(db, parent_id=menu_id, deleted=0)
menu_dao: CRUDMenu = CRUDMenu(Menu)
+74
View File
@@ -0,0 +1,74 @@
from sqlalchemy import Select
from sqlalchemy import delete as sa_delete
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy_crud_plus import CRUDPlus
from backend.app.admin.model import OperaLog
from backend.app.admin.schema.opera_log import CreateOperaLogParam
class CRUDOperaLog(CRUDPlus[OperaLog]):
"""操作日志数据库操作类"""
async def get_select(self, username: str | None, status: int | None, ip: str | None) -> Select:
"""
获取操作日志列表查询表达式
:param username: 用户名
:param status: 操作状态
:param ip: IP 地址
:return:
"""
filters = {}
if username is not None:
filters['username__like'] = f'%{username}%'
if status is not None:
filters['status__eq'] = status
if ip is not None:
filters['ip__like'] = f'%{ip}%'
return await self.select_order('created_time', 'desc', **filters)
async def create(self, db: AsyncSession, obj: CreateOperaLogParam) -> None:
"""
创建操作日志
:param db: 数据库会话
:param obj: 操作日志创建参数
:return:
"""
await self.create_model(db, obj)
async def bulk_create(self, db: AsyncSession, objs: list[CreateOperaLogParam]) -> None:
"""
批量创建操作日志
:param db: 数据库会话
:param objs: 操作日志创建参数列表
:return:
"""
await self.create_models(db, objs)
async def delete(self, db: AsyncSession, pks: list[int]) -> int:
"""
批量删除操作日志
:param db: 数据库会话
:param pks: 操作日志 ID 列表
:return:
"""
return await self.delete_model_by_column(db, allow_multiple=True, id__in=pks)
@staticmethod
async def delete_all(db: AsyncSession) -> None:
"""
删除所有日志
:param db: 数据库会话
:return:
"""
await db.execute(sa_delete(OperaLog))
opera_log_dao: CRUDOperaLog = CRUDOperaLog(OperaLog)
+215
View File
@@ -0,0 +1,215 @@
from collections.abc import Sequence
from typing import Any
from sqlalchemy import Select, and_, delete, insert, select
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy_crud_plus import CRUDPlus, JoinConfig
from backend.app.admin.model import DataScope, Menu, Role, role_data_scope, role_menu
from backend.app.admin.schema.role import (
CreateRoleMenuParam,
CreateRoleParam,
CreateRoleScopeParam,
UpdateRoleMenuParam,
UpdateRoleParam,
UpdateRoleScopeParam,
)
from backend.utils.serializers import select_join_serialize
from backend.utils.timezone import timezone
class CRUDRole(CRUDPlus[Role]):
"""角色数据库操作类"""
async def get(self, db: AsyncSession, role_id: int) -> Role | None:
"""
获取角色详情
:param db: 数据库会话
:param role_id: 角色 ID
:return:
"""
return await self.select_model(db, role_id, deleted=0)
@staticmethod
async def get_menus(db: AsyncSession, role_id: int) -> Sequence[Menu] | None:
"""
获取角色菜单
:param db: 数据库会话
:param role_id: 角色 ID
:return:
"""
menu_stmt = (
select(Menu)
.join(role_menu, Menu.id == role_menu.c.menu_id)
.where(role_menu.c.role_id == role_id, Menu.deleted == 0)
)
result = await db.execute(menu_stmt)
return result.scalars().all()
async def get_join(self, db: AsyncSession, role_id: int) -> Any:
"""
获取角色及关联数据
:param db: 数据库会话
:param role_id: 角色 ID
:return:
"""
result = await self.select_models(
db,
id=role_id,
deleted=0,
join_conditions=[
JoinConfig(model=role_menu, join_on=role_menu.c.role_id == self.model.id),
JoinConfig(
model=Menu,
join_on=and_(Menu.id == role_menu.c.menu_id, Menu.deleted == 0),
fill_result=True,
),
JoinConfig(model=role_data_scope, join_on=role_data_scope.c.role_id == self.model.id),
JoinConfig(
model=DataScope,
join_on=and_(DataScope.id == role_data_scope.c.data_scope_id, DataScope.deleted == 0),
fill_result=True,
),
],
)
return select_join_serialize(result, relationships=['Role-m2m-Menu', 'Role-m2m-DataScope:scopes'])
async def get_all(self, db: AsyncSession) -> Sequence[Role]:
"""
获取所有角色
:param db: 数据库会话
:return:
"""
return await self.select_models(db, deleted=0)
async def get_all_by_ids(self, db: AsyncSession, role_ids: list[int]) -> Sequence[Role]:
"""
通过 ID 列表批量获取角色
:param db: 数据库会话
:param role_ids: 角色 ID 列表
:return:
"""
return await self.select_models(db, id__in=role_ids, deleted=0)
async def get_select(self, name: str | None, status: int | None) -> Select:
"""
获取角色列表查询表达式
:param name: 角色名称
:param status: 角色状态
:return:
"""
filters = {'deleted': 0}
if name is not None:
filters['name__like'] = f'%{name}%'
if status is not None:
filters['status'] = status
return await self.select_order('id', **filters)
async def get_by_name(self, db: AsyncSession, name: str) -> Role | None:
"""
通过名称获取角色
:param db: 数据库会话
:param name: 角色名称
:return:
"""
return await self.select_model_by_column(db, name=name, deleted=0)
async def create(self, db: AsyncSession, obj: CreateRoleParam) -> None:
"""
创建角色
:param db: 数据库会话
:param obj: 创建角色参数
:return:
"""
await self.create_model(db, obj)
async def update(self, db: AsyncSession, role_id: int, obj: UpdateRoleParam) -> int:
"""
更新角色
:param db: 数据库会话
:param role_id: 角色 ID
:param obj: 更新角色参数
:return:
"""
return await self.update_model_by_column(db, obj, id=role_id, deleted=0)
@staticmethod
async def update_menus(db: AsyncSession, role_id: int, menu_ids: UpdateRoleMenuParam) -> int:
"""
更新角色菜单
:param db: 数据库会话
:param role_id: 角色 ID
:param menu_ids: 菜单 ID 列表
:return:
"""
role_menu_stmt = delete(role_menu).where(role_menu.c.role_id == role_id)
await db.execute(role_menu_stmt)
if menu_ids.menus:
role_menu_data = [
CreateRoleMenuParam(role_id=role_id, menu_id=menu_id).model_dump() for menu_id in menu_ids.menus
]
role_menu_stmt = insert(role_menu)
await db.execute(role_menu_stmt, role_menu_data)
return len(menu_ids.menus)
@staticmethod
async def update_scopes(db: AsyncSession, role_id: int, scope_ids: UpdateRoleScopeParam) -> int:
"""
更新角色数据范围
:param db: 数据库会话
:param role_id: 角色 ID
:param scope_ids: 权限范围 ID 列表
:return:
"""
role_scope_stmt = delete(role_data_scope).where(role_data_scope.c.role_id == role_id)
await db.execute(role_scope_stmt)
if scope_ids.scopes:
role_scope_data = [
CreateRoleScopeParam(role_id=role_id, data_scope_id=scope_id).model_dump()
for scope_id in scope_ids.scopes
]
role_scope_stmt = insert(role_data_scope)
await db.execute(role_scope_stmt, role_scope_data)
return len(scope_ids.scopes)
async def delete(self, db: AsyncSession, role_ids: list[int]) -> int:
"""
批量删除角色
:param db: 数据库会话
:param role_ids: 角色 ID 列表
:return:
"""
return await self.delete_model_by_column(
db,
allow_multiple=True,
logical_deletion=True,
deleted_flag_column='deleted',
deleted_flag_value=self.model.id,
deleted_at_column='deleted_time',
deleted_at_factory=timezone.now(),
id__in=role_ids,
deleted=0,
)
role_dao: CRUDRole = CRUDRole(Role)
+418
View File
@@ -0,0 +1,418 @@
from collections.abc import Sequence
from typing import Any
import bcrypt
from sqlalchemy import Select, and_, delete, insert, select
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy_crud_plus import CRUDPlus, JoinConfig
from backend.app.admin.model import (
DataRule,
DataScope,
Dept,
Menu,
Role,
User,
data_scope_rule,
role_data_scope,
role_menu,
user_role,
)
from backend.app.admin.schema.user import (
AddOAuth2UserParam,
AddUserParam,
AddUserRoleParam,
UpdateUserParam,
)
from backend.app.admin.utils.password_security import get_hash_password
from backend.common.enums import StatusType
from backend.common.exception import errors
from backend.plugin.core import check_plugin_installed
from backend.utils.serializers import select_join_serialize
from backend.utils.timezone import timezone
class CRUDUser(CRUDPlus[User]):
"""用户数据库操作类"""
async def get(self, db: AsyncSession, user_id: int) -> User | None:
"""
获取用户详情
:param db: 数据库会话
:param user_id: 用户 ID
:return:
"""
return await self.select_model(db, user_id, deleted=0)
async def get_by_username(self, db: AsyncSession, username: str) -> User | None:
"""
通过用户名获取用户
:param db: 数据库会话
:param username: 用户名
:return:
"""
return await self.select_model_by_column(db, username=username, deleted=0)
async def get_all_by_usernames(self, db: AsyncSession, usernames: list[str]) -> Sequence[User]:
"""
通过用户名列表批量获取用户
:param db: 数据库会话
:param usernames: 用户名列表
:return:
"""
return await self.select_models(db, username__in=usernames, deleted=0)
async def get_by_nickname(self, db: AsyncSession, nickname: str) -> User | None:
"""
通过昵称获取用户
:param db: 数据库会话
:param nickname: 用户昵称
:return:
"""
return await self.select_model_by_column(db, nickname=nickname, deleted=0)
async def check_email(self, db: AsyncSession, email: str) -> User | None:
"""
检查邮箱是否已被绑定
:param db: 数据库会话
:param email: 电子邮箱
:return:
"""
return await self.select_model_by_column(db, email=email, deleted=0)
async def get_select(self, dept: int | None, username: str | None, phone: str | None, status: int | None) -> Select:
"""
获取用户列表查询表达式
:param dept: 部门 ID
:param username: 用户名
:param phone: 电话号码
:param status: 用户状态
:return:
"""
filters = {'deleted': 0}
if dept:
filters['dept_id'] = dept
if username:
filters['username__like'] = f'%{username}%'
if phone:
filters['phone__like'] = f'%{phone}%'
if status is not None:
filters['status'] = status
return await self.select_order(
'id',
'desc',
join_conditions=[
JoinConfig(
model=Dept,
join_on=and_(Dept.id == self.model.dept_id, Dept.deleted == 0),
fill_result=True,
),
JoinConfig(model=user_role, join_on=user_role.c.user_id == self.model.id),
JoinConfig(
model=Role,
join_on=and_(Role.id == user_role.c.role_id, Role.deleted == 0),
fill_result=True,
),
],
**filters,
)
async def add(self, db: AsyncSession, obj: AddUserParam) -> None:
"""
添加用户
:param db: 数据库会话
:param obj: 添加用户参数
:return:
"""
salt = bcrypt.gensalt()
obj.password = get_hash_password(obj.password, salt)
dict_obj = obj.model_dump(exclude={'roles'})
dict_obj.update({'salt': salt})
new_user = self.model(**dict_obj)
db.add(new_user)
await db.flush()
if obj.roles:
role_stmt = select(Role).where(Role.id.in_(obj.roles), Role.deleted == 0)
result = await db.execute(role_stmt)
roles = result.scalars().all()
user_role_data = [AddUserRoleParam(user_id=new_user.id, role_id=role.id).model_dump() for role in roles]
user_role_stmt = insert(user_role)
await db.execute(user_role_stmt, user_role_data)
async def add_by_oauth2(self, db: AsyncSession, obj: AddOAuth2UserParam) -> None:
"""
通过 OAuth2 添加用户
:param db: 数据库会话
:param obj: 注册用户参数
:return:
"""
dict_obj = obj.model_dump()
dict_obj.update({'is_staff': True, 'salt': None})
new_user = self.model(**dict_obj)
db.add(new_user)
await db.flush()
role_stmt = select(Role).where(Role.status == StatusType.enable, Role.deleted == 0)
result = await db.execute(role_stmt)
role = result.scalars().first() # 默认绑定第一个角色
if role is None:
raise errors.NotFoundError(msg='未找到可用角色,请联系系统管理员')
user_role_stmt = insert(user_role).values(AddUserRoleParam(user_id=new_user.id, role_id=role.id).model_dump())
await db.execute(user_role_stmt)
async def update(self, db: AsyncSession, user_id: int, obj: UpdateUserParam) -> int:
"""
更新用户信息
:param db: 数据库会话
:param user_id: 用户 ID
:param obj: 更新用户参数
:return:
"""
role_ids = obj.roles
del obj.roles
count = await self.update_model_by_column(db, obj, id=user_id, deleted=0)
user_role_stmt = delete(user_role).where(user_role.c.user_id == user_id)
await db.execute(user_role_stmt)
if role_ids:
role_stmt = select(Role).where(Role.id.in_(role_ids), Role.deleted == 0)
result = await db.execute(role_stmt)
roles = result.scalars().all()
user_role_data = [AddUserRoleParam(user_id=user_id, role_id=role.id).model_dump() for role in roles]
user_role_stmt = insert(user_role)
await db.execute(user_role_stmt, user_role_data)
return count
async def update_login_time(self, db: AsyncSession, username: str) -> int:
"""
更新用户上次登录时间
:param db: 数据库会话
:param username: 用户名
:return:
"""
return await self.update_model_by_column(db, {'last_login_time': timezone.now()}, username=username, deleted=0)
async def update_password_changed_time(self, db: AsyncSession, user_id: int) -> int:
"""
更新用户上次密码变更时间
:param db: 数据库会话
:param user_id: 用户 ID
:return:
"""
return await self.update_model_by_column(
db, {'last_password_changed_time': timezone.now()}, id=user_id, deleted=0
)
async def update_nickname(self, db: AsyncSession, user_id: int, nickname: str) -> int:
"""
更新用户昵称
:param db: 数据库会话
:param user_id: 用户 ID
:param nickname: 用户昵称
:return:
"""
return await self.update_model_by_column(db, {'nickname': nickname}, id=user_id, deleted=0)
async def update_avatar(self, db: AsyncSession, user_id: int, avatar: str) -> int:
"""
更新用户头像
:param db: 数据库会话
:param user_id: 用户 ID
:param avatar: 头像地址
:return:
"""
return await self.update_model_by_column(db, {'avatar': avatar}, id=user_id, deleted=0)
async def update_email(self, db: AsyncSession, user_id: int, email: str) -> int:
"""
更新用户邮箱
:param db: 数据库会话
:param user_id: 用户 ID
:param email: 邮箱
:return:
"""
return await self.update_model_by_column(db, {'email': email}, id=user_id, deleted=0)
async def reset_password(self, db: AsyncSession, pk: int, password: str) -> int:
"""
重置用户密码
:param db: 数据库会话
:param pk: 用户 ID
:param password: 新密码
:return:
"""
salt = bcrypt.gensalt()
new_pwd = get_hash_password(password, salt)
return await self.update_model_by_column(db, {'password': new_pwd, 'salt': salt}, flush=True, id=pk, deleted=0)
async def set_super(self, db: AsyncSession, user_id: int, *, is_super: bool) -> int:
"""
设置用户超级管理员状态
:param db: 数据库会话
:param user_id: 用户 ID
:param is_super: 是否超级管理员
:return:
"""
return await self.update_model_by_column(db, {'is_superuser': is_super}, id=user_id, deleted=0)
async def set_staff(self, db: AsyncSession, user_id: int, *, is_staff: bool) -> int:
"""
设置用户后台登录状态
:param db: 数据库会话
:param user_id: 用户 ID
:param is_staff: 是否可登录后台
:return:
"""
return await self.update_model_by_column(db, {'is_staff': is_staff}, id=user_id, deleted=0)
async def set_status(self, db: AsyncSession, user_id: int, status: int) -> int:
"""
设置用户状态
:param db: 数据库会话
:param user_id: 用户 ID
:param status: 状态
:return:
"""
return await self.update_model_by_column(db, {'status': status}, id=user_id, deleted=0)
async def set_multi_login(self, db: AsyncSession, user_id: int, *, multi_login: bool) -> int:
"""
设置用户多端登录状态
:param db: 数据库会话
:param user_id: 用户 ID
:param multi_login: 是否允许多端登录
:return:
"""
return await self.update_model_by_column(db, {'is_multi_login': multi_login}, id=user_id, deleted=0)
async def delete(self, db: AsyncSession, user_id: int) -> int:
"""
删除用户
:param db: 数据库会话
:param user_id: 用户 ID
:return:
"""
if check_plugin_installed('oauth2'):
try:
from backend.plugin.oauth2.crud.crud_user_social import user_social_dao
await user_social_dao.delete_by_user_id(db, user_id)
except ImportError:
raise errors.ServerError(msg='OAuth2 插件用法导入失败,请联系系统管理员')
user_role_stmt = delete(user_role).where(user_role.c.user_id == user_id)
await db.execute(user_role_stmt)
return await self.delete_model_by_column(
db,
logical_deletion=True,
deleted_flag_column='deleted',
deleted_flag_value=self.model.id,
deleted_at_column='deleted_time',
deleted_at_factory=timezone.now(),
id=user_id,
deleted=0,
)
async def get_join(
self,
db: AsyncSession,
*,
user_id: int | None = None,
username: str | None = None,
) -> Any | None:
"""
获取用户关联信息
:param db: 数据库会话
:param user_id: 用户 ID
:param username: 用户名
:return:
"""
filters = {'deleted': 0}
if user_id:
filters['id'] = user_id
if username:
filters['username'] = username
result = await self.select_models(
db,
join_conditions=[
JoinConfig(
model=Dept,
join_on=and_(Dept.id == self.model.dept_id, Dept.deleted == 0),
fill_result=True,
),
JoinConfig(model=user_role, join_on=user_role.c.user_id == self.model.id),
JoinConfig(
model=Role,
join_on=and_(Role.id == user_role.c.role_id, Role.deleted == 0),
fill_result=True,
),
JoinConfig(model=role_menu, join_on=role_menu.c.role_id == Role.id),
JoinConfig(
model=Menu,
join_on=and_(Menu.id == role_menu.c.menu_id, Menu.deleted == 0),
fill_result=True,
),
JoinConfig(model=role_data_scope, join_on=role_data_scope.c.role_id == Role.id),
JoinConfig(
model=DataScope,
join_on=and_(DataScope.id == role_data_scope.c.data_scope_id, DataScope.deleted == 0),
fill_result=True,
),
JoinConfig(model=data_scope_rule, join_on=data_scope_rule.c.data_scope_id == DataScope.id),
JoinConfig(
model=DataRule,
join_on=and_(DataRule.id == data_scope_rule.c.data_rule_id, DataRule.deleted == 0),
fill_result=True,
),
],
**filters,
)
return select_join_serialize(
result,
relationships=[
'User-m2o-Dept',
'User-m2m-Role',
'Role-m2m-Menu',
'Role-m2m-DataScope:scopes',
'DataScope-m2m-DataRule:rules',
],
)
user_dao: CRUDUser = CRUDUser(User)
@@ -0,0 +1,34 @@
from collections.abc import Sequence
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy_crud_plus import CRUDPlus
from backend.app.admin.model.user_password_history import UserPasswordHistory
from backend.app.admin.schema.user_password_history import CreateUserPasswordHistoryParam
class CRUDUserPasswordHistory(CRUDPlus[UserPasswordHistory]):
"""用户密码历史记录数据库操作类"""
async def create(self, db: AsyncSession, obj: CreateUserPasswordHistoryParam) -> None:
"""
创建密码历史记录
:param db: 数据库会话
:param obj: 创建密码历史记录参数
:return:
"""
await self.create_model(db, obj)
async def get_by_user_id(self, db: AsyncSession, user_id: int) -> Sequence[UserPasswordHistory]:
"""
获取用户的密码历史记录
:param db: 数据库会话
:param user_id: 用户 ID
:return:
"""
return await self.select_models_order(db, 'id', 'desc', self.model.user_id == user_id)
user_password_history_dao: CRUDUserPasswordHistory = CRUDUserPasswordHistory(UserPasswordHistory)
+13
View File
@@ -0,0 +1,13 @@
from backend.app.admin.model.data_rule import DataRule as DataRule
from backend.app.admin.model.data_scope import DataScope as DataScope
from backend.app.admin.model.dept import Dept as Dept
from backend.app.admin.model.login_log import LoginLog as LoginLog
from backend.app.admin.model.m2m import data_scope_rule as data_scope_rule
from backend.app.admin.model.m2m import role_data_scope as role_data_scope
from backend.app.admin.model.m2m import role_menu as role_menu
from backend.app.admin.model.m2m import user_role as user_role
from backend.app.admin.model.menu import Menu as Menu
from backend.app.admin.model.opera_log import OperaLog as OperaLog
from backend.app.admin.model.role import Role as Role
from backend.app.admin.model.user import User as User
from backend.app.admin.model.user_password_history import UserPasswordHistory as UserPasswordHistory
+25
View File
@@ -0,0 +1,25 @@
import sqlalchemy as sa
from sqlalchemy.orm import Mapped, mapped_column
from backend.common.model import Base, id_key
class DataRule(Base):
"""数据规则表"""
__tablename__ = 'sys_data_rule'
__table_args__ = (
sa.UniqueConstraint('name', 'deleted', name='uk_sys_data_rule_name_deleted'),
{'comment': '数据规则表'},
)
id: Mapped[id_key] = mapped_column(init=False)
name: Mapped[str] = mapped_column(sa.String(512), comment='名称')
model: Mapped[str] = mapped_column(sa.String(64), comment='模型名称')
column: Mapped[str] = mapped_column(sa.String(32), comment='模型字段名')
operator: Mapped[int] = mapped_column(comment='运算符(0and、1or')
expression: Mapped[int] = mapped_column(
comment='表达式(0==、1!=、2>、3>=、4<、5<=、6in、7not_in',
)
value: Mapped[str] = mapped_column(sa.String(256), comment='规则值')
+19
View File
@@ -0,0 +1,19 @@
import sqlalchemy as sa
from sqlalchemy.orm import Mapped, mapped_column
from backend.common.model import Base, id_key
class DataScope(Base):
"""数据范围表"""
__tablename__ = 'sys_data_scope'
__table_args__ = (
sa.UniqueConstraint('name', 'deleted', name='uk_sys_data_scope_name_deleted'),
{'comment': '数据范围表'},
)
id: Mapped[id_key] = mapped_column(init=False)
name: Mapped[str] = mapped_column(sa.String(64), comment='名称')
status: Mapped[int] = mapped_column(default=1, comment='状态(0停用 1正常)')
+26
View File
@@ -0,0 +1,26 @@
import sqlalchemy as sa
from sqlalchemy.orm import Mapped, mapped_column
from backend.common.model import Base, id_key
class Dept(Base):
"""部门表"""
__tablename__ = 'sys_dept'
__table_args__ = (
sa.UniqueConstraint('name', 'deleted', name='uk_sys_dept_name_deleted'),
{'comment': '部门表'},
)
id: Mapped[id_key] = mapped_column(init=False)
name: Mapped[str] = mapped_column(sa.String(64), comment='部门名称')
sort: Mapped[int] = mapped_column(default=0, comment='排序')
leader: Mapped[str | None] = mapped_column(sa.String(32), default=None, comment='负责人')
phone: Mapped[str | None] = mapped_column(sa.String(11), default=None, comment='手机')
email: Mapped[str | None] = mapped_column(sa.String(64), default=None, comment='邮箱')
status: Mapped[int] = mapped_column(default=1, comment='部门状态(0停用 1正常)')
# 父级部门
parent_id: Mapped[int | None] = mapped_column(sa.BigInteger, default=None, index=True, comment='父部门ID')
+35
View File
@@ -0,0 +1,35 @@
from datetime import datetime
import sqlalchemy as sa
from sqlalchemy.orm import Mapped, mapped_column
from backend.common.model import DataClassBase, TimeZone, UniversalText, id_key
from backend.utils.timezone import timezone
class LoginLog(DataClassBase):
"""登录日志表"""
__tablename__ = 'sys_login_log'
id: Mapped[id_key] = mapped_column(init=False)
user_uuid: Mapped[str] = mapped_column(sa.String(64), comment='用户UUID')
username: Mapped[str] = mapped_column(sa.String(64), comment='用户名')
status: Mapped[int] = mapped_column(insert_default=0, comment='登录状态(0失败 1成功)')
ip: Mapped[str] = mapped_column(sa.String(64), comment='登录IP地址')
country: Mapped[str | None] = mapped_column(sa.String(64), comment='国家')
region: Mapped[str | None] = mapped_column(sa.String(64), comment='地区')
city: Mapped[str | None] = mapped_column(sa.String(64), comment='城市')
user_agent: Mapped[str | None] = mapped_column(sa.String(512), comment='请求头')
os: Mapped[str | None] = mapped_column(sa.String(64), comment='操作系统')
browser: Mapped[str | None] = mapped_column(sa.String(64), comment='浏览器')
device: Mapped[str | None] = mapped_column(sa.String(64), comment='设备')
msg: Mapped[str] = mapped_column(UniversalText, comment='提示消息')
login_time: Mapped[datetime] = mapped_column(TimeZone, comment='登录时间')
created_time: Mapped[datetime] = mapped_column(
TimeZone,
init=False,
default_factory=timezone.now,
comment='创建时间',
)
+39
View File
@@ -0,0 +1,39 @@
import sqlalchemy as sa
from backend.common.model import MappedBase
# 用户角色表
user_role = sa.Table(
'sys_user_role',
MappedBase.metadata,
sa.Column('id', sa.BigInteger, primary_key=True, unique=True, index=True, autoincrement=True, comment='主键ID'),
sa.Column('user_id', sa.BigInteger, primary_key=True, comment='用户ID'),
sa.Column('role_id', sa.BigInteger, primary_key=True, comment='角色ID'),
)
# 角色菜单表
role_menu = sa.Table(
'sys_role_menu',
MappedBase.metadata,
sa.Column('id', sa.BigInteger, primary_key=True, unique=True, index=True, autoincrement=True, comment='主键ID'),
sa.Column('role_id', sa.BigInteger, primary_key=True, comment='角色ID'),
sa.Column('menu_id', sa.BigInteger, primary_key=True, comment='菜单ID'),
)
# 角色数据范围表
role_data_scope = sa.Table(
'sys_role_data_scope',
MappedBase.metadata,
sa.Column('id', sa.BigInteger, primary_key=True, unique=True, index=True, autoincrement=True, comment='主键 ID'),
sa.Column('role_id', sa.BigInteger, primary_key=True, comment='角色 ID'),
sa.Column('data_scope_id', sa.BigInteger, primary_key=True, comment='数据范围 ID'),
)
# 数据范围规则表
data_scope_rule = sa.Table(
'sys_data_scope_rule',
MappedBase.metadata,
sa.Column('id', sa.BigInteger, primary_key=True, unique=True, index=True, autoincrement=True, comment='主键ID'),
sa.Column('data_scope_id', sa.BigInteger, primary_key=True, comment='数据范围 ID'),
sa.Column('data_rule_id', sa.BigInteger, primary_key=True, comment='数据规则 ID'),
)
+29
View File
@@ -0,0 +1,29 @@
import sqlalchemy as sa
from sqlalchemy.orm import Mapped, mapped_column
from backend.common.model import Base, UniversalText, id_key
class Menu(Base):
"""菜单表"""
__tablename__ = 'sys_menu'
id: Mapped[id_key] = mapped_column(init=False)
title: Mapped[str] = mapped_column(sa.String(64), comment='菜单标题')
name: Mapped[str] = mapped_column(sa.String(64), comment='菜单名称')
path: Mapped[str | None] = mapped_column(sa.String(200), comment='路由地址')
sort: Mapped[int] = mapped_column(default=0, comment='排序')
icon: Mapped[str | None] = mapped_column(sa.String(128), default=None, comment='菜单图标')
type: Mapped[int] = mapped_column(default=0, comment='菜单类型(0目录 1菜单 2按钮 3内嵌 4外链)')
component: Mapped[str | None] = mapped_column(sa.String(256), default=None, comment='组件路径')
perms: Mapped[str | None] = mapped_column(sa.String(128), default=None, comment='权限标识')
status: Mapped[int] = mapped_column(default=1, comment='菜单状态(0停用 1正常)')
display: Mapped[int] = mapped_column(default=1, comment='是否显示(0否 1是)')
cache: Mapped[int] = mapped_column(default=1, comment='是否缓存(0否 1是)')
link: Mapped[str | None] = mapped_column(UniversalText, default=None, comment='外链地址')
remark: Mapped[str | None] = mapped_column(UniversalText, default=None, comment='备注')
# 父级菜单
parent_id: Mapped[int | None] = mapped_column(sa.BigInteger, default=None, index=True, comment='父菜单ID')
+38
View File
@@ -0,0 +1,38 @@
from datetime import datetime
import sqlalchemy as sa
from sqlalchemy.orm import Mapped, mapped_column
from backend.common.model import DataClassBase, TimeZone, UniversalText, id_key
from backend.utils.timezone import timezone
class OperaLog(DataClassBase):
"""操作日志表"""
__tablename__ = 'sys_opera_log'
id: Mapped[id_key] = mapped_column(init=False)
trace_id: Mapped[str] = mapped_column(sa.String(32), comment='请求跟踪 ID')
username: Mapped[str | None] = mapped_column(sa.String(64), comment='用户名')
method: Mapped[str] = mapped_column(sa.String(32), comment='请求方法')
title: Mapped[str] = mapped_column(sa.String(256), comment='操作模块')
path: Mapped[str] = mapped_column(sa.String(512), comment='请求路径')
ip: Mapped[str] = mapped_column(sa.String(64), comment='IP 地址')
country: Mapped[str | None] = mapped_column(sa.String(64), comment='国家')
region: Mapped[str | None] = mapped_column(sa.String(64), comment='地区')
city: Mapped[str | None] = mapped_column(sa.String(64), comment='城市')
user_agent: Mapped[str | None] = mapped_column(sa.String(512), comment='用户代理')
os: Mapped[str | None] = mapped_column(sa.String(64), comment='操作系统')
browser: Mapped[str | None] = mapped_column(sa.String(64), comment='浏览器')
device: Mapped[str | None] = mapped_column(sa.String(64), comment='设备')
args: Mapped[str | None] = mapped_column(sa.JSON(), comment='请求参数')
status: Mapped[int] = mapped_column(comment='操作状态(0异常 1正常)')
code: Mapped[str] = mapped_column(sa.String(32), insert_default='200', comment='操作状态码')
msg: Mapped[str | None] = mapped_column(UniversalText, comment='提示消息')
cost_time: Mapped[float] = mapped_column(insert_default=0.0, comment='请求耗时(ms')
opera_time: Mapped[datetime] = mapped_column(TimeZone, comment='操作时间')
created_time: Mapped[datetime] = mapped_column(
TimeZone, init=False, default_factory=timezone.now, comment='创建时间'
)
+21
View File
@@ -0,0 +1,21 @@
import sqlalchemy as sa
from sqlalchemy.orm import Mapped, mapped_column
from backend.common.model import Base, UniversalText, id_key
class Role(Base):
"""角色表"""
__tablename__ = 'sys_role'
__table_args__ = (
sa.UniqueConstraint('name', 'deleted', name='uk_sys_role_name_deleted'),
{'comment': '角色表'},
)
id: Mapped[id_key] = mapped_column(init=False)
name: Mapped[str] = mapped_column(sa.String(32), comment='角色名称')
status: Mapped[int] = mapped_column(default=1, comment='角色状态(0停用 1正常)')
is_filter_scopes: Mapped[bool] = mapped_column(default=True, comment='过滤数据权限(0否 1是)')
remark: Mapped[str | None] = mapped_column(UniversalText, default=None, comment='备注')
+44
View File
@@ -0,0 +1,44 @@
from datetime import datetime
import sqlalchemy as sa
from sqlalchemy.orm import Mapped, mapped_column
from backend.common.model import Base, TimeZone, id_key
from backend.database.db import uuid4_str
from backend.utils.timezone import timezone
class User(Base):
"""用户表"""
__tablename__ = 'sys_user'
__table_args__ = (
sa.UniqueConstraint('username', 'deleted', name='uk_sys_user_username_deleted'),
sa.UniqueConstraint('email', 'deleted', name='uk_sys_user_email_deleted'),
{'comment': '用户表'},
)
id: Mapped[id_key] = mapped_column(init=False)
uuid: Mapped[str] = mapped_column(sa.String(64), init=False, default_factory=uuid4_str, unique=True)
username: Mapped[str] = mapped_column(sa.String(64), index=True, comment='用户名')
nickname: Mapped[str] = mapped_column(sa.String(64), comment='昵称')
password: Mapped[str | None] = mapped_column(sa.String(256), comment='密码')
salt: Mapped[bytes | None] = mapped_column(sa.LargeBinary(256), comment='加密盐')
email: Mapped[str | None] = mapped_column(sa.String(256), default=None, index=True, comment='邮箱')
phone: Mapped[str | None] = mapped_column(sa.String(11), default=None, comment='手机号')
avatar: Mapped[str | None] = mapped_column(sa.String(256), default=None, comment='头像')
status: Mapped[int] = mapped_column(default=1, index=True, comment='用户账号状态(0停用 1正常)')
is_superuser: Mapped[bool] = mapped_column(default=False, comment='超级权限(0否 1是)')
is_staff: Mapped[bool] = mapped_column(default=False, comment='后台管理登陆(0否 1是)')
is_multi_login: Mapped[bool] = mapped_column(default=False, comment='是否重复登陆(0否 1是)')
join_time: Mapped[datetime] = mapped_column(TimeZone, init=False, default_factory=timezone.now, comment='注册时间')
last_login_time: Mapped[datetime | None] = mapped_column(
TimeZone, init=False, onupdate=timezone.now, comment='上次登录时间'
)
last_password_changed_time: Mapped[datetime | None] = mapped_column(
TimeZone, init=False, default_factory=timezone.now, comment='上次密码变更时间'
)
# 逻辑外键
dept_id: Mapped[int | None] = mapped_column(sa.BigInteger, default=None, comment='部门关联ID')
@@ -0,0 +1,24 @@
from datetime import datetime
import sqlalchemy as sa
from sqlalchemy.orm import Mapped, mapped_column
from backend.common.model import DataClassBase, TimeZone, id_key
from backend.utils.timezone import timezone
class UserPasswordHistory(DataClassBase):
"""用户密码历史记录表"""
__tablename__ = 'sys_user_password_history'
id: Mapped[id_key] = mapped_column(init=False)
user_id: Mapped[int] = mapped_column(sa.BigInteger, index=True, comment='用户 ID')
password: Mapped[str] = mapped_column(sa.String(256), comment='历史密码')
created_time: Mapped[datetime] = mapped_column(
TimeZone,
init=False,
default_factory=timezone.now,
comment='创建时间',
)
+12
View File
@@ -0,0 +1,12 @@
from pydantic import Field
from backend.common.schema import SchemaBase
class GetCaptchaDetail(SchemaBase):
"""验证码详情"""
is_enabled: bool = Field(description='是否启用')
expire_seconds: int = Field(description='过期秒数')
uuid: str = Field(description='图片唯一标识')
image: str = Field(description='图片内容')
+55
View File
@@ -0,0 +1,55 @@
from datetime import datetime
from pydantic import ConfigDict, Field
from backend.common.enums import RoleDataRuleExpressionType, RoleDataRuleOperatorType
from backend.common.schema import SchemaBase
class DataRuleSchemaBase(SchemaBase):
"""数据规则基础模型"""
name: str = Field(description='规则名称')
model: str = Field(description='模型名称')
column: str = Field(description='字段名称')
operator: RoleDataRuleOperatorType = Field(description='操作符(AND/OR')
expression: RoleDataRuleExpressionType = Field(description='表达式类型')
value: str = Field(description='规则值')
class CreateDataRuleParam(DataRuleSchemaBase):
"""创建数据规则参数"""
class UpdateDataRuleParam(DataRuleSchemaBase):
"""更新数据规则参数"""
class DeleteDataRuleParam(SchemaBase):
"""删除数据规则参数"""
pks: list[int] = Field(description='规则 ID 列表')
class GetDataRuleDetail(DataRuleSchemaBase):
"""数据规则详情"""
model_config = ConfigDict(from_attributes=True, frozen=True)
id: int = Field(description='规则 ID')
created_time: datetime = Field(description='创建时间')
updated_time: datetime | None = Field(None, description='更新时间')
class GetDataRuleColumnDetail(SchemaBase):
"""数据规则可用模型字段详情"""
key: str = Field(description='字段名')
comment: str | None = Field(description='字段评论')
class GetDataRuleTemplateVariableDetail(SchemaBase):
"""数据规则可用模板变量详情"""
key: str = Field(description='变量标识')
comment: str = Field(description='变量描述')
+57
View File
@@ -0,0 +1,57 @@
from datetime import datetime
from pydantic import ConfigDict, Field
from backend.app.admin.schema.data_rule import GetDataRuleDetail
from backend.common.enums import StatusType
from backend.common.schema import SchemaBase
class DataScopeSchemaBase(SchemaBase):
"""数据范围基础模型"""
name: str = Field(description='名称')
status: StatusType = Field(description='状态')
class CreateDataScopeParam(DataScopeSchemaBase):
"""创建数据范围参数"""
class UpdateDataScopeParam(DataScopeSchemaBase):
"""更新数据范围参数"""
class CreateDataScopeRuleParam(SchemaBase):
"""创建数据范围规则参数"""
data_scope_id: int = Field(description='数据范围 ID')
data_rule_id: int = Field(description='数据规则 ID')
class UpdateDataScopeRuleParam(SchemaBase):
"""更新数据范围规则参数"""
rules: list[int] = Field(description='数据规则 ID 列表')
class DeleteDataScopeParam(SchemaBase):
"""删除数据范围参数"""
pks: list[int] = Field(description='数据范围 ID 列表')
class GetDataScopeDetail(DataScopeSchemaBase):
"""数据范围详情"""
model_config = ConfigDict(from_attributes=True)
id: int = Field(description='数据范围 ID')
created_time: datetime = Field(description='创建时间')
updated_time: datetime | None = Field(None, description='更新时间')
class GetDataScopeWithRelationDetail(GetDataScopeDetail):
"""数据范围关联详情"""
rules: list[GetDataRuleDetail | None] = Field([], description='数据规则列表')
+44
View File
@@ -0,0 +1,44 @@
from datetime import datetime
from pydantic import ConfigDict, Field
from backend.common.enums import StatusType
from backend.common.schema import CustomEmailStr, CustomPhoneNumber, SchemaBase
class DeptSchemaBase(SchemaBase):
"""部门基础模型"""
name: str = Field(description='部门名称')
parent_id: int | None = Field(None, description='部门父级 ID')
sort: int = Field(0, ge=0, description='排序')
leader: str | None = Field(None, description='负责人')
phone: CustomPhoneNumber | None = Field(None, description='联系电话')
email: CustomEmailStr | None = Field(None, description='邮箱')
status: StatusType = Field(description='状态')
class CreateDeptParam(DeptSchemaBase):
"""创建部门参数"""
class UpdateDeptParam(DeptSchemaBase):
"""更新部门参数"""
class GetDeptDetail(DeptSchemaBase):
"""部门详情"""
model_config = ConfigDict(from_attributes=True)
id: int = Field(description='部门 ID')
deleted: int = Field(description='是否已删除(0:否;id:是)')
created_time: datetime = Field(description='创建时间')
updated_time: datetime | None = Field(None, description='更新时间')
deleted_time: datetime | None = Field(None, description='删除时间')
class GetDeptTree(GetDeptDetail):
"""获取部门树"""
children: list['GetDeptTree'] | None = Field(None, description='子菜单')
+46
View File
@@ -0,0 +1,46 @@
from datetime import datetime
from pydantic import ConfigDict, Field
from backend.common.schema import SchemaBase
class LoginLogSchemaBase(SchemaBase):
"""登录日志基础模型"""
user_uuid: str = Field(description='用户 UUID')
username: str = Field(description='用户名')
status: int = Field(description='登录状态')
ip: str = Field(description='IP 地址')
country: str | None = Field(None, description='国家')
region: str | None = Field(None, description='地区')
city: str | None = Field(None, description='城市')
user_agent: str | None = Field(description='用户代理')
browser: str | None = Field(None, description='浏览器')
os: str | None = Field(None, description='操作系统')
device: str | None = Field(None, description='设备')
msg: str = Field(description='消息')
login_time: datetime = Field(description='登录时间')
class CreateLoginLogParam(LoginLogSchemaBase):
"""创建登录日志参数"""
class UpdateLoginLogParam(LoginLogSchemaBase):
"""更新登录日志参数"""
class DeleteLoginLogParam(SchemaBase):
"""删除登录日志参数"""
pks: list[int] = Field(description='登录日志 ID 列表')
class GetLoginLogDetail(LoginLogSchemaBase):
"""登录日志详情"""
model_config = ConfigDict(from_attributes=True)
id: int = Field(description='日志 ID')
created_time: datetime = Field(description='创建时间')
+49
View File
@@ -0,0 +1,49 @@
from datetime import datetime
from pydantic import ConfigDict, Field
from backend.common.enums import MenuType, StatusType
from backend.common.schema import SchemaBase
class MenuSchemaBase(SchemaBase):
"""菜单基础模型"""
title: str = Field(description='菜单标题')
name: str = Field(description='菜单名称')
path: str | None = Field(None, description='路由地址')
parent_id: int | None = Field(None, description='菜单父级 ID')
sort: int = Field(0, ge=0, description='排序')
icon: str | None = Field(None, description='图标')
type: MenuType = Field(description='菜单类型(0目录 1菜单 2按钮 3内嵌 4外链)')
component: str | None = Field(None, description='组件路径')
perms: str | None = Field(None, description='权限标识')
status: StatusType = Field(description='状态')
display: StatusType = Field(description='是否显示')
cache: StatusType = Field(description='是否缓存')
link: str | None = Field(None, description='外链地址')
remark: str | None = Field(None, description='备注')
class CreateMenuParam(MenuSchemaBase):
"""创建菜单参数"""
class UpdateMenuParam(MenuSchemaBase):
"""更新菜单参数"""
class GetMenuDetail(MenuSchemaBase):
"""菜单详情"""
model_config = ConfigDict(from_attributes=True)
id: int = Field(description='菜单 ID')
created_time: datetime = Field(description='创建时间')
updated_time: datetime | None = Field(None, description='更新时间')
class GetMenuTree(GetMenuDetail):
"""获取菜单树"""
children: list['GetMenuTree'] | None = Field(None, description='子菜单')
+102
View File
@@ -0,0 +1,102 @@
from pydantic import Field
from backend.common.schema import SchemaBase
class CpuInfo(SchemaBase):
"""CPU 信息"""
physical_num: int = Field(description='物理核心数')
logical_num: int = Field(description='逻辑核心数')
max_freq: float = Field(description='最大频率(MHz')
min_freq: float = Field(description='最小频率(MHz')
current_freq: float = Field(description='当前频率(MHz')
usage: float = Field(description='使用率(%')
class MemInfo(SchemaBase):
"""内存信息"""
total: float = Field(description='总容量(GB')
used: float = Field(description='已使用(GB')
free: float = Field(description='可用(GB')
usage: float = Field(description='使用率(%')
class SysInfo(SchemaBase):
"""系统信息"""
name: str = Field(description='主机名')
os: str = Field(description='操作系统')
ip: str = Field(description='IP 地址')
arch: str = Field(description='系统架构')
class DiskInfo(SchemaBase):
"""磁盘信息"""
dir: str = Field(description='挂载点')
device: str = Field(description='设备名称')
type: str = Field(description='文件系统类型')
total: str = Field(description='总容量')
used: str = Field(description='已使用')
free: str = Field(description='可用')
usage: str = Field(description='使用率(%')
class ServiceInfo(SchemaBase):
"""服务进程信息"""
name: str = Field(description='服务名称')
version: str = Field(description='版本')
home: str = Field(description='安装路径')
startup: str = Field(description='启动时间')
elapsed: str = Field(description='运行时长')
cpu_usage: str = Field(description='CPU 使用率')
mem_vms: str = Field(description='虚拟内存')
mem_rss: str = Field(description='物理内存')
mem_free: str = Field(description='可用内存')
class ServerMonitorInfo(SchemaBase):
"""服务器监控信息"""
cpu: CpuInfo = Field(description='CPU 信息')
mem: MemInfo = Field(description='内存信息')
sys: SysInfo = Field(description='系统信息')
disk: list[DiskInfo] = Field(description='磁盘信息')
service: ServiceInfo = Field(description='服务信息')
class RedisServerInfo(SchemaBase):
"""Redis 服务器信息"""
redis_version: str = Field(description='版本号')
redis_mode: str = Field(description='运行模式')
role: str = Field(description='节点角色')
tcp_port: str = Field(description='监听端口')
uptime: str = Field(description='运行时长')
connected_clients: str = Field(description='已连接客户端数')
blocked_clients: str = Field(description='阻塞客户端数')
used_memory_human: str = Field(description='已使用内存')
used_memory_rss_human: str = Field(description='RSS 内存')
maxmemory_human: str = Field(description='最大内存限制')
mem_fragmentation_ratio: str = Field(description='内存碎片率')
instantaneous_ops_per_sec: str = Field(description='每秒操作数')
total_commands_processed: str = Field(description='命令处理总数')
rejected_connections: str = Field(description='拒绝连接数')
keys_num: str = Field(description='键总数')
class RedisCommandStat(SchemaBase):
"""Redis 命令统计"""
name: str = Field(description='命令名称')
value: str = Field(description='调用次数')
class RedisMonitorInfo(SchemaBase):
"""Redis 监控信息"""
info: RedisServerInfo = Field(description='服务器信息')
stats: list[RedisCommandStat] = Field(description='命令统计')
+54
View File
@@ -0,0 +1,54 @@
from datetime import datetime
from typing import Any
from pydantic import ConfigDict, Field
from backend.common.enums import StatusType
from backend.common.schema import SchemaBase
class OperaLogSchemaBase(SchemaBase):
"""操作日志基础模型"""
trace_id: str = Field(description='追踪 ID')
username: str | None = Field(None, description='用户名')
method: str = Field(description='请求方法')
title: str = Field(description='操作标题')
path: str = Field(description='请求路径')
ip: str = Field(description='IP 地址')
country: str | None = Field(None, description='国家')
region: str | None = Field(None, description='地区')
city: str | None = Field(None, description='城市')
user_agent: str | None = Field(description='用户代理')
os: str | None = Field(None, description='操作系统')
browser: str | None = Field(None, description='浏览器')
device: str | None = Field(None, description='设备')
args: dict[str, Any] | None = Field(None, description='请求参数')
status: StatusType = Field(description='状态')
code: str = Field(description='状态码')
msg: str | None = Field(None, description='消息')
cost_time: float = Field(description='耗时')
opera_time: datetime = Field(description='操作时间')
class CreateOperaLogParam(OperaLogSchemaBase):
"""创建操作日志参数"""
class UpdateOperaLogParam(OperaLogSchemaBase):
"""更新操作日志参数"""
class DeleteOperaLogParam(SchemaBase):
"""删除操作日志参数"""
pks: list[int] = Field(description='操作日志 ID 列表')
class GetOperaLogDetail(OperaLogSchemaBase):
"""操作日志详情"""
model_config = ConfigDict(from_attributes=True)
id: int = Field(description='日志 ID')
created_time: datetime = Field(description='创建时间')
+74
View File
@@ -0,0 +1,74 @@
from datetime import datetime
from pydantic import ConfigDict, Field
from backend.app.admin.schema.data_scope import GetDataScopeWithRelationDetail
from backend.app.admin.schema.menu import GetMenuDetail
from backend.common.enums import StatusType
from backend.common.schema import SchemaBase
class RoleSchemaBase(SchemaBase):
"""角色基础模型"""
name: str = Field(description='角色名称')
status: StatusType = Field(description='状态')
is_filter_scopes: bool = Field(True, description='过滤数据权限')
remark: str | None = Field(None, description='备注')
class CreateRoleParam(RoleSchemaBase):
"""创建角色参数"""
class UpdateRoleParam(RoleSchemaBase):
"""更新角色参数"""
class DeleteRoleParam(SchemaBase):
"""删除角色参数"""
pks: list[int] = Field(description='角色 ID 列表')
class CreateRoleMenuParam(SchemaBase):
"""创建角色菜单参数"""
role_id: int = Field(description='角色 ID')
menu_id: int = Field(description='菜单 ID')
class UpdateRoleMenuParam(SchemaBase):
"""更新角色菜单参数"""
menus: list[int] = Field(description='菜单 ID 列表')
class CreateRoleScopeParam(SchemaBase):
"""创建角色数据范围参数"""
role_id: int = Field(description='角色 ID')
data_scope_id: int = Field(description='数据范围 ID')
class UpdateRoleScopeParam(SchemaBase):
"""更新角色数据范围参数"""
scopes: list[int] = Field(description='数据范围 ID 列表')
class GetRoleDetail(RoleSchemaBase):
"""角色详情"""
model_config = ConfigDict(from_attributes=True)
id: int = Field(description='角色 ID')
created_time: datetime = Field(description='创建时间')
updated_time: datetime | None = Field(None, description='更新时间')
class GetRoleWithRelationDetail(GetRoleDetail):
"""角色关联详情"""
menus: list[GetMenuDetail | None] = Field([], description='菜单详情列表')
scopes: list[GetDataScopeWithRelationDetail | None] = Field([], description='数据范围列表')
+50
View File
@@ -0,0 +1,50 @@
from datetime import datetime
from pydantic import Field
from backend.app.admin.schema.user import GetUserInfoDetail
from backend.common.enums import StatusType
from backend.common.schema import SchemaBase
class GetSwaggerToken(SchemaBase):
"""Swagger 认证令牌"""
access_token: str = Field(description='访问令牌')
token_type: str = Field('Bearer', description='令牌类型')
user: GetUserInfoDetail = Field(description='用户信息')
class AccessTokenBase(SchemaBase):
"""访问令牌基础模型"""
access_token: str = Field(description='访问令牌')
access_token_expire_time: datetime = Field(description='令牌过期时间')
session_uuid: str = Field(description='会话 UUID')
class GetNewToken(AccessTokenBase):
"""获取新令牌"""
class GetLoginToken(AccessTokenBase):
"""获取登录令牌"""
password_expire_days_remaining: int | None = Field(None, description='密码过期剩余天数')
user: GetUserInfoDetail = Field(description='用户信息')
class GetTokenDetail(SchemaBase):
"""令牌详情"""
id: int = Field(description='用户 ID')
session_uuid: str = Field(description='会话 UUID')
username: str = Field(description='用户名')
nickname: str = Field(description='昵称')
ip: str = Field(description='IP 地址')
os: str = Field(description='操作系统')
browser: str = Field(description='浏览器')
device: str = Field(description='设备')
status: StatusType = Field(description='状态')
last_login_time: str = Field(description='最后登录时间')
expire_time: datetime = Field(description='过期时间')
+121
View File
@@ -0,0 +1,121 @@
from datetime import datetime
from typing import Annotated, Any
from pydantic import ConfigDict, Field, HttpUrl, PlainSerializer, model_validator
from typing_extensions import Self
from backend.app.admin.schema.dept import GetDeptDetail
from backend.app.admin.schema.role import GetRoleWithRelationDetail
from backend.common.enums import StatusType
from backend.common.schema import CustomEmailStr, CustomPhoneNumber, SchemaBase, ser_string
class AuthSchemaBase(SchemaBase):
"""用户认证基础模型"""
username: str = Field(description='用户名')
password: str = Field(description='密码')
class AuthLoginParam(AuthSchemaBase):
"""用户登录参数"""
uuid: str | None = Field(None, description='验证码 UUID')
captcha: str | None = Field(None, description='验证码')
class AddUserParam(AuthSchemaBase):
"""添加用户参数"""
nickname: str | None = Field(None, description='昵称')
email: CustomEmailStr | None = Field(None, description='邮箱')
phone: CustomPhoneNumber | None = Field(None, description='手机号码')
dept_id: int = Field(description='部门 ID')
roles: list[int] = Field(description='角色 ID 列表')
class AddUserRoleParam(SchemaBase):
"""添加用户角色"""
user_id: int = Field(description='用户 ID')
role_id: int = Field(description='角色 ID')
class AddOAuth2UserParam(AuthSchemaBase):
"""添加 OAuth2 用户参数"""
password: str | None = Field(None, description='密码')
nickname: str | None = Field(None, description='昵称')
email: CustomEmailStr | None = Field(None, description='邮箱')
avatar: Annotated[HttpUrl, PlainSerializer(ser_string)] | None = Field(None, description='头像地址')
class ResetPasswordParam(SchemaBase):
"""重置密码参数"""
old_password: str = Field(description='旧密码')
new_password: str = Field(description='新密码')
confirm_password: str = Field(description='确认密码')
class UserInfoSchemaBase(SchemaBase):
"""用户信息基础模型"""
dept_id: int | None = Field(None, description='部门 ID')
username: str = Field(description='用户名')
nickname: str = Field(description='昵称')
avatar: Annotated[HttpUrl, PlainSerializer(ser_string)] | None = Field(None, description='头像地址')
email: CustomEmailStr | None = Field(None, description='邮箱')
phone: CustomPhoneNumber | None = Field(None, description='手机号')
class UpdateUserParam(UserInfoSchemaBase):
"""更新用户参数"""
roles: list[int] = Field(description='角色 ID 列表')
class GetUserInfoDetail(UserInfoSchemaBase):
"""用户信息详情"""
model_config = ConfigDict(from_attributes=True)
dept_id: int | None = Field(None, description='部门 ID')
id: int = Field(description='用户 ID')
uuid: str = Field(description='用户 UUID')
status: StatusType = Field(description='状态')
is_superuser: bool = Field(description='是否超级管理员')
is_staff: bool = Field(description='是否管理员')
is_multi_login: bool = Field(description='是否允许多端登录')
join_time: datetime = Field(description='加入时间')
last_login_time: datetime | None = Field(None, description='最后登录时间')
class GetUserInfoWithRelationDetail(GetUserInfoDetail):
"""用户信息关联详情"""
model_config = ConfigDict(from_attributes=True)
dept: GetDeptDetail | None = Field(None, description='部门信息')
roles: list[GetRoleWithRelationDetail] = Field(description='角色列表')
class GetCurrentUserInfoWithRelationDetail(GetUserInfoWithRelationDetail):
"""当前用户信息关联详情"""
model_config = ConfigDict(from_attributes=True)
dept: str | None = Field(None, description='部门名称')
roles: list[str] = Field(description='角色名称列表')
@model_validator(mode='before')
@classmethod
def handel(cls, data: Any) -> Self:
"""处理部门和角色数据"""
dept = data['dept']
if dept:
data['dept'] = dept['name']
roles = data['roles']
if roles:
data['roles'] = [role['name'] for role in roles]
return data
@@ -0,0 +1,14 @@
from pydantic import Field
from backend.common.schema import SchemaBase
class UserPasswordHistorySchemaBase(SchemaBase):
"""用户历史密码记录基础模型"""
user_id: int = Field(description='用户 ID')
password: str = Field(description='历史密码')
class CreateUserPasswordHistoryParam(UserPasswordHistorySchemaBase):
"""创建用户历史密码记录"""
+272
View File
@@ -0,0 +1,272 @@
from fastapi import Request, Response
from fastapi.security import HTTPBasicCredentials
from sqlalchemy.ext.asyncio import AsyncSession
from starlette.background import BackgroundTask, BackgroundTasks
from backend.app.admin.crud.crud_menu import menu_dao
from backend.app.admin.crud.crud_user import user_dao
from backend.app.admin.model import User
from backend.app.admin.schema.token import GetLoginToken, GetNewToken
from backend.app.admin.schema.user import AuthLoginParam
from backend.app.admin.service.login_log_service import login_log_service
from backend.app.admin.service.user_password_history_service import password_security_service
from backend.app.admin.utils.password_security import password_verify
from backend.common.context import ctx
from backend.common.enums import LoginLogStatusType, StatusType
from backend.common.exception import errors
from backend.common.i18n import t
from backend.common.log import log
from backend.common.response.response_code import CustomErrorCode
from backend.common.security.jwt import jwt_decode
from backend.common.security.token import (
create_access_token,
create_new_token,
create_refresh_token,
get_token,
get_user_sessions,
revoke_token,
)
from backend.core.conf import settings
from backend.database.db import uuid4_str
from backend.database.redis import redis_client
from backend.utils.dynamic_config import load_login_config
from backend.utils.timezone import timezone
class AuthService:
"""认证服务类"""
@staticmethod
async def user_verify(*, db: AsyncSession, username: str, password: str) -> tuple[User, int | None]:
"""
验证用户名和密码
:param db: 数据库会话
:param username: 用户名
:param password: 密码
:return:
"""
user = await user_dao.get_by_username(db, username)
if not user:
raise errors.NotFoundError(msg='用户名或密码有误')
await password_security_service.check_status(user.id, user.status)
if user.password is None or not password_verify(password, user.password):
await password_security_service.handle_login_failure(db, user.id)
raise errors.AuthorizationError(msg='用户名或密码有误')
days_remaining = await password_security_service.check_password_expiry_status(
db, user.last_password_changed_time
)
await password_security_service.handle_login_success(user.id)
return user, days_remaining
async def swagger_login(self, *, db: AsyncSession, obj: HTTPBasicCredentials) -> tuple[str, User]:
"""
Swagger 文档登录
:param db: 数据库会话
:param obj: 登录凭证
:return:
"""
user, _ = await self.user_verify(db=db, username=obj.username, password=obj.password)
await user_dao.update_login_time(db, obj.username)
access_token_data = await create_access_token(
user.id,
multi_login=user.is_multi_login,
swagger=True,
)
return access_token_data.access_token, user
async def login(
self,
*,
db: AsyncSession,
response: Response,
obj: AuthLoginParam,
background_tasks: BackgroundTasks,
) -> GetLoginToken:
"""
用户登录
:param db: 数据库会话
:param response: 响应对象
:param obj: 登录参数
:param background_tasks: 后台任务
:return:
"""
user = None
try:
await load_login_config(db)
if settings.LOGIN_CAPTCHA_ENABLED:
if not obj.uuid or not obj.captcha:
raise errors.RequestError(msg=t('error.captcha.invalid'))
captcha_key = f'{settings.LOGIN_CAPTCHA_REDIS_PREFIX}:{obj.uuid}'
captcha_code = await redis_client.get(captcha_key)
if not captcha_code:
raise errors.RequestError(msg=t('error.captcha.expired'))
if captcha_code.lower() != obj.captcha.lower():
raise errors.CustomError(error=CustomErrorCode.CAPTCHA_ERROR)
await redis_client.delete(captcha_key)
user, days_remaining = await self.user_verify(db=db, username=obj.username, password=obj.password)
await user_dao.update_login_time(db, obj.username)
await db.refresh(user)
access_token_data = await create_access_token(
user.id,
multi_login=user.is_multi_login,
# extra info
username=user.username,
nickname=user.nickname,
last_login_time=timezone.to_str(user.last_login_time),
ip=ctx.ip,
os=ctx.os,
browser=ctx.browser,
device=ctx.device,
)
refresh_token_data = await create_refresh_token(
access_token_data.session_uuid,
user.id,
multi_login=user.is_multi_login,
)
response.set_cookie(
key=settings.COOKIE_REFRESH_TOKEN_KEY,
value=refresh_token_data.refresh_token,
max_age=settings.COOKIE_REFRESH_TOKEN_EXPIRE_SECONDS,
expires=timezone.to_utc(refresh_token_data.refresh_token_expire_time),
httponly=True,
)
except errors.NotFoundError as e:
log.error('登陆错误: 用户名不存在')
raise errors.NotFoundError(msg=e.msg)
except (errors.RequestError, errors.CustomError) as e:
if not user:
log.error(f'登陆错误: {e.msg}')
task = BackgroundTask(
login_log_service.create,
user_uuid=user.uuid if user else uuid4_str(),
username=obj.username,
login_time=timezone.now(),
status=LoginLogStatusType.fail.value,
msg=e.msg,
)
raise errors.RequestError(code=e.code, msg=e.msg, background=task)
except Exception as e:
log.error(f'登陆错误: {e}')
raise
else:
background_tasks.add_task(
login_log_service.create,
user_uuid=user.uuid,
username=obj.username,
login_time=timezone.now(),
status=LoginLogStatusType.success.value,
msg=t('success.login.success'),
)
data = GetLoginToken(
access_token=access_token_data.access_token,
access_token_expire_time=access_token_data.access_token_expire_time,
session_uuid=access_token_data.session_uuid,
password_expire_days_remaining=days_remaining,
user=user, # type: ignore
)
return data
@staticmethod
async def get_codes(*, db: AsyncSession, request: Request) -> list[str]:
"""
获取用户权限码
:param db: 数据库会话
:param request: FastAPI 请求对象
:return:
"""
codes = set()
if request.user.is_superuser:
menus = await menu_dao.get_all(db, None, None)
for menu in menus:
if menu.status == StatusType.enable and menu.perms:
codes.update(menu.perms.split(','))
else:
roles = [role for role in request.user.roles if role.status == StatusType.enable]
if roles:
for role in roles:
for menu in role.menus:
if menu.status == StatusType.enable and menu.perms:
codes.update(menu.perms.split(','))
return list(codes)
@staticmethod
async def refresh_token(*, db: AsyncSession, request: Request, response: Response) -> GetNewToken:
"""
刷新令牌
:param db: 数据库会话
:param request: FastAPI 请求对象
:param response: FastAPI 响应对象
:return:
"""
refresh_token = request.cookies.get(settings.COOKIE_REFRESH_TOKEN_KEY)
if not refresh_token:
raise errors.RequestError(msg='Refresh Token 已过期,请重新登录')
token_payload = jwt_decode(refresh_token)
user = await user_dao.get(db, token_payload.user_id)
if not user:
raise errors.NotFoundError(msg='用户不存在')
if not user.status:
raise errors.AuthorizationError(msg='用户已被锁定, 请联系统管理员')
if not user.is_multi_login and await get_user_sessions(user.id) - {token_payload.session_uuid}:
raise errors.ForbiddenError(msg='此用户已在异地登录,请重新登录并及时修改密码')
new_token = await create_new_token(
refresh_token,
token_payload.session_uuid,
user.id,
multi_login=user.is_multi_login,
# extra info
username=user.username,
nickname=user.nickname,
last_login_time=timezone.to_str(user.last_login_time),
ip=ctx.ip,
os=ctx.os,
browser=ctx.browser,
device_type=ctx.device,
)
response.set_cookie(
key=settings.COOKIE_REFRESH_TOKEN_KEY,
value=new_token.new_refresh_token,
max_age=settings.COOKIE_REFRESH_TOKEN_EXPIRE_SECONDS,
expires=timezone.to_utc(new_token.new_refresh_token_expire_time),
httponly=True,
)
data = GetNewToken(
access_token=new_token.new_access_token,
access_token_expire_time=new_token.new_access_token_expire_time,
session_uuid=new_token.session_uuid,
)
return data
@staticmethod
async def logout(*, request: Request, response: Response) -> None:
"""
用户登出
:param request: FastAPI 请求对象
:param response: FastAPI 响应对象
:return:
"""
try:
token = get_token(request)
token_payload = jwt_decode(token)
except errors.TokenError:
return
finally:
response.delete_cookie(settings.COOKIE_REFRESH_TOKEN_KEY)
await revoke_token(token_payload.user_id, token_payload.session_uuid)
auth_service: AuthService = AuthService()
@@ -0,0 +1,156 @@
from collections.abc import Sequence
from typing import Any
from sqlalchemy import Table
from sqlalchemy.ext.asyncio import AsyncSession
from backend.app.admin.crud.crud_data_rule import data_rule_dao
from backend.app.admin.model import DataRule
from backend.app.admin.schema.data_rule import (
CreateDataRuleParam,
DeleteDataRuleParam,
GetDataRuleColumnDetail,
GetDataRuleTemplateVariableDetail,
UpdateDataRuleParam,
)
from backend.app.admin.utils.cache import user_cache_manager
from backend.common.exception import errors
from backend.common.pagination import paging_data
from backend.common.security.permission import get_data_permission_models
from backend.core.conf import settings
class DataRuleService:
"""数据规则服务类"""
@staticmethod
async def get(*, db: AsyncSession, pk: int) -> DataRule:
"""
获取数据规则详情
:param db: 数据库会话
:param pk: 规则 ID
:return:
"""
data_rule = await data_rule_dao.get(db, pk)
if not data_rule:
raise errors.NotFoundError(msg='数据规则不存在')
return data_rule
@staticmethod
async def get_models() -> list[str]:
"""获取所有数据规则可用模型"""
model_template_variables = [var['key'] for var in settings.DATA_PERMISSION_MODEL_TEMPLATE_VARIABLES]
models = [
m for m in list(get_data_permission_models().keys()) if m not in settings.DATA_PERMISSION_MODEL_EXCLUDE
]
return model_template_variables + models
@staticmethod
async def get_value_template_variables() -> list[GetDataRuleTemplateVariableDetail]:
"""获取所有数据规则值可用模板变量"""
return [GetDataRuleTemplateVariableDetail(**var) for var in settings.DATA_PERMISSION_TEMPLATE_VARIABLES]
@staticmethod
async def get_columns(model: str) -> list[GetDataRuleColumnDetail]:
"""
获取数据规则可用模型的字段列表
:param model: 模型名称
:return:
"""
column_template_variables = [
GetDataRuleColumnDetail(key=var['key'], comment=var['comment'])
for var in settings.DATA_PERMISSION_COLUMN_TEMPLATE_VARIABLES
]
model_template_variable_keys = {var['key'] for var in settings.DATA_PERMISSION_MODEL_TEMPLATE_VARIABLES}
if model in model_template_variable_keys:
return column_template_variables
available_models = get_data_permission_models()
if model not in available_models:
raise errors.NotFoundError(msg='数据规则可用模型不存在')
model_ins = available_models[model]
table = model_ins if isinstance(model_ins, Table) else model_ins.__table__
model_columns = [
GetDataRuleColumnDetail(key=column.key, comment=column.comment)
for column in table.columns
if column.key not in settings.DATA_PERMISSION_COLUMN_EXCLUDE
]
return model_columns + column_template_variables
@staticmethod
async def get_list(*, db: AsyncSession, name: str | None) -> dict[str, Any]:
"""
获取数据规则列表
:param db: 数据库会话
:param name: 规则名称
:return:
"""
data_rule_select = await data_rule_dao.get_select(name=name)
return await paging_data(db, data_rule_select)
@staticmethod
async def get_all(*, db: AsyncSession) -> Sequence[DataRule]:
"""
获取所有数据规则
:param db: 数据库会话
:return:
"""
data_rules = await data_rule_dao.get_all(db)
return data_rules
@staticmethod
async def create(*, db: AsyncSession, obj: CreateDataRuleParam) -> None:
"""
创建数据规则
:param db: 数据库会话
:param obj: 规则创建参数
:return:
"""
data_rule = await data_rule_dao.get_by_name(db, obj.name)
if data_rule:
raise errors.ConflictError(msg='数据规则已存在')
await data_rule_dao.create(db, obj)
@staticmethod
async def update(*, db: AsyncSession, pk: int, obj: UpdateDataRuleParam) -> int:
"""
更新数据规则
:param db: 数据库会话
:param pk: 规则 ID
:param obj: 规则更新参数
:return:
"""
data_rule = await data_rule_dao.get(db, pk)
if not data_rule:
raise errors.NotFoundError(msg='数据规则不存在')
if data_rule.name != obj.name and await data_rule_dao.get_by_name(db, obj.name):
raise errors.ConflictError(msg='数据规则已存在')
count = await data_rule_dao.update(db, pk, obj)
await user_cache_manager.clear_by_data_rule_id(db, [pk])
return count
@staticmethod
async def delete(*, db: AsyncSession, obj: DeleteDataRuleParam) -> int:
"""
批量删除数据规则
:param db: 数据库会话
:param obj: 规则 ID 列表
:return:
"""
count = await data_rule_dao.delete(db, obj.pks)
await user_cache_manager.clear_by_data_rule_id(db, obj.pks)
return count
data_rule_service: DataRuleService = DataRuleService()
@@ -0,0 +1,146 @@
from collections.abc import Sequence
from typing import Any
from sqlalchemy.ext.asyncio import AsyncSession
from backend.app.admin.crud.crud_data_rule import data_rule_dao
from backend.app.admin.crud.crud_data_scope import data_scope_dao
from backend.app.admin.model import DataScope
from backend.app.admin.schema.data_scope import (
CreateDataScopeParam,
DeleteDataScopeParam,
UpdateDataScopeParam,
UpdateDataScopeRuleParam,
)
from backend.app.admin.utils.cache import user_cache_manager
from backend.common.exception import errors
from backend.common.pagination import paging_data
class DataScopeService:
"""数据范围服务类"""
@staticmethod
async def get(*, db: AsyncSession, pk: int) -> DataScope:
"""
获取数据范围详情
:param db: 数据库会话
:param pk: 范围 ID
:return:
"""
data_scope = await data_scope_dao.get(db, pk)
if not data_scope:
raise errors.NotFoundError(msg='数据范围不存在')
return data_scope
@staticmethod
async def get_all(*, db: AsyncSession) -> Sequence[DataScope]:
"""
获取所有数据范围
:param db: 数据库会话
:return:
"""
data_scopes = await data_scope_dao.get_all(db)
return data_scopes
@staticmethod
async def get_rules(*, db: AsyncSession, pk: int) -> DataScope:
"""
获取数据范围规则
:param db: 数据库会话
:param pk: 范围 ID
:return:
"""
data_scope = await data_scope_dao.get_join(db, pk)
if not data_scope:
raise errors.NotFoundError(msg='数据范围不存在')
return data_scope
@staticmethod
async def get_list(*, db: AsyncSession, name: str | None, status: int | None) -> dict[str, Any]:
"""
获取数据范围列表
:param db: 数据库会话
:param name: 范围名称
:param status: 范围状态
:return:
"""
data_scope_select = await data_scope_dao.get_select(name, status)
return await paging_data(db, data_scope_select)
@staticmethod
async def create(*, db: AsyncSession, obj: CreateDataScopeParam) -> None:
"""
创建数据范围
:param db: 数据库会话
:param obj: 数据范围参数
:return:
"""
data_scope = await data_scope_dao.get_by_name(db, obj.name)
if data_scope:
raise errors.ConflictError(msg='数据范围已存在')
await data_scope_dao.create(db, obj)
@staticmethod
async def update(*, db: AsyncSession, pk: int, obj: UpdateDataScopeParam) -> int:
"""
更新数据范围
:param db: 数据库会话
:param pk: 范围 ID
:param obj: 数据范围更新参数
:return:
"""
data_scope = await data_scope_dao.get(db, pk)
if not data_scope:
raise errors.NotFoundError(msg='数据范围不存在')
if data_scope.name != obj.name and await data_scope_dao.get_by_name(db, obj.name):
raise errors.ConflictError(msg='数据范围已存在')
count = await data_scope_dao.update(db, pk, obj)
await user_cache_manager.clear_by_data_scope_id(db, [pk])
return count
@staticmethod
async def update_data_scope_rule(*, db: AsyncSession, pk: int, rule_ids: UpdateDataScopeRuleParam) -> int:
"""
更新数据范围规则
:param db: 数据库会话
:param pk: 范围 ID
:param rule_ids: 规则 ID 列表
:return:
"""
data_scope = await data_scope_dao.get(db, pk)
if not data_scope:
raise errors.NotFoundError(msg='数据范围不存在')
if rule_ids.rules:
rules = await data_rule_dao.get_all_by_ids(db, list(set(rule_ids.rules)))
if {rule.id for rule in rules} != set(rule_ids.rules):
raise errors.NotFoundError(msg='数据规则不存在')
count = await data_scope_dao.update_rules(db, pk, rule_ids)
await user_cache_manager.clear_by_data_scope_id(db, [pk])
return count
@staticmethod
async def delete(*, db: AsyncSession, obj: DeleteDataScopeParam) -> int:
"""
批量删除数据范围
:param db: 数据库会话
:param obj: 范围 ID 列表
:return:
"""
count = await data_scope_dao.delete(db, obj.pks)
await user_cache_manager.clear_by_data_scope_id(db, obj.pks)
return count
data_scope_service: DataScopeService = DataScopeService()
+119
View File
@@ -0,0 +1,119 @@
from typing import Any
from sqlalchemy import ColumnElement
from sqlalchemy.ext.asyncio import AsyncSession
from backend.app.admin.crud.crud_dept import dept_dao
from backend.app.admin.model import Dept
from backend.app.admin.schema.dept import CreateDeptParam, UpdateDeptParam
from backend.common.exception import errors
from backend.utils.build_tree import get_tree_data
class DeptService:
"""部门服务类"""
@staticmethod
async def get(*, db: AsyncSession, pk: int) -> Dept:
"""
获取部门详情
:param db: 数据库会话
:param pk: 部门 ID
:return:
"""
dept = await dept_dao.get(db, pk)
if not dept:
raise errors.NotFoundError(msg='部门不存在')
return dept
@staticmethod
async def get_tree(
*,
db: AsyncSession,
data_filter: ColumnElement[bool],
name: str | None,
leader: str | None,
phone: str | None,
status: int | None,
) -> list[dict[str, Any]]:
"""
获取部门树形结构
:param db: 数据库会话
:param data_filter: 请求用户
:param name: 部门名称
:param leader: 部门负责人
:param phone: 联系电话
:param status: 状态
:return:
"""
dept_select = await dept_dao.get_all(db, data_filter, name, leader, phone, status)
tree_data = get_tree_data(dept_select)
return tree_data
@staticmethod
async def create(*, db: AsyncSession, obj: CreateDeptParam) -> None:
"""
创建部门
:param db: 数据库会话
:param obj: 部门创建参数
:return:
"""
dept = await dept_dao.get_by_name(db, obj.name)
if dept:
raise errors.ConflictError(msg='部门名称已存在')
if obj.parent_id is not None:
parent_dept = await dept_dao.get(db, obj.parent_id)
if not parent_dept:
raise errors.NotFoundError(msg='父级部门不存在')
await dept_dao.create(db, obj)
@staticmethod
async def update(*, db: AsyncSession, pk: int, obj: UpdateDeptParam) -> int:
"""
更新部门
:param db: 数据库会话
:param pk: 部门 ID
:param obj: 部门更新参数
:return:
"""
dept = await dept_dao.get(db, pk)
if not dept:
raise errors.NotFoundError(msg='部门不存在')
if dept.name != obj.name and await dept_dao.get_by_name(db, obj.name):
raise errors.ConflictError(msg='部门名称已存在')
if obj.parent_id:
parent_dept = await dept_dao.get(db, obj.parent_id)
if not parent_dept:
raise errors.NotFoundError(msg='父级部门不存在')
if obj.parent_id == dept.id:
raise errors.ForbiddenError(msg='禁止关联自身为父级')
count = await dept_dao.update(db, pk, obj)
return count
@staticmethod
async def delete(*, db: AsyncSession, pk: int) -> int:
"""
删除部门
:param db: 数据库会话
:param pk: 部门 ID
:return:
"""
dept = await dept_dao.get_join(db, pk)
if not dept:
raise errors.NotFoundError(msg='部门不存在')
if dept.users:
raise errors.ConflictError(msg='部门下存在用户,无法删除')
children = await dept_dao.get_children(db, pk)
if children:
raise errors.ConflictError(msg='部门下存在子部门,无法删除')
count = await dept_dao.delete(db, pk)
return count
dept_service: DeptService = DeptService()
@@ -0,0 +1,90 @@
from datetime import datetime
from typing import Any
from sqlalchemy.ext.asyncio import AsyncSession
from backend.app.admin.crud.crud_login_log import login_log_dao
from backend.app.admin.schema.login_log import CreateLoginLogParam, DeleteLoginLogParam
from backend.common.context import ctx
from backend.common.log import log
from backend.common.pagination import paging_data
from backend.database.db import async_db_session
class LoginLogService:
"""登录日志服务类"""
@staticmethod
async def get_list(*, db: AsyncSession, username: str | None, status: int | None, ip: str | None) -> dict[str, Any]:
"""
获取登录日志列表
:param db: 数据库会话
:param username: 用户名
:param status: 状态
:param ip: IP 地址
:return:
"""
log_select = await login_log_dao.get_select(username=username, status=status, ip=ip)
return await paging_data(db, log_select)
@staticmethod
async def create(
*,
user_uuid: str,
username: str,
login_time: datetime,
status: int,
msg: str,
) -> None:
"""
创建登录日志
:param user_uuid: 用户 UUID
:param username: 用户名
:param login_time: 登录时间
:param status: 状态
:param msg: 消息
:return:
"""
try:
obj = CreateLoginLogParam(
user_uuid=user_uuid,
username=username,
status=status,
ip=ctx.ip,
country=ctx.country,
region=ctx.region,
city=ctx.city,
user_agent=ctx.user_agent,
browser=ctx.browser,
os=ctx.os,
device=ctx.device,
msg=msg,
login_time=login_time,
)
# 为后台任务创建独立数据库会话
async with async_db_session.begin() as db:
await login_log_dao.create(db, obj)
except Exception as e:
log.error(f'登录日志创建失败: {e}')
@staticmethod
async def delete(*, db: AsyncSession, obj: DeleteLoginLogParam) -> int:
"""
批量删除登录日志
:param db: 数据库会话
:param obj: 日志 ID 列表
:return:
"""
count = await login_log_dao.delete(db, obj.pks)
return count
@staticmethod
async def delete_all(*, db: AsyncSession) -> None:
"""清空所有登录日志"""
await login_log_dao.delete_all(db)
login_log_service: LoginLogService = LoginLogService()
+137
View File
@@ -0,0 +1,137 @@
from typing import Any
from fastapi import Request
from sqlalchemy.ext.asyncio import AsyncSession
from backend.app.admin.crud.crud_menu import menu_dao
from backend.app.admin.model import Menu
from backend.app.admin.schema.menu import CreateMenuParam, UpdateMenuParam
from backend.app.admin.utils.cache import user_cache_manager
from backend.common.enums import StatusType
from backend.common.exception import errors
from backend.utils.build_tree import get_tree_data, get_vben5_tree_data
class MenuService:
"""菜单服务类"""
@staticmethod
async def get(*, db: AsyncSession, pk: int) -> Menu:
"""
获取菜单详情
:param db: 数据库会话
:param pk: 菜单 ID
:return:
"""
menu = await menu_dao.get(db, menu_id=pk)
if not menu:
raise errors.NotFoundError(msg='菜单不存在')
return menu
@staticmethod
async def get_tree(*, db: AsyncSession, title: str | None, status: int | None) -> list[dict[str, Any]]:
"""
获取菜单树形结构
:param db: 数据库会话
:param title: 菜单标题
:param status: 状态
:return:
"""
menu_data = await menu_dao.get_all(db, title=title, status=status)
menu_tree = get_tree_data(menu_data)
return menu_tree
@staticmethod
async def get_sidebar(*, db: AsyncSession, request: Request) -> list[dict[str, Any] | None]:
"""
获取用户的菜单侧边栏
:param db: 数据库会话
:param request: FastAPI 请求对象
:return:
"""
menu_data = None
if request.user.is_superuser:
menu_data = await menu_dao.get_sidebar(db, None)
else:
roles = [role for role in request.user.roles if role.status == StatusType.enable]
menu_ids = set()
if roles:
for role in roles:
menu_ids.update(menu.id for menu in role.menus)
menu_data = await menu_dao.get_sidebar(db, list(menu_ids))
if menu_data:
return get_vben5_tree_data(menu_data)
return []
@staticmethod
async def create(*, db: AsyncSession, obj: CreateMenuParam) -> None:
"""
创建菜单
:param db: 数据库会话
:param obj: 菜单创建参数
:return:
"""
title = await menu_dao.get_by_title(db, obj.title)
if title:
raise errors.ConflictError(msg='菜单标题已存在')
if obj.parent_id:
parent_menu = await menu_dao.get(db, obj.parent_id)
if not parent_menu:
raise errors.NotFoundError(msg='父级菜单不存在')
await menu_dao.create(db, obj)
@staticmethod
async def update(*, db: AsyncSession, pk: int, obj: UpdateMenuParam) -> int:
"""
更新菜单
:param db: 数据库会话
:param pk: 菜单 ID
:param obj: 菜单更新参数
:return:
"""
menu = await menu_dao.get(db, pk)
if not menu:
raise errors.NotFoundError(msg='菜单不存在')
if menu.title != obj.title and await menu_dao.get_by_title(db, obj.title):
raise errors.ConflictError(msg='菜单标题已存在')
if obj.parent_id:
parent_menu = await menu_dao.get(db, obj.parent_id)
if not parent_menu:
raise errors.NotFoundError(msg='父级菜单不存在')
if obj.parent_id == menu.id:
raise errors.ForbiddenError(msg='禁止关联自身为父级')
count = await menu_dao.update(db, pk, obj)
await user_cache_manager.clear_by_menu_id(db, [pk])
return count
@staticmethod
async def delete(*, db: AsyncSession, pk: int) -> int:
"""
删除菜单
:param db: 数据库会话
:param pk: 菜单 ID
:return:
"""
children = await menu_dao.get_children(db, pk)
if children:
raise errors.ConflictError(msg='菜单下存在子菜单,无法删除')
count = await menu_dao.delete(db, pk)
if count:
await user_cache_manager.clear_by_menu_id(db, [pk])
return count
menu_service: MenuService = MenuService()
@@ -0,0 +1,72 @@
from typing import Any
from sqlalchemy.ext.asyncio import AsyncSession
from backend.app.admin.crud.crud_opera_log import opera_log_dao
from backend.app.admin.schema.opera_log import CreateOperaLogParam, DeleteOperaLogParam
from backend.common.pagination import paging_data
class OperaLogService:
"""操作日志服务类"""
@staticmethod
async def get_list(*, db: AsyncSession, username: str | None, status: int | None, ip: str | None) -> dict[str, Any]:
"""
获取操作日志列表
:param db: 数据库会话
:param username: 用户名
:param status: 状态
:param ip: IP 地址
:return:
"""
log_select = await opera_log_dao.get_select(username=username, status=status, ip=ip)
return await paging_data(db, log_select)
@staticmethod
async def create(*, db: AsyncSession, obj: CreateOperaLogParam) -> None:
"""
创建操作日志
:param db: 数据库会话
:param obj: 操作日志创建参数
:return:
"""
await opera_log_dao.create(db, obj)
@staticmethod
async def bulk_create(*, db: AsyncSession, objs: list[CreateOperaLogParam]) -> None:
"""
批量创建操作日志
:param db: 数据库会话
:param objs: 操作日志创建参数列表
:return:
"""
await opera_log_dao.bulk_create(db, objs)
@staticmethod
async def delete(*, db: AsyncSession, obj: DeleteOperaLogParam) -> int:
"""
批量删除操作日志
:param db: 数据库会话
:param obj: 日志 ID 列表
:return:
"""
count = await opera_log_dao.delete(db, obj.pks)
return count
@staticmethod
async def delete_all(*, db: AsyncSession) -> None:
"""
清空所有操作日志
:param db: 数据库会话
:return:
"""
await opera_log_dao.delete_all(db)
opera_log_service: OperaLogService = OperaLogService()
+135
View File
@@ -0,0 +1,135 @@
import io
import json
from typing import Any
import anyio
from fastapi import UploadFile
from starlette.concurrency import run_in_threadpool
from backend.common.enums import PluginType, StatusType
from backend.common.exception import errors
from backend.core.conf import settings
from backend.core.path_conf import PLUGIN_DIR
from backend.database.redis import redis_client
from backend.plugin.core import get_required_plugins
from backend.plugin.installer import install_git_plugin, install_zip_plugin, remove_plugin, zip_plugin
from backend.plugin.requirements import uninstall_requirements_async
from backend.utils.timezone import timezone
class PluginService:
"""插件服务类"""
@staticmethod
async def get_all() -> list[dict[str, Any]]:
"""获取所有插件"""
changed_key = f'{settings.PLUGIN_REDIS_PREFIX}:changed'
keys = [key for key in await redis_client.get_by_prefix(settings.PLUGIN_REDIS_PREFIX) if key != changed_key]
if not keys:
return []
result = []
plugin_infos = await redis_client.mget(*keys)
for info in plugin_infos:
if info is None:
continue
plugin_info = json.loads(info)
if isinstance(plugin_info, dict):
result.append(plugin_info)
return result
@staticmethod
async def changed() -> str | None:
"""检查插件是否发生变更"""
return await redis_client.get(f'{settings.PLUGIN_REDIS_PREFIX}:changed')
@staticmethod
async def install(*, type: PluginType, file: UploadFile | None = None, repo_url: str | None = None) -> str:
"""
安装插件
:param type: 插件类型
:param file: 插件 zip 压缩包
:param repo_url: git 仓库地址
:return:
"""
if settings.ENVIRONMENT != 'dev':
raise errors.RequestError(msg='禁止在非开发环境下安装插件')
if type == PluginType.zip:
if not file:
raise errors.RequestError(msg='ZIP 压缩包不能为空')
return await install_zip_plugin(file)
if not repo_url:
raise errors.RequestError(msg='Git 仓库地址不能为空')
return await install_git_plugin(repo_url)
@staticmethod
async def uninstall(*, plugin: str) -> None:
"""
卸载插件
:param plugin: 插件名称
:return:
"""
if settings.ENVIRONMENT != 'dev':
raise errors.RequestError(msg='禁止在非开发环境下卸载插件')
if plugin in get_required_plugins():
raise errors.RequestError(msg=f'插件 {plugin} 为必需插件,禁止卸载')
plugin_dir = anyio.Path(PLUGIN_DIR / plugin)
if not await plugin_dir.exists():
raise errors.NotFoundError(msg='插件不存在')
await uninstall_requirements_async(plugin)
backup_file = PLUGIN_DIR / f'{plugin}.{timezone.now().strftime("%Y%m%d%H%M%S")}.backup.zip'
await run_in_threadpool(zip_plugin, plugin_dir, backup_file)
await run_in_threadpool(remove_plugin, plugin_dir)
await redis_client.delete(f'{settings.PLUGIN_REDIS_PREFIX}:{plugin}')
await redis_client.set(f'{settings.PLUGIN_REDIS_PREFIX}:changed', 'true')
@staticmethod
async def update_status(*, plugin: str) -> None:
"""
更新插件状态
:param plugin: 插件名称
:return:
"""
plugin_key = f'{settings.PLUGIN_REDIS_PREFIX}:{plugin}'
plugin_info = await redis_client.get(plugin_key)
if not plugin_info:
raise errors.NotFoundError(msg='插件不存在')
plugin_info = json.loads(plugin_info)
# 更新持久缓存状态
new_status = (
str(StatusType.enable.value)
if plugin_info['plugin']['enable'] == str(StatusType.disable.value)
else str(StatusType.disable.value)
)
plugin_info['plugin']['enable'] = new_status
await redis_client.set(plugin_key, json.dumps(plugin_info, ensure_ascii=False))
await redis_client.set(f'{settings.PLUGIN_REDIS_PREFIX}:changed', 'true')
@staticmethod
async def build(*, plugin: str) -> io.BytesIO:
"""
打包插件为 zip 压缩包
:param plugin: 插件名称
:return:
"""
plugin_dir = anyio.Path(PLUGIN_DIR / plugin)
if not await plugin_dir.exists():
raise errors.NotFoundError(msg='插件不存在')
bio = io.BytesIO()
await run_in_threadpool(zip_plugin, plugin_dir, bio)
bio.seek(0)
return bio
plugin_service: PluginService = PluginService()
+193
View File
@@ -0,0 +1,193 @@
from collections.abc import Sequence
from typing import Any
from sqlalchemy.ext.asyncio import AsyncSession
from backend.app.admin.crud.crud_data_scope import data_scope_dao
from backend.app.admin.crud.crud_menu import menu_dao
from backend.app.admin.crud.crud_role import role_dao
from backend.app.admin.model import Role
from backend.app.admin.schema.role import (
CreateRoleParam,
DeleteRoleParam,
UpdateRoleMenuParam,
UpdateRoleParam,
UpdateRoleScopeParam,
)
from backend.app.admin.utils.cache import user_cache_manager
from backend.common.exception import errors
from backend.common.pagination import paging_data
from backend.utils.build_tree import get_tree_data
class RoleService:
"""角色服务类"""
@staticmethod
async def get(*, db: AsyncSession, pk: int) -> Role:
"""
获取角色详情
:param db: 数据库会话
:param pk: 角色 ID
:return:
"""
role = await role_dao.get_join(db, pk)
if not role:
raise errors.NotFoundError(msg='角色不存在')
return role
@staticmethod
async def get_all(*, db: AsyncSession) -> Sequence[Role]:
"""
获取所有角色
:param db: 数据库会话
:return:
"""
roles = await role_dao.get_all(db)
return roles
@staticmethod
async def get_list(*, db: AsyncSession, name: str | None, status: int | None) -> dict[str, Any]:
"""
获取角色列表
:param db: 数据库会话
:param name: 角色名称
:param status: 状态
:return:
"""
role_select = await role_dao.get_select(name=name, status=status)
return await paging_data(db, role_select)
@staticmethod
async def get_menu_tree(*, db: AsyncSession, pk: int) -> list[dict[str, Any] | None]:
"""
获取角色的菜单树形结构
:param db: 数据库会话
:param pk: 角色 ID
:return:
"""
role = await role_dao.get(db, pk)
if not role:
raise errors.NotFoundError(msg='角色不存在')
menus = await role_dao.get_menus(db, pk)
menu_tree = get_tree_data(menus) if menus else []
return menu_tree
@staticmethod
async def get_scopes(*, db: AsyncSession, pk: int) -> list[int]:
"""
获取角色数据范围列表
:param db: 数据库会话
:param pk:
:return:
"""
role = await role_dao.get_join(db, pk)
if not role:
raise errors.NotFoundError(msg='角色不存在')
scope_ids = [scope.id for scope in role.scopes]
return scope_ids
@staticmethod
async def create(*, db: AsyncSession, obj: CreateRoleParam) -> None:
"""
创建角色
:param db: 数据库会话
:param obj: 角色创建参数
:return:
"""
role = await role_dao.get_by_name(db, obj.name)
if role:
raise errors.ConflictError(msg='角色已存在')
await role_dao.create(db, obj)
@staticmethod
async def update(*, db: AsyncSession, pk: int, obj: UpdateRoleParam) -> int:
"""
更新角色
:param db: 数据库会话
:param pk: 角色 ID
:param obj: 角色更新参数
:return:
"""
role = await role_dao.get(db, pk)
if not role:
raise errors.NotFoundError(msg='角色不存在')
if role.name != obj.name and await role_dao.get_by_name(db, obj.name):
raise errors.ConflictError(msg='角色已存在')
count = await role_dao.update(db, pk, obj)
await user_cache_manager.clear_by_role_id(db, [pk])
return count
@staticmethod
async def update_role_menu(*, db: AsyncSession, pk: int, menu_ids: UpdateRoleMenuParam) -> int:
"""
更新角色菜单
:param db: 数据库会话
:param pk: 角色 ID
:param menu_ids: 菜单 ID 列表
:return:
"""
role = await role_dao.get(db, pk)
if not role:
raise errors.NotFoundError(msg='角色不存在')
if menu_ids.menus:
menus = await menu_dao.get_all_by_ids(db, list(set(menu_ids.menus)))
if {menu.id for menu in menus} != set(menu_ids.menus):
raise errors.NotFoundError(msg='菜单不存在')
count = await role_dao.update_menus(db, pk, menu_ids)
await user_cache_manager.clear_by_role_id(db, [pk])
return count
@staticmethod
async def update_role_scope(*, db: AsyncSession, pk: int, scope_ids: UpdateRoleScopeParam) -> int:
"""
更新角色数据范围
:param db: 数据库会话
:param pk: 角色 ID
:param scope_ids: 权限规则 ID 列表
:return:
"""
role = await role_dao.get(db, pk)
if not role:
raise errors.NotFoundError(msg='角色不存在')
if scope_ids.scopes:
scopes = await data_scope_dao.get_all_by_ids(db, list(set(scope_ids.scopes)))
if {scope.id for scope in scopes} != set(scope_ids.scopes):
raise errors.NotFoundError(msg='数据范围不存在')
count = await role_dao.update_scopes(db, pk, scope_ids)
await user_cache_manager.clear_by_role_id(db, [pk])
return count
@staticmethod
async def delete(*, db: AsyncSession, obj: DeleteRoleParam) -> int:
"""
批量删除角色
:param db: 数据库会话
:param obj: 角色 ID 列表
:return:
"""
count = await role_dao.delete(db, obj.pks)
await user_cache_manager.clear_by_role_id(db, obj.pks)
return count
role_service: RoleService = RoleService()
@@ -0,0 +1,122 @@
import math
from datetime import datetime, timedelta
from sqlalchemy.ext.asyncio import AsyncSession
from backend.app.admin.crud.crud_user_password_history import user_password_history_dao
from backend.app.admin.schema.user_password_history import CreateUserPasswordHistoryParam
from backend.common.exception import errors
from backend.core.conf import settings
from backend.database.redis import redis_client
from backend.utils.dynamic_config import load_user_security_config
from backend.utils.timezone import timezone
class UserPasswordHistoryService:
"""用户密码历史服务类"""
@staticmethod
async def check_status(user_id: int, user_status: int) -> None:
"""
检查用户状态
:param user_id: 用户 ID
:param user_status: 用户状态
:return:
"""
if not user_status:
raise errors.AuthorizationError(msg='用户已被锁定, 请联系统管理员')
lock_key = f'{settings.USER_LOCK_REDIS_PREFIX}:{user_id}'
locked_until_str = await redis_client.get(lock_key)
if locked_until_str:
locked_until = timezone.from_str(locked_until_str)
now = timezone.now()
if locked_until > now:
remaining_minutes = math.ceil((locked_until - now).total_seconds() / 60)
raise errors.AuthorizationError(msg=f'账号已被锁定,请在 {remaining_minutes} 分钟后重试')
await redis_client.delete(lock_key)
await redis_client.delete(f'{settings.LOGIN_FAILURE_PREFIX}:{user_id}')
@staticmethod
async def handle_login_failure(db: AsyncSession, user_id: int) -> None:
"""
处理登录失败
:param db: 数据库会话
:param user_id: 用户 ID
:return:
"""
await load_user_security_config(db)
if settings.USER_LOCK_THRESHOLD == 0:
return
failure_key = f'{settings.LOGIN_FAILURE_PREFIX}:{user_id}'
failure_count = await redis_client.get(failure_key)
failure_count = int(failure_count) if failure_count else 0
failure_count += 1
await redis_client.set(failure_key, str(failure_count), ex=settings.USER_LOCK_SECONDS)
if failure_count >= settings.USER_LOCK_THRESHOLD:
locked_until = timezone.now() + timedelta(seconds=settings.USER_LOCK_SECONDS)
await redis_client.set(
f'{settings.USER_LOCK_REDIS_PREFIX}:{user_id}',
timezone.to_str(locked_until),
ex=settings.USER_LOCK_SECONDS,
)
raise errors.AuthorizationError(msg='登录失败次数过多,账号已被锁定')
@staticmethod
async def check_password_expiry_status(db: AsyncSession, password_changed_time: datetime) -> int | None:
"""
检查密码过期状态
:param db: 数据库会话
:param password_changed_time: 密码修改时间
:return:
"""
await load_user_security_config(db)
if settings.USER_PASSWORD_EXPIRY_DAYS == 0:
return None
if not password_changed_time:
raise errors.AuthorizationError(msg='密码已过期,请修改密码后重新登录')
expiry_time = password_changed_time + timedelta(days=settings.USER_PASSWORD_EXPIRY_DAYS)
days_remaining = (expiry_time - timezone.now()).days
if days_remaining < 0:
raise errors.AuthorizationError(msg='密码已过期,请修改密码后重新登录')
if days_remaining <= settings.USER_PASSWORD_REMINDER_DAYS:
return days_remaining
return None
@staticmethod
async def handle_login_success(user_id: int) -> None:
"""
处理登录成功
:param user_id: 用户 ID
:return:
"""
await redis_client.delete(f'{settings.USER_LOCK_REDIS_PREFIX}:{user_id}')
await redis_client.delete(f'{settings.LOGIN_FAILURE_PREFIX}:{user_id}')
@staticmethod
async def save_password_history(db: AsyncSession, obj: CreateUserPasswordHistoryParam) -> None:
"""
保存密码历史记录
:param db: 数据库会话
:param obj: 创建密码历史记录参数
:return:
"""
await user_password_history_dao.create(db, obj)
password_security_service: UserPasswordHistoryService = UserPasswordHistoryService()
+315
View File
@@ -0,0 +1,315 @@
from collections.abc import Sequence
from typing import Any
from fastapi import Request
from sqlalchemy.ext.asyncio import AsyncSession
from backend.app.admin.crud.crud_dept import dept_dao
from backend.app.admin.crud.crud_role import role_dao
from backend.app.admin.crud.crud_user import user_dao
from backend.app.admin.model import Role, User
from backend.app.admin.schema.user import (
AddUserParam,
ResetPasswordParam,
UpdateUserParam,
)
from backend.app.admin.schema.user_password_history import CreateUserPasswordHistoryParam
from backend.app.admin.service.user_password_history_service import password_security_service
from backend.app.admin.utils.password_security import password_verify, validate_new_password
from backend.common.context import ctx
from backend.common.enums import UserPermissionType
from backend.common.exception import errors
from backend.common.pagination import paging_data
from backend.common.response.response_code import CustomErrorCode
from backend.common.security.jwt import jwt_decode
from backend.common.security.token import get_token, revoke_user_tokens
from backend.core.conf import settings
from backend.database.redis import redis_client
from backend.utils.serializers import select_join_serialize
class UserService:
"""用户服务类"""
@staticmethod
async def get_userinfo(*, db: AsyncSession, pk: int | None = None, username: str | None = None) -> User:
"""
获取用户信息
:param db: 数据库会话
:param pk: 用户 ID
:param username: 用户名
:return:
"""
user = await user_dao.get_join(db, user_id=pk, username=username)
if not user:
raise errors.NotFoundError(msg='用户不存在')
return user
@staticmethod
async def get_roles(*, db: AsyncSession, pk: int) -> Sequence[Role]:
"""
获取用户所有角色
:param db: 数据库会话
:param pk: 用户 ID
:return:
"""
user = await user_dao.get_join(db, user_id=pk)
if not user:
raise errors.NotFoundError(msg='用户不存在')
return user.roles
@staticmethod
async def get_list(*, db: AsyncSession, dept: int, username: str, phone: str, status: int) -> dict[str, Any]:
"""
获取用户列表
:param db: 数据库会话
:param dept: 部门 ID
:param username: 用户名
:param phone: 手机号
:param status: 状态
:return:
"""
user_select = await user_dao.get_select(dept=dept, username=username, phone=phone, status=status)
data = await paging_data(db, user_select)
if data['items']:
serialized_items = select_join_serialize(data['items'], relationships=['User-m2o-Dept', 'User-m2m-Role'])
# 确保返回的是列表,即使只有一个元素
data['items'] = [serialized_items] if not isinstance(serialized_items, list) else serialized_items
return data
@staticmethod
async def create(*, db: AsyncSession, obj: AddUserParam) -> None:
"""
创建用户
:param db: 数据库会话
:param obj: 用户添加参数
:return:
"""
if await user_dao.get_by_username(db, obj.username):
raise errors.ConflictError(msg='用户名已注册')
if obj.email and await user_dao.check_email(db, obj.email):
raise errors.ConflictError(msg='邮箱已被绑定')
if not obj.password:
raise errors.RequestError(msg='密码不允许为空')
if not await dept_dao.get(db, obj.dept_id):
raise errors.NotFoundError(msg='部门不存在')
if obj.roles:
roles = await role_dao.get_all_by_ids(db, list(set(obj.roles)))
if {role.id for role in roles} != set(obj.roles):
raise errors.NotFoundError(msg='角色不存在')
obj.nickname = obj.nickname or obj.username
await user_dao.add(db, obj)
@staticmethod
async def update(*, db: AsyncSession, pk: int, obj: UpdateUserParam) -> int:
"""
更新用户信息
:param db: 数据库会话
:param pk: 用户 ID
:param obj: 用户更新参数
:return:
"""
user = await user_dao.get_join(db, user_id=pk)
if not user:
raise errors.NotFoundError(msg='用户不存在')
if obj.username != user.username and await user_dao.get_by_username(db, obj.username):
raise errors.ConflictError(msg='用户名已注册')
if obj.email and obj.email != user.email:
email_user = await user_dao.check_email(db, obj.email)
if email_user:
raise errors.ConflictError(msg='邮箱已被绑定')
if obj.dept_id and obj.dept_id != user.dept_id and not await dept_dao.get(db, dept_id=obj.dept_id):
raise errors.NotFoundError(msg='部门不存在')
if obj.roles:
roles = await role_dao.get_all_by_ids(db, list(set(obj.roles)))
if {role.id for role in roles} != set(obj.roles):
raise errors.NotFoundError(msg='角色不存在')
count = await user_dao.update(db, user.id, obj)
await redis_client.delete(f'{settings.JWT_USER_REDIS_PREFIX}:{user.id}')
return count
@staticmethod
async def update_permission(*, db: AsyncSession, request: Request, pk: int, type: UserPermissionType) -> int: # ruff:ignore[complex-structure]
"""
更新用户权限
:param db: 数据库会话
:param request: FastAPI 请求对象
:param pk: 用户 ID
:param type: 权限类型
:return:
"""
match type:
case UserPermissionType.superuser:
user = await user_dao.get(db, pk)
if not user:
raise errors.NotFoundError(msg='用户不存在')
if pk == request.user.id:
raise errors.ForbiddenError(msg='禁止修改自身权限')
count = await user_dao.set_super(db, pk, is_super=not user.is_superuser)
case UserPermissionType.staff:
user = await user_dao.get(db, pk)
if not user:
raise errors.NotFoundError(msg='用户不存在')
if pk == request.user.id:
raise errors.ForbiddenError(msg='禁止修改自身权限')
count = await user_dao.set_staff(db, pk, is_staff=not user.is_staff)
case UserPermissionType.status:
user = await user_dao.get(db, pk)
if not user:
raise errors.NotFoundError(msg='用户不存在')
if pk == request.user.id:
raise errors.ForbiddenError(msg='禁止修改自身权限')
count = await user_dao.set_status(db, pk, 0 if user.status == 1 else 1)
case UserPermissionType.multi_login:
user = await user_dao.get(db, pk)
if not user:
raise errors.NotFoundError(msg='用户不存在')
multi_login = user.is_multi_login if pk != request.user.id else request.user.is_multi_login
new_multi_login = not multi_login
count = await user_dao.set_multi_login(db, pk, multi_login=new_multi_login)
token = get_token(request)
token_payload = jwt_decode(token)
if pk == request.user.id:
# 系统管理员修改自身时,除当前 token 外,其他 token 失效
if not new_multi_login:
await revoke_user_tokens(user.id, exclude_session_uuid=token_payload.session_uuid)
else:
# 系统管理员修改他人时,他人 token 全部失效
if not new_multi_login:
await revoke_user_tokens(user.id)
case _:
raise errors.RequestError(msg='权限类型不存在')
await redis_client.delete(f'{settings.JWT_USER_REDIS_PREFIX}:{user.id}')
return count
@staticmethod
async def reset_password(*, db: AsyncSession, pk: int, password: str) -> int:
"""
重置用户密码
:param db: 数据库会话
:param pk: 用户 ID
:param password: 新密码
:return:
"""
user = await user_dao.get(db, pk)
if not user:
raise errors.NotFoundError(msg='用户不存在')
await validate_new_password(db, user.id, password)
count = await user_dao.reset_password(db, user.id, password)
history_obj = CreateUserPasswordHistoryParam(user_id=user.id, password=user.password)
await password_security_service.save_password_history(db, history_obj)
await user_dao.update_password_changed_time(db, user.id)
await revoke_user_tokens(user.id)
await redis_client.delete(f'{settings.JWT_USER_REDIS_PREFIX}:{user.id}')
return count
@staticmethod
async def update_nickname(*, db: AsyncSession, user_id: int, nickname: str) -> int:
"""
更新当前用户昵称
:param db: 数据库会话
:param user_id: 用户 ID
:param nickname: 用户昵称
:return:
"""
count = await user_dao.update_nickname(db, user_id, nickname)
await redis_client.delete(f'{settings.JWT_USER_REDIS_PREFIX}:{user_id}')
return count
@staticmethod
async def update_avatar(*, db: AsyncSession, user_id: int, avatar: str) -> int:
"""
更新当前用户头像
:param db: 数据库会话
:param user_id: 用户 ID
:param avatar: 头像地址
:return:
"""
count = await user_dao.update_avatar(db, user_id, avatar)
await redis_client.delete(f'{settings.JWT_USER_REDIS_PREFIX}:{user_id}')
return count
@staticmethod
async def update_email(*, db: AsyncSession, user_id: int, captcha: str, email: str) -> int:
"""
更新当前用户邮箱
:param db: 数据库会话
:param user_id: 用户 ID
:param captcha: 邮箱验证码
:param email: 邮箱
:return:
"""
captcha_key = f'{settings.EMAIL_CAPTCHA_REDIS_PREFIX}:{ctx.ip}'
captcha_code = await redis_client.get(captcha_key)
if not captcha_code:
raise errors.RequestError(msg='验证码已失效,请重新获取')
if captcha != captcha_code:
raise errors.CustomError(error=CustomErrorCode.CAPTCHA_ERROR)
email_user = await user_dao.check_email(db, email)
if email_user and email_user.id != user_id:
raise errors.ConflictError(msg='邮箱已被绑定')
await redis_client.delete(captcha_key)
count = await user_dao.update_email(db, user_id, email)
await redis_client.delete(f'{settings.JWT_USER_REDIS_PREFIX}:{user_id}')
return count
@staticmethod
async def update_password(*, db: AsyncSession, user_id: int, obj: ResetPasswordParam) -> int:
"""
更新当前用户密码
:param db: 数据库会话
:param user_id: 用户 ID
:param obj: 密码重置参数
:return:
"""
user = await user_dao.get(db, user_id)
if user.password and not password_verify(obj.old_password, user.password):
raise errors.RequestError(msg='原密码错误')
if obj.new_password != obj.confirm_password:
raise errors.RequestError(msg='两次密码输入不一致')
await validate_new_password(db, user_id, obj.new_password)
count = await user_dao.reset_password(db, user_id, obj.new_password)
history_obj = CreateUserPasswordHistoryParam(user_id=user.id, password=user.password)
await password_security_service.save_password_history(db, history_obj)
await user_dao.update_password_changed_time(db, user.id)
await revoke_user_tokens(user_id)
await redis_client.delete(f'{settings.JWT_USER_REDIS_PREFIX}:{user_id}')
return count
@staticmethod
async def delete(*, db: AsyncSession, pk: int) -> int:
"""
删除用户
:param db: 数据库会话
:param pk: 用户 ID
:return:
"""
user = await user_dao.get(db, pk)
if not user:
raise errors.NotFoundError(msg='用户不存在')
count = await user_dao.delete(db, user.id)
await revoke_user_tokens(user.id)
await redis_client.delete(f'{settings.JWT_USER_REDIS_PREFIX}:{user.id}')
return count
user_service: UserService = UserService()
View File
@@ -0,0 +1,7 @@
from starlette.testclient import TestClient
def test_logout(client: TestClient, token_headers: dict[str, str]) -> None:
response = client.post('/auth/logout', headers=token_headers)
assert response.status_code == 200
assert response.json()['code'] == 200
View File
+98
View File
@@ -0,0 +1,98 @@
from collections.abc import Sequence
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from backend.app.admin.model import data_scope_rule, role_data_scope, role_menu, user_role
from backend.core.conf import settings
from backend.database.redis import redis_client
class UserCacheManager:
"""用户缓存管理"""
@staticmethod
async def clear(user_ids: Sequence[int]) -> None:
"""
清理用户缓存
:param user_ids: 用户 ID 列表
:return:
"""
if user_ids:
await redis_client.delete(*[f'{settings.JWT_USER_REDIS_PREFIX}:{user_id}' for user_id in user_ids])
async def clear_by_role_id(self, db: AsyncSession, role_ids: list[int]) -> None:
"""
通过角色 ID 清理用户缓存
:param db: 数据库会话
:param role_ids: 角色 ID 列表
:return:
"""
stmt = select(user_role.c.user_id).where(user_role.c.role_id.in_(role_ids)).distinct()
result = await db.execute(stmt)
user_ids = result.scalars().all()
await self.clear(user_ids)
async def clear_by_menu_id(self, db: AsyncSession, menu_ids: list[int]) -> None:
"""
通过菜单 ID 清理用户缓存
:param db: 数据库会话
:param menu_ids: 菜单 ID 列表
:return:
"""
stmt = (
select(user_role.c.user_id)
.join(role_menu, user_role.c.role_id == role_menu.c.role_id)
.where(role_menu.c.menu_id.in_(menu_ids))
.distinct()
)
result = await db.execute(stmt)
user_ids = result.scalars().all()
await self.clear(user_ids)
async def clear_by_data_scope_id(self, db: AsyncSession, scope_ids: list[int]) -> None:
"""
通过数据范围 ID 清理用户缓存
:param db: 数据库会话
:param scope_ids: 数据范围 ID 列表
:return:
"""
stmt = (
select(user_role.c.user_id)
.join(role_data_scope, user_role.c.role_id == role_data_scope.c.role_id)
.where(role_data_scope.c.data_scope_id.in_(scope_ids))
.distinct()
)
result = await db.execute(stmt)
user_ids = result.scalars().all()
await self.clear(user_ids)
async def clear_by_data_rule_id(self, db: AsyncSession, rule_ids: list[int]) -> None:
"""
通过数据规则 ID 清理用户缓存
:param db: 数据库会话
:param rule_ids: 数据规则 ID 列表
:return:
"""
stmt = (
select(user_role.c.user_id)
.join(role_data_scope, user_role.c.role_id == role_data_scope.c.role_id)
.join(data_scope_rule, role_data_scope.c.data_scope_id == data_scope_rule.c.data_scope_id)
.where(data_scope_rule.c.data_rule_id.in_(rule_ids))
.distinct()
)
result = await db.execute(stmt)
user_ids = result.scalars().all()
await self.clear(user_ids)
user_cache_manager: UserCacheManager = UserCacheManager()
@@ -0,0 +1,68 @@
from pwdlib import PasswordHash
from pwdlib.hashers.bcrypt import BcryptHasher
from sqlalchemy.ext.asyncio import AsyncSession
from backend.app.admin.crud.crud_user_password_history import user_password_history_dao
from backend.common.exception import errors
from backend.core.conf import settings
from backend.utils.dynamic_config import load_user_security_config
from backend.utils.pattern_validate import is_has_letter, is_has_number, is_has_special_char
password_hash = PasswordHash((BcryptHasher(),))
def get_hash_password(password: str, salt: bytes | None) -> str:
"""
使用哈希算法加密密码
:param password: 密码
:param salt: 盐值
:return:
"""
return password_hash.hash(password, salt=salt)
def password_verify(plain_password: str, hashed_password: str) -> bool:
"""
密码验证
:param plain_password: 待验证的密码
:param hashed_password: 哈希密码
:return:
"""
return password_hash.verify(plain_password, hashed_password)
async def validate_new_password(db: AsyncSession, user_id: int, new_password: str) -> None:
"""
验证新密码
:param db: 数据库会话
:param user_id: 用户ID
:param new_password: 新密码
:return:
"""
await load_user_security_config(db)
if len(new_password) < settings.USER_PASSWORD_MIN_LENGTH:
raise errors.RequestError(msg=f'密码长度不能少于 {settings.USER_PASSWORD_MIN_LENGTH} 个字符')
if len(new_password) > settings.USER_PASSWORD_MAX_LENGTH:
raise errors.RequestError(msg=f'密码长度不能超过 {settings.USER_PASSWORD_MAX_LENGTH} 个字符')
if not is_has_number(new_password):
raise errors.RequestError(msg='密码必须包含数字')
if not is_has_letter(new_password):
raise errors.RequestError(msg='密码必须包含字母')
if settings.USER_PASSWORD_REQUIRE_SPECIAL_CHAR and not is_has_special_char(new_password):
raise errors.RequestError(msg='密码必须包含特殊字符(如:!@#$%')
password_history = await user_password_history_dao.get_by_user_id(db, user_id)
for hist in password_history[: settings.USER_PASSWORD_HISTORY_CHECK_COUNT]:
if password_verify(new_password, hist.password):
raise errors.RequestError(
msg=f'新密码不能与最近 {settings.USER_PASSWORD_HISTORY_CHECK_COUNT} 次使用的密码相同'
)

Some files were not shown because too many files have changed in this diff Show More