@@ -9,24 +9,31 @@ export type PermissionCheck = {
99 verb : VERB ;
1010} ;
1111
12+ const isVerbAllowed = ( operations : string [ ] , verb : string ) : boolean =>
13+ operations . includes ( '*' ) || operations . includes ( verb ) ;
14+
1215/**
1316 * Helper function to check if a permission (resource + verb) is allowed
17+ *
1418 * Handles wildcards: "*" in resource means all resources, "*" in operations means all operations
19+ * Handles exceptions to the wildcard rules, e.g. "operations":[],"resource":"devices/console" prevents the user from accessing the device console.
1520 */
1621const isPermissionAllowed = ( userPermissions : Permission [ ] , permissionCheck : PermissionCheck ) : boolean => {
17- for ( const permission of userPermissions ) {
18- // Check if resource matches (exact match or wildcard)
19- const resourceMatches = permission . resource === '*' || permission . resource === permissionCheck . kind ;
20- if ( resourceMatches ) {
21- // Check if operation/verb matches (exact match or wildcard)
22- const verbMatches = permission . operations . includes ( '*' ) || permission . operations . includes ( permissionCheck . verb ) ;
23- if ( verbMatches ) {
24- return true ;
25- }
26- }
22+ const kindPermissions = userPermissions . filter ( ( permission ) => permission . resource === permissionCheck . kind ) ;
23+
24+ // Check for explicit permission denial for this resource ( kind)
25+ if ( kindPermissions . some ( ( permission ) => permission . operations . length === 0 ) ) {
26+ return false ;
27+ }
28+
29+ // Check for explicit permission grant for this resource (kind) and verb
30+ if ( kindPermissions . some ( ( permission ) => isVerbAllowed ( permission . operations , permissionCheck . verb ) ) ) {
31+ return true ;
2732 }
2833
29- return false ;
34+ // If there are no exceptions for this resource/verb, check for wildcard permissions
35+ const wildcardPermissions = userPermissions . filter ( ( permission ) => permission . resource === '*' ) ;
36+ return wildcardPermissions . some ( ( p ) => isVerbAllowed ( p . operations , permissionCheck . verb ) ) ;
3037} ;
3138
3239export type PermissionsContextType = {
0 commit comments