From 79ae96f15d410e74ea5e472fd69dcc097937a1fb Mon Sep 17 00:00:00 2001 From: UlricQin Date: Wed, 4 Nov 2020 21:47:55 +0800 Subject: [PATCH] add some api --- src/models/role_global_user.go | 6 ++++++ src/models/role_operation.go | 6 ++++++ src/modules/rdb/http/router.go | 7 +++++++ src/modules/rdb/http/router_perm.go | 5 +++++ src/modules/rdb/http/router_role.go | 5 +++++ src/modules/rdb/http/router_self.go | 32 +++++++++++++++++++++++++++++ 6 files changed, 61 insertions(+) diff --git a/src/models/role_global_user.go b/src/models/role_global_user.go index a76364dc..32db4954 100644 --- a/src/models/role_global_user.go +++ b/src/models/role_global_user.go @@ -5,6 +5,12 @@ type RoleGlobalUser struct { UserId int64 `json:"user_id" xorm:"'user_id'"` } +func RoleGlobalUserAll() ([]RoleGlobalUser, error) { + var objs []RoleGlobalUser + err := DB["rdb"].Find(&objs) + return objs, err +} + // UserHasGlobalRole 查看某个用户是否有某个全局角色 func UserHasGlobalRole(userId int64, roleIds []int64) (bool, error) { cnt, err := DB["rdb"].Where("user_id=?", userId).In("role_id", roleIds).Count(new(RoleGlobalUser)) diff --git a/src/models/role_operation.go b/src/models/role_operation.go index fefe22b2..4139a745 100644 --- a/src/models/role_operation.go +++ b/src/models/role_operation.go @@ -10,6 +10,12 @@ type RoleOperation struct { Operation string `json:"operation"` } +func RoleOperationAll() ([]RoleOperation, error) { + var objs []RoleOperation + err := DB["rdb"].OrderBy("id").Find(&objs) + return objs, err +} + func OperationsOfRoles(rids []int64) ([]string, error) { if len(rids) == 0 { return []string{}, nil diff --git a/src/modules/rdb/http/router.go b/src/modules/rdb/http/router.go index dc18670d..76d1f756 100644 --- a/src/modules/rdb/http/router.go +++ b/src/modules/rdb/http/router.go @@ -177,5 +177,12 @@ func Config(r *gin.Engine) { v1.POST("/login", v1Login) v1.POST("/send-login-code-by-sms", v1SendLoginCodeBySms) v1.POST("/send-login-code-by-email", v1SendLoginCodeByEmail) + + // 第三方系统获取某个用户的所有权限点 + v1.GET("/perms/global", v1PermGlobalOps) + + // 第三方系统同步权限表的数据 + v1.GET("/table/sync/role-operation", v1RoleOperationGets) + v1.GET("/table/sync/role-global-user", v1RoleGlobalUserGets) } } diff --git a/src/modules/rdb/http/router_perm.go b/src/modules/rdb/http/router_perm.go index 99d94670..bc31ee59 100644 --- a/src/modules/rdb/http/router_perm.go +++ b/src/modules/rdb/http/router_perm.go @@ -41,3 +41,8 @@ func v1CandoNodeOps(c *gin.Context) { renderData(c, ret, nil) } + +func v1RoleGlobalUserGets(c *gin.Context) { + objs, err := models.RoleGlobalUserAll() + renderData(c, objs, err) +} diff --git a/src/modules/rdb/http/router_role.go b/src/modules/rdb/http/router_role.go index f3c5432d..e7caefb6 100644 --- a/src/modules/rdb/http/router_role.go +++ b/src/modules/rdb/http/router_role.go @@ -142,3 +142,8 @@ func roleGlobalUsersUnbind(c *gin.Context) { renderMessage(c, obj.UnbindUsers(f.Ids)) } + +func v1RoleOperationGets(c *gin.Context) { + objs, err := models.RoleOperationAll() + renderData(c, objs, err) +} \ No newline at end of file diff --git a/src/modules/rdb/http/router_self.go b/src/modules/rdb/http/router_self.go index afed43c4..d9a104a0 100644 --- a/src/modules/rdb/http/router_self.go +++ b/src/modules/rdb/http/router_self.go @@ -113,3 +113,35 @@ func permGlobalOps(c *gin.Context) { renderData(c, operations, err) } + +func v1PermGlobalOps(c *gin.Context) { + user, err := models.UserGet("username=?", queryStr(c, "username")) + dangerous(err) + + operations := make(map[string]struct{}) + + if user.IsRoot == 1 { + for _, system := range config.GlobalOps { + for _, group := range system.Groups { + for _, op := range group.Ops { + operations[op.En] = struct{}{} + } + } + } + + renderData(c, operations, nil) + return + } + + roleIds, err := models.RoleIdsGetByUserId(user.Id) + dangerous(err) + + ops, err := models.OperationsOfRoles(roleIds) + dangerous(err) + + for _, op := range ops { + operations[op] = struct{}{} + } + + renderData(c, operations, err) +}