2021-11-10 01:44:22 +00:00
|
|
|
package role
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-11-10 07:50:43 +00:00
|
|
|
"errors"
|
2021-11-11 03:20:08 +00:00
|
|
|
"strings"
|
2021-11-10 07:50:43 +00:00
|
|
|
"sync"
|
2021-11-10 01:44:22 +00:00
|
|
|
|
2021-11-11 03:20:08 +00:00
|
|
|
"github.com/Selly-Modules/logger"
|
2021-11-10 07:50:43 +00:00
|
|
|
"github.com/Selly-Modules/mongodb"
|
2021-11-11 03:20:08 +00:00
|
|
|
"github.com/Selly-Modules/usermngmt/cache"
|
2021-11-10 07:50:43 +00:00
|
|
|
"github.com/Selly-Modules/usermngmt/internal"
|
2021-11-10 04:01:39 +00:00
|
|
|
"github.com/Selly-Modules/usermngmt/model"
|
2021-11-11 03:20:08 +00:00
|
|
|
"github.com/thoas/go-funk"
|
2021-11-10 07:50:43 +00:00
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
2021-11-10 01:44:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Create ...
|
2021-11-10 04:42:23 +00:00
|
|
|
func Create(payload model.RoleCreateOptions) error {
|
2021-11-10 07:50:43 +00:00
|
|
|
var (
|
|
|
|
ctx = context.Background()
|
|
|
|
)
|
|
|
|
|
|
|
|
// Validate payload
|
|
|
|
if err := payload.Validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// New role data from payload
|
|
|
|
doc := newRole(payload)
|
|
|
|
|
|
|
|
// Create role
|
|
|
|
if err := create(ctx, doc); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-10 01:44:22 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-11-10 07:50:43 +00:00
|
|
|
|
|
|
|
// newRole ...
|
|
|
|
func newRole(payload model.RoleCreateOptions) model.DBRole {
|
|
|
|
timeNow := internal.Now()
|
|
|
|
return model.DBRole{
|
|
|
|
ID: mongodb.NewObjectID(),
|
|
|
|
Name: payload.Name,
|
2021-11-10 08:00:36 +00:00
|
|
|
Code: internal.GenerateCode(payload.Name),
|
2021-11-10 07:50:43 +00:00
|
|
|
CreatedAt: timeNow,
|
|
|
|
UpdatedAt: timeNow,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update ...
|
|
|
|
func Update(roleID string, payload model.RoleUpdateOptions) error {
|
|
|
|
var (
|
|
|
|
ctx = context.Background()
|
|
|
|
)
|
|
|
|
|
|
|
|
// Validate payload
|
|
|
|
if err := payload.Validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate roleID
|
|
|
|
id, isValid := mongodb.NewIDFromString(roleID)
|
|
|
|
if !isValid {
|
|
|
|
return errors.New("invalid role id data")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup condition
|
|
|
|
cond := bson.M{
|
|
|
|
"_id": id,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup update data
|
|
|
|
updateData := bson.M{
|
|
|
|
"$set": bson.M{
|
|
|
|
"name": payload.Name,
|
2021-11-10 08:00:36 +00:00
|
|
|
"code": internal.GenerateCode(payload.Name),
|
2021-11-10 07:50:43 +00:00
|
|
|
"updatedAt": internal.Now(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update
|
|
|
|
if err := updateOneByCondition(ctx, cond, updateData); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// All ...
|
|
|
|
func All(queryParams model.RoleAllQuery) (r model.RoleAll) {
|
|
|
|
var (
|
|
|
|
ctx = context.Background()
|
|
|
|
wg sync.WaitGroup
|
|
|
|
cond = bson.M{}
|
|
|
|
)
|
|
|
|
query := model.CommonQuery{
|
|
|
|
Page: queryParams.Page,
|
|
|
|
Limit: queryParams.Limit,
|
|
|
|
Sort: bson.M{"createdAt": -1},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assign condition
|
|
|
|
query.SetDefaultLimit()
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
docs := findByCondition(ctx, cond, query.GetFindOptionsUsingPage())
|
|
|
|
r.List = getResponseList(docs)
|
|
|
|
}()
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
r.Total = countByCondition(ctx, cond)
|
|
|
|
}()
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func getResponseList(roles []model.DBRole) []model.Role {
|
|
|
|
res := make([]model.Role, 0)
|
|
|
|
for _, role := range roles {
|
|
|
|
res = append(res, model.Role{
|
|
|
|
ID: role.ID.Hex(),
|
|
|
|
Name: role.Name,
|
|
|
|
Code: role.Code,
|
|
|
|
CreatedAt: role.CreatedAt,
|
|
|
|
UpdatedAt: role.UpdatedAt,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
2021-11-11 03:20:08 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|