add isPermissionMethod #8

Merged
lqhoang99 merged 6 commits from feature/isPermission into master 2021-11-11 10:07:15 +00:00
12 changed files with 277 additions and 17 deletions

View File

@ -1,6 +1,7 @@
package usermngmt package usermngmt
import ( import (
"github.com/Selly-Modules/usermngmt/cache"
"github.com/Selly-Modules/usermngmt/model" "github.com/Selly-Modules/usermngmt/model"
"github.com/Selly-Modules/usermngmt/permission" "github.com/Selly-Modules/usermngmt/permission"
"github.com/Selly-Modules/usermngmt/role" "github.com/Selly-Modules/usermngmt/role"
@ -33,8 +34,8 @@ func (s Service) ChangeUserStatus(userID, newStatus string) error {
return user.ChangeUserStatus(userID, newStatus) return user.ChangeUserStatus(userID, newStatus)
} }
// GetAllUser ... // GetAllUsers ...
func (s Service) GetAllUser(query model.UserAllQuery) model.UserAll { func (s Service) GetAllUsers(query model.UserAllQuery) model.UserAll {
return user.All(query) return user.All(query)
} }
@ -48,6 +49,11 @@ func (s Service) LoginWithEmailAndPassword(email, password string) (model.User,
return user.LoginWithEmailAndPassword(email, password) return user.LoginWithEmailAndPassword(email, password)
} }
// HasPermission ...
func (s Service) HasPermission(userID, permission string) bool {
return user.HasPermission(userID, permission)
}
// //
// Role // Role
// //
@ -56,7 +62,11 @@ func (s Service) LoginWithEmailAndPassword(email, password string) (model.User,
// CreateRole ... // CreateRole ...
func (s Service) CreateRole(payload model.RoleCreateOptions) error { func (s Service) CreateRole(payload model.RoleCreateOptions) error {
return role.Create(payload) if err := role.Create(payload); err != nil {
return err
}
cache.Roles()
return nil
} }
// UpdateRole ... // UpdateRole ...
@ -77,12 +87,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) if err := permission.Create(payload); err != nil {
return err
}
cache.Roles()
return nil
} }
// 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) if err := permission.Update(permissionID, payload); err != nil {
return err
}
cache.Roles()
return nil
} }
// GetAllPermissions ... // GetAllPermissions ...

29
cache/cache.go vendored Normal file
View File

@ -0,0 +1,29 @@
namhq1989 commented 2021-11-11 03:54:47 +00:00 (Migrated from github.com)
Review

đặt constant phía trên luôn

const cacheTime = 24 * 30 * time.Hour // 30 days
đặt constant phía trên luôn ```go const cacheTime = 24 * 30 * time.Hour // 30 days ```
namhq1989 commented 2021-11-11 03:54:47 +00:00 (Migrated from github.com)
Review

đặt constant phía trên luôn

const cacheTime = 24 * 30 * time.Hour // 30 days
đặt constant phía trên luôn ```go const cacheTime = 24 * 30 * time.Hour // 30 days ```
lqhoang99 commented 2021-11-11 08:16:46 +00:00 (Migrated from github.com)
Review

done

done
lqhoang99 commented 2021-11-11 08:16:46 +00:00 (Migrated from github.com)
Review

done

done
namhq1989 commented 2021-11-11 09:55:16 +00:00 (Migrated from github.com)
Review

đặt tên biến lại chớ trùng với tên package rồi, mc (mem-cache)

đặt tên biến lại chớ trùng với tên package rồi, `mc` (mem-cache)
namhq1989 commented 2021-11-11 09:55:16 +00:00 (Migrated from github.com)
Review

đặt tên biến lại chớ trùng với tên package rồi, mc (mem-cache)

đặt tên biến lại chớ trùng với tên package rồi, `mc` (mem-cache)
lqhoang99 commented 2021-11-11 09:59:02 +00:00 (Migrated from github.com)
Review

done

done
lqhoang99 commented 2021-11-11 09:59:02 +00:00 (Migrated from github.com)
Review

done

done
package cache
namhq1989 commented 2021-11-11 03:54:47 +00:00 (Migrated from github.com)
Review

đặt constant phía trên luôn

const cacheTime = 24 * 30 * time.Hour // 30 days
đặt constant phía trên luôn ```go const cacheTime = 24 * 30 * time.Hour // 30 days ```
lqhoang99 commented 2021-11-11 08:16:46 +00:00 (Migrated from github.com)
Review

done

done
namhq1989 commented 2021-11-11 09:55:16 +00:00 (Migrated from github.com)
Review

đặt tên biến lại chớ trùng với tên package rồi, mc (mem-cache)

đặt tên biến lại chớ trùng với tên package rồi, `mc` (mem-cache)
lqhoang99 commented 2021-11-11 09:59:02 +00:00 (Migrated from github.com)
Review

done

done
namhq1989 commented 2021-11-11 03:54:47 +00:00 (Migrated from github.com)
Review

đặt constant phía trên luôn

const cacheTime = 24 * 30 * time.Hour // 30 days
đặt constant phía trên luôn ```go const cacheTime = 24 * 30 * time.Hour // 30 days ```
lqhoang99 commented 2021-11-11 08:16:46 +00:00 (Migrated from github.com)
Review

done

done
namhq1989 commented 2021-11-11 09:55:16 +00:00 (Migrated from github.com)
Review

đặt tên biến lại chớ trùng với tên package rồi, mc (mem-cache)

đặt tên biến lại chớ trùng với tên package rồi, `mc` (mem-cache)
lqhoang99 commented 2021-11-11 09:59:02 +00:00 (Migrated from github.com)
Review

done

done
import (
namhq1989 commented 2021-11-11 03:54:47 +00:00 (Migrated from github.com)
Review

đặt constant phía trên luôn

const cacheTime = 24 * 30 * time.Hour // 30 days
đặt constant phía trên luôn ```go const cacheTime = 24 * 30 * time.Hour // 30 days ```
lqhoang99 commented 2021-11-11 08:16:46 +00:00 (Migrated from github.com)
Review

done

done
namhq1989 commented 2021-11-11 09:55:16 +00:00 (Migrated from github.com)
Review

đặt tên biến lại chớ trùng với tên package rồi, mc (mem-cache)

đặt tên biến lại chớ trùng với tên package rồi, `mc` (mem-cache)
lqhoang99 commented 2021-11-11 09:59:02 +00:00 (Migrated from github.com)
Review

done

done
"log"
namhq1989 commented 2021-11-11 03:54:47 +00:00 (Migrated from github.com)
Review

đặt constant phía trên luôn

const cacheTime = 24 * 30 * time.Hour // 30 days
đặt constant phía trên luôn ```go const cacheTime = 24 * 30 * time.Hour // 30 days ```
lqhoang99 commented 2021-11-11 08:16:46 +00:00 (Migrated from github.com)
Review

done

done
namhq1989 commented 2021-11-11 09:55:16 +00:00 (Migrated from github.com)
Review

đặt tên biến lại chớ trùng với tên package rồi, mc (mem-cache)

đặt tên biến lại chớ trùng với tên package rồi, `mc` (mem-cache)
lqhoang99 commented 2021-11-11 09:59:02 +00:00 (Migrated from github.com)
Review

done

done
"time"
namhq1989 commented 2021-11-11 03:54:47 +00:00 (Migrated from github.com)
Review

đặt constant phía trên luôn

const cacheTime = 24 * 30 * time.Hour // 30 days
đặt constant phía trên luôn ```go const cacheTime = 24 * 30 * time.Hour // 30 days ```
lqhoang99 commented 2021-11-11 08:16:46 +00:00 (Migrated from github.com)
Review

done

done
namhq1989 commented 2021-11-11 09:55:16 +00:00 (Migrated from github.com)
Review

đặt tên biến lại chớ trùng với tên package rồi, mc (mem-cache)

đặt tên biến lại chớ trùng với tên package rồi, `mc` (mem-cache)
lqhoang99 commented 2021-11-11 09:59:02 +00:00 (Migrated from github.com)
Review

done

done
namhq1989 commented 2021-11-11 03:54:47 +00:00 (Migrated from github.com)
Review

đặt constant phía trên luôn

const cacheTime = 24 * 30 * time.Hour // 30 days
đặt constant phía trên luôn ```go const cacheTime = 24 * 30 * time.Hour // 30 days ```
lqhoang99 commented 2021-11-11 08:16:46 +00:00 (Migrated from github.com)
Review

done

done
namhq1989 commented 2021-11-11 09:55:16 +00:00 (Migrated from github.com)
Review

đặt tên biến lại chớ trùng với tên package rồi, mc (mem-cache)

đặt tên biến lại chớ trùng với tên package rồi, `mc` (mem-cache)
lqhoang99 commented 2021-11-11 09:59:02 +00:00 (Migrated from github.com)
Review

done

done
"github.com/allegro/bigcache/v3"
namhq1989 commented 2021-11-11 03:54:47 +00:00 (Migrated from github.com)
Review

đặt constant phía trên luôn

const cacheTime = 24 * 30 * time.Hour // 30 days
đặt constant phía trên luôn ```go const cacheTime = 24 * 30 * time.Hour // 30 days ```
lqhoang99 commented 2021-11-11 08:16:46 +00:00 (Migrated from github.com)
Review

done

done
namhq1989 commented 2021-11-11 09:55:16 +00:00 (Migrated from github.com)
Review

đặt tên biến lại chớ trùng với tên package rồi, mc (mem-cache)

đặt tên biến lại chớ trùng với tên package rồi, `mc` (mem-cache)
lqhoang99 commented 2021-11-11 09:59:02 +00:00 (Migrated from github.com)
Review

done

done
)
namhq1989 commented 2021-11-11 03:54:47 +00:00 (Migrated from github.com)
Review

đặt constant phía trên luôn

const cacheTime = 24 * 30 * time.Hour // 30 days
đặt constant phía trên luôn ```go const cacheTime = 24 * 30 * time.Hour // 30 days ```
lqhoang99 commented 2021-11-11 08:16:46 +00:00 (Migrated from github.com)
Review

done

done
namhq1989 commented 2021-11-11 09:55:16 +00:00 (Migrated from github.com)
Review

đặt tên biến lại chớ trùng với tên package rồi, mc (mem-cache)

đặt tên biến lại chớ trùng với tên package rồi, `mc` (mem-cache)
lqhoang99 commented 2021-11-11 09:59:02 +00:00 (Migrated from github.com)
Review

done

done
namhq1989 commented 2021-11-11 03:54:47 +00:00 (Migrated from github.com)
Review

đặt constant phía trên luôn

const cacheTime = 24 * 30 * time.Hour // 30 days
đặt constant phía trên luôn ```go const cacheTime = 24 * 30 * time.Hour // 30 days ```
lqhoang99 commented 2021-11-11 08:16:46 +00:00 (Migrated from github.com)
Review

done

done
namhq1989 commented 2021-11-11 09:55:16 +00:00 (Migrated from github.com)
Review

đặt tên biến lại chớ trùng với tên package rồi, mc (mem-cache)

đặt tên biến lại chớ trùng với tên package rồi, `mc` (mem-cache)
lqhoang99 commented 2021-11-11 09:59:02 +00:00 (Migrated from github.com)
Review

done

done
var mc *bigcache.BigCache
namhq1989 commented 2021-11-11 03:54:47 +00:00 (Migrated from github.com)
Review

đặt constant phía trên luôn

const cacheTime = 24 * 30 * time.Hour // 30 days
đặt constant phía trên luôn ```go const cacheTime = 24 * 30 * time.Hour // 30 days ```
lqhoang99 commented 2021-11-11 08:16:46 +00:00 (Migrated from github.com)
Review

done

done
namhq1989 commented 2021-11-11 09:55:16 +00:00 (Migrated from github.com)
Review

đặt tên biến lại chớ trùng với tên package rồi, mc (mem-cache)

đặt tên biến lại chớ trùng với tên package rồi, `mc` (mem-cache)
lqhoang99 commented 2021-11-11 09:59:02 +00:00 (Migrated from github.com)
Review

done

done
namhq1989 commented 2021-11-11 03:54:47 +00:00 (Migrated from github.com)
Review

đặt constant phía trên luôn

const cacheTime = 24 * 30 * time.Hour // 30 days
đặt constant phía trên luôn ```go const cacheTime = 24 * 30 * time.Hour // 30 days ```
lqhoang99 commented 2021-11-11 08:16:46 +00:00 (Migrated from github.com)
Review

done

done
namhq1989 commented 2021-11-11 09:55:16 +00:00 (Migrated from github.com)
Review

đặt tên biến lại chớ trùng với tên package rồi, mc (mem-cache)

đặt tên biến lại chớ trùng với tên package rồi, `mc` (mem-cache)
lqhoang99 commented 2021-11-11 09:59:02 +00:00 (Migrated from github.com)
Review

done

done
// Init ...
namhq1989 commented 2021-11-11 03:54:47 +00:00 (Migrated from github.com)
Review

đặt constant phía trên luôn

const cacheTime = 24 * 30 * time.Hour // 30 days
đặt constant phía trên luôn ```go const cacheTime = 24 * 30 * time.Hour // 30 days ```
lqhoang99 commented 2021-11-11 08:16:46 +00:00 (Migrated from github.com)
Review

done

done
namhq1989 commented 2021-11-11 09:55:16 +00:00 (Migrated from github.com)
Review

đặt tên biến lại chớ trùng với tên package rồi, mc (mem-cache)

đặt tên biến lại chớ trùng với tên package rồi, `mc` (mem-cache)
lqhoang99 commented 2021-11-11 09:59:02 +00:00 (Migrated from github.com)
Review

done

done
func Init() {
namhq1989 commented 2021-11-11 03:54:47 +00:00 (Migrated from github.com)
Review

đặt constant phía trên luôn

const cacheTime = 24 * 30 * time.Hour // 30 days
đặt constant phía trên luôn ```go const cacheTime = 24 * 30 * time.Hour // 30 days ```
lqhoang99 commented 2021-11-11 08:16:46 +00:00 (Migrated from github.com)
Review

done

done
namhq1989 commented 2021-11-11 09:55:16 +00:00 (Migrated from github.com)
Review

đặt tên biến lại chớ trùng với tên package rồi, mc (mem-cache)

đặt tên biến lại chớ trùng với tên package rồi, `mc` (mem-cache)
lqhoang99 commented 2021-11-11 09:59:02 +00:00 (Migrated from github.com)
Review

done

done
// The time after which entries can be evicted is 30 days
namhq1989 commented 2021-11-11 03:54:47 +00:00 (Migrated from github.com)
Review

đặt constant phía trên luôn

const cacheTime = 24 * 30 * time.Hour // 30 days
đặt constant phía trên luôn ```go const cacheTime = 24 * 30 * time.Hour // 30 days ```
lqhoang99 commented 2021-11-11 08:16:46 +00:00 (Migrated from github.com)
Review

done

done
namhq1989 commented 2021-11-11 09:55:16 +00:00 (Migrated from github.com)
Review

đặt tên biến lại chớ trùng với tên package rồi, mc (mem-cache)

đặt tên biến lại chớ trùng với tên package rồi, `mc` (mem-cache)
lqhoang99 commented 2021-11-11 09:59:02 +00:00 (Migrated from github.com)
Review

done

done
const cacheTime = 24 * 30 * time.Hour // 30 days
namhq1989 commented 2021-11-11 03:54:47 +00:00 (Migrated from github.com)
Review

đặt constant phía trên luôn

const cacheTime = 24 * 30 * time.Hour // 30 days
đặt constant phía trên luôn ```go const cacheTime = 24 * 30 * time.Hour // 30 days ```
lqhoang99 commented 2021-11-11 08:16:46 +00:00 (Migrated from github.com)
Review

done

done
namhq1989 commented 2021-11-11 09:55:16 +00:00 (Migrated from github.com)
Review

đặt tên biến lại chớ trùng với tên package rồi, mc (mem-cache)

đặt tên biến lại chớ trùng với tên package rồi, `mc` (mem-cache)
lqhoang99 commented 2021-11-11 09:59:02 +00:00 (Migrated from github.com)
Review

done

done
c, err := bigcache.NewBigCache(bigcache.DefaultConfig(cacheTime))
namhq1989 commented 2021-11-11 03:54:47 +00:00 (Migrated from github.com)
Review

đặt constant phía trên luôn

const cacheTime = 24 * 30 * time.Hour // 30 days
đặt constant phía trên luôn ```go const cacheTime = 24 * 30 * time.Hour // 30 days ```
lqhoang99 commented 2021-11-11 08:16:46 +00:00 (Migrated from github.com)
Review

done

done
namhq1989 commented 2021-11-11 09:55:16 +00:00 (Migrated from github.com)
Review

đặt tên biến lại chớ trùng với tên package rồi, mc (mem-cache)

đặt tên biến lại chớ trùng với tên package rồi, `mc` (mem-cache)
lqhoang99 commented 2021-11-11 09:59:02 +00:00 (Migrated from github.com)
Review

done

done
if err != nil {
namhq1989 commented 2021-11-11 03:54:47 +00:00 (Migrated from github.com)
Review

đặt constant phía trên luôn

const cacheTime = 24 * 30 * time.Hour // 30 days
đặt constant phía trên luôn ```go const cacheTime = 24 * 30 * time.Hour // 30 days ```
lqhoang99 commented 2021-11-11 08:16:46 +00:00 (Migrated from github.com)
Review

done

done
namhq1989 commented 2021-11-11 09:55:16 +00:00 (Migrated from github.com)
Review

đặt tên biến lại chớ trùng với tên package rồi, mc (mem-cache)

đặt tên biến lại chớ trùng với tên package rồi, `mc` (mem-cache)
lqhoang99 commented 2021-11-11 09:59:02 +00:00 (Migrated from github.com)
Review

done

done
log.Fatalf("Cannot init Cache %v", err)
namhq1989 commented 2021-11-11 03:54:47 +00:00 (Migrated from github.com)
Review

đặt constant phía trên luôn

const cacheTime = 24 * 30 * time.Hour // 30 days
đặt constant phía trên luôn ```go const cacheTime = 24 * 30 * time.Hour // 30 days ```
lqhoang99 commented 2021-11-11 08:16:46 +00:00 (Migrated from github.com)
Review

done

done
namhq1989 commented 2021-11-11 09:55:16 +00:00 (Migrated from github.com)
Review

đặt tên biến lại chớ trùng với tên package rồi, mc (mem-cache)

đặt tên biến lại chớ trùng với tên package rồi, `mc` (mem-cache)
lqhoang99 commented 2021-11-11 09:59:02 +00:00 (Migrated from github.com)
Review

done

done
}
namhq1989 commented 2021-11-11 03:54:47 +00:00 (Migrated from github.com)
Review

đặt constant phía trên luôn

const cacheTime = 24 * 30 * time.Hour // 30 days
đặt constant phía trên luôn ```go const cacheTime = 24 * 30 * time.Hour // 30 days ```
lqhoang99 commented 2021-11-11 08:16:46 +00:00 (Migrated from github.com)
Review

done

done
namhq1989 commented 2021-11-11 09:55:16 +00:00 (Migrated from github.com)
Review

đặt tên biến lại chớ trùng với tên package rồi, mc (mem-cache)

đặt tên biến lại chớ trùng với tên package rồi, `mc` (mem-cache)
lqhoang99 commented 2021-11-11 09:59:02 +00:00 (Migrated from github.com)
Review

done

done
mc = c
namhq1989 commented 2021-11-11 03:54:47 +00:00 (Migrated from github.com)
Review

đặt constant phía trên luôn

const cacheTime = 24 * 30 * time.Hour // 30 days
đặt constant phía trên luôn ```go const cacheTime = 24 * 30 * time.Hour // 30 days ```
lqhoang99 commented 2021-11-11 08:16:46 +00:00 (Migrated from github.com)
Review

done

done
namhq1989 commented 2021-11-11 09:55:16 +00:00 (Migrated from github.com)
Review

đặt tên biến lại chớ trùng với tên package rồi, mc (mem-cache)

đặt tên biến lại chớ trùng với tên package rồi, `mc` (mem-cache)
lqhoang99 commented 2021-11-11 09:59:02 +00:00 (Migrated from github.com)
Review

done

done
namhq1989 commented 2021-11-11 03:54:47 +00:00 (Migrated from github.com)
Review

đặt constant phía trên luôn

const cacheTime = 24 * 30 * time.Hour // 30 days
đặt constant phía trên luôn ```go const cacheTime = 24 * 30 * time.Hour // 30 days ```
lqhoang99 commented 2021-11-11 08:16:46 +00:00 (Migrated from github.com)
Review

done

done
namhq1989 commented 2021-11-11 09:55:16 +00:00 (Migrated from github.com)
Review

đặt tên biến lại chớ trùng với tên package rồi, mc (mem-cache)

đặt tên biến lại chớ trùng với tên package rồi, `mc` (mem-cache)
lqhoang99 commented 2021-11-11 09:59:02 +00:00 (Migrated from github.com)
Review

done

done
// Cache roles
namhq1989 commented 2021-11-11 03:54:47 +00:00 (Migrated from github.com)
Review

đặt constant phía trên luôn

const cacheTime = 24 * 30 * time.Hour // 30 days
đặt constant phía trên luôn ```go const cacheTime = 24 * 30 * time.Hour // 30 days ```
lqhoang99 commented 2021-11-11 08:16:46 +00:00 (Migrated from github.com)
Review

done

done
namhq1989 commented 2021-11-11 09:55:16 +00:00 (Migrated from github.com)
Review

đặt tên biến lại chớ trùng với tên package rồi, mc (mem-cache)

đặt tên biến lại chớ trùng với tên package rồi, `mc` (mem-cache)
lqhoang99 commented 2021-11-11 09:59:02 +00:00 (Migrated from github.com)
Review

done

done
Roles()
namhq1989 commented 2021-11-11 03:54:47 +00:00 (Migrated from github.com)
Review

đặt constant phía trên luôn

const cacheTime = 24 * 30 * time.Hour // 30 days
đặt constant phía trên luôn ```go const cacheTime = 24 * 30 * time.Hour // 30 days ```
lqhoang99 commented 2021-11-11 08:16:46 +00:00 (Migrated from github.com)
Review

done

done
namhq1989 commented 2021-11-11 09:55:16 +00:00 (Migrated from github.com)
Review

đặt tên biến lại chớ trùng với tên package rồi, mc (mem-cache)

đặt tên biến lại chớ trùng với tên package rồi, `mc` (mem-cache)
lqhoang99 commented 2021-11-11 09:59:02 +00:00 (Migrated from github.com)
Review

done

done
}
namhq1989 commented 2021-11-11 03:54:47 +00:00 (Migrated from github.com)
Review

đặt constant phía trên luôn

const cacheTime = 24 * 30 * time.Hour // 30 days
đặt constant phía trên luôn ```go const cacheTime = 24 * 30 * time.Hour // 30 days ```
lqhoang99 commented 2021-11-11 08:16:46 +00:00 (Migrated from github.com)
Review

done

done
namhq1989 commented 2021-11-11 09:55:16 +00:00 (Migrated from github.com)
Review

đặt tên biến lại chớ trùng với tên package rồi, mc (mem-cache)

đặt tên biến lại chớ trùng với tên package rồi, `mc` (mem-cache)
lqhoang99 commented 2021-11-11 09:59:02 +00:00 (Migrated from github.com)
Review

done

done
namhq1989 commented 2021-11-11 03:54:47 +00:00 (Migrated from github.com)
Review

đặt constant phía trên luôn

const cacheTime = 24 * 30 * time.Hour // 30 days
đặt constant phía trên luôn ```go const cacheTime = 24 * 30 * time.Hour // 30 days ```
lqhoang99 commented 2021-11-11 08:16:46 +00:00 (Migrated from github.com)
Review

done

done
namhq1989 commented 2021-11-11 09:55:16 +00:00 (Migrated from github.com)
Review

đặt tên biến lại chớ trùng với tên package rồi, mc (mem-cache)

đặt tên biến lại chớ trùng với tên package rồi, `mc` (mem-cache)
lqhoang99 commented 2021-11-11 09:59:02 +00:00 (Migrated from github.com)
Review

done

done
// GetInstance ...
namhq1989 commented 2021-11-11 03:54:47 +00:00 (Migrated from github.com)
Review

đặt constant phía trên luôn

const cacheTime = 24 * 30 * time.Hour // 30 days
đặt constant phía trên luôn ```go const cacheTime = 24 * 30 * time.Hour // 30 days ```
lqhoang99 commented 2021-11-11 08:16:46 +00:00 (Migrated from github.com)
Review

done

done
namhq1989 commented 2021-11-11 09:55:16 +00:00 (Migrated from github.com)
Review

đặt tên biến lại chớ trùng với tên package rồi, mc (mem-cache)

đặt tên biến lại chớ trùng với tên package rồi, `mc` (mem-cache)
lqhoang99 commented 2021-11-11 09:59:02 +00:00 (Migrated from github.com)
Review

done

done
func GetInstance() *bigcache.BigCache {
namhq1989 commented 2021-11-11 03:54:47 +00:00 (Migrated from github.com)
Review

đặt constant phía trên luôn

const cacheTime = 24 * 30 * time.Hour // 30 days
đặt constant phía trên luôn ```go const cacheTime = 24 * 30 * time.Hour // 30 days ```
lqhoang99 commented 2021-11-11 08:16:46 +00:00 (Migrated from github.com)
Review

done

done
namhq1989 commented 2021-11-11 09:55:16 +00:00 (Migrated from github.com)
Review

đặt tên biến lại chớ trùng với tên package rồi, mc (mem-cache)

đặt tên biến lại chớ trùng với tên package rồi, `mc` (mem-cache)
lqhoang99 commented 2021-11-11 09:59:02 +00:00 (Migrated from github.com)
Review

done

done
return mc
namhq1989 commented 2021-11-11 03:54:47 +00:00 (Migrated from github.com)
Review

đặt constant phía trên luôn

const cacheTime = 24 * 30 * time.Hour // 30 days
đặt constant phía trên luôn ```go const cacheTime = 24 * 30 * time.Hour // 30 days ```
lqhoang99 commented 2021-11-11 08:16:46 +00:00 (Migrated from github.com)
Review

done

done
namhq1989 commented 2021-11-11 09:55:16 +00:00 (Migrated from github.com)
Review

đặt tên biến lại chớ trùng với tên package rồi, mc (mem-cache)

đặt tên biến lại chớ trùng với tên package rồi, `mc` (mem-cache)
lqhoang99 commented 2021-11-11 09:59:02 +00:00 (Migrated from github.com)
Review

done

done
}
namhq1989 commented 2021-11-11 03:54:47 +00:00 (Migrated from github.com)
Review

đặt constant phía trên luôn

const cacheTime = 24 * 30 * time.Hour // 30 days
đặt constant phía trên luôn ```go const cacheTime = 24 * 30 * time.Hour // 30 days ```
lqhoang99 commented 2021-11-11 08:16:46 +00:00 (Migrated from github.com)
Review

done

done
namhq1989 commented 2021-11-11 09:55:16 +00:00 (Migrated from github.com)
Review

đặt tên biến lại chớ trùng với tên package rồi, mc (mem-cache)

đặt tên biến lại chớ trùng với tên package rồi, `mc` (mem-cache)
lqhoang99 commented 2021-11-11 09:59:02 +00:00 (Migrated from github.com)
Review

done

done

64
cache/db.go vendored Normal file
View File

@ -0,0 +1,64 @@
package cache
import (
"context"
"github.com/Selly-Modules/logger"
"github.com/Selly-Modules/usermngmt/database"
"github.com/Selly-Modules/usermngmt/model"
"go.mongodb.org/mongo-driver/mongo/options"
)
func roleFindByCondition(ctx context.Context, cond interface{}, opts ...*options.FindOptions) (docs []model.DBRole) {
var (
col = database.GetRoleCol()
)
docs = make([]model.DBRole, 0)
cursor, err := col.Find(ctx, cond, opts...)
if err != nil {
logger.Error("usermngmt - Role - 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 - Role - Decode", logger.LogData{
"cond": cond,
"opts": opts,
"err": err.Error(),
})
return
}
return
}
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
}

8
cache/model.go vendored Normal file
View File

@ -0,0 +1,8 @@
package cache
// CachedRole ...
type CachedRole struct {
Role string `json:"role"`
IsAdmin bool `json:"isAdmin"`
Permissions []string `json:"permissions"`
}

70
cache/role.go vendored Normal file
View File

@ -0,0 +1,70 @@
package cache
import (
"context"
"encoding/json"
"sync"
"github.com/Selly-Modules/logger"
"github.com/Selly-Modules/usermngmt/model"
"go.mongodb.org/mongo-driver/bson"
)
// Roles ...
func Roles() {
var (
ctx = context.Background()
wg sync.WaitGroup
cond = bson.M{}
)
// Find
roles := roleFindByCondition(ctx, cond)
permissions := permissionFindByCondition(ctx, cond)
wg.Add(len(roles))
for _, value := range roles {
go func(role model.DBRole) {
defer wg.Done()
rolePermissions := make([]string, 0)
// Get role permissions
for _, permission := range permissions {
if permission.RoleID == role.ID {
rolePermissions = append(rolePermissions, permission.Code)
}
}
// Cache Role
entry, _ := json.Marshal(CachedRole{
Role: role.Code,
IsAdmin: role.IsAdmin,
Permissions: rolePermissions,
})
if err := mc.Set(role.ID.Hex(), entry); err != nil {
logger.Error("usermngmt - CacheRole", logger.LogData{
"err": err.Error(),
})
return
}
}(value)
}
wg.Wait()
return
}
// GetCachedRole ...
func GetCachedRole(key string) CachedRole {
entry, err := mc.Get(key)
if err != nil {
Roles()
entry, _ = mc.Get(key)
}
var cachedRole CachedRole
if err = json.Unmarshal(entry, &cachedRole); err != nil {
logger.Error("usermngmt - GetCachedRole - Unmarshal", logger.LogData{
"err": err.Error(),
})
}
return cachedRole
}

4
go.mod
View File

@ -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
View File

@ -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=

View File

@ -7,4 +7,6 @@ const (
passwordHashingCost = 14 passwordHashingCost = 14
TablePrefixDefault = "usermngmt" TablePrefixDefault = "usermngmt"
RoleTypeAdmin = "admin"
) )

View File

@ -7,20 +7,9 @@ import (
"github.com/Selly-Modules/logger" "github.com/Selly-Modules/logger"
"github.com/Selly-Modules/usermngmt/database" "github.com/Selly-Modules/usermngmt/database"
"github.com/Selly-Modules/usermngmt/model" "github.com/Selly-Modules/usermngmt/model"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo/options" "go.mongodb.org/mongo-driver/mongo/options"
) )
func findByID(ctx context.Context, id primitive.ObjectID) (model.DBRole, error) {
var (
doc model.DBRole
col = database.GetRoleCol()
)
err := col.FindOne(ctx, bson.M{"_id": id}).Decode(&doc)
return doc, err
}
func create(ctx context.Context, doc model.DBRole) error { func create(ctx context.Context, doc model.DBRole) error {
var ( var (
col = database.GetRoleCol() col = database.GetRoleCol()

View File

@ -66,6 +66,21 @@ func roleFindByID(ctx context.Context, id primitive.ObjectID) (model.DBRole, err
return doc, err return doc, err
} }
// permissionCountByCondition ...
func permissionCountByCondition(ctx context.Context, cond interface{}) int64 {
var (
col = database.GetPermissionCol()
)
total, err := col.CountDocuments(ctx, cond)
if err != nil {
logger.Error("usermngmt - Permission - CountDocuments", logger.LogData{
"err": err.Error(),
"cond": cond,
})
}
return total
}
func create(ctx context.Context, doc model.DBUser) error { func create(ctx context.Context, doc model.DBUser) error {
var ( var (
col = database.GetUserCol() col = database.GetUserCol()

View File

@ -7,9 +7,12 @@ import (
"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"
"go.mongodb.org/mongo-driver/bson/primitive"
) )
// Create ... // Create ...
@ -319,3 +322,55 @@ func LoginWithEmailAndPassword(email, password string) (result model.User, err e
result = getResponse(ctx, user) result = getResponse(ctx, user)
return return
} }
// HasPermission ...
func HasPermission(userID, permission string) (result bool) {
var (
ctx = context.Background()
)
// Validate userID, permission
if userID == "" || permission == "" {
logger.Error("usermngmt - HasPermission: email or password cannot be empty", logger.LogData{
"userID": userID,
"permission": permission,
})
namhq1989 commented 2021-11-10 10:09:57 +00:00 (Migrated from github.com)
Review

func này muốn nhanh thì làm thêm in-memory cache (https://github.com/allegro/bigcache) như sau:

  • init thì call func A query hết role và permission ra lưu vào mem
  • khi có thay đổi (CRUD) thì call lại func A
  • khi cần check thì chỉ cần lấy từ mem lên để check
func này muốn nhanh thì làm thêm in-memory cache (https://github.com/allegro/bigcache) như sau: - init thì call func A query hết role và permission ra lưu vào mem - khi có thay đổi (CRUD) thì call lại func A - khi cần check thì chỉ cần lấy từ mem lên để check
lqhoang99 commented 2021-11-11 03:20:41 +00:00 (Migrated from github.com)
Review

done

done
return
}
id, isValid := mongodb.NewIDFromString(userID)
if !isValid {
logger.Error("usermngmt - HasPermission: invalid user id", logger.LogData{
"userID": userID,
"permission": permission,
})
return
}
// Find user
user, _ := findByID(ctx, id)
if user.ID.IsZero() {
logger.Error("usermngmt - HasPermission: user not found", logger.LogData{
"userID": userID,
"permission": permission,
})
return
}
return checkUserHasPermissionFromCache(user.RoleID, permission)
}
func checkUserHasPermissionFromCache(roleID primitive.ObjectID, permission string) bool {
cachedRole := cache.GetCachedRole(roleID.Hex())
// Check permission
if cachedRole.IsAdmin {
return true
}
if _, isValid := funk.FindString(cachedRole.Permissions, func(s string) bool {
return s == permission
}); isValid {
namhq1989 commented 2021-11-11 04:06:38 +00:00 (Migrated from github.com)
Review

chỗ này thiếu

  1. trong package cache phải có 1 hàm GetCachedRoles trả về CachedRoles, trong hàm đó nếu check cachedRoles = nil thì phải call hàm Roles để tạo cache mới
  2. ở chỗ này gọi hàm GetCachedRoles
  3. dựa vào user.RoleID để pick ra data của role đó, sau đó check mảng permission

2 bước 2,3 nên đưa ra 1 func checkUserHasPermissionFromCache để làm cho gọn code

chỗ này thiếu 1. trong package ` cache` phải có 1 hàm `GetCachedRoles` trả về `CachedRoles`, trong hàm đó nếu check `cachedRoles = nil` thì phải call hàm `Roles` để tạo cache mới 2. ở chỗ này gọi hàm `GetCachedRoles` 3. dựa vào user.RoleID để pick ra data của role đó, sau đó check mảng permission ---- 2 bước 2,3 nên đưa ra 1 func `checkUserHasPermissionFromCache` để làm cho gọn code
lqhoang99 commented 2021-11-11 08:16:36 +00:00 (Migrated from github.com)
Review

done

done
return true
}
return false
}

View File

@ -5,6 +5,7 @@ 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"
) )
@ -54,6 +55,9 @@ func Init(config Config) (*Service, error) {
return nil, err return nil, err
namhq1989 commented 2021-11-11 04:07:25 +00:00 (Migrated from github.com)
Review

func này gọi trong hàm Init của cache luôn

func này gọi trong hàm Init của `cache` luôn
lqhoang99 commented 2021-11-11 08:16:32 +00:00 (Migrated from github.com)
Review

done

done
} }
// Init cache
cache.Init()
// Set database // Set database
database.Set(db, config.TablePrefix) database.Set(db, config.TablePrefix)