module_access.go
3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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
}