@@ -37,6 +37,9 @@ import (
37
37
)
38
38
39
39
type EnforcerUtil interface {
40
+ GetAppAndEnvRBACNamesByAppAndEnvIds (IdToAppEnvPairs map [int ][2 ]int ) (map [int ]string , map [int ]string , map [int ]* app.App , map [int ]* repository.Environment , error )
41
+ IsAuthorizedForApp (appId int , rbacResults map [string ]bool , appIdtoApp map [int ]* app.App ) bool
42
+ IsAuthorizedForEnv (appId int , envId int , appResults map [string ]bool , appIdtoApp map [int ]* app.App , envIdToEnv map [int ]* repository.Environment ) bool
40
43
GetAppRBACName (appName string ) string
41
44
GetRbacObjectsForAllApps (appType helper.AppType ) map [int ]string
42
45
GetRbacObjectsForAllAppsWithTeamID (teamID int , appType helper.AppType ) map [int ]string
@@ -111,6 +114,87 @@ func NewEnforcerUtilImpl(logger *zap.SugaredLogger, teamRepository team.TeamRepo
111
114
}
112
115
}
113
116
117
+ func (impl EnforcerUtilImpl ) IsAuthorizedForApp (appId int , rbacResults map [string ]bool , appIdtoApp map [int ]* app.App ) bool {
118
+ app , appExists := appIdtoApp [appId ]
119
+ if ! appExists {
120
+ return false
121
+ }
122
+
123
+ appObject := fmt .Sprintf ("%s/%s" , app .Team .Name , app .AppName )
124
+ if authorized , exists := rbacResults [appObject ]; exists && authorized {
125
+ return true
126
+ }
127
+ return false
128
+ }
129
+
130
+ func (impl EnforcerUtilImpl ) IsAuthorizedForEnv (appId int , envId int , rbacResults map [string ]bool , appIdtoApp map [int ]* app.App , envIdToEnv map [int ]* repository.Environment ) bool {
131
+ app , appExists := appIdtoApp [appId ]
132
+ if ! appExists {
133
+ return false
134
+ }
135
+ env , envExists := envIdToEnv [envId ]
136
+ if ! envExists {
137
+ return false
138
+ }
139
+
140
+ envObject := fmt .Sprintf ("%s/%s" , env .EnvironmentIdentifier , app .AppName )
141
+ if authorized , exists := rbacResults [envObject ]; exists && authorized {
142
+ return true
143
+ }
144
+ return false
145
+ }
146
+
147
+ func (impl EnforcerUtilImpl ) GetAppAndEnvRBACNamesByAppAndEnvIds (appEnvPairs map [int ][2 ]int ) (map [int ]string , map [int ]string , map [int ]* app.App , map [int ]* repository.Environment , error ) {
148
+ appObjects := make (map [int ]string )
149
+ envObjects := make (map [int ]string )
150
+
151
+ appIds := make ([]* int , 0 , len (appEnvPairs ))
152
+ envIds := make ([]* int , 0 , len (appEnvPairs ))
153
+ for _ , pair := range appEnvPairs {
154
+ appId := pair [0 ]
155
+ envId := pair [1 ]
156
+ appIds = append (appIds , & appId )
157
+ envIds = append (envIds , & envId )
158
+ }
159
+ appIdToAppMap := make (map [int ]* app.App )
160
+ envIdToEnvMap := make (map [int ]* repository.Environment )
161
+ applications , err := impl .appRepo .FindAppAndProjectByAppIds (appIds )
162
+
163
+ if err != nil {
164
+ return nil , nil , appIdToAppMap , envIdToEnvMap , err
165
+ }
166
+
167
+ for _ , app := range applications {
168
+ appIdToAppMap [app .Id ] = app
169
+ }
170
+
171
+ environments , err := impl .environmentRepository .FindByIds (envIds )
172
+ if err != nil {
173
+ return nil , nil , appIdToAppMap , envIdToEnvMap , err
174
+ }
175
+
176
+ for _ , env := range environments {
177
+ envIdToEnvMap [env .Id ] = env
178
+ }
179
+
180
+ for id , pair := range appEnvPairs {
181
+ appId := pair [0 ]
182
+ envId := pair [1 ]
183
+ // check if app and env exists
184
+ // handling for deleted app and env
185
+ if _ , ok := appIdToAppMap [appId ]; ! ok {
186
+ continue
187
+ }
188
+ if _ , ok := envIdToEnvMap [envId ]; ! ok {
189
+ continue
190
+ }
191
+ appObjects [id ] = fmt .Sprintf ("%s/%s" , appIdToAppMap [appId ].Team .Name , appIdToAppMap [appId ].AppName )
192
+ envObjects [id ] = fmt .Sprintf ("%s/%s" , envIdToEnvMap [envId ].EnvironmentIdentifier , appIdToAppMap [appId ].AppName )
193
+ }
194
+
195
+ return appObjects , envObjects , appIdToAppMap , envIdToEnvMap , nil
196
+ }
197
+
114
198
func (impl EnforcerUtilImpl ) GetRbacObjectsByEnvIdsAndAppId (envIds []int , appId int ) (map [int ]string , map [string ]string ) {
115
199
116
200
objects := make (map [int ]string )
0 commit comments