add some api

This commit is contained in:
UlricQin 2020-11-04 21:47:55 +08:00
parent ac6d269d90
commit 79ae96f15d
6 changed files with 61 additions and 0 deletions

View File

@ -5,6 +5,12 @@ type RoleGlobalUser struct {
UserId int64 `json:"user_id" xorm:"'user_id'"` UserId int64 `json:"user_id" xorm:"'user_id'"`
} }
func RoleGlobalUserAll() ([]RoleGlobalUser, error) {
var objs []RoleGlobalUser
err := DB["rdb"].Find(&objs)
return objs, err
}
// UserHasGlobalRole 查看某个用户是否有某个全局角色 // UserHasGlobalRole 查看某个用户是否有某个全局角色
func UserHasGlobalRole(userId int64, roleIds []int64) (bool, error) { func UserHasGlobalRole(userId int64, roleIds []int64) (bool, error) {
cnt, err := DB["rdb"].Where("user_id=?", userId).In("role_id", roleIds).Count(new(RoleGlobalUser)) cnt, err := DB["rdb"].Where("user_id=?", userId).In("role_id", roleIds).Count(new(RoleGlobalUser))

View File

@ -10,6 +10,12 @@ type RoleOperation struct {
Operation string `json:"operation"` 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) { func OperationsOfRoles(rids []int64) ([]string, error) {
if len(rids) == 0 { if len(rids) == 0 {
return []string{}, nil return []string{}, nil

View File

@ -177,5 +177,12 @@ func Config(r *gin.Engine) {
v1.POST("/login", v1Login) v1.POST("/login", v1Login)
v1.POST("/send-login-code-by-sms", v1SendLoginCodeBySms) v1.POST("/send-login-code-by-sms", v1SendLoginCodeBySms)
v1.POST("/send-login-code-by-email", v1SendLoginCodeByEmail) 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)
} }
} }

View File

@ -41,3 +41,8 @@ func v1CandoNodeOps(c *gin.Context) {
renderData(c, ret, nil) renderData(c, ret, nil)
} }
func v1RoleGlobalUserGets(c *gin.Context) {
objs, err := models.RoleGlobalUserAll()
renderData(c, objs, err)
}

View File

@ -142,3 +142,8 @@ func roleGlobalUsersUnbind(c *gin.Context) {
renderMessage(c, obj.UnbindUsers(f.Ids)) renderMessage(c, obj.UnbindUsers(f.Ids))
} }
func v1RoleOperationGets(c *gin.Context) {
objs, err := models.RoleOperationAll()
renderData(c, objs, err)
}

View File

@ -113,3 +113,35 @@ func permGlobalOps(c *gin.Context) {
renderData(c, operations, err) 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)
}