2021-03-24 17:18:39 +08:00
|
|
|
|
package ctx
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2021-06-09 16:58:57 +08:00
|
|
|
|
"fmt"
|
2021-03-24 17:18:39 +08:00
|
|
|
|
"mayfly-go/base/biz"
|
2021-06-09 16:58:57 +08:00
|
|
|
|
"mayfly-go/base/cache"
|
|
|
|
|
|
"time"
|
2021-03-24 17:18:39 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2021-05-08 18:00:33 +08:00
|
|
|
|
type Permission struct {
|
2021-06-09 16:58:57 +08:00
|
|
|
|
NeedToken bool // 是否需要token
|
|
|
|
|
|
Code string // 权限code
|
2021-03-24 17:18:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-06-09 16:58:57 +08:00
|
|
|
|
func NewPermission(code string) *Permission {
|
|
|
|
|
|
return &Permission{NeedToken: true, Code: code}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (p *Permission) WithNeedToken(needToken bool) *Permission {
|
|
|
|
|
|
p.NeedToken = needToken
|
|
|
|
|
|
return p
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type PermissionCodeRegistry interface {
|
|
|
|
|
|
// 保存用户权限code
|
|
|
|
|
|
SaveCodes(userId uint64, codes []string)
|
|
|
|
|
|
|
|
|
|
|
|
// 判断用户是否拥有该code的权限
|
|
|
|
|
|
HasCode(userId uint64, code string) bool
|
|
|
|
|
|
|
|
|
|
|
|
Remove(userId uint64)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type DefaultPermissionCodeRegistry struct {
|
|
|
|
|
|
cache *cache.TimedCache
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (r *DefaultPermissionCodeRegistry) SaveCodes(userId uint64, codes []string) {
|
|
|
|
|
|
if r.cache == nil {
|
|
|
|
|
|
r.cache = cache.NewTimedCache(30*time.Minute, 5*time.Second).WithUpdateAccessTime(true)
|
|
|
|
|
|
}
|
|
|
|
|
|
r.cache.Put(fmt.Sprintf("%v", userId), codes)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (r *DefaultPermissionCodeRegistry) HasCode(userId uint64, code string) bool {
|
|
|
|
|
|
if r.cache == nil {
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|
|
|
|
|
|
codes, found := r.cache.Get(fmt.Sprintf("%v", userId))
|
|
|
|
|
|
if !found {
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|
|
|
|
|
|
for _, v := range codes.([]string) {
|
|
|
|
|
|
if v == code {
|
|
|
|
|
|
return true
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (r *DefaultPermissionCodeRegistry) Remove(userId uint64) {
|
|
|
|
|
|
r.cache.Delete(fmt.Sprintf("%v", userId))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 保存用户权限code
|
|
|
|
|
|
func SavePermissionCodes(userId uint64, codes []string) {
|
|
|
|
|
|
permissionCodeRegistry.SaveCodes(userId, codes)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 删除用户权限code
|
|
|
|
|
|
func DeletePermissionCodes(userId uint64) {
|
|
|
|
|
|
permissionCodeRegistry.Remove(userId)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 设置权限code注册器
|
|
|
|
|
|
func SetPermissionCodeRegistery(pcr PermissionCodeRegistry) {
|
|
|
|
|
|
permissionCodeRegistry = pcr
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
|
permissionCodeRegistry PermissionCodeRegistry = &DefaultPermissionCodeRegistry{}
|
|
|
|
|
|
permissionError = biz.NewBizErrCode(biz.TokenErrorCode, biz.TokenErrorMsg)
|
|
|
|
|
|
)
|
2021-03-24 17:18:39 +08:00
|
|
|
|
|
2021-05-08 18:00:33 +08:00
|
|
|
|
func PermissionHandler(rc *ReqCtx) error {
|
2021-06-09 16:58:57 +08:00
|
|
|
|
permission := rc.RequiredPermission
|
|
|
|
|
|
// 如果需要的权限信息不为空,并且不需要token,则不返回错误,继续后续逻辑
|
|
|
|
|
|
if permission != nil && !permission.NeedToken {
|
2021-03-24 17:18:39 +08:00
|
|
|
|
return nil
|
|
|
|
|
|
}
|
2021-05-08 18:00:33 +08:00
|
|
|
|
tokenStr := rc.GinCtx.Request.Header.Get("Authorization")
|
2021-06-09 16:58:57 +08:00
|
|
|
|
// header不存在则从查询参数token中获取
|
2021-05-08 18:00:33 +08:00
|
|
|
|
if tokenStr == "" {
|
|
|
|
|
|
tokenStr = rc.GinCtx.Query("token")
|
|
|
|
|
|
}
|
2021-03-24 17:18:39 +08:00
|
|
|
|
if tokenStr == "" {
|
|
|
|
|
|
return permissionError
|
|
|
|
|
|
}
|
|
|
|
|
|
loginAccount, err := ParseToken(tokenStr)
|
|
|
|
|
|
if err != nil || loginAccount == nil {
|
|
|
|
|
|
return permissionError
|
|
|
|
|
|
}
|
2021-06-09 16:58:57 +08:00
|
|
|
|
// 权限不为nil,并且permission code不为空,则校验是否有权限code
|
|
|
|
|
|
if permission != nil && permission.Code != "" {
|
|
|
|
|
|
if !permissionCodeRegistry.HasCode(loginAccount.Id, permission.Code) {
|
|
|
|
|
|
return permissionError
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-03-24 17:18:39 +08:00
|
|
|
|
rc.LoginAccount = loginAccount
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|