- 角色管理初版
This commit is contained in:
@@ -1,10 +1,9 @@
|
||||
package authz
|
||||
|
||||
type IAuthz interface {
|
||||
AddStoreRole4User(userID string, storeID int) (err error)
|
||||
DelStoreRole4User(userID string, storeID int) (err error)
|
||||
AddRole4User(userID, roleName string) (err error)
|
||||
DelRole4User(userID, roleName string) (err error)
|
||||
GetUserRoleList(userID string) (roleList []string, err error)
|
||||
GetRoleUserList(roleName string) (userIDList []string, err error)
|
||||
AddRole4User(userID string, r *RoleInfo) (err error)
|
||||
DeleteRole4User(userID string, r *RoleInfo) (err error)
|
||||
GetUserRoleList(userID string) (roleList []*RoleInfo, err error)
|
||||
GetRoleUserList(r *RoleInfo) (userIDList []string, err error)
|
||||
// GetAllRoleList() (roleList []*RoleInfo)
|
||||
}
|
||||
|
||||
@@ -1,23 +1,101 @@
|
||||
package authz
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
"git.rosy.net.cn/jx-callback/business/model"
|
||||
"git.rosy.net.cn/jx-callback/business/model/dao"
|
||||
)
|
||||
|
||||
const (
|
||||
StoreRoleBoss = "StoreBoss"
|
||||
|
||||
RoleNameSep = "/"
|
||||
RolePrefix = "role"
|
||||
StoreRolePrefix = "store"
|
||||
RolePrefix = "Role"
|
||||
StoreRolePrefix = "Store"
|
||||
)
|
||||
|
||||
func GenStoreRoleName(storeID int) (roleName string) {
|
||||
return strings.Join([]string{
|
||||
type RoleInfo struct {
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
StoreID int `json:"storeID,omitempty"` // 如果这个值非0,表示门店角色
|
||||
}
|
||||
|
||||
var (
|
||||
storeRoleDescriptionMap = map[string]string{
|
||||
StoreRoleBoss: "门店老板",
|
||||
}
|
||||
StoreRoleList = []*RoleInfo{
|
||||
&RoleInfo{
|
||||
Name: StoreRoleBoss,
|
||||
Description: storeRoleDescriptionMap[StoreRoleBoss],
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
func GetRoleDescription(name string, storeID int) (description string) {
|
||||
if storeID != 0 {
|
||||
description = storeRoleDescriptionMap[name]
|
||||
} else {
|
||||
if confList, err := dao.QueryConfigs(dao.GetDB(), name, model.ConfigTypeRole, ""); err == nil && len(confList) > 0 {
|
||||
description = confList[0].Value
|
||||
}
|
||||
}
|
||||
return description
|
||||
}
|
||||
|
||||
func ValidateRole(name string, storeID int) (err error) {
|
||||
if GetRoleDescription(name, storeID) == "" {
|
||||
err = fmt.Errorf("角色%s不合法", name)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func NewRole(name string, storeID int) (r *RoleInfo) {
|
||||
r = &RoleInfo{
|
||||
Name: name,
|
||||
StoreID: storeID,
|
||||
Description: GetRoleDescription(name, storeID),
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
func NewRoleByModel(conf *model.NewConfig) (r *RoleInfo) {
|
||||
if conf.Type != model.ConfigTypeRole {
|
||||
r = NewRole(conf.Key, 0)
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
func NewRoleByFullName(fullRoleName string) (r *RoleInfo) {
|
||||
strList := strings.Split(fullRoleName, RoleNameSep)
|
||||
if len(strList) == 2 {
|
||||
r = NewRole(strList[1], 0)
|
||||
} else if len(strList) == 4 {
|
||||
r = NewRole(strList[1], int(utils.Str2Int64(strList[3])))
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
func (r *RoleInfo) GetFullName() (fullRoleName string) {
|
||||
strList := []string{
|
||||
RolePrefix,
|
||||
StoreRolePrefix,
|
||||
utils.Int2Str(storeID),
|
||||
}, RoleNameSep)
|
||||
r.Name,
|
||||
}
|
||||
if r.StoreID > 0 {
|
||||
strList = append(strList, StoreRolePrefix, utils.Int2Str(r.StoreID))
|
||||
}
|
||||
fullRoleName = strings.Join(strList, RoleNameSep)
|
||||
return fullRoleName
|
||||
}
|
||||
|
||||
func RoleName2StoreID(roleName string) (storeID int) {
|
||||
if list := strings.Split(roleName, RoleNameSep); len(list) == 4 {
|
||||
storeID = int(utils.Str2Int64(list[2]))
|
||||
}
|
||||
return storeID
|
||||
}
|
||||
|
||||
func GetStoreIDFromRole(roleName string) (storeID int) {
|
||||
@@ -27,3 +105,30 @@ func GetStoreIDFromRole(roleName string) (storeID int) {
|
||||
}
|
||||
return storeID
|
||||
}
|
||||
|
||||
func RoleList2StoreIDList(roleList []*RoleInfo) (storeIDs []int) {
|
||||
for _, v := range roleList {
|
||||
if v.StoreID > 0 {
|
||||
storeIDs = append(storeIDs, v.StoreID)
|
||||
}
|
||||
}
|
||||
return storeIDs
|
||||
}
|
||||
|
||||
func FullRoleName2RoleList(fullRoleNameList []string) (roleList []*RoleInfo) {
|
||||
if len(fullRoleNameList) > 0 {
|
||||
roleList = make([]*RoleInfo, len(fullRoleNameList))
|
||||
for k, v := range fullRoleNameList {
|
||||
roleList[k] = NewRoleByFullName(v)
|
||||
}
|
||||
}
|
||||
return roleList
|
||||
}
|
||||
|
||||
func RoleList2Map(roleList []*RoleInfo) (roleMap map[string]*RoleInfo) {
|
||||
roleMap = make(map[string]*RoleInfo)
|
||||
for _, v := range roleList {
|
||||
roleMap[v.Name] = v
|
||||
}
|
||||
return roleMap
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package casbinauth
|
||||
|
||||
import (
|
||||
"git.rosy.net.cn/jx-callback/business/authz"
|
||||
"git.rosy.net.cn/jx-callback/globals"
|
||||
"github.com/casbin/casbin"
|
||||
)
|
||||
|
||||
@@ -15,30 +16,30 @@ func New(modelFile string) (authObj authz.IAuthz, err error) {
|
||||
return obj, err
|
||||
}
|
||||
|
||||
func (c *CasbinAuthz) AddStoreRole4User(userID string, storeID int) (err error) {
|
||||
return c.AddRole4User(userID, authz.GenStoreRoleName(storeID))
|
||||
}
|
||||
|
||||
func (c *CasbinAuthz) DelStoreRole4User(userID string, storeID int) (err error) {
|
||||
return c.DelRole4User(userID, authz.GenStoreRoleName(storeID))
|
||||
}
|
||||
|
||||
func (c *CasbinAuthz) AddRole4User(userID, roleName string) (err error) {
|
||||
_, err = c.enforcer.AddRoleForUser(userID, roleName)
|
||||
func (c *CasbinAuthz) AddRole4User(userID string, r *authz.RoleInfo) (err error) {
|
||||
_, err = c.enforcer.AddRoleForUser(userID, r.GetFullName())
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *CasbinAuthz) DelRole4User(userID, roleName string) (err error) {
|
||||
_, err = c.enforcer.DeleteRoleForUser(userID, roleName)
|
||||
func (c *CasbinAuthz) DeleteRole4User(userID string, r *authz.RoleInfo) (err error) {
|
||||
_, err = c.enforcer.DeleteRoleForUser(userID, r.GetFullName())
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *CasbinAuthz) GetUserRoleList(userID string) (roleList []string, err error) {
|
||||
roleList, err = c.enforcer.GetRolesForUser(userID)
|
||||
func (c *CasbinAuthz) GetUserRoleList(userID string) (roleList []*authz.RoleInfo, err error) {
|
||||
roleNameList, err := c.enforcer.GetRolesForUser(userID)
|
||||
if err == nil && len(roleNameList) > 0 {
|
||||
roleList = authz.FullRoleName2RoleList(roleNameList)
|
||||
}
|
||||
return roleList, err
|
||||
}
|
||||
|
||||
func (c *CasbinAuthz) GetRoleUserList(roleName string) (userIDList []string, err error) {
|
||||
userIDList, err = c.enforcer.GetUsersForRole(roleName)
|
||||
func (c *CasbinAuthz) GetRoleUserList(r *authz.RoleInfo) (userIDList []string, err error) {
|
||||
globals.SugarLogger.Debug(r.GetFullName())
|
||||
userIDList, err = c.enforcer.GetUsersForRole(r.GetFullName())
|
||||
return userIDList, err
|
||||
}
|
||||
|
||||
// func (c *CasbinAuthz) GetAllRoleList() (roleList []*authz.RoleInfo) {
|
||||
// return authz.FullRoleName2RoleList(c.enforcer.GetAllRoles())
|
||||
// }
|
||||
|
||||
Reference in New Issue
Block a user