fix comment again
This commit is contained in:
parent
b2373bbada
commit
7c232c0434
18
action.go
18
action.go
|
@ -61,7 +61,11 @@ func (s Service) HasPermission(userID, permission string) bool {
|
||||||
|
|
||||||
// CreateRole ...
|
// CreateRole ...
|
||||||
func (s Service) CreateRole(payload model.RoleCreateOptions) error {
|
func (s Service) CreateRole(payload model.RoleCreateOptions) error {
|
||||||
return role.Create(payload)
|
err := role.Create(payload)
|
||||||
|
if err == nil {
|
||||||
|
role.CacheRoles()
|
||||||
|
}
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateRole ...
|
// UpdateRole ...
|
||||||
|
@ -82,12 +86,20 @@ func (s Service) GetAllRoles(query model.RoleAllQuery) model.RoleAll {
|
||||||
|
|
||||||
// CreatePermission ...
|
// CreatePermission ...
|
||||||
func (s Service) CreatePermission(payload model.PermissionCreateOptions) error {
|
func (s Service) CreatePermission(payload model.PermissionCreateOptions) error {
|
||||||
return permission.Create(payload)
|
err := permission.Create(payload)
|
||||||
|
if err == nil {
|
||||||
|
role.CacheRoles()
|
||||||
|
}
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdatePermission ...
|
// UpdatePermission ...
|
||||||
func (s Service) UpdatePermission(permissionID string, payload model.PermissionUpdateOptions) error {
|
func (s Service) UpdatePermission(permissionID string, payload model.PermissionUpdateOptions) error {
|
||||||
return permission.Update(permissionID, payload)
|
err := permission.Update(permissionID, payload)
|
||||||
|
if err == nil {
|
||||||
|
role.CacheRoles()
|
||||||
|
}
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetAllPermissions ...
|
// GetAllPermissions ...
|
||||||
|
|
|
@ -0,0 +1,25 @@
|
||||||
|
package cache
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/allegro/bigcache/v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
var cache *bigcache.BigCache
|
||||||
|
|
||||||
|
// Init ...
|
||||||
|
func Init() {
|
||||||
|
// The time after which entries can be evicted is 5 years
|
||||||
|
c, err := bigcache.NewBigCache(bigcache.DefaultConfig(43800 * time.Hour))
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Cannot init Cache %v", err)
|
||||||
|
}
|
||||||
|
cache = c
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetInstance ...
|
||||||
|
func GetInstance() *bigcache.BigCache {
|
||||||
|
return cache
|
||||||
|
}
|
4
go.mod
4
go.mod
|
@ -5,7 +5,10 @@ go 1.17
|
||||||
require (
|
require (
|
||||||
github.com/Selly-Modules/logger v0.0.0-20210809034923-140a51f39ec9
|
github.com/Selly-Modules/logger v0.0.0-20210809034923-140a51f39ec9
|
||||||
github.com/Selly-Modules/mongodb v0.0.0-20211013094205-a8ab24a96c4c
|
github.com/Selly-Modules/mongodb v0.0.0-20211013094205-a8ab24a96c4c
|
||||||
|
github.com/allegro/bigcache/v3 v3.0.1
|
||||||
|
github.com/thoas/go-funk v0.9.1
|
||||||
go.mongodb.org/mongo-driver v1.7.4
|
go.mongodb.org/mongo-driver v1.7.4
|
||||||
|
golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
@ -32,7 +35,6 @@ require (
|
||||||
go.uber.org/atomic v1.7.0 // indirect
|
go.uber.org/atomic v1.7.0 // indirect
|
||||||
go.uber.org/multierr v1.6.0 // indirect
|
go.uber.org/multierr v1.6.0 // indirect
|
||||||
go.uber.org/zap v1.18.1 // indirect
|
go.uber.org/zap v1.18.1 // indirect
|
||||||
golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073 // indirect
|
|
||||||
golang.org/x/lint v0.0.0-20201208152925-83fdc39ff7b5 // indirect
|
golang.org/x/lint v0.0.0-20201208152925-83fdc39ff7b5 // indirect
|
||||||
golang.org/x/mod v0.3.0 // indirect
|
golang.org/x/mod v0.3.0 // indirect
|
||||||
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e // indirect
|
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e // indirect
|
||||||
|
|
4
go.sum
4
go.sum
|
@ -3,6 +3,8 @@ github.com/Selly-Modules/logger v0.0.0-20210809034923-140a51f39ec9 h1:AuJ/IIZ7yp
|
||||||
github.com/Selly-Modules/logger v0.0.0-20210809034923-140a51f39ec9/go.mod h1:RWhSQ3F01an8KD00VjzRBZOMcE5eV2Cy0/l4ZkeieyU=
|
github.com/Selly-Modules/logger v0.0.0-20210809034923-140a51f39ec9/go.mod h1:RWhSQ3F01an8KD00VjzRBZOMcE5eV2Cy0/l4ZkeieyU=
|
||||||
github.com/Selly-Modules/mongodb v0.0.0-20211013094205-a8ab24a96c4c h1:1l6QmAl43maG9zFyUXrPQVUjyVt0vy/2Saz992UR+Sc=
|
github.com/Selly-Modules/mongodb v0.0.0-20211013094205-a8ab24a96c4c h1:1l6QmAl43maG9zFyUXrPQVUjyVt0vy/2Saz992UR+Sc=
|
||||||
github.com/Selly-Modules/mongodb v0.0.0-20211013094205-a8ab24a96c4c/go.mod h1:C9O0Bgl9i6szjntMjBdEvaFSqG2UPOgHUspIWIJ93JQ=
|
github.com/Selly-Modules/mongodb v0.0.0-20211013094205-a8ab24a96c4c/go.mod h1:C9O0Bgl9i6szjntMjBdEvaFSqG2UPOgHUspIWIJ93JQ=
|
||||||
|
github.com/allegro/bigcache/v3 v3.0.1 h1:Q4Xl3chywXuJNOw7NV+MeySd3zGQDj4KCpkCg0te8mc=
|
||||||
|
github.com/allegro/bigcache/v3 v3.0.1/go.mod h1:aPyh7jEvrog9zAwx5N7+JUQX5dZTSGpxF1LAR4dr35I=
|
||||||
github.com/armon/go-radix v1.0.0 h1:F4z6KzEeeQIMeLFa97iZU6vupzoecKdU5TX24SNppXI=
|
github.com/armon/go-radix v1.0.0 h1:F4z6KzEeeQIMeLFa97iZU6vupzoecKdU5TX24SNppXI=
|
||||||
github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
|
github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
|
||||||
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
|
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
|
||||||
|
@ -91,6 +93,8 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
|
||||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||||
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
|
github.com/thoas/go-funk v0.9.1 h1:O549iLZqPpTUQ10ykd26sZhzD+rmR5pWhuElrhbC20M=
|
||||||
|
github.com/thoas/go-funk v0.9.1/go.mod h1:+IWnUfUmFO1+WVYQWQtIJHeRRdaIyyYglZN7xzUPe4Q=
|
||||||
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
|
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
|
||||||
github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c=
|
github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c=
|
||||||
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
|
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
|
||||||
|
|
|
@ -7,4 +7,6 @@ const (
|
||||||
passwordHashingCost = 14
|
passwordHashingCost = 14
|
||||||
|
|
||||||
TablePrefixDefault = "usermngmt"
|
TablePrefixDefault = "usermngmt"
|
||||||
|
|
||||||
|
RoleTypeAdmin = "admin"
|
||||||
)
|
)
|
||||||
|
|
27
role/db.go
27
role/db.go
|
@ -54,6 +54,33 @@ func updateOneByCondition(ctx context.Context, cond interface{}, payload interfa
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func permissionFindByCondition(ctx context.Context, cond interface{}, opts ...*options.FindOptions) (docs []model.DBPermission) {
|
||||||
|
var (
|
||||||
|
col = database.GetPermissionCol()
|
||||||
|
)
|
||||||
|
docs = make([]model.DBPermission, 0)
|
||||||
|
|
||||||
|
cursor, err := col.Find(ctx, cond, opts...)
|
||||||
|
if err != nil {
|
||||||
|
logger.Error("usermngmt - Permission - Find", logger.LogData{
|
||||||
|
"cond": cond,
|
||||||
|
"opts": opts,
|
||||||
|
"err": err.Error(),
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer cursor.Close(ctx)
|
||||||
|
if err = cursor.All(ctx, &docs); err != nil {
|
||||||
|
logger.Error("usermngmt - Permission - Decode", logger.LogData{
|
||||||
|
"cond": cond,
|
||||||
|
"opts": opts,
|
||||||
|
"err": err.Error(),
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func findByCondition(ctx context.Context, cond interface{}, opts ...*options.FindOptions) (docs []model.DBRole) {
|
func findByCondition(ctx context.Context, cond interface{}, opts ...*options.FindOptions) (docs []model.DBRole) {
|
||||||
var (
|
var (
|
||||||
col = database.GetRoleCol()
|
col = database.GetRoleCol()
|
||||||
|
|
|
@ -3,11 +3,15 @@ package role
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
"github.com/Selly-Modules/logger"
|
||||||
"github.com/Selly-Modules/mongodb"
|
"github.com/Selly-Modules/mongodb"
|
||||||
|
"github.com/Selly-Modules/usermngmt/cache"
|
||||||
"github.com/Selly-Modules/usermngmt/internal"
|
"github.com/Selly-Modules/usermngmt/internal"
|
||||||
"github.com/Selly-Modules/usermngmt/model"
|
"github.com/Selly-Modules/usermngmt/model"
|
||||||
|
"github.com/thoas/go-funk"
|
||||||
"go.mongodb.org/mongo-driver/bson"
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -132,3 +136,49 @@ func getResponseList(roles []model.DBRole) []model.Role {
|
||||||
|
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CacheRoles ...
|
||||||
|
func CacheRoles() {
|
||||||
|
var (
|
||||||
|
ctx = context.Background()
|
||||||
|
wg sync.WaitGroup
|
||||||
|
)
|
||||||
|
|
||||||
|
// Find
|
||||||
|
roles := findByCondition(ctx, bson.M{})
|
||||||
|
|
||||||
|
wg.Add(len(roles))
|
||||||
|
for _, value := range roles {
|
||||||
|
go func(role model.DBRole) {
|
||||||
|
defer wg.Done()
|
||||||
|
|
||||||
|
// Check and set role admin: admin
|
||||||
|
if role.IsAdmin {
|
||||||
|
if err := cache.GetInstance().Set(role.ID.Hex(), []byte(internal.RoleTypeAdmin)); err != nil {
|
||||||
|
logger.Error("usermngmt - CacheRole", logger.LogData{
|
||||||
|
"err": err.Error(),
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set role by permission with format: permissionCode,permissionCode,...
|
||||||
|
permissions := permissionFindByCondition(ctx, bson.M{
|
||||||
|
"roleId": role.ID,
|
||||||
|
})
|
||||||
|
permissionCodes := funk.Map(permissions, func(i model.DBPermission) string {
|
||||||
|
return i.Code
|
||||||
|
}).([]string)
|
||||||
|
permissionCodeString := strings.Join(permissionCodes, ",")
|
||||||
|
if err := cache.GetInstance().Set(role.ID.Hex(), []byte(permissionCodeString)); err != nil {
|
||||||
|
logger.Error("usermngmt - CacheRole", logger.LogData{
|
||||||
|
"err": err.Error(),
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}(value)
|
||||||
|
}
|
||||||
|
|
||||||
|
wg.Done()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
|
@ -3,12 +3,15 @@ package user
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/Selly-Modules/logger"
|
"github.com/Selly-Modules/logger"
|
||||||
"github.com/Selly-Modules/mongodb"
|
"github.com/Selly-Modules/mongodb"
|
||||||
|
"github.com/Selly-Modules/usermngmt/cache"
|
||||||
"github.com/Selly-Modules/usermngmt/internal"
|
"github.com/Selly-Modules/usermngmt/internal"
|
||||||
"github.com/Selly-Modules/usermngmt/model"
|
"github.com/Selly-Modules/usermngmt/model"
|
||||||
|
"github.com/thoas/go-funk"
|
||||||
"go.mongodb.org/mongo-driver/bson"
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -328,7 +331,7 @@ func HasPermission(userID, permission string) (result bool) {
|
||||||
|
|
||||||
// Validate userID, permission
|
// Validate userID, permission
|
||||||
if userID == "" || permission == "" {
|
if userID == "" || permission == "" {
|
||||||
logger.Error("usermngmt - IsPermission: email or password cannot be empty", logger.LogData{
|
logger.Error("usermngmt - HasPermission: email or password cannot be empty", logger.LogData{
|
||||||
"userID": userID,
|
"userID": userID,
|
||||||
"permission": permission,
|
"permission": permission,
|
||||||
})
|
})
|
||||||
|
@ -336,7 +339,7 @@ func HasPermission(userID, permission string) (result bool) {
|
||||||
}
|
}
|
||||||
id, isValid := mongodb.NewIDFromString(userID)
|
id, isValid := mongodb.NewIDFromString(userID)
|
||||||
if !isValid {
|
if !isValid {
|
||||||
logger.Error("usermngmt - IsPermission: invalid user id", logger.LogData{
|
logger.Error("usermngmt - HasPermission: invalid user id", logger.LogData{
|
||||||
"userID": userID,
|
"userID": userID,
|
||||||
"permission": permission,
|
"permission": permission,
|
||||||
})
|
})
|
||||||
|
@ -346,26 +349,23 @@ func HasPermission(userID, permission string) (result bool) {
|
||||||
// Find user
|
// Find user
|
||||||
user, _ := findByID(ctx, id)
|
user, _ := findByID(ctx, id)
|
||||||
if user.ID.IsZero() {
|
if user.ID.IsZero() {
|
||||||
logger.Error("usermngmt - IsPermission: user not found", logger.LogData{
|
logger.Error("usermngmt - HasPermission: user not found", logger.LogData{
|
||||||
"userID": userID,
|
"userID": userID,
|
||||||
"permission": permission,
|
"permission": permission,
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check isAdmin
|
// Get rolePermissions
|
||||||
if role, _ := roleFindByID(ctx, user.RoleID); role.IsAdmin {
|
// Role is saved with the value "admin" or "permissionCode,permissionCode,..."
|
||||||
result = true
|
entry, _ := cache.GetInstance().Get(user.RoleID.Hex())
|
||||||
return
|
rolePermissions := strings.Split(string(entry), ",")
|
||||||
}
|
|
||||||
|
|
||||||
// Check permission
|
// Check Permission
|
||||||
if total := permissionCountByCondition(ctx, bson.M{
|
if _, isValid = funk.FindString(rolePermissions, func(s string) bool {
|
||||||
"roleId": user.RoleID,
|
return s == permission || s == internal.RoleTypeAdmin
|
||||||
"code": permission,
|
}); isValid {
|
||||||
}); total > 0 {
|
return isValid
|
||||||
result = true
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|
|
@ -5,8 +5,10 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/Selly-Modules/mongodb"
|
"github.com/Selly-Modules/mongodb"
|
||||||
|
"github.com/Selly-Modules/usermngmt/cache"
|
||||||
"github.com/Selly-Modules/usermngmt/database"
|
"github.com/Selly-Modules/usermngmt/database"
|
||||||
"github.com/Selly-Modules/usermngmt/internal"
|
"github.com/Selly-Modules/usermngmt/internal"
|
||||||
|
"github.com/Selly-Modules/usermngmt/role"
|
||||||
)
|
)
|
||||||
|
|
||||||
// MongoDBConfig ...
|
// MongoDBConfig ...
|
||||||
|
@ -54,6 +56,9 @@ func Init(config Config) (*Service, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Init cache
|
||||||
|
cache.Init()
|
||||||
|
|
||||||
// Set database
|
// Set database
|
||||||
database.Set(db, config.TablePrefix)
|
database.Set(db, config.TablePrefix)
|
||||||
|
|
||||||
|
@ -61,6 +66,9 @@ func Init(config Config) (*Service, error) {
|
||||||
config: config,
|
config: config,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Cache role
|
||||||
|
role.CacheRoles()
|
||||||
|
|
||||||
return s, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue