- 重构authz结构

- 角色管理初版完成
This commit is contained in:
gazebo
2019-08-08 17:06:58 +08:00
parent 60f3ec9c3b
commit b51614946f
18 changed files with 161 additions and 98 deletions

View File

@@ -1,5 +1,37 @@
package authz
import (
"strings"
"git.rosy.net.cn/baseapi/utils"
)
const (
StoreRoleBoss = "StoreBoss"
RoleNameSep = "/"
RolePrefix = "Role"
StoreRolePrefix = "Store"
)
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],
},
}
)
type IAuthz interface {
AddRole4User(userID string, r *RoleInfo) (err error)
DeleteRole4User(userID string, r *RoleInfo) (err error)
@@ -7,3 +39,15 @@ type IAuthz interface {
GetRoleUserList(r *RoleInfo) (userIDList []string, err error)
// GetAllRoleList() (roleList []*RoleInfo)
}
func (r *RoleInfo) GetFullName() (fullRoleName string) {
strList := []string{
RolePrefix,
r.Name,
}
if r.StoreID > 0 {
strList = append(strList, StoreRolePrefix, utils.Int2Str(r.StoreID))
}
fullRoleName = strings.Join(strList, RoleNameSep)
return fullRoleName
}

View File

@@ -1,43 +1,18 @@
package authz
package autils
import (
"fmt"
"strings"
"git.rosy.net.cn/baseapi/utils"
"git.rosy.net.cn/jx-callback/business/authz"
"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"
)
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]
description = authz.StoreRoleDescriptionMap[name]
} else {
if confList, err := dao.QueryConfigs(dao.GetDB(), name, model.ConfigTypeRole, ""); err == nil && len(confList) > 0 {
description = confList[0].Value
@@ -53,8 +28,8 @@ func ValidateRole(name string, storeID int) (err error) {
return err
}
func NewRole(name string, storeID int) (r *RoleInfo) {
r = &RoleInfo{
func NewRole(name string, storeID int) (r *authz.RoleInfo) {
r = &authz.RoleInfo{
Name: name,
StoreID: storeID,
Description: GetRoleDescription(name, storeID),
@@ -62,15 +37,15 @@ func NewRole(name string, storeID int) (r *RoleInfo) {
return r
}
func NewRoleByModel(conf *model.NewConfig) (r *RoleInfo) {
func NewRoleByModel(conf *model.NewConfig) (r *authz.RoleInfo) {
if conf.Type != model.ConfigTypeRole {
r = NewRole(conf.Key, 0)
}
return r
}
func NewRoleByFullName(fullRoleName string) (r *RoleInfo) {
strList := strings.Split(fullRoleName, RoleNameSep)
func NewRoleByFullName(fullRoleName string) (r *authz.RoleInfo) {
strList := strings.Split(fullRoleName, authz.RoleNameSep)
if len(strList) == 2 {
r = NewRole(strList[1], 0)
} else if len(strList) == 4 {
@@ -79,34 +54,22 @@ func NewRoleByFullName(fullRoleName string) (r *RoleInfo) {
return r
}
func (r *RoleInfo) GetFullName() (fullRoleName string) {
strList := []string{
RolePrefix,
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 {
if list := strings.Split(roleName, authz.RoleNameSep); len(list) == 4 {
storeID = int(utils.Str2Int64(list[2]))
}
return storeID
}
func GetStoreIDFromRole(roleName string) (storeID int) {
list := strings.Split(roleName, RoleNameSep)
list := strings.Split(roleName, authz.RoleNameSep)
if len(list) == 3 {
storeID = int(utils.Str2Int64WithDefault(list[2], 0))
}
return storeID
}
func RoleList2StoreIDList(roleList []*RoleInfo) (storeIDs []int) {
func RoleList2StoreIDList(roleList []*authz.RoleInfo) (storeIDs []int) {
for _, v := range roleList {
if v.StoreID > 0 {
storeIDs = append(storeIDs, v.StoreID)
@@ -115,9 +78,9 @@ func RoleList2StoreIDList(roleList []*RoleInfo) (storeIDs []int) {
return storeIDs
}
func FullRoleName2RoleList(fullRoleNameList []string) (roleList []*RoleInfo) {
func FullRoleName2RoleList(fullRoleNameList []string) (roleList []*authz.RoleInfo) {
if len(fullRoleNameList) > 0 {
roleList = make([]*RoleInfo, len(fullRoleNameList))
roleList = make([]*authz.RoleInfo, len(fullRoleNameList))
for k, v := range fullRoleNameList {
roleList[k] = NewRoleByFullName(v)
}
@@ -125,8 +88,8 @@ func FullRoleName2RoleList(fullRoleNameList []string) (roleList []*RoleInfo) {
return roleList
}
func RoleList2Map(roleList []*RoleInfo) (roleMap map[string]*RoleInfo) {
roleMap = make(map[string]*RoleInfo)
func RoleList2Map(roleList []*authz.RoleInfo) (roleMap map[string]*authz.RoleInfo) {
roleMap = make(map[string]*authz.RoleInfo)
for _, v := range roleList {
roleMap[v.Name] = v
}

View File

@@ -1,30 +1,16 @@
package casbinauth
import (
jxmodel "git.rosy.net.cn/jx-callback/business/model"
"git.rosy.net.cn/jx-callback/globals"
"github.com/astaxie/beego/orm"
"github.com/casbin/casbin/model"
"github.com/casbin/casbin/persist"
)
type CasbinRule struct {
ID int `orm:"column(id)" json:"id"`
PType string
V0 string
V1 string
V2 string
V3 string
V4 string
V5 string
}
type Adapter struct {
}
func RegisterModel() {
orm.RegisterModel(new(CasbinRule))
}
// finalizer is the destructor for Adapter.
func finalizer(a *Adapter) {
}
@@ -33,7 +19,7 @@ func NewAdapter() *Adapter {
return &Adapter{}
}
func loadPolicyLine(line CasbinRule, model model.Model) {
func loadPolicyLine(line jxmodel.CasbinRule, model model.Model) {
lineText := line.PType
if line.V0 != "" {
lineText += ", " + line.V0
@@ -58,7 +44,7 @@ func loadPolicyLine(line CasbinRule, model model.Model) {
}
func (a *Adapter) LoadPolicy(model model.Model) error {
var lines []CasbinRule
var lines []jxmodel.CasbinRule
o := orm.NewOrm()
_, err := o.QueryTable("casbin_rule").Limit(-1).All(&lines)
if err != nil {
@@ -71,8 +57,8 @@ func (a *Adapter) LoadPolicy(model model.Model) error {
return nil
}
func savePolicyLine(ptype string, rule []string) CasbinRule {
line := CasbinRule{}
func savePolicyLine(ptype string, rule []string) jxmodel.CasbinRule {
line := jxmodel.CasbinRule{}
line.PType = ptype
if len(rule) > 0 {
@@ -111,7 +97,7 @@ func (a *Adapter) SavePolicy(model model.Model) error {
o := orm.NewOrm()
a.clearAll(o)
var lines []CasbinRule
var lines []jxmodel.CasbinRule
for ptype, ast := range model["p"] {
for _, rule := range ast.Policy {
@@ -149,7 +135,7 @@ func (a *Adapter) RemovePolicy(sec string, ptype string, rule []string) error {
// RemoveFilteredPolicy removes policy rules that match the filter from the storage.
func (a *Adapter) RemoveFilteredPolicy(sec string, ptype string, fieldIndex int, fieldValues ...string) error {
line := CasbinRule{}
line := jxmodel.CasbinRule{}
line.PType = ptype
filter := []string{}

View File

@@ -2,6 +2,7 @@ package casbinauth
import (
"git.rosy.net.cn/jx-callback/business/authz"
"git.rosy.net.cn/jx-callback/business/authz/autils"
"git.rosy.net.cn/jx-callback/globals"
"github.com/casbin/casbin"
)
@@ -29,7 +30,7 @@ func (c *CasbinAuthz) DeleteRole4User(userID string, r *authz.RoleInfo) (err err
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)
roleList = autils.FullRoleName2RoleList(roleNameList)
}
return roleList, err
}

View File

@@ -4,6 +4,7 @@ import (
"git.rosy.net.cn/jx-callback/business/auth2"
"git.rosy.net.cn/jx-callback/business/auth2/authprovider/weixin"
"git.rosy.net.cn/jx-callback/business/authz"
"git.rosy.net.cn/jx-callback/business/authz/autils"
"git.rosy.net.cn/jx-callback/business/model"
"git.rosy.net.cn/jx-callback/business/model/dao"
"git.rosy.net.cn/jx-callback/business/model/legacymodel"
@@ -92,7 +93,7 @@ func TransferLegacyWeixins() (err error) {
})
}
if v.JxStoreID > 0 && user.Type&model.UserTypeOperator == 0 { // 运营就不加到门店老板组里了
api2.RoleMan.AddRole4User(user.GetID(), authz.NewRole(authz.StoreRoleBoss, v.JxStoreID))
api2.RoleMan.AddRole4User(user.GetID(), autils.NewRole(authz.StoreRoleBoss, v.JxStoreID))
}
}
}

View File

@@ -7,6 +7,7 @@ import (
"git.rosy.net.cn/jx-callback/globals/api2"
"git.rosy.net.cn/jx-callback/business/authz"
"git.rosy.net.cn/jx-callback/business/authz/autils"
_ "git.rosy.net.cn/jx-callback/business/partner/purchase/ebai"
_ "git.rosy.net.cn/jx-callback/business/partner/purchase/elm"
_ "git.rosy.net.cn/jx-callback/business/partner/purchase/jd"
@@ -22,7 +23,7 @@ func TestTransferLegacyWeixins(t *testing.T) {
}
func TestCasbin(t *testing.T) {
userList, err := api2.RoleMan.GetRoleUserList(authz.NewRole(authz.StoreRoleBoss, 100324))
userList, err := api2.RoleMan.GetRoleUserList(autils.NewRole(authz.StoreRoleBoss, 100324))
t.Log(utils.Format4Output(userList, false))
if err != nil {
t.Fatal(err)

View File

@@ -6,12 +6,14 @@ import (
"strings"
"time"
"git.rosy.net.cn/baseapi/utils/errlist"
"git.rosy.net.cn/jx-callback/globals/api2"
"git.rosy.net.cn/baseapi/platformapi/dingdingapi"
"git.rosy.net.cn/jx-callback/business/auth2/authprovider/mobile"
"git.rosy.net.cn/jx-callback/business/authz"
"git.rosy.net.cn/jx-callback/business/authz/autils"
"git.rosy.net.cn/jx-callback/business/jxutils/msg"
"git.rosy.net.cn/jx-callback/business/partner"
@@ -259,15 +261,25 @@ func DeleteConfig(ctx *jxcontext.Context, key, configType string) (err error) {
}
}
case model.ConfigTypeRole:
userIDs, err2 := api2.RoleMan.GetRoleUserList(authz.NewRole(key, 0))
if err = err2; err == nil {
if len(userIDs) > 0 {
userList, err2 := GetUsers(ctx, 0, "", userIDs, "", "")
if err = err2; err == nil {
err = fmt.Errorf("还有人员在使用角色:%s人员信息:%s", key, utils.MustMarshal(utils.Struct2Map(userList, "compact")))
}
errList := errlist.New()
userIDs, err2 := api2.RoleMan.GetRoleUserList(autils.NewRole(key, 0))
if err = err2; err == nil && len(userIDs) > 0 {
userList, err2 := GetUsers(ctx, 0, "", userIDs, "", "")
if err = err2; err == nil {
err = fmt.Errorf("还有人员在使用角色:%s人员信息:%s", key, utils.MustMarshal(utils.Struct2Map(userList, "compact")))
}
}
errList.AddErr(err)
storeList, err2 := dao.GetStoreList4Role(db, key)
if err = err2; err == nil && len(storeList) > 0 {
storeIDs := make([]int, len(storeList))
for k, v := range storeList {
storeIDs[k] = v.ID
}
err = fmt.Errorf("还有门店在使用角色:%s门店信息:%s", key, utils.MustMarshal(storeIDs))
}
errList.AddErr(err)
err = errList.GetErrListAsOne()
}
if err == nil {
_, err = dao.DeleteEntityLogically(db, &model.NewConfig{}, nil, ctx.GetUserName(), map[string]interface{}{

View File

@@ -5,6 +5,7 @@ import (
"testing"
"git.rosy.net.cn/jx-callback/business/jxutils/jxcontext"
"git.rosy.net.cn/jx-callback/globals/api2"
"git.rosy.net.cn/jx-callback/globals/testinit"
_ "git.rosy.net.cn/jx-callback/business/partner/purchase/ebai"
@@ -16,6 +17,7 @@ import (
func init() {
testinit.Init()
api2.Init()
}
func TestGetQiniuUploadToken(t *testing.T) {

View File

@@ -532,6 +532,10 @@ func UpdateStore(ctx *jxcontext.Context, storeID int, payload map[string]interfa
}
valid := dao.StrictMakeMapByStructObject(payload, store, userName)
if err = dao.ValidateRoles(db, utils.Interface2String(valid["marketManRole"]), utils.Interface2String(valid["OperatorRole"])); err != nil {
return 0, err
}
if payload["lng"] != nil || payload["lat"] != nil {
intLng := jxutils.StandardCoordinate2Int(utils.Interface2Float64WithDefault(payload["lng"], 0.0))
intLat := jxutils.StandardCoordinate2Int(utils.Interface2Float64WithDefault(payload["lat"], 0.0))
@@ -708,7 +712,10 @@ func CreateStore(ctx *jxcontext.Context, storeExt *StoreExt, userName string) (i
if store.ID != 0 && !jxutils.IsLegalStoreID(store.ID) {
return 0, fmt.Errorf("ID:%d不是合法的京西门店编号", store.ID)
}
db := dao.GetDB()
if err = dao.ValidateRoles(db, store.MarketManRole, store.OperatorRole); err != nil {
return 0, err
}
existingID := store.ID
store.Lng = jxutils.StandardCoordinate2Int(storeExt.FloatLng)
store.Lat = jxutils.StandardCoordinate2Int(storeExt.FloatLat)
@@ -736,7 +743,7 @@ func CreateStore(ctx *jxcontext.Context, storeExt *StoreExt, userName string) (i
}
dao.WrapAddIDCULDEntity(store, userName)
store.ID = existingID
if err = dao.CreateEntity(nil, store); err == nil {
if err = dao.CreateEntity(db, store); err == nil {
UpdateOrCreateCourierStores(ctx, store.ID, false, false, false)
BindMobile2Store(ctx, storeExt.Tel1, storeExt.ID)
return store.ID, err

View File

@@ -5,7 +5,7 @@ import (
)
func TestCheckSkuDiffBetweenJxAndVendor(t *testing.T) {
vendorIDList := []string{}
storeIDList := []string{}
vendorIDList := []int{}
storeIDList := []int{}
CheckSkuDiffBetweenJxAndVendor(vendorIDList, storeIDList)
}
}

View File

@@ -11,6 +11,7 @@ import (
"git.rosy.net.cn/jx-callback/business/auth2"
"git.rosy.net.cn/jx-callback/business/auth2/authprovider/dingding"
"git.rosy.net.cn/jx-callback/business/authz"
"git.rosy.net.cn/jx-callback/business/authz/autils"
"git.rosy.net.cn/jx-callback/business/jxutils/jxcontext"
"git.rosy.net.cn/jx-callback/business/model"
"git.rosy.net.cn/jx-callback/business/model/dao"
@@ -205,7 +206,7 @@ func GetRoleUserList(ctx *jxcontext.Context, r *authz.RoleInfo) (userIDList []st
func AddRoles4User(ctx *jxcontext.Context, userID string, rList []*authz.RoleInfo) (err error) {
errList := errlist.New()
for _, v := range rList {
if err = authz.ValidateRole(v.Name, v.StoreID); err == nil {
if err = autils.ValidateRole(v.Name, v.StoreID); err == nil {
if err = api2.RoleMan.AddRole4User(userID, v); err != nil {
errList.AddErr(err)
}
@@ -227,7 +228,7 @@ func DeleteRoles4User(ctx *jxcontext.Context, userID string, rList []*authz.Role
}
func AddUsers4Role(ctx *jxcontext.Context, r *authz.RoleInfo, userIDList []string) (err error) {
if err = authz.ValidateRole(r.Name, r.StoreID); err != nil {
if err = autils.ValidateRole(r.Name, r.StoreID); err != nil {
return err
}

View File

@@ -0,0 +1,12 @@
package model
type CasbinRule struct {
ID int `orm:"column(id)" json:"id"`
PType string
V0 string
V1 string
V2 string
V3 string
V4 string
V5 string
}

View File

@@ -3,6 +3,8 @@ package dao
import (
"fmt"
"git.rosy.net.cn/baseapi/utils/errlist"
"git.rosy.net.cn/baseapi/utils"
"git.rosy.net.cn/jx-callback/business/model"
)
@@ -36,3 +38,14 @@ func QueryConfigs(db *DaoDB, key, configType, keyword string) (configList []*mod
}
return configList, err
}
func ValidateRoles(db *DaoDB, roles ...string) (err error) {
errList := errlist.New()
for _, v := range roles {
if v != "" {
_, err2 := QueryConfigs(db, v, model.ConfigTypeRole, "")
errList.AddErr(err2)
}
}
return errList.GetErrListAsOne()
}

View File

@@ -430,3 +430,17 @@ func GetOpenedStoreCouriersByStoreID(db *DaoDB, storeID, vendorID int) (storeMap
}
return storeMaps, nil
}
func GetStoreList4Role(db *DaoDB, shortRoleName string) (storeList []*model.Store, err error) {
sql := `
SELECT t1.*
FROM store t1
WHERE t1.deleted_at = ? AND (t1.market_man_role = ? OR t1.operator_role = ?)`
sqlParams := []interface{}{
utils.DefaultTimeValue,
shortRoleName,
shortRoleName,
}
err = GetRows(db, &storeList, sql, sqlParams...)
return storeList, err
}

View File

@@ -30,3 +30,11 @@ func TestFormalizeStoreStatus(t *testing.T) {
t.Fatal(err)
}
}
func TestGetStoreList4Role(t *testing.T) {
storeList, err := GetStoreList4Role(GetDB(), "NiuBi")
t.Log(utils.Format4Output(storeList, false))
if err != nil {
t.Fatal(err)
}
}

View File

@@ -3,6 +3,7 @@ package controllers
import (
"git.rosy.net.cn/jx-callback/business/auth2"
"git.rosy.net.cn/jx-callback/business/authz"
"git.rosy.net.cn/jx-callback/business/authz/autils"
"git.rosy.net.cn/jx-callback/business/jxstore/cms"
"git.rosy.net.cn/jx-callback/business/jxutils"
"git.rosy.net.cn/jx-callback/business/model"
@@ -123,7 +124,7 @@ func (c *User2Controller) GetUserRoleList() {
// @router /GetRoleUserList [get]
func (c *User2Controller) GetRoleUserList() {
c.callGetRoleUserList(func(params *tUser2GetRoleUserListParams) (retVal interface{}, errCode string, err error) {
retVal, err = cms.GetRoleUserList(params.Ctx, authz.NewRole(params.RoleName, params.StoreID))
retVal, err = cms.GetRoleUserList(params.Ctx, autils.NewRole(params.RoleName, params.StoreID))
return retVal, "", err
})
}
@@ -177,7 +178,7 @@ func (c *User2Controller) AddUsers4Role() {
c.callAddUsers4Role(func(params *tUser2AddUsers4RoleParams) (retVal interface{}, errCode string, err error) {
var userIDs []string
if err = jxutils.Strings2Objs(params.UserIDs, &userIDs); err == nil {
err = cms.AddUsers4Role(params.Ctx, authz.NewRole(params.RoleName, params.StoreID), userIDs)
err = cms.AddUsers4Role(params.Ctx, autils.NewRole(params.RoleName, params.StoreID), userIDs)
}
return retVal, "", err
})
@@ -196,7 +197,7 @@ func (c *User2Controller) DeleteUsers4Role() {
c.callDeleteUsers4Role(func(params *tUser2DeleteUsers4RoleParams) (retVal interface{}, errCode string, err error) {
var userIDs []string
if err = jxutils.Strings2Objs(params.UserIDs, &userIDs); err == nil {
err = cms.DeleteUsers4Role(params.Ctx, authz.NewRole(params.RoleName, params.StoreID), userIDs)
err = cms.DeleteUsers4Role(params.Ctx, autils.NewRole(params.RoleName, params.StoreID), userIDs)
}
return retVal, "", err
})

View File

@@ -1,7 +1,6 @@
package beegodb
import (
"git.rosy.net.cn/jx-callback/business/authz/casbinauth"
"git.rosy.net.cn/jx-callback/business/model"
"git.rosy.net.cn/jx-callback/business/model/legacymodel"
"git.rosy.net.cn/jx-callback/globals"
@@ -53,7 +52,7 @@ func Init() {
orm.RegisterModel(&model.ActMap{}, &model.ActStoreSkuMap{})
orm.RegisterModel(&model.NewConfig{})
casbinauth.RegisterModel()
orm.RegisterModel(&model.CasbinRule{})
// create table
orm.RunSyncdb("default", false, true)
}

View File

@@ -2,12 +2,10 @@ package testinit
import (
"git.rosy.net.cn/jx-callback/globals/api"
"git.rosy.net.cn/jx-callback/globals/api2"
"git.rosy.net.cn/jx-callback/globals/testinit1"
)
func Init() {
testinit1.Init()
api.Init()
api2.Init()
}