作者 yangfu

feat : 增加模块权限验证

  1 +package command
  2 +
  3 +import (
  4 + "fmt"
  5 + "github.com/beego/beego/v2/core/validation"
  6 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/domain"
  7 +)
  8 +
  9 +type CheckModuleAccessCommand struct {
  10 + // 操作人
  11 + Operator domain.Operator `json:"-"`
  12 + // 模块
  13 + Module string `json:"module"`
  14 + // 参数
  15 + Options []string `json:"options"`
  16 +}
  17 +
  18 +func (cmd *CheckModuleAccessCommand) Valid(validation *validation.Validation) {
  19 +
  20 +}
  21 +
  22 +func (cmd *CheckModuleAccessCommand) ValidateCommand() error {
  23 + valid := validation.Validation{}
  24 + b, err := valid.Valid(cmd)
  25 + if err != nil {
  26 + return err
  27 + }
  28 + if !b {
  29 + for _, validErr := range valid.Errors {
  30 + return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
  31 + }
  32 + }
  33 + return nil
  34 +}
  1 +package dto
  2 +
  3 +// 数控中心报表
  4 +type ModuleAccessToDigitalControlCentreDto struct {
  5 + // 用户的角色权限列表
  6 + UserRoles []string `json:"userRoles"`
  7 + // 列表
  8 + Allows []*ModuleAccessAllowItem `json:"modules"`
  9 +}
  10 +
  11 +type ModuleAccessAllowItem struct {
  12 + Allow bool `json:"allow"`
  13 + SettingCode string `json:"settingCode"`
  14 + ConfigRoles []string `json:"configRoles"`
  15 +}
  1 +package service
  2 +
  3 +import (
  4 + "fmt"
  5 + "github.com/linmadan/egglib-go/core/application"
  6 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/auth/command"
  7 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/auth/dto"
  8 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway/allied_creation_basic"
  9 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway/allied_creation_user"
  10 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/log"
  11 + "strings"
  12 +)
  13 +
  14 +func (svr AuthService) CheckModuleAccess(cmd *command.CheckModuleAccessCommand) (interface{}, error) {
  15 + var (
  16 + result interface{}
  17 + err error
  18 + )
  19 +
  20 + switch cmd.Module {
  21 + case "DigitalControlCentre":
  22 + result, err = svr.digitalControlCentreModuleAccess(cmd)
  23 + break
  24 + default:
  25 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "unknown module "+cmd.Module)
  26 + }
  27 + if err != nil {
  28 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  29 + }
  30 + return result, nil
  31 +}
  32 +
  33 +func (svr AuthService) digitalControlCentreModuleAccess(cmd *command.CheckModuleAccessCommand) (interface{}, error) {
  34 + if len(cmd.Options) == 0 {
  35 + return nil, fmt.Errorf("options is empty")
  36 + }
  37 +
  38 + alliedCreationBasic := allied_creation_basic.NewHttplibAlliedCreationBasic(cmd.Operator)
  39 +
  40 + response := dto.ModuleAccessToDigitalControlCentreDto{
  41 + Allows: make([]*dto.ModuleAccessAllowItem, 0),
  42 + }
  43 + var mapAllowItem = make(map[string]*dto.ModuleAccessAllowItem)
  44 + for i := range cmd.Options {
  45 + allow := &dto.ModuleAccessAllowItem{
  46 + SettingCode: cmd.Options[i],
  47 + Allow: false,
  48 + ConfigRoles: []string{},
  49 + }
  50 + response.Allows = append(response.Allows, allow)
  51 + mapAllowItem[allow.SettingCode] = allow
  52 + }
  53 +
  54 + alliedCreationUser := allied_creation_user.NewHttplibAlliedCreationUser(cmd.Operator)
  55 + user, err := alliedCreationUser.UserGet(allied_creation_user.ReqGetUser{
  56 + UserId: int(cmd.Operator.UserId),
  57 + })
  58 + if err != nil {
  59 + log.Logger.Error(err.Error())
  60 + return nil, fmt.Errorf("user get fail")
  61 + }
  62 + roles := make([]string, 0)
  63 + for i := range user.UserRole {
  64 + role := user.UserRole[i]
  65 + roles = append(roles, role.RoleName)
  66 + }
  67 + if len(roles) == 0 {
  68 + return response, nil
  69 + }
  70 + response.UserRoles = roles
  71 +
  72 + for _, code := range cmd.Options {
  73 + settingCode := code
  74 + if len(settingCode) == 0 {
  75 + return nil, fmt.Errorf("setting code is empty")
  76 + }
  77 + result, err := alliedCreationBasic.SystemSettingGet(allied_creation_basic.ReqSystemSettingGet{
  78 + SettingCode: settingCode,
  79 + })
  80 + if err != nil {
  81 + //log.Logger.Error(err.Error(), map[string]interface{}{"company": cmd.Operator.CompanyId})
  82 + //return nil, fmt.Errorf("system setting get fail")
  83 + continue
  84 + }
  85 + configRoles := strings.Split(result.Value, "|")
  86 + if len(configRoles) == 0 {
  87 + continue
  88 + }
  89 + var allow bool = false
  90 + for _, vi := range configRoles {
  91 + for _, vj := range roles {
  92 + if vi == vj {
  93 + allow = true
  94 + break
  95 + }
  96 + }
  97 + if allow {
  98 + break
  99 + }
  100 + }
  101 + if v, ok := mapAllowItem[settingCode]; ok {
  102 + v.Allow = allow
  103 + v.ConfigRoles = configRoles
  104 + }
  105 + }
  106 +
  107 + return response, nil
  108 +}
@@ -225,3 +225,16 @@ func (controller *AuthController) CaptchaInit() { @@ -225,3 +225,16 @@ func (controller *AuthController) CaptchaInit() {
225 data, err := authService.CaptchaInit(cmd) 225 data, err := authService.CaptchaInit(cmd)
226 controller.Response(data, err) 226 controller.Response(data, err)
227 } 227 }
  228 +
  229 +func (controller *AuthController) CheckModuleAccessCommand() {
  230 + authService := service.AuthService{}
  231 + cmd := &command.CheckModuleAccessCommand{}
  232 + err := controller.Unmarshal(cmd)
  233 + if err != nil {
  234 + controller.Response(nil, err)
  235 + return
  236 + }
  237 + cmd.Operator = controller.GetOperator()
  238 + data, err := authService.CheckModuleAccess(cmd)
  239 + controller.Response(data, err)
  240 +}
@@ -21,4 +21,6 @@ func init() { @@ -21,4 +21,6 @@ func init() {
21 web.Router("/v1/auth/user-sign-up", &controllers.AuthController{}, "Post:UserSignUp") 21 web.Router("/v1/auth/user-sign-up", &controllers.AuthController{}, "Post:UserSignUp")
22 web.Router("/v1/auth/reset-password", &controllers.AuthController{}, "Post:ResetPassword") //公司重置密码 22 web.Router("/v1/auth/reset-password", &controllers.AuthController{}, "Post:ResetPassword") //公司重置密码
23 web.Router("/v1/auth/org-switch", &controllers.AuthController{}, "Post:OrgSwitch") 23 web.Router("/v1/auth/org-switch", &controllers.AuthController{}, "Post:OrgSwitch")
  24 +
  25 + web.Router("/v1/auth/check-module-access", &controllers.AuthController{}, "Post:CheckModuleAccessCommand")
24 } 26 }