作者 yangfu

角色关联的用户、

分配、取消分配角色
@@ -70,3 +70,11 @@ func CreatePgCreateOrgService(options map[string]interface{}) (service.PgCreateO @@ -70,3 +70,11 @@ func CreatePgCreateOrgService(options map[string]interface{}) (service.PgCreateO
70 } 70 }
71 return domainService.NewPgCreateOrgService(transactionContext) 71 return domainService.NewPgCreateOrgService(transactionContext)
72 } 72 }
  73 +
  74 +func CreatePgRoleAccessMenusService(options map[string]interface{}) (service.PgRoleAccessMenusService, error) {
  75 + var transactionContext *pgTransaction.TransactionContext
  76 + if value, ok := options["transactionContext"]; ok {
  77 + transactionContext = value.(*pgTransaction.TransactionContext)
  78 + }
  79 + return domainService.NewPgRoleAccessMenusService(transactionContext)
  80 +}
@@ -2,7 +2,6 @@ package command @@ -2,7 +2,6 @@ package command
2 2
3 import ( 3 import (
4 "fmt" 4 "fmt"
5 -  
6 "github.com/beego/beego/v2/core/validation" 5 "github.com/beego/beego/v2/core/validation"
7 ) 6 )
8 7
@@ -23,7 +22,7 @@ type CreateMenuCommand struct { @@ -23,7 +22,7 @@ type CreateMenuCommand struct {
23 Sort int `json:"sort" valid:"Required"` 22 Sort int `json:"sort" valid:"Required"`
24 // 菜单说明 23 // 菜单说明
25 Desc string `json:"desc,omitempty"` 24 Desc string `json:"desc,omitempty"`
26 - // 菜单是否公开状态,[0:隐藏],[1:显示],默认显示 25 + // 菜单是否公开状态,[2:隐藏],[1:显示],默认显示
27 IsPublish int `json:"isPublish" valid:"Required"` 26 IsPublish int `json:"isPublish" valid:"Required"`
28 // 启用状态(启用:1 禁用:2),默认启用 27 // 启用状态(启用:1 禁用:2),默认启用
29 EnableStatus int `json:"enableStatus" ` 28 EnableStatus int `json:"enableStatus" `
@@ -45,4 +44,22 @@ func (createMenuCommand *CreateMenuCommand) ValidateCommand() error { @@ -45,4 +44,22 @@ func (createMenuCommand *CreateMenuCommand) ValidateCommand() error {
45 } 44 }
46 } 45 }
47 return nil 46 return nil
  47 +
  48 + //valid := validation.Validation{}
  49 + //b, err := valid.Valid(createMenuCommand)
  50 + //if err != nil {
  51 + // return err
  52 + //}
  53 + //if !b {
  54 + // elem := reflect.TypeOf(createMenuCommand).Elem()
  55 + // for _, validErr := range valid.Errors {
  56 + // field, isExist := elem.FieldByName(validErr.Field)
  57 + // if isExist {
  58 + // return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
  59 + // } else {
  60 + // return fmt.Errorf(validErr.Message)
  61 + // }
  62 + // }
  63 + //}
  64 + //return nil
48 } 65 }
@@ -106,6 +106,13 @@ func (menuService *MenuService) GetMenu(getMenuQuery *query.GetMenuQuery) (inter @@ -106,6 +106,13 @@ func (menuService *MenuService) GetMenu(getMenuQuery *query.GetMenuQuery) (inter
106 if err != nil { 106 if err != nil {
107 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 107 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
108 } 108 }
  109 + if menu.ParentId != 0 {
  110 + if _, parentMenu, err := factory.FastPgMenu(transactionContext, menu.ParentId); err != nil {
  111 + return nil, err
  112 + } else {
  113 + menu.ParentMenuName = parentMenu.MenuName
  114 + }
  115 + }
109 if menu == nil { 116 if menu == nil {
110 return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(getMenuQuery.MenuId))) 117 return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(getMenuQuery.MenuId)))
111 } else { 118 } else {
@@ -10,7 +10,7 @@ import ( @@ -10,7 +10,7 @@ import (
10 10
11 type AssginRoleToUsersCommand struct { 11 type AssginRoleToUsersCommand struct {
12 // 角色ID 12 // 角色ID
13 - RoleId int64 `cname:"角色ID" json:"roleId,string" valid:"Required"` 13 + RoleId int64 `cname:"角色ID" json:"roleId" valid:"Required"`
14 // 用户列表 14 // 用户列表
15 UserIds []int64 `cname:"用户列表" json:"userIds,omitempty"` 15 UserIds []int64 `cname:"用户列表" json:"userIds,omitempty"`
16 } 16 }
@@ -10,7 +10,7 @@ import ( @@ -10,7 +10,7 @@ import (
10 10
11 type UnAssginRoleToUsersCommand struct { 11 type UnAssginRoleToUsersCommand struct {
12 // 角色ID 12 // 角色ID
13 - RoleId int64 `cname:"角色ID" json:"roleId,string" valid:"Required"` 13 + RoleId int64 `cname:"角色ID" json:"roleId" valid:"Required"`
14 // 用户列表 14 // 用户列表
15 UserIds []int64 `cname:"用户列表" json:"userIds,omitempty"` 15 UserIds []int64 `cname:"用户列表" json:"userIds,omitempty"`
16 } 16 }
  1 +package dto
  2 +
  3 +import "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
  4 +
  5 +type RoleRelatedUsersDto struct {
  6 + NotInRoleUser []interface{} `json:"notInRoleUser"`
  7 + RoleUser []interface{} `json:"roleUser"`
  8 +}
  9 +
  10 +func (dto *RoleRelatedUsersDto) LoadDto(users []*domain.User, relatedRoleId int64) error {
  11 + var notInRoleUser = make([]interface{}, 0)
  12 + var roleUser = make([]interface{}, 0)
  13 +
  14 + for i := 0; i < len(users); i++ {
  15 + if ok := users[i].ExistsUserRole(relatedRoleId); ok {
  16 + roleUser = append(roleUser, newRelatedUsers(users[i]))
  17 + } else {
  18 + notInRoleUser = append(notInRoleUser, newRelatedUsers(users[i]))
  19 + }
  20 + }
  21 + dto.NotInRoleUser = notInRoleUser
  22 + dto.RoleUser = roleUser
  23 + return nil
  24 +}
  25 +
  26 +func newRelatedUsers(user *domain.User) map[string]interface{} {
  27 + return map[string]interface{}{
  28 + "departmentName": user.Ext.DepName,
  29 + "userId": user.UserId,
  30 + "userName": user.Ext.UserName,
  31 + }
  32 +}
@@ -10,11 +10,13 @@ import ( @@ -10,11 +10,13 @@ import (
10 10
11 type GetRoleRelatedUsersQuery struct { 11 type GetRoleRelatedUsersQuery struct {
12 // 角色ID 12 // 角色ID
13 - RoleId int64 `cname:"角色ID" json:"roleId,string" valid:"Required"` 13 + RoleId int64 `cname:"角色ID" json:"roleId" valid:"Required"`
14 // 组织ID 14 // 组织ID
15 OrgId int64 `cname:"组织ID" json:"orgId,string,omitempty"` 15 OrgId int64 `cname:"组织ID" json:"orgId,string,omitempty"`
16 // 部门编号 16 // 部门编号
17 - DepartmentId int64 `cname:"部门编号" json:"departmentId,string,omitempty"` 17 + DepartmentId int64 `cname:"部门编号" json:"departmentId,omitempty"`
  18 + // 只需要关联的用户 true:仅返回关联用户信息 false:返回所有其他信息(未关联的用户)
  19 + //OnlyRelatedUser bool `cname:"部门编号" json:"onlyRelatedUser,omitempty"`
18 } 20 }
19 21
20 func (getRoleRelatedUsersQuery *GetRoleRelatedUsersQuery) Valid(validation *validation.Validation) { 22 func (getRoleRelatedUsersQuery *GetRoleRelatedUsersQuery) Valid(validation *validation.Validation) {
@@ -6,6 +6,7 @@ import ( @@ -6,6 +6,7 @@ import (
6 "github.com/linmadan/egglib-go/utils/tool_funs" 6 "github.com/linmadan/egglib-go/utils/tool_funs"
7 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/factory" 7 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/factory"
8 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/role/command" 8 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/role/command"
  9 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/role/dto"
9 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/role/query" 10 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/role/query"
10 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain" 11 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
11 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/utils" 12 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/utils"
@@ -17,8 +18,8 @@ type RoleService struct { @@ -17,8 +18,8 @@ type RoleService struct {
17 } 18 }
18 19
19 // 分配角色给多个用户 20 // 分配角色给多个用户
20 -func (roleService *RoleService) AssginRoleToUsers(assginRoleToUsersCommand *command.AssginRoleToUsersCommand) (interface{}, error) {  
21 - if err := assginRoleToUsersCommand.ValidateCommand(); err != nil { 21 +func (roleService *RoleService) AssginRoleToUsers(assignRoleToUsersCommand *command.AssginRoleToUsersCommand) (interface{}, error) {
  22 + if err := assignRoleToUsersCommand.ValidateCommand(); err != nil {
22 return nil, application.ThrowError(application.ARG_ERROR, err.Error()) 23 return nil, application.ThrowError(application.ARG_ERROR, err.Error())
23 } 24 }
24 transactionContext, err := factory.CreateTransactionContext(nil) 25 transactionContext, err := factory.CreateTransactionContext(nil)
@@ -31,10 +32,30 @@ func (roleService *RoleService) AssginRoleToUsers(assginRoleToUsersCommand *comm @@ -31,10 +32,30 @@ func (roleService *RoleService) AssginRoleToUsers(assginRoleToUsersCommand *comm
31 defer func() { 32 defer func() {
32 transactionContext.RollbackTransaction() 33 transactionContext.RollbackTransaction()
33 }() 34 }()
  35 +
  36 + _, role, err := factory.FastPgRole(transactionContext, assignRoleToUsersCommand.RoleId)
  37 + if err != nil {
  38 + return nil, err
  39 + }
  40 + userRepository, _, err := factory.FastPgUser(transactionContext, 0)
  41 + for i := range assignRoleToUsersCommand.UserIds {
  42 + userId := assignRoleToUsersCommand.UserIds[i]
  43 + if user, err := userRepository.FindOne(map[string]interface{}{"userId": userId}); err != nil {
  44 + return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
  45 + } else {
  46 + if !user.ExistsUserRole(role.RoleId) {
  47 + user.UserRole = append(user.UserRole, role.CloneSample())
  48 + if _, err := userRepository.Save(user); err != nil {
  49 + return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
  50 + }
  51 + }
  52 + }
  53 + }
  54 +
34 if err := transactionContext.CommitTransaction(); err != nil { 55 if err := transactionContext.CommitTransaction(); err != nil {
35 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 56 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
36 } 57 }
37 - return nil, nil 58 + return struct{}{}, nil
38 } 59 }
39 60
40 // 创建角色 61 // 创建角色
@@ -143,10 +164,26 @@ func (roleService *RoleService) GetRoleAccessMenus(getRoleAccessMenusQuery *quer @@ -143,10 +164,26 @@ func (roleService *RoleService) GetRoleAccessMenus(getRoleAccessMenusQuery *quer
143 defer func() { 164 defer func() {
144 transactionContext.RollbackTransaction() 165 transactionContext.RollbackTransaction()
145 }() 166 }()
  167 +
  168 + _, role, err := factory.FastPgRole(transactionContext, getRoleAccessMenusQuery.RoleId)
  169 + if err != nil {
  170 + return nil, err
  171 + }
  172 + roleAccessMenusService, _ := factory.CreatePgRoleAccessMenusService(map[string]interface{}{
  173 + "transactionContext": transactionContext,
  174 + })
  175 + menus, err := roleAccessMenusService.AccessMenus(nil, []int64{role.RoleId}, domain.AccessMenusOptions{
  176 + CompanyId: role.CompanyId,
  177 + ALLDisableMenu: 1,
  178 + })
  179 + if err != nil {
  180 + return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
  181 + }
  182 +
146 if err := transactionContext.CommitTransaction(); err != nil { 183 if err := transactionContext.CommitTransaction(); err != nil {
147 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 184 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
148 } 185 }
149 - return nil, nil 186 + return menus, nil
150 } 187 }
151 188
152 // 获取角色相关联的用户 189 // 获取角色相关联的用户
@@ -164,10 +201,35 @@ func (roleService *RoleService) GetRoleRelatedUsers(getRoleRelatedUsersQuery *qu @@ -164,10 +201,35 @@ func (roleService *RoleService) GetRoleRelatedUsers(getRoleRelatedUsersQuery *qu
164 defer func() { 201 defer func() {
165 transactionContext.RollbackTransaction() 202 transactionContext.RollbackTransaction()
166 }() 203 }()
  204 +
  205 + _, role, err := factory.FastPgRole(transactionContext, getRoleRelatedUsersQuery.RoleId)
  206 + if err != nil {
  207 + return nil, err
  208 + }
  209 + if !role.DeletedAt.IsZero() {
  210 + return nil, fmt.Errorf("角色不存在")
  211 + }
  212 + // 1.角色关联、未关联的用户
  213 + userRepository, _, err := factory.FastPgUser(transactionContext, 0)
  214 + if err != nil {
  215 + return nil, err
  216 + }
  217 + queryOptions := make(map[string]interface{})
  218 + queryOptions["companyId"] = role.CompanyId
  219 + queryOptions["organizationId"] = getRoleRelatedUsersQuery.OrgId
  220 + if getRoleRelatedUsersQuery.DepartmentId > 0 {
  221 + queryOptions["departmentId"] = getRoleRelatedUsersQuery.DepartmentId
  222 + }
  223 + _, users, err := userRepository.Find(queryOptions)
  224 + if err != nil {
  225 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  226 + }
  227 + roleRelatedUsersDto := &dto.RoleRelatedUsersDto{}
  228 + roleRelatedUsersDto.LoadDto(users, role.RoleId)
167 if err := transactionContext.CommitTransaction(); err != nil { 229 if err := transactionContext.CommitTransaction(); err != nil {
168 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 230 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
169 } 231 }
170 - return nil, nil 232 + return roleRelatedUsersDto, nil
171 } 233 }
172 234
173 // 返回角色列表 235 // 返回角色列表
@@ -238,6 +300,9 @@ func (roleService *RoleService) RemoveRole(removeRoleCommand *command.RemoveRole @@ -238,6 +300,9 @@ func (roleService *RoleService) RemoveRole(removeRoleCommand *command.RemoveRole
238 if role == nil { 300 if role == nil {
239 return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(removeRoleCommand.RoleId))) 301 return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(removeRoleCommand.RoleId)))
240 } 302 }
  303 + if role.RoleType == domain.RoleTypeAdmin {
  304 + return nil, application.ThrowError(application.BUSINESS_ERROR, "主管理员角色不可删除")
  305 + }
241 role.DeletedAt = time.Now() 306 role.DeletedAt = time.Now()
242 if role, err := roleRepository.Remove(role); err != nil { 307 if role, err := roleRepository.Remove(role); err != nil {
243 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 308 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
@@ -250,8 +315,8 @@ func (roleService *RoleService) RemoveRole(removeRoleCommand *command.RemoveRole @@ -250,8 +315,8 @@ func (roleService *RoleService) RemoveRole(removeRoleCommand *command.RemoveRole
250 } 315 }
251 316
252 // 取消用户分配的角色 317 // 取消用户分配的角色
253 -func (roleService *RoleService) UnAssginRoleToUsers(unAssginRoleToUsersCommand *command.UnAssginRoleToUsersCommand) (interface{}, error) {  
254 - if err := unAssginRoleToUsersCommand.ValidateCommand(); err != nil { 318 +func (roleService *RoleService) UnAssginRoleToUsers(unAssignRoleToUsersCommand *command.UnAssginRoleToUsersCommand) (interface{}, error) {
  319 + if err := unAssignRoleToUsersCommand.ValidateCommand(); err != nil {
255 return nil, application.ThrowError(application.ARG_ERROR, err.Error()) 320 return nil, application.ThrowError(application.ARG_ERROR, err.Error())
256 } 321 }
257 transactionContext, err := factory.CreateTransactionContext(nil) 322 transactionContext, err := factory.CreateTransactionContext(nil)
@@ -264,10 +329,29 @@ func (roleService *RoleService) UnAssginRoleToUsers(unAssginRoleToUsersCommand * @@ -264,10 +329,29 @@ func (roleService *RoleService) UnAssginRoleToUsers(unAssginRoleToUsersCommand *
264 defer func() { 329 defer func() {
265 transactionContext.RollbackTransaction() 330 transactionContext.RollbackTransaction()
266 }() 331 }()
  332 +
  333 + _, role, err := factory.FastPgRole(transactionContext, unAssignRoleToUsersCommand.RoleId)
  334 + if err != nil {
  335 + return nil, err
  336 + }
  337 + userRepository, _, err := factory.FastPgUser(transactionContext, 0)
  338 + for i := range unAssignRoleToUsersCommand.UserIds {
  339 + userId := unAssignRoleToUsersCommand.UserIds[i]
  340 + if user, err := userRepository.FindOne(map[string]interface{}{"userId": userId}); err != nil {
  341 + return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
  342 + } else {
  343 + if user.RemoveUserRole(role.RoleId) {
  344 + if _, err := userRepository.Save(user); err != nil {
  345 + return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
  346 + }
  347 + }
  348 + }
  349 + }
  350 +
267 if err := transactionContext.CommitTransaction(); err != nil { 351 if err := transactionContext.CommitTransaction(); err != nil {
268 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 352 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
269 } 353 }
270 - return nil, nil 354 + return struct{}{}, nil
271 } 355 }
272 356
273 // 更新角色 357 // 更新角色
@@ -2,7 +2,6 @@ package dto @@ -2,7 +2,6 @@ package dto
2 2
3 import ( 3 import (
4 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain" 4 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
5 - "sort"  
6 ) 5 )
7 6
8 type UserAccessMenuDto []*Menu 7 type UserAccessMenuDto []*Menu
@@ -37,55 +36,21 @@ type Menu struct { @@ -37,55 +36,21 @@ type Menu struct {
37 EnableStatus int `json:"enableStatus,omitempty"` 36 EnableStatus int `json:"enableStatus,omitempty"`
38 } 37 }
39 38
40 -func (dto *UserAccessMenuDto) LoadDto(menus []*domain.Menu, alias []*domain.CustomizeMenu, access []interface{}, allDisableMenu int) error {  
41 - var mapMenus = make(map[int64]*domain.Menu) 39 +func (dto *UserAccessMenuDto) LoadDto(menus []*domain.Menu) error {
42 for i := range menus { 40 for i := range menus {
43 - menus[i].EnableStatus = domain.MenuStatusDisable  
44 - mapMenus[menus[i].MenuId] = menus[i]  
45 - }  
46 - // 设置别名  
47 - for i := range alias {  
48 - if v, ok := mapMenus[alias[i].MenuId]; ok {  
49 - v.MenuName = alias[i].MenuAlias  
50 - v.Sort = alias[i].Sort  
51 - }  
52 - }  
53 - if allDisableMenu == 1 {  
54 - for i := range access {  
55 - if v, ok := mapMenus[access[i].(int64)]; ok {  
56 - *dto = append(*dto, &Menu{  
57 - MenuId: v.MenuId,  
58 - ParentId: v.ParentId,  
59 - MenuName: v.MenuName,  
60 - Code: v.Code,  
61 - MenuType: v.MenuType,  
62 - Icon: v.Icon,  
63 - Sort: v.Sort,  
64 - EnableStatus: v.EnableStatus,  
65 - })  
66 - }  
67 - }  
68 - } else {  
69 - for i := range access {  
70 - if v, ok := mapMenus[access[i].(int64)]; ok {  
71 - v.EnableStatus = domain.MenuStatusEnable  
72 - }  
73 - }  
74 - for _, v := range mapMenus {  
75 - *dto = append(*dto, &Menu{  
76 - MenuId: v.MenuId,  
77 - ParentId: v.ParentId,  
78 - MenuName: v.MenuName,  
79 - Code: v.Code,  
80 - MenuType: v.MenuType,  
81 - Icon: v.Icon,  
82 - Sort: v.Sort,  
83 - EnableStatus: v.EnableStatus,  
84 - })  
85 - } 41 + v := menus[i]
  42 + *dto = append(*dto, &Menu{
  43 + MenuId: v.MenuId,
  44 + ParentId: v.ParentId,
  45 + MenuName: v.MenuName,
  46 + Code: v.Code,
  47 + MenuType: v.MenuType,
  48 + Icon: v.Icon,
  49 + Sort: v.Sort,
  50 + EnableStatus: v.EnableStatus,
  51 + })
86 } 52 }
87 53
88 - sort.Stable(dto)  
89 return nil 54 return nil
90 } 55 }
91 56
@@ -16,7 +16,7 @@ type GetUserAccessMenusQuery struct { @@ -16,7 +16,7 @@ type GetUserAccessMenusQuery struct {
16 // 菜单类别 web app 16 // 菜单类别 web app
17 MenuCategory string `cname:"菜单类别 web app" json:"menuCategory,omitempty"` 17 MenuCategory string `cname:"菜单类别 web app" json:"menuCategory,omitempty"`
18 // 返回所有不可以用的菜单 18 // 返回所有不可以用的菜单
19 - ALLDisableMenu int `cname:"同时返回所有不可以用的菜单 1:包含不可用的菜单 0:过滤不可用的菜单 " json:"aLLDisableMenu"` 19 + ALLDisableMenu int `cname:"同时返回所有不可以用的菜单 1:包含不可用的菜单 0:过滤不可用的菜单 " json:"allDisableMenu"`
20 } 20 }
21 21
22 func (getUserAccessMenusQuery *GetUserAccessMenusQuery) Valid(validation *validation.Validation) { 22 func (getUserAccessMenusQuery *GetUserAccessMenusQuery) Valid(validation *validation.Validation) {
@@ -2,7 +2,6 @@ package service @@ -2,7 +2,6 @@ package service
2 2
3 import ( 3 import (
4 "fmt" 4 "fmt"
5 - "github.com/emirpasic/gods/sets/hashset"  
6 "github.com/linmadan/egglib-go/core/application" 5 "github.com/linmadan/egglib-go/core/application"
7 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/factory" 6 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/factory"
8 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/user/command" 7 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/user/command"
@@ -10,7 +9,6 @@ import ( @@ -10,7 +9,6 @@ import (
10 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/user/query" 9 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/user/query"
11 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain" 10 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
12 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/utils" 11 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/utils"
13 - "strconv"  
14 "time" 12 "time"
15 ) 13 )
16 14
@@ -357,52 +355,24 @@ func (userService *UserService) GetUserAccessMenus(getUserAccessMenusQuery *quer @@ -357,52 +355,24 @@ func (userService *UserService) GetUserAccessMenus(getUserAccessMenusQuery *quer
357 if err != nil { 355 if err != nil {
358 return nil, err 356 return nil, err
359 } 357 }
360 - menuIdSet := hashset.New()  
361 - for i := range user.UserRole {  
362 - var role *domain.Role  
363 - if _, role, err = factory.FastPgRole(transactionContext, user.UserRole[i].RoleId); err != nil {  
364 - return nil, err  
365 - }  
366 - // 只要当前登录组织的有权限菜单  
367 - if getUserAccessMenusQuery.OrgId > 0 && getUserAccessMenusQuery.OrgId != role.OrgId {  
368 - continue  
369 - }  
370 - for i := 0; i < len(role.AccessMenus); i++ {  
371 - menuIdSet.Add(role.AccessMenus[i])  
372 - }  
373 - }  
374 358
375 - // 2.所有的菜单  
376 - menuRepository, _, err := factory.FastPgMenu(transactionContext, 0)  
377 - if err != nil {  
378 - return nil, err  
379 - }  
380 - queryOptions := make(map[string]interface{})  
381 - queryOptions["isPublish"] = domain.MenuPublic  
382 - if len(getUserAccessMenusQuery.MenuCategory) > 0 {  
383 - queryOptions["code"] = ""  
384 - if m, e := menuRepository.FindOne(map[string]interface{}{"code": getUserAccessMenusQuery.MenuCategory}); e == nil && m != nil {  
385 - queryOptions["category"] = strconv.Itoa(int(m.MenuId))  
386 - }  
387 - }  
388 - _, menus, err := menuRepository.Find(queryOptions)  
389 - if err != nil {  
390 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())  
391 - } 359 + roleAccessMenusService, _ := factory.CreatePgRoleAccessMenusService(map[string]interface{}{
  360 + "transactionContext": transactionContext,
  361 + })
392 362
393 - // 3.自定义菜单  
394 - customizeMenuRepository, _, err := factory.FastPgCustomizeMenu(transactionContext, 0)  
395 - if err != nil {  
396 - return nil, err  
397 - }  
398 - _, customizeMenus, err := customizeMenuRepository.Find(map[string]interface{}{"companyId": user.CompanyId}) 363 + menus, err := roleAccessMenusService.AccessMenus(nil, user.UserOrgIds(), domain.AccessMenusOptions{
  364 + CompanyId: user.CompanyId,
  365 + MenuCategory: getUserAccessMenusQuery.MenuCategory,
  366 + OrgId: getUserAccessMenusQuery.OrgId,
  367 + ALLDisableMenu: getUserAccessMenusQuery.ALLDisableMenu,
  368 + })
399 if err != nil { 369 if err != nil {
400 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 370 + return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
401 } 371 }
402 372
403 - // 4.适配 373 + // 2.适配
404 accessMenuDto := dto.NewUserAccessMenuDto() 374 accessMenuDto := dto.NewUserAccessMenuDto()
405 - accessMenuDto.LoadDto(menus, customizeMenus, menuIdSet.Values(), getUserAccessMenusQuery.ALLDisableMenu) 375 + accessMenuDto.LoadDto(menus)
406 if err := transactionContext.CommitTransaction(); err != nil { 376 if err := transactionContext.CommitTransaction(); err != nil {
407 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 377 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
408 } 378 }
@@ -70,6 +70,9 @@ type Menu struct { @@ -70,6 +70,9 @@ type Menu struct {
70 IsPublish int `json:"isPublish,omitempty"` 70 IsPublish int `json:"isPublish,omitempty"`
71 // 启用状态(启用:1 禁用:2),默认启用 71 // 启用状态(启用:1 禁用:2),默认启用
72 EnableStatus int `json:"enableStatus,omitempty"` 72 EnableStatus int `json:"enableStatus,omitempty"`
  73 +
  74 + // 父级菜单名称
  75 + ParentMenuName string `json:"parentMenuName,omitempty"`
73 } 76 }
74 77
75 type MenuRepository interface { 78 type MenuRepository interface {
@@ -99,6 +99,17 @@ func (role *Role) Update(data map[string]interface{}) error { @@ -99,6 +99,17 @@ func (role *Role) Update(data map[string]interface{}) error {
99 return nil 99 return nil
100 } 100 }
101 101
  102 +type AccessMenusOptions struct {
  103 + // 当前登录组织orgId
  104 + OrgId int64 `cname:"当前登录组织Id" json:"orgId"`
  105 + // 菜单类别 web app
  106 + MenuCategory string `cname:"菜单类别 web app" json:"menuCategory,omitempty"`
  107 + // 返回所有不可以用的菜单
  108 + ALLDisableMenu int `cname:"同时返回所有不可以用的菜单 1:包含不可用的菜单 0:过滤不可用的菜单 " json:"aLLDisableMenu"`
  109 + // 所属公司
  110 + CompanyId int64
  111 +}
  112 +
102 /***** 1.自定义函数模块 *****/ 113 /***** 1.自定义函数模块 *****/
103 /*1.1 拷贝简单的角色信息*/ 114 /*1.1 拷贝简单的角色信息*/
104 115
  1 +package service
  2 +
  3 +import "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
  4 +
  5 +// 角色有权限的菜单
  6 +type PgRoleAccessMenusService interface {
  7 + AccessMenus(options *domain.CheckOptions, roleId []int64, option domain.AccessMenusOptions) ([]*domain.Menu, error)
  8 +}
@@ -180,14 +180,55 @@ func (user *User) SetEnableStatus(enableStatus int) error { @@ -180,14 +180,55 @@ func (user *User) SetEnableStatus(enableStatus int) error {
180 return nil 180 return nil
181 } 181 }
182 182
  183 +func (user *User) UserOrgIds() []int64 {
  184 + var roleIds = make([]int64, len(user.UserOrg))
  185 + for i := range user.UserOrg {
  186 + roleIds[i] = user.UserOrg[i].OrgId
  187 + }
  188 + return roleIds
  189 +}
  190 +
  191 +// ExistsUserRole 用户是否已经拥有角色
  192 +//
  193 +// roleId 角色ID
  194 +func (user *User) ExistsUserRole(roleId int64) bool {
  195 + for i := range user.UserRole {
  196 + if user.UserRole[i].RoleId == roleId {
  197 + return true
  198 + }
  199 + }
  200 + return false
  201 +}
  202 +
  203 +// RemoveUserRole 移除用户拥有的角色
  204 +//
  205 +// roleId 角色ID
  206 +func (user *User) RemoveUserRole(roleId int64) bool {
  207 + var res bool = false
  208 + if user.ExistsUserRole(roleId) {
  209 +
  210 + var roles []*Role
  211 + for i := range user.UserRole {
  212 + if user.UserRole[i].RoleId != roleId {
  213 + roles = append(roles, user.UserRole[i])
  214 + } else {
  215 + res = true
  216 + }
  217 + }
  218 + user.UserRole = roles
  219 + return res
  220 + }
  221 + return res
  222 +}
  223 +
183 /***** 2.缓存模块 *****/ 224 /***** 2.缓存模块 *****/
184 225
185 -func (m *User) CacheKeyFunc() string { 226 +func (user *User) CacheKeyFunc() string {
186 if constant.DISABLE_REPOSITORY_CACHE { 227 if constant.DISABLE_REPOSITORY_CACHE {
187 return "" 228 return ""
188 } 229 }
189 - if m.UserId == 0 { 230 + if user.UserId == 0 {
190 return "" 231 return ""
191 } 232 }
192 - return fmt.Sprintf("%v:cache:users:id:%v", constant.CACHE_PREFIX, m.UserId) 233 + return fmt.Sprintf("%v:cache:users:id:%v", constant.CACHE_PREFIX, user.UserId)
193 } 234 }
  1 +package domainService
  2 +
  3 +import (
  4 + "fmt"
  5 + "github.com/emirpasic/gods/sets/hashset"
  6 + "github.com/linmadan/egglib-go/core/application"
  7 + pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
  8 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
  9 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/repository"
  10 + "strconv"
  11 +)
  12 +
  13 +// 角色有权限的菜单
  14 +type PgRoleAccessMenusService struct {
  15 + transactionContext *pgTransaction.TransactionContext
  16 +}
  17 +
  18 +// DataAuth 数据权限验证
  19 +//
  20 +// options 数据参数
  21 +// data 需要验证权限的数据
  22 +func (ptr *PgRoleAccessMenusService) AccessMenus(options *domain.CheckOptions, roleId []int64, option domain.AccessMenusOptions) ([]*domain.Menu, error) {
  23 + var err error
  24 + var menus []*domain.Menu
  25 + menuIdSet := hashset.New()
  26 + if len(roleId) == 0 {
  27 + return menus, nil
  28 + }
  29 +
  30 + // 1.角色有权限的菜单列表
  31 + roleRepository, _ := repository.NewRoleRepository(ptr.transactionContext)
  32 + for i := range roleId {
  33 + var role *domain.Role
  34 + if role, _ = roleRepository.FindOne(map[string]interface{}{"roleId": roleId[i]}); role == nil {
  35 + continue
  36 + }
  37 + // 只要当前登录组织的有权限菜单
  38 + if option.OrgId > 0 && option.OrgId != role.OrgId {
  39 + continue
  40 + }
  41 + for i := 0; i < len(role.AccessMenus); i++ {
  42 + menuIdSet.Add(role.AccessMenus[i])
  43 + }
  44 + }
  45 +
  46 + // 2.获取所有公开的菜单
  47 + menuRepository, _ := repository.NewMenuRepository(ptr.transactionContext)
  48 + if err != nil {
  49 + return nil, err
  50 + }
  51 + queryOptions := make(map[string]interface{})
  52 + queryOptions["isPublish"] = domain.MenuPublic
  53 + if len(option.MenuCategory) > 0 {
  54 + queryOptions["code"] = ""
  55 + if m, e := menuRepository.FindOne(map[string]interface{}{"code": option.MenuCategory}); e == nil && m != nil {
  56 + queryOptions["category"] = strconv.Itoa(int(m.MenuId))
  57 + }
  58 + }
  59 + _, menus, err = menuRepository.Find(queryOptions)
  60 + if err != nil {
  61 + return nil, err
  62 + }
  63 +
  64 + // 3.获取自定义菜单
  65 + customizeMenuRepository, _ := repository.NewCustomizeMenuRepository(ptr.transactionContext)
  66 + if err != nil {
  67 + return nil, err
  68 + }
  69 + _, alias, err := customizeMenuRepository.Find(map[string]interface{}{"companyId": option.CompanyId})
  70 + if err != nil {
  71 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  72 + }
  73 +
  74 + // 4. 设置别名、返回有效的菜单列表
  75 + var mapMenus = make(map[int64]*domain.Menu)
  76 + for i := range menus {
  77 + menus[i].EnableStatus = domain.MenuStatusDisable
  78 + mapMenus[menus[i].MenuId] = menus[i]
  79 + }
  80 + for i := range alias {
  81 + if v, ok := mapMenus[alias[i].MenuId]; ok {
  82 + v.MenuName = alias[i].MenuAlias // 设置别名
  83 + v.Sort = alias[i].Sort
  84 + }
  85 + }
  86 + values := menuIdSet.Values()
  87 + for i := range values {
  88 + menuId := (values[i]).(int64) // 设置菜单权限状态
  89 + if v, ok := mapMenus[menuId]; ok {
  90 + v.EnableStatus = domain.MenuStatusEnable
  91 + }
  92 + }
  93 +
  94 + // 5.返回数据
  95 + // 返回所有菜单
  96 + if option.ALLDisableMenu == 1 {
  97 + return menus, nil
  98 + }
  99 + // 返回有权限的菜单
  100 + var enableMenus []*domain.Menu
  101 + for i := range menus {
  102 + if menus[i].EnableStatus == domain.MenuStatusEnable {
  103 + enableMenus = append(enableMenus, menus[i])
  104 + }
  105 + }
  106 + return enableMenus, nil
  107 +}
  108 +
  109 +func NewPgRoleAccessMenusService(transactionContext *pgTransaction.TransactionContext) (*PgRoleAccessMenusService, error) {
  110 + if transactionContext == nil {
  111 + return nil, fmt.Errorf("transactionContext参数不能为nil")
  112 + } else {
  113 + return &PgRoleAccessMenusService{
  114 + transactionContext: transactionContext,
  115 + }, nil
  116 + }
  117 +}
@@ -63,7 +63,7 @@ func (controller *UserController) GetUserAccessMenus() { @@ -63,7 +63,7 @@ func (controller *UserController) GetUserAccessMenus() {
63 menuCategory := controller.GetString("menuCategory") 63 menuCategory := controller.GetString("menuCategory")
64 getUserAccessMenusQuery.MenuCategory = menuCategory 64 getUserAccessMenusQuery.MenuCategory = menuCategory
65 getUserAccessMenusQuery.OrgId, _ = controller.GetInt64("orgId", 0) 65 getUserAccessMenusQuery.OrgId, _ = controller.GetInt64("orgId", 0)
66 - getUserAccessMenusQuery.ALLDisableMenu, _ = controller.GetInt("aLLDisableMenu", 0) 66 + getUserAccessMenusQuery.ALLDisableMenu, _ = controller.GetInt("allDisableMenu", 0)
67 data, err := userService.GetUserAccessMenus(getUserAccessMenusQuery) 67 data, err := userService.GetUserAccessMenus(getUserAccessMenusQuery)
68 controller.Response(data, err) 68 controller.Response(data, err)
69 } 69 }