|
|
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
|
+} |