module_access.go 3.1 KB
package service

import (
	"fmt"
	"github.com/linmadan/egglib-go/core/application"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/auth/command"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/auth/dto"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway/allied_creation_basic"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway/allied_creation_user"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/log"
	"strings"
)

func (svr AuthService) CheckModuleAccess(cmd *command.CheckModuleAccessCommand) (interface{}, error) {
	var (
		result interface{}
		err    error
	)

	switch cmd.Module {
	case "DigitalControlCentre":
		result, err = svr.digitalControlCentreModuleAccess(cmd)
		break
	default:
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "unknown module "+cmd.Module)
	}
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	return result, nil
}

func (svr AuthService) digitalControlCentreModuleAccess(cmd *command.CheckModuleAccessCommand) (interface{}, error) {
	if len(cmd.Options) == 0 {
		return nil, fmt.Errorf("options is empty")
	}

	alliedCreationBasic := allied_creation_basic.NewHttplibAlliedCreationBasic(cmd.Operator)

	response := dto.ModuleAccessToDigitalControlCentreDto{
		Allows: make([]*dto.ModuleAccessAllowItem, 0),
	}
	var mapAllowItem = make(map[string]*dto.ModuleAccessAllowItem)
	for i := range cmd.Options {
		allow := &dto.ModuleAccessAllowItem{
			SettingCode: cmd.Options[i],
			Allow:       false,
			ConfigRoles: []string{},
		}
		response.Allows = append(response.Allows, allow)
		mapAllowItem[allow.SettingCode] = allow
	}

	alliedCreationUser := allied_creation_user.NewHttplibAlliedCreationUser(cmd.Operator)
	user, err := alliedCreationUser.UserGet(allied_creation_user.ReqGetUser{
		UserId: int(cmd.Operator.UserId),
	})
	if err != nil {
		log.Logger.Error(err.Error())
		return nil, fmt.Errorf("user get fail")
	}
	roles := make([]string, 0)
	for i := range user.UserRole {
		role := user.UserRole[i]
		roles = append(roles, role.RoleName)
	}
	if len(roles) == 0 {
		return response, nil
	}
	response.UserRoles = roles

	for _, code := range cmd.Options {
		settingCode := code
		if len(settingCode) == 0 {
			return nil, fmt.Errorf("setting code is empty")
		}
		result, err := alliedCreationBasic.SystemSettingGet(allied_creation_basic.ReqSystemSettingGet{
			SettingCode: settingCode,
		})
		if err != nil {
			//log.Logger.Error(err.Error(), map[string]interface{}{"company": cmd.Operator.CompanyId})
			//return nil, fmt.Errorf("system setting get fail")
			continue
		}
		configRoles := strings.Split(result.Value, ";")
		if len(configRoles) == 0 {
			continue
		}
		var allow bool = false
		for _, vi := range configRoles {
			for _, vj := range roles {
				if vi == vj {
					allow = true
					break
				}
			}
			if allow {
				break
			}
		}
		if v, ok := mapAllowItem[settingCode]; ok {
			v.Allow = allow
			v.ConfigRoles = configRoles
		}
	}

	return response, nil
}