...
|
...
|
@@ -6,6 +6,7 @@ import ( |
|
|
"github.com/linmadan/egglib-go/utils/tool_funs"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/factory"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/role/command"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/role/dto"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/role/query"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/utils"
|
...
|
...
|
@@ -17,8 +18,8 @@ type RoleService struct { |
|
|
}
|
|
|
|
|
|
// 分配角色给多个用户
|
|
|
func (roleService *RoleService) AssginRoleToUsers(assginRoleToUsersCommand *command.AssginRoleToUsersCommand) (interface{}, error) {
|
|
|
if err := assginRoleToUsersCommand.ValidateCommand(); err != nil {
|
|
|
func (roleService *RoleService) AssginRoleToUsers(assignRoleToUsersCommand *command.AssginRoleToUsersCommand) (interface{}, error) {
|
|
|
if err := assignRoleToUsersCommand.ValidateCommand(); err != nil {
|
|
|
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
|
|
|
}
|
|
|
transactionContext, err := factory.CreateTransactionContext(nil)
|
...
|
...
|
@@ -31,10 +32,30 @@ func (roleService *RoleService) AssginRoleToUsers(assginRoleToUsersCommand *comm |
|
|
defer func() {
|
|
|
transactionContext.RollbackTransaction()
|
|
|
}()
|
|
|
|
|
|
_, role, err := factory.FastPgRole(transactionContext, assignRoleToUsersCommand.RoleId)
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
userRepository, _, err := factory.FastPgUser(transactionContext, 0)
|
|
|
for i := range assignRoleToUsersCommand.UserIds {
|
|
|
userId := assignRoleToUsersCommand.UserIds[i]
|
|
|
if user, err := userRepository.FindOne(map[string]interface{}{"userId": userId}); err != nil {
|
|
|
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
|
|
|
} else {
|
|
|
if !user.ExistsUserRole(role.RoleId) {
|
|
|
user.UserRole = append(user.UserRole, role.CloneSample())
|
|
|
if _, err := userRepository.Save(user); err != nil {
|
|
|
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if err := transactionContext.CommitTransaction(); err != nil {
|
|
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
}
|
|
|
return nil, nil
|
|
|
return struct{}{}, nil
|
|
|
}
|
|
|
|
|
|
// 创建角色
|
...
|
...
|
@@ -143,10 +164,26 @@ func (roleService *RoleService) GetRoleAccessMenus(getRoleAccessMenusQuery *quer |
|
|
defer func() {
|
|
|
transactionContext.RollbackTransaction()
|
|
|
}()
|
|
|
|
|
|
_, role, err := factory.FastPgRole(transactionContext, getRoleAccessMenusQuery.RoleId)
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
roleAccessMenusService, _ := factory.CreatePgRoleAccessMenusService(map[string]interface{}{
|
|
|
"transactionContext": transactionContext,
|
|
|
})
|
|
|
menus, err := roleAccessMenusService.AccessMenus(nil, []int64{role.RoleId}, domain.AccessMenusOptions{
|
|
|
CompanyId: role.CompanyId,
|
|
|
ALLDisableMenu: 1,
|
|
|
})
|
|
|
if err != nil {
|
|
|
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
|
|
|
}
|
|
|
|
|
|
if err := transactionContext.CommitTransaction(); err != nil {
|
|
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
}
|
|
|
return nil, nil
|
|
|
return menus, nil
|
|
|
}
|
|
|
|
|
|
// 获取角色相关联的用户
|
...
|
...
|
@@ -164,10 +201,35 @@ func (roleService *RoleService) GetRoleRelatedUsers(getRoleRelatedUsersQuery *qu |
|
|
defer func() {
|
|
|
transactionContext.RollbackTransaction()
|
|
|
}()
|
|
|
|
|
|
_, role, err := factory.FastPgRole(transactionContext, getRoleRelatedUsersQuery.RoleId)
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
if !role.DeletedAt.IsZero() {
|
|
|
return nil, fmt.Errorf("角色不存在")
|
|
|
}
|
|
|
// 1.角色关联、未关联的用户
|
|
|
userRepository, _, err := factory.FastPgUser(transactionContext, 0)
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
queryOptions := make(map[string]interface{})
|
|
|
queryOptions["companyId"] = role.CompanyId
|
|
|
queryOptions["organizationId"] = getRoleRelatedUsersQuery.OrgId
|
|
|
if getRoleRelatedUsersQuery.DepartmentId > 0 {
|
|
|
queryOptions["departmentId"] = getRoleRelatedUsersQuery.DepartmentId
|
|
|
}
|
|
|
_, users, err := userRepository.Find(queryOptions)
|
|
|
if err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
roleRelatedUsersDto := &dto.RoleRelatedUsersDto{}
|
|
|
roleRelatedUsersDto.LoadDto(users, role.RoleId)
|
|
|
if err := transactionContext.CommitTransaction(); err != nil {
|
|
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
}
|
|
|
return nil, nil
|
|
|
return roleRelatedUsersDto, nil
|
|
|
}
|
|
|
|
|
|
// 返回角色列表
|
...
|
...
|
@@ -238,6 +300,9 @@ func (roleService *RoleService) RemoveRole(removeRoleCommand *command.RemoveRole |
|
|
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())
|
...
|
...
|
@@ -250,8 +315,8 @@ func (roleService *RoleService) RemoveRole(removeRoleCommand *command.RemoveRole |
|
|
}
|
|
|
|
|
|
// 取消用户分配的角色
|
|
|
func (roleService *RoleService) UnAssginRoleToUsers(unAssginRoleToUsersCommand *command.UnAssginRoleToUsersCommand) (interface{}, error) {
|
|
|
if err := unAssginRoleToUsersCommand.ValidateCommand(); err != nil {
|
|
|
func (roleService *RoleService) UnAssginRoleToUsers(unAssignRoleToUsersCommand *command.UnAssginRoleToUsersCommand) (interface{}, error) {
|
|
|
if err := unAssignRoleToUsersCommand.ValidateCommand(); err != nil {
|
|
|
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
|
|
|
}
|
|
|
transactionContext, err := factory.CreateTransactionContext(nil)
|
...
|
...
|
@@ -264,10 +329,29 @@ func (roleService *RoleService) UnAssginRoleToUsers(unAssginRoleToUsersCommand * |
|
|
defer func() {
|
|
|
transactionContext.RollbackTransaction()
|
|
|
}()
|
|
|
|
|
|
_, role, err := factory.FastPgRole(transactionContext, unAssignRoleToUsersCommand.RoleId)
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
userRepository, _, err := factory.FastPgUser(transactionContext, 0)
|
|
|
for i := range unAssignRoleToUsersCommand.UserIds {
|
|
|
userId := unAssignRoleToUsersCommand.UserIds[i]
|
|
|
if user, err := userRepository.FindOne(map[string]interface{}{"userId": userId}); err != nil {
|
|
|
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
|
|
|
} else {
|
|
|
if user.RemoveUserRole(role.RoleId) {
|
|
|
if _, err := userRepository.Save(user); err != nil {
|
|
|
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if err := transactionContext.CommitTransaction(); err != nil {
|
|
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
}
|
|
|
return nil, nil
|
|
|
return struct{}{}, nil
|
|
|
}
|
|
|
|
|
|
// 更新角色
|
...
|
...
|
|