正在显示
6 个修改的文件
包含
91 行增加
和
9 行删除
| @@ -28,8 +28,11 @@ func (this *BaseController) Prepare() { | @@ -28,8 +28,11 @@ func (this *BaseController) Prepare() { | ||
| 28 | this.Ctx.WriteString("") | 28 | this.Ctx.WriteString("") |
| 29 | return | 29 | return |
| 30 | } | 30 | } |
| 31 | - p := this.Ctx.Input.GetData("RouterPattern") | ||
| 32 | - fmt.Println("====>r:", p) | 31 | + // p := this.Ctx.Input.GetData("RouterPattern") |
| 32 | + // userid := this.GetUserId() | ||
| 33 | + // companyid := this.GetCompanyId() | ||
| 34 | + //权限校验 | ||
| 35 | + | ||
| 33 | } | 36 | } |
| 34 | 37 | ||
| 35 | func (this *BaseController) GetAppHead() (appHead protocol.BaseHeader) { | 38 | func (this *BaseController) GetAppHead() (appHead protocol.BaseHeader) { |
| @@ -117,6 +117,3 @@ var LogRouter = func(ctx *context.Context) { | @@ -117,6 +117,3 @@ var LogRouter = func(ctx *context.Context) { | ||
| 117 | } | 117 | } |
| 118 | 118 | ||
| 119 | //CheckOperation 检查操作权限,beforeController | 119 | //CheckOperation 检查操作权限,beforeController |
| 120 | -var CheckOperation = func(ctx *context.Context) { | ||
| 121 | - | ||
| 122 | -} |
| @@ -8,6 +8,7 @@ import ( | @@ -8,6 +8,7 @@ import ( | ||
| 8 | "oppmg/common/log" | 8 | "oppmg/common/log" |
| 9 | "oppmg/models" | 9 | "oppmg/models" |
| 10 | "oppmg/protocol" | 10 | "oppmg/protocol" |
| 11 | + serverbac "oppmg/services/rbac" | ||
| 11 | "oppmg/services/ucenter" | 12 | "oppmg/services/ucenter" |
| 12 | "oppmg/storage/redisdata" | 13 | "oppmg/storage/redisdata" |
| 13 | "oppmg/utils" | 14 | "oppmg/utils" |
| @@ -237,6 +238,8 @@ func LoginAuthByUCenter(account, password string) (protocol.LoginAuthToken, erro | @@ -237,6 +238,8 @@ func LoginAuthByUCenter(account, password string) (protocol.LoginAuthToken, erro | ||
| 237 | if err != nil { | 238 | if err != nil { |
| 238 | log.Error("更新用户数据失败:%s", err) | 239 | log.Error("更新用户数据失败:%s", err) |
| 239 | } | 240 | } |
| 241 | + | ||
| 242 | + InitPermission(usercompanyid) | ||
| 240 | return logintoken, err | 243 | return logintoken, err |
| 241 | } | 244 | } |
| 242 | 245 | ||
| @@ -474,6 +477,7 @@ func LoginAuthBySmsCode(phone string, code string) (protocol.LoginAuthToken, err | @@ -474,6 +477,7 @@ func LoginAuthBySmsCode(phone string, code string) (protocol.LoginAuthToken, err | ||
| 474 | if err != nil { | 477 | if err != nil { |
| 475 | log.Error("更新用户数据失败:%s", err) | 478 | log.Error("更新用户数据失败:%s", err) |
| 476 | } | 479 | } |
| 480 | + InitPermission(usercompanyid) | ||
| 477 | return logintoken, err | 481 | return logintoken, err |
| 478 | } | 482 | } |
| 479 | 483 | ||
| @@ -504,7 +508,42 @@ func SmsCodeCheck(phone string, code string) error { | @@ -504,7 +508,42 @@ func SmsCodeCheck(phone string, code string) error { | ||
| 504 | } | 508 | } |
| 505 | 509 | ||
| 506 | //InitPermission 登录时权限初始化 | 510 | //InitPermission 登录时权限初始化 |
| 507 | -func InitPermission(usercompanyid int, userid int64) error { | ||
| 508 | - | 511 | +func InitPermission(usercompanyid int64) error { |
| 512 | + var ( | ||
| 513 | + err error | ||
| 514 | + permissionMap map[string]serverbac.PermissionOptionObject | ||
| 515 | + ) | ||
| 516 | + permissionMap, err = serverbac.GetUserPermission(usercompanyid) | ||
| 517 | + if err != nil { | ||
| 518 | + log.Error("获取用户的权限失败") | ||
| 519 | + return err | ||
| 520 | + } | ||
| 521 | + err = redisdata.SetUserPermission(permissionMap, usercompanyid) | ||
| 522 | + if err != nil { | ||
| 523 | + log.Error("缓存用户权限失败:%s", err) | ||
| 524 | + } | ||
| 509 | return nil | 525 | return nil |
| 510 | } | 526 | } |
| 527 | + | ||
| 528 | +func ValidUserPermission(urlPath string, userid int64, companyid int64) bool { | ||
| 529 | + var ( | ||
| 530 | + err error | ||
| 531 | + permissionbase serverbac.PermissionBase | ||
| 532 | + ok bool = false | ||
| 533 | + permissionObj serverbac.PermissionOptionObject | ||
| 534 | + ) | ||
| 535 | + permissionbase, ok = serverbac.RouterPermission[urlPath] | ||
| 536 | + if !ok { | ||
| 537 | + return true | ||
| 538 | + } | ||
| 539 | + permissionObj, err = redisdata.GetUserPermission(userid, permissionbase.CodeName) | ||
| 540 | + if err != nil { | ||
| 541 | + log.Error("未取到权限数据") | ||
| 542 | + return false | ||
| 543 | + } | ||
| 544 | + ok = permissionObj.GetValidFunc(permissionbase.ActionName) | ||
| 545 | + if ok { | ||
| 546 | + return true | ||
| 547 | + } | ||
| 548 | + return false | ||
| 549 | +} |
| @@ -35,7 +35,7 @@ type PermissionBase struct { | @@ -35,7 +35,7 @@ type PermissionBase struct { | ||
| 35 | ActionName string | 35 | ActionName string |
| 36 | } | 36 | } |
| 37 | 37 | ||
| 38 | -var routerPermission = map[string]PermissionBase{ | 38 | +var RouterPermission = map[string]PermissionBase{ |
| 39 | "/v1/department/list": PermissionBase{CodeName: M_ENTERPRISE_ORGANIZATION, ActionName: "default"}, | 39 | "/v1/department/list": PermissionBase{CodeName: M_ENTERPRISE_ORGANIZATION, ActionName: "default"}, |
| 40 | "/v1/department/add": PermissionBase{CodeName: M_ENTERPRISE_ORGANIZATION, ActionName: "default"}, | 40 | "/v1/department/add": PermissionBase{CodeName: M_ENTERPRISE_ORGANIZATION, ActionName: "default"}, |
| 41 | "/v1/department/edit": PermissionBase{CodeName: M_ENTERPRISE_ORGANIZATION, ActionName: "default"}, | 41 | "/v1/department/edit": PermissionBase{CodeName: M_ENTERPRISE_ORGANIZATION, ActionName: "default"}, |
| @@ -133,7 +133,7 @@ func GetUserPermission(userCompanyid int64) (map[string]PermissionOptionObject, | @@ -133,7 +133,7 @@ func GetUserPermission(userCompanyid int64) (map[string]PermissionOptionObject, | ||
| 133 | if fn, ok := CodePermissionObject[v.Code]; ok { | 133 | if fn, ok := CodePermissionObject[v.Code]; ok { |
| 134 | obj := fn() | 134 | obj := fn() |
| 135 | if err = json.Unmarshal([]byte(v.Opption), obj); err != nil { | 135 | if err = json.Unmarshal([]byte(v.Opption), obj); err != nil { |
| 136 | - log.Debug("解析权限配置option 失败%s", err) | 136 | + log.Debug("解析权限配置option:%s %s失败%s", v.Code, v.Opption, err) |
| 137 | } | 137 | } |
| 138 | objMap[v.Code] = obj | 138 | objMap[v.Code] = obj |
| 139 | } else { | 139 | } else { |
| @@ -32,3 +32,8 @@ func GetKeyCaptchAuth(phone string) string { | @@ -32,3 +32,8 @@ func GetKeyCaptchAuth(phone string) string { | ||
| 32 | key := fmt.Sprintf("%s%s:%s", KEY_PREFIX, KEY_CAPTCHA_AUTH, phone) | 32 | key := fmt.Sprintf("%s%s:%s", KEY_PREFIX, KEY_CAPTCHA_AUTH, phone) |
| 33 | return key | 33 | return key |
| 34 | } | 34 | } |
| 35 | + | ||
| 36 | +func GetKeyUserPermission(userid int64) string { | ||
| 37 | + key := fmt.Sprintf("%s%s:%d", KEY_PREFIX, KEY_USER_PERMISSION, userid) | ||
| 38 | + return key | ||
| 39 | +} |
| @@ -2,10 +2,13 @@ package redisdata | @@ -2,10 +2,13 @@ package redisdata | ||
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | "encoding/json" | 4 | "encoding/json" |
| 5 | + "errors" | ||
| 5 | "oppmg/common/log" | 6 | "oppmg/common/log" |
| 6 | "oppmg/common/redis" | 7 | "oppmg/common/redis" |
| 7 | "oppmg/protocol" | 8 | "oppmg/protocol" |
| 9 | + "oppmg/services/rbac" | ||
| 8 | "strings" | 10 | "strings" |
| 11 | + "time" | ||
| 9 | ) | 12 | ) |
| 10 | 13 | ||
| 11 | func SetLoginToken(param protocol.LoginAuthToken, userid int64, companyid int64) error { | 14 | func SetLoginToken(param protocol.LoginAuthToken, userid int64, companyid int64) error { |
| @@ -96,3 +99,38 @@ func GetCaptchAuth(phone string) (string, error) { | @@ -96,3 +99,38 @@ func GetCaptchAuth(phone string) (string, error) { | ||
| 96 | r, err := client.Get(key).Result() | 99 | r, err := client.Get(key).Result() |
| 97 | return r, err | 100 | return r, err |
| 98 | } | 101 | } |
| 102 | + | ||
| 103 | +func SetUserPermission(objMap map[string]rbac.PermissionOptionObject, usercompanyid int64) error { | ||
| 104 | + key := GetKeyUserPermission(usercompanyid) | ||
| 105 | + client := redis.GetRedis() | ||
| 106 | + for k := range objMap { | ||
| 107 | + s, err := json.Marshal(objMap[k]) | ||
| 108 | + if err != nil { | ||
| 109 | + log.Error("解析错误:%s", err) | ||
| 110 | + continue | ||
| 111 | + } | ||
| 112 | + err = client.HSet(key, k, s).Err() | ||
| 113 | + if err != nil { | ||
| 114 | + log.Error("设置权限缓存失败:%s", err) | ||
| 115 | + } | ||
| 116 | + } | ||
| 117 | + client.Expire(key, 60*60*6*time.Second) | ||
| 118 | + return nil | ||
| 119 | +} | ||
| 120 | + | ||
| 121 | +func GetUserPermission(userid int64, field string) (rbac.PermissionOptionObject, error) { | ||
| 122 | + key := GetKeyUserPermission(userid) | ||
| 123 | + client := redis.GetRedis() | ||
| 124 | + str, err := client.HGet(key, field).Result() | ||
| 125 | + if err != nil { | ||
| 126 | + return nil, err | ||
| 127 | + } | ||
| 128 | + var permissionObj rbac.PermissionOptionObject | ||
| 129 | + fn, ok := rbac.CodePermissionObject[field] | ||
| 130 | + if !ok { | ||
| 131 | + return nil, errors.New("cannot get object") | ||
| 132 | + } | ||
| 133 | + permissionObj = fn() | ||
| 134 | + err = json.Unmarshal([]byte(str), permissionObj) | ||
| 135 | + return permissionObj, err | ||
| 136 | +} |
-
请 注册 或 登录 后发表评论