@@ -147,9 +147,13 @@ func (api *API) putOrgRoles(rw http.ResponseWriter, r *http.Request) {
147
147
UUID : organization .ID ,
148
148
Valid : true ,
149
149
},
150
- SitePermissions : db2sdk .List (req .SitePermissions , sdkPermissionToDB ),
151
- OrgPermissions : db2sdk .List (req .OrganizationPermissions , sdkPermissionToDB ),
152
- UserPermissions : db2sdk .List (req .UserPermissions , sdkPermissionToDB ),
150
+ // Invalid permissions are filtered out. If this is changed
151
+ // to throw an error, then the story of a previously valid role
152
+ // now being invalid has to be addressed. Coder can change permissions,
153
+ // objects, and actions at any time.
154
+ SitePermissions : db2sdk .List (filterInvalidPermissions (req .SitePermissions ), sdkPermissionToDB ),
155
+ OrgPermissions : db2sdk .List (filterInvalidPermissions (req .OrganizationPermissions ), sdkPermissionToDB ),
156
+ UserPermissions : db2sdk .List (filterInvalidPermissions (req .UserPermissions ), sdkPermissionToDB ),
153
157
})
154
158
if httpapi .Is404Error (err ) {
155
159
httpapi .ResourceNotFound (rw )
@@ -247,6 +251,24 @@ func (api *API) deleteOrgRole(rw http.ResponseWriter, r *http.Request) {
247
251
httpapi .Write (ctx , rw , http .StatusNoContent , nil )
248
252
}
249
253
254
+ func filterInvalidPermissions (permissions []codersdk.Permission ) []codersdk.Permission {
255
+ // Filter out any invalid permissions
256
+ var validPermissions []codersdk.Permission
257
+ for _ , permission := range permissions {
258
+ err := (& rbac.Permission {
259
+ Negate : permission .Negate ,
260
+ ResourceType : string (permission .ResourceType ),
261
+ Action : policy .Action (permission .Action ),
262
+ }).Valid ()
263
+ if err == nil {
264
+ validPermissions = append (validPermissions , permission )
265
+ } else {
266
+ fmt .Println (err .Error ())
267
+ }
268
+ }
269
+ return validPermissions
270
+ }
271
+
250
272
func sdkPermissionToDB (p codersdk.Permission ) database.CustomRolePermission {
251
273
return database.CustomRolePermission {
252
274
Negate : p .Negate ,
0 commit comments