cahce.go
3.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package cachex
import (
"fmt"
"github.com/tiptok/gocomm/pkg/cache"
"github.com/tiptok/gocomm/pkg/log"
"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/application/factory"
"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/infrastructure/dao"
"regexp"
"strings"
)
//redis key
func UserRoleAccessCacheKey(userId int64) string {
return fmt.Sprintf("godevp:user:access:%v", userId)
}
type CacheService struct {
}
func (svr *CacheService) CacheUserAccess(userId int64) func() (interface{}, error) {
return func() (interface{}, error) {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
UserRepository, _ = factory.CreateUserRepository(transactionContext)
RoleAccessDao, _ = dao.NewRoleAccessDao(transactionContext)
AccessRepository, _ = factory.CreateAccessRepository(transactionContext)
err error
)
if err = transactionContext.StartTransaction(); err != nil {
log.Error(err)
return nil, err
}
defer func() {
transactionContext.RollbackTransaction()
}()
var user *domain.User
if user, err = UserRepository.FindOne(map[string]interface{}{"id": userId}); err != nil {
return []*domain.Access{}, nil
}
accessIds, _ := RoleAccessDao.GetRoleAccess(user.Roles...)
if len(accessIds) == 0 {
return []*domain.Access{}, nil
}
_, accesses, _ := AccessRepository.Find(map[string]interface{}{"inAccessIds": accessIds})
err = transactionContext.CommitTransaction()
return accesses, nil
}
}
func (svr *CacheService) ValidUserAccess(userId int64, object string, method string) (result bool, err error) {
var userAccess []*domain.Access
defer func() {
log.Info(fmt.Sprintf("ValidUserAccess user:%v object:%v method:%v result:%v", userId, object, method, result))
}()
cache.GetObject(UserRoleAccessCacheKey(userId), &userAccess, 3600, svr.CacheUserAccess(userId))
for i := range userAccess {
if KeyMatch3(object, userAccess[i].Object) && KeyEqual(userAccess[i].Action, method) {
result = true
return
}
}
return
}
// KeyMatch3 determines whether key1 matches the pattern of key2 (similar to RESTful path), key2 can contain a *.
// For example, "/foo/bar" matches "/foo/*", "/resource1" matches "/{resource}"
func KeyMatch3(key1 string, key2 string) bool {
key2 = strings.Replace(key2, "/*", "/.*", -1)
re := regexp.MustCompile(`\{[^/]+\}`)
key2 = re.ReplaceAllString(key2, "$1[^/]+$2")
return RegexMatch(key1, "^"+key2+"$")
}
// KeyEqual case key1='*' or key1=' ' result=true
func KeyEqual(key1 string, key2 string) bool {
if key1 == "*" {
return true
}
if len(key1) == 0 {
return true
}
key1 = strings.ToLower(strings.TrimSpace(key1))
key2 = strings.ToLower(strings.TrimSpace(key2))
return strings.EqualFold(key1, key2)
}
// RegexMatch determines whether key1 matches the pattern of key2 in regular expression.
func RegexMatch(key1 string, key2 string) bool {
res, err := regexp.MatchString(key2, key1)
if err != nil {
panic(err)
}
return res
}