作者 yangfu

角色关联的用户、

分配、取消分配角色
... ... @@ -70,3 +70,11 @@ func CreatePgCreateOrgService(options map[string]interface{}) (service.PgCreateO
}
return domainService.NewPgCreateOrgService(transactionContext)
}
func CreatePgRoleAccessMenusService(options map[string]interface{}) (service.PgRoleAccessMenusService, error) {
var transactionContext *pgTransaction.TransactionContext
if value, ok := options["transactionContext"]; ok {
transactionContext = value.(*pgTransaction.TransactionContext)
}
return domainService.NewPgRoleAccessMenusService(transactionContext)
}
... ...
... ... @@ -2,7 +2,6 @@ package command
import (
"fmt"
"github.com/beego/beego/v2/core/validation"
)
... ... @@ -23,7 +22,7 @@ type CreateMenuCommand struct {
Sort int `json:"sort" valid:"Required"`
// 菜单说明
Desc string `json:"desc,omitempty"`
// 菜单是否公开状态,[0:隐藏],[1:显示],默认显示
// 菜单是否公开状态,[2:隐藏],[1:显示],默认显示
IsPublish int `json:"isPublish" valid:"Required"`
// 启用状态(启用:1 禁用:2),默认启用
EnableStatus int `json:"enableStatus" `
... ... @@ -45,4 +44,22 @@ func (createMenuCommand *CreateMenuCommand) ValidateCommand() error {
}
}
return nil
//valid := validation.Validation{}
//b, err := valid.Valid(createMenuCommand)
//if err != nil {
// return err
//}
//if !b {
// elem := reflect.TypeOf(createMenuCommand).Elem()
// for _, validErr := range valid.Errors {
// field, isExist := elem.FieldByName(validErr.Field)
// if isExist {
// return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
// } else {
// return fmt.Errorf(validErr.Message)
// }
// }
//}
//return nil
}
... ...
... ... @@ -106,6 +106,13 @@ func (menuService *MenuService) GetMenu(getMenuQuery *query.GetMenuQuery) (inter
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if menu.ParentId != 0 {
if _, parentMenu, err := factory.FastPgMenu(transactionContext, menu.ParentId); err != nil {
return nil, err
} else {
menu.ParentMenuName = parentMenu.MenuName
}
}
if menu == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(getMenuQuery.MenuId)))
} else {
... ...
... ... @@ -10,7 +10,7 @@ import (
type AssginRoleToUsersCommand struct {
// 角色ID
RoleId int64 `cname:"角色ID" json:"roleId,string" valid:"Required"`
RoleId int64 `cname:"角色ID" json:"roleId" valid:"Required"`
// 用户列表
UserIds []int64 `cname:"用户列表" json:"userIds,omitempty"`
}
... ...
... ... @@ -10,7 +10,7 @@ import (
type UnAssginRoleToUsersCommand struct {
// 角色ID
RoleId int64 `cname:"角色ID" json:"roleId,string" valid:"Required"`
RoleId int64 `cname:"角色ID" json:"roleId" valid:"Required"`
// 用户列表
UserIds []int64 `cname:"用户列表" json:"userIds,omitempty"`
}
... ...
package dto
import "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
type RoleRelatedUsersDto struct {
NotInRoleUser []interface{} `json:"notInRoleUser"`
RoleUser []interface{} `json:"roleUser"`
}
func (dto *RoleRelatedUsersDto) LoadDto(users []*domain.User, relatedRoleId int64) error {
var notInRoleUser = make([]interface{}, 0)
var roleUser = make([]interface{}, 0)
for i := 0; i < len(users); i++ {
if ok := users[i].ExistsUserRole(relatedRoleId); ok {
roleUser = append(roleUser, newRelatedUsers(users[i]))
} else {
notInRoleUser = append(notInRoleUser, newRelatedUsers(users[i]))
}
}
dto.NotInRoleUser = notInRoleUser
dto.RoleUser = roleUser
return nil
}
func newRelatedUsers(user *domain.User) map[string]interface{} {
return map[string]interface{}{
"departmentName": user.Ext.DepName,
"userId": user.UserId,
"userName": user.Ext.UserName,
}
}
... ...
... ... @@ -10,11 +10,13 @@ import (
type GetRoleRelatedUsersQuery struct {
// 角色ID
RoleId int64 `cname:"角色ID" json:"roleId,string" valid:"Required"`
RoleId int64 `cname:"角色ID" json:"roleId" valid:"Required"`
// 组织ID
OrgId int64 `cname:"组织ID" json:"orgId,string,omitempty"`
// 部门编号
DepartmentId int64 `cname:"部门编号" json:"departmentId,string,omitempty"`
DepartmentId int64 `cname:"部门编号" json:"departmentId,omitempty"`
// 只需要关联的用户 true:仅返回关联用户信息 false:返回所有其他信息(未关联的用户)
//OnlyRelatedUser bool `cname:"部门编号" json:"onlyRelatedUser,omitempty"`
}
func (getRoleRelatedUsersQuery *GetRoleRelatedUsersQuery) Valid(validation *validation.Validation) {
... ...
... ... @@ -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
}
// 更新角色
... ...
... ... @@ -2,7 +2,6 @@ package dto
import (
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
"sort"
)
type UserAccessMenuDto []*Menu
... ... @@ -37,55 +36,21 @@ type Menu struct {
EnableStatus int `json:"enableStatus,omitempty"`
}
func (dto *UserAccessMenuDto) LoadDto(menus []*domain.Menu, alias []*domain.CustomizeMenu, access []interface{}, allDisableMenu int) error {
var mapMenus = make(map[int64]*domain.Menu)
func (dto *UserAccessMenuDto) LoadDto(menus []*domain.Menu) error {
for i := range menus {
menus[i].EnableStatus = domain.MenuStatusDisable
mapMenus[menus[i].MenuId] = menus[i]
}
// 设置别名
for i := range alias {
if v, ok := mapMenus[alias[i].MenuId]; ok {
v.MenuName = alias[i].MenuAlias
v.Sort = alias[i].Sort
}
}
if allDisableMenu == 1 {
for i := range access {
if v, ok := mapMenus[access[i].(int64)]; ok {
*dto = append(*dto, &Menu{
MenuId: v.MenuId,
ParentId: v.ParentId,
MenuName: v.MenuName,
Code: v.Code,
MenuType: v.MenuType,
Icon: v.Icon,
Sort: v.Sort,
EnableStatus: v.EnableStatus,
})
}
}
} else {
for i := range access {
if v, ok := mapMenus[access[i].(int64)]; ok {
v.EnableStatus = domain.MenuStatusEnable
}
}
for _, v := range mapMenus {
*dto = append(*dto, &Menu{
MenuId: v.MenuId,
ParentId: v.ParentId,
MenuName: v.MenuName,
Code: v.Code,
MenuType: v.MenuType,
Icon: v.Icon,
Sort: v.Sort,
EnableStatus: v.EnableStatus,
})
}
v := menus[i]
*dto = append(*dto, &Menu{
MenuId: v.MenuId,
ParentId: v.ParentId,
MenuName: v.MenuName,
Code: v.Code,
MenuType: v.MenuType,
Icon: v.Icon,
Sort: v.Sort,
EnableStatus: v.EnableStatus,
})
}
sort.Stable(dto)
return nil
}
... ...
... ... @@ -16,7 +16,7 @@ type GetUserAccessMenusQuery struct {
// 菜单类别 web app
MenuCategory string `cname:"菜单类别 web app" json:"menuCategory,omitempty"`
// 返回所有不可以用的菜单
ALLDisableMenu int `cname:"同时返回所有不可以用的菜单 1:包含不可用的菜单 0:过滤不可用的菜单 " json:"aLLDisableMenu"`
ALLDisableMenu int `cname:"同时返回所有不可以用的菜单 1:包含不可用的菜单 0:过滤不可用的菜单 " json:"allDisableMenu"`
}
func (getUserAccessMenusQuery *GetUserAccessMenusQuery) Valid(validation *validation.Validation) {
... ...
... ... @@ -2,7 +2,6 @@ package service
import (
"fmt"
"github.com/emirpasic/gods/sets/hashset"
"github.com/linmadan/egglib-go/core/application"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/factory"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/user/command"
... ... @@ -10,7 +9,6 @@ import (
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/user/query"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/utils"
"strconv"
"time"
)
... ... @@ -357,52 +355,24 @@ func (userService *UserService) GetUserAccessMenus(getUserAccessMenusQuery *quer
if err != nil {
return nil, err
}
menuIdSet := hashset.New()
for i := range user.UserRole {
var role *domain.Role
if _, role, err = factory.FastPgRole(transactionContext, user.UserRole[i].RoleId); err != nil {
return nil, err
}
// 只要当前登录组织的有权限菜单
if getUserAccessMenusQuery.OrgId > 0 && getUserAccessMenusQuery.OrgId != role.OrgId {
continue
}
for i := 0; i < len(role.AccessMenus); i++ {
menuIdSet.Add(role.AccessMenus[i])
}
}
// 2.所有的菜单
menuRepository, _, err := factory.FastPgMenu(transactionContext, 0)
if err != nil {
return nil, err
}
queryOptions := make(map[string]interface{})
queryOptions["isPublish"] = domain.MenuPublic
if len(getUserAccessMenusQuery.MenuCategory) > 0 {
queryOptions["code"] = ""
if m, e := menuRepository.FindOne(map[string]interface{}{"code": getUserAccessMenusQuery.MenuCategory}); e == nil && m != nil {
queryOptions["category"] = strconv.Itoa(int(m.MenuId))
}
}
_, menus, err := menuRepository.Find(queryOptions)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
roleAccessMenusService, _ := factory.CreatePgRoleAccessMenusService(map[string]interface{}{
"transactionContext": transactionContext,
})
// 3.自定义菜单
customizeMenuRepository, _, err := factory.FastPgCustomizeMenu(transactionContext, 0)
if err != nil {
return nil, err
}
_, customizeMenus, err := customizeMenuRepository.Find(map[string]interface{}{"companyId": user.CompanyId})
menus, err := roleAccessMenusService.AccessMenus(nil, user.UserOrgIds(), domain.AccessMenusOptions{
CompanyId: user.CompanyId,
MenuCategory: getUserAccessMenusQuery.MenuCategory,
OrgId: getUserAccessMenusQuery.OrgId,
ALLDisableMenu: getUserAccessMenusQuery.ALLDisableMenu,
})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
// 4.适配
// 2.适配
accessMenuDto := dto.NewUserAccessMenuDto()
accessMenuDto.LoadDto(menus, customizeMenus, menuIdSet.Values(), getUserAccessMenusQuery.ALLDisableMenu)
accessMenuDto.LoadDto(menus)
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
... ...
... ... @@ -70,6 +70,9 @@ type Menu struct {
IsPublish int `json:"isPublish,omitempty"`
// 启用状态(启用:1 禁用:2),默认启用
EnableStatus int `json:"enableStatus,omitempty"`
// 父级菜单名称
ParentMenuName string `json:"parentMenuName,omitempty"`
}
type MenuRepository interface {
... ...
... ... @@ -99,6 +99,17 @@ func (role *Role) Update(data map[string]interface{}) error {
return nil
}
type AccessMenusOptions struct {
// 当前登录组织orgId
OrgId int64 `cname:"当前登录组织Id" json:"orgId"`
// 菜单类别 web app
MenuCategory string `cname:"菜单类别 web app" json:"menuCategory,omitempty"`
// 返回所有不可以用的菜单
ALLDisableMenu int `cname:"同时返回所有不可以用的菜单 1:包含不可用的菜单 0:过滤不可用的菜单 " json:"aLLDisableMenu"`
// 所属公司
CompanyId int64
}
/***** 1.自定义函数模块 *****/
/*1.1 拷贝简单的角色信息*/
... ...
package service
import "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
// 角色有权限的菜单
type PgRoleAccessMenusService interface {
AccessMenus(options *domain.CheckOptions, roleId []int64, option domain.AccessMenusOptions) ([]*domain.Menu, error)
}
... ...
... ... @@ -180,14 +180,55 @@ func (user *User) SetEnableStatus(enableStatus int) error {
return nil
}
func (user *User) UserOrgIds() []int64 {
var roleIds = make([]int64, len(user.UserOrg))
for i := range user.UserOrg {
roleIds[i] = user.UserOrg[i].OrgId
}
return roleIds
}
// ExistsUserRole 用户是否已经拥有角色
//
// roleId 角色ID
func (user *User) ExistsUserRole(roleId int64) bool {
for i := range user.UserRole {
if user.UserRole[i].RoleId == roleId {
return true
}
}
return false
}
// RemoveUserRole 移除用户拥有的角色
//
// roleId 角色ID
func (user *User) RemoveUserRole(roleId int64) bool {
var res bool = false
if user.ExistsUserRole(roleId) {
var roles []*Role
for i := range user.UserRole {
if user.UserRole[i].RoleId != roleId {
roles = append(roles, user.UserRole[i])
} else {
res = true
}
}
user.UserRole = roles
return res
}
return res
}
/***** 2.缓存模块 *****/
func (m *User) CacheKeyFunc() string {
func (user *User) CacheKeyFunc() string {
if constant.DISABLE_REPOSITORY_CACHE {
return ""
}
if m.UserId == 0 {
if user.UserId == 0 {
return ""
}
return fmt.Sprintf("%v:cache:users:id:%v", constant.CACHE_PREFIX, m.UserId)
return fmt.Sprintf("%v:cache:users:id:%v", constant.CACHE_PREFIX, user.UserId)
}
... ...
package domainService
import (
"fmt"
"github.com/emirpasic/gods/sets/hashset"
"github.com/linmadan/egglib-go/core/application"
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"
"strconv"
)
// 角色有权限的菜单
type PgRoleAccessMenusService struct {
transactionContext *pgTransaction.TransactionContext
}
// DataAuth 数据权限验证
//
// options 数据参数
// data 需要验证权限的数据
func (ptr *PgRoleAccessMenusService) AccessMenus(options *domain.CheckOptions, roleId []int64, option domain.AccessMenusOptions) ([]*domain.Menu, error) {
var err error
var menus []*domain.Menu
menuIdSet := hashset.New()
if len(roleId) == 0 {
return menus, nil
}
// 1.角色有权限的菜单列表
roleRepository, _ := repository.NewRoleRepository(ptr.transactionContext)
for i := range roleId {
var role *domain.Role
if role, _ = roleRepository.FindOne(map[string]interface{}{"roleId": roleId[i]}); role == nil {
continue
}
// 只要当前登录组织的有权限菜单
if option.OrgId > 0 && option.OrgId != role.OrgId {
continue
}
for i := 0; i < len(role.AccessMenus); i++ {
menuIdSet.Add(role.AccessMenus[i])
}
}
// 2.获取所有公开的菜单
menuRepository, _ := repository.NewMenuRepository(ptr.transactionContext)
if err != nil {
return nil, err
}
queryOptions := make(map[string]interface{})
queryOptions["isPublish"] = domain.MenuPublic
if len(option.MenuCategory) > 0 {
queryOptions["code"] = ""
if m, e := menuRepository.FindOne(map[string]interface{}{"code": option.MenuCategory}); e == nil && m != nil {
queryOptions["category"] = strconv.Itoa(int(m.MenuId))
}
}
_, menus, err = menuRepository.Find(queryOptions)
if err != nil {
return nil, err
}
// 3.获取自定义菜单
customizeMenuRepository, _ := repository.NewCustomizeMenuRepository(ptr.transactionContext)
if err != nil {
return nil, err
}
_, alias, err := customizeMenuRepository.Find(map[string]interface{}{"companyId": option.CompanyId})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
// 4. 设置别名、返回有效的菜单列表
var mapMenus = make(map[int64]*domain.Menu)
for i := range menus {
menus[i].EnableStatus = domain.MenuStatusDisable
mapMenus[menus[i].MenuId] = menus[i]
}
for i := range alias {
if v, ok := mapMenus[alias[i].MenuId]; ok {
v.MenuName = alias[i].MenuAlias // 设置别名
v.Sort = alias[i].Sort
}
}
values := menuIdSet.Values()
for i := range values {
menuId := (values[i]).(int64) // 设置菜单权限状态
if v, ok := mapMenus[menuId]; ok {
v.EnableStatus = domain.MenuStatusEnable
}
}
// 5.返回数据
// 返回所有菜单
if option.ALLDisableMenu == 1 {
return menus, nil
}
// 返回有权限的菜单
var enableMenus []*domain.Menu
for i := range menus {
if menus[i].EnableStatus == domain.MenuStatusEnable {
enableMenus = append(enableMenus, menus[i])
}
}
return enableMenus, nil
}
func NewPgRoleAccessMenusService(transactionContext *pgTransaction.TransactionContext) (*PgRoleAccessMenusService, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &PgRoleAccessMenusService{
transactionContext: transactionContext,
}, nil
}
}
... ...
... ... @@ -63,7 +63,7 @@ func (controller *UserController) GetUserAccessMenus() {
menuCategory := controller.GetString("menuCategory")
getUserAccessMenusQuery.MenuCategory = menuCategory
getUserAccessMenusQuery.OrgId, _ = controller.GetInt64("orgId", 0)
getUserAccessMenusQuery.ALLDisableMenu, _ = controller.GetInt("aLLDisableMenu", 0)
getUserAccessMenusQuery.ALLDisableMenu, _ = controller.GetInt("allDisableMenu", 0)
data, err := userService.GetUserAccessMenus(getUserAccessMenusQuery)
controller.Response(data, err)
}
... ...