pg_role_access_menus_service.go 3.4 KB
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.OperateInfo, roleIds []int64, option domain.AccessMenusOptions) ([]*domain.Menu, error) {
	var err error
	var menus []*domain.Menu
	menuIdSet := hashset.New()
	if len(roleIds) == 0 {
		return menus, nil
	}

	// 1.角色有权限的菜单列表
	roleRepository, _ := repository.NewRoleRepository(ptr.transactionContext)
	for i := range roleIds {
		var role *domain.Role
		if role, _ = roleRepository.FindOne(map[string]interface{}{"roleId": roleIds[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
	}
}