Bladeren bron

feat(database): 添加用户角色并调整授权逻辑

- 在 users 表中添加 role 字段,支持 admin、user 和 api 三种角色- 修改 AuthController 中的权限检查逻辑,允许 api 角色访问受保护的路由
- 更新 User 模型,添加 Api 角色枚举值
-调整 UsersController 中的 myAdmin 方法,暂时移除管理员权限检查
wui 10 maanden geleden
bovenliggende
commit
dd24475ec2

+ 1 - 1
app/Controllers/Http/AuthController.ts

@@ -22,7 +22,7 @@ export default class AuthController {
         } catch (error) {
             throw new AuthenticationException(error.message, error.code)
         }
-        if (token.user.role !== UserRoles.Admin) {
+        if (token.user.role === UserRoles.User) {
             throw new AuthenticationException('Unauthorized access', 'E_UNAUTHORIZED_ACCESS')
         }
         return token

+ 1 - 1
app/Controllers/Http/UsersController.ts

@@ -61,7 +61,7 @@ export default class UsersController {
     }
 
     public async myAdmin({ auth, bouncer }: HttpContextContract) {
-        await bouncer.authorize('admin')
+        // await bouncer.authorize('admin')
         return auth.user
     }
 

+ 1 - 0
app/Models/User.ts

@@ -5,6 +5,7 @@ import AppBaseModel from './AppBaseModel'
 
 export enum UserRoles {
     Admin = 'admin',
+    Api = 'api',
     User = 'user'
 }
 

+ 15 - 0
database/migrations/1741942692579_users.ts

@@ -0,0 +1,15 @@
+import BaseSchema from '@ioc:Adonis/Lucid/Schema'
+
+export default class extends BaseSchema {
+  protected tableName = 'users'
+
+  public async up () {
+      this.schema.alterTable(this.tableName, (table) => {
+          table.enum('role', ['admin', 'user', 'api']).defaultTo('user').nullable().alter()
+      })
+  }
+
+  public async down () {
+    this.schema.dropTable(this.tableName)
+  }
+}