作者 tangxuhui
... ... @@ -94,3 +94,11 @@ func CreateBatchAddUserService(options map[string]interface{}) (service.PgBatchA
}
return domainService.NewPgBatchAddUserService(transactionContext)
}
func CreatePgBatchRemoveRoleService(options map[string]interface{}) (service.PgBatchRemoveRoleService, error) {
var transactionContext *pgTransaction.TransactionContext
if value, ok := options["transactionContext"]; ok {
transactionContext = value.(*pgTransaction.TransactionContext)
}
return domainService.NewPgBatchRemoveRoleService(transactionContext)
}
... ...
... ... @@ -290,34 +290,17 @@ func (roleService *RoleService) RemoveRole(removeRoleCommand *command.RemoveRole
defer func() {
transactionContext.RollbackTransaction()
}()
// TODO:数据权限
var roleRepository domain.RoleRepository
if value, err := factory.CreateRoleRepository(map[string]interface{}{
batchRemoveRoleService, _ := factory.CreatePgBatchRemoveRoleService(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
roleRepository = value
}
role, err := roleRepository.FindOne(map[string]interface{}{"roleId": removeRoleCommand.RoleId})
if err != nil {
})
if _, err := batchRemoveRoleService.BatchRemove(removeRoleCommand.OperateInfo, []int64{removeRoleCommand.RoleId}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if role == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(removeRoleCommand.RoleId)))
}
if role.RoleType == domain.RoleTypeAdmin {
return nil, application.ThrowError(application.BUSINESS_ERROR, "主管理员角色不可删除")
}
role.DeletedAt = time.Now()
if role, err := roleRepository.Remove(role); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return role, nil
}
return struct{}{}, nil
}
// 批量移除角色
... ... @@ -335,32 +318,14 @@ func (roleService *RoleService) BatchDeleteRole(removeRoleCommand *command.Batch
defer func() {
transactionContext.RollbackTransaction()
}()
// TODO:数据权限
var roleRepository domain.RoleRepository
if value, err := factory.CreateRoleRepository(map[string]interface{}{
batchRemoveRoleService, _ := factory.CreatePgBatchRemoveRoleService(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
roleRepository = value
}
for i := range removeRoleCommand.RoleIds {
roleId := removeRoleCommand.RoleIds[i]
role, err := roleRepository.FindOne(map[string]interface{}{"roleId": roleId})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if role == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%v", roleId))
}
if role.RoleType == domain.RoleTypeAdmin {
return nil, application.ThrowError(application.BUSINESS_ERROR, "主管理员角色不可删除")
}
role.DeletedAt = time.Now()
if _, err := roleRepository.Remove(role); err != nil {
})
if _, err := batchRemoveRoleService.BatchRemove(removeRoleCommand.OperateInfo, removeRoleCommand.RoleIds); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
... ... @@ -423,7 +388,6 @@ func (roleService *RoleService) UpdateRole(updateRoleCommand *command.UpdateRole
defer func() {
transactionContext.RollbackTransaction()
}()
// TODO:数据权限
roleRepository, role, err := factory.FastPgRole(transactionContext, updateRoleCommand.RoleId)
if err != nil {
return nil, err
... ...
... ... @@ -110,6 +110,14 @@ type AccessMenusOptions struct {
CompanyId int64
}
func (role *Role) IsRemoved() bool {
if !role.DeletedAt.IsZero() {
return false
}
role.DeletedAt = time.Now()
return true
}
/***** 1.自定义函数模块 *****/
/*1.1 拷贝简单的角色信息*/
... ...
package service
import "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
// PgAuthAccountDestroyService 账号注销服务
type PgBatchRemoveRoleService interface {
BatchRemove(optUser *domain.OperateInfo, roleIds []int64) (interface{}, error)
}
... ...
... ... @@ -215,7 +215,7 @@ func (user *User) RemoveUserRole(roleId int64) bool {
var res bool = false
if user.ExistsUserRole(roleId) {
var roles []*Role
var roles = make([]*Role, 0)
for i := range user.UserRole {
if user.UserRole[i].RoleId != roleId {
roles = append(roles, user.UserRole[i])
... ...
package domainService
import (
"fmt"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/repository"
)
// PgCreateRoleService 传教角色服务
type PgBatchRemoveRoleService struct {
transactionContext *pgTransaction.TransactionContext
}
// CreateRole 批量删除角色
//
// optUser 操作用户
// roleInfo 角色信息
func (ptr *PgBatchRemoveRoleService) BatchRemove(optUser *domain.OperateInfo, roleIds []int64) (interface{}, error) {
roleRepository, _ := repository.NewRoleRepository(ptr.transactionContext)
userRepository, _ := repository.NewUserRepository(ptr.transactionContext)
for i := range roleIds {
roleId := roleIds[i]
role, err := roleRepository.FindOne(map[string]interface{}{"roleId": roleId})
if err != nil {
return nil, err
}
if role.RoleType == domain.RoleTypeAdmin {
return nil, fmt.Errorf("主管理员角色不可删除")
}
if ok := role.IsRemoved(); !ok {
continue
}
// 1.移除角色
if _, err := roleRepository.Remove(role); err != nil {
return nil, err
}
// 2.移除用户中有配置该角色的数据
var offset, limit int = 0, repository.MaxRowLimit
for {
_, users, e := userRepository.Find(map[string]interface{}{"companyId": optUser.CompanyId, "roleId": roleId, "limit": limit, "offset": offset})
for i := 0; i < len(users); i++ {
if !users[i].RemoveUserRole(roleId) {
continue
}
if _, err := userRepository.Save(users[i]); err != nil {
return nil, err
}
}
if len(users) == 0 || len(users) < repository.MaxRowLimit || e != nil {
break
}
offset += limit
}
}
return struct{}{}, nil
}
func NewPgBatchRemoveRoleService(transactionContext *pgTransaction.TransactionContext) (*PgBatchRemoveRoleService, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &PgBatchRemoveRoleService{
transactionContext: transactionContext,
}, nil
}
}
... ...
package repository
//func PGConten
const MaxRowLimit = 100
... ...
... ... @@ -202,6 +202,8 @@ func (repository *UserRepository) Find(queryOptions map[string]interface{}) (int
}
query.SetWhereByQueryOption("user_base_id=?", "userBaseId")
query.SetWhereByQueryOption("(user_type & ?)>0", "userType")
query.SetWhereByQueryOption(fmt.Sprintf(`user_role @> '[{"roleId":%v}]'`, queryOptions["roleId"]), "roleId")
if v, ok := queryOptions["depName"]; ok && len(v.(string)) > 0 {
query.Where(fmt.Sprintf(`ext->>'depName' like '%%%v%%'`, v))
}
... ...