Skip to content

Add synchronious permission checks to permission Service #316

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 55 additions & 1 deletion src/app/core/_services/permission/permission.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ import { Perm, PermissionResource, PermissionType } from '@src/app/core/_constan
* @property resource - The resource for which the permission is checked (e.g., "Agent").
* @property type - The type of permission being checked (e.g., "CREATE", "READ", "UPDATE", "DELETE").
*/
interface PermissionCheck {
export interface PermissionCheck {
resource: PermissionResource;
type: PermissionType;
}

@Injectable({ providedIn: 'root' })
export class PermissionService {
private permissions$ = new BehaviorSubject<Permission>({});
private currentPermissions: Permission = {}; // Cached static access

constructor(
private gs: GlobalService,
Expand All @@ -41,6 +42,7 @@ export class PermissionService {
const responseData = { data: response.data, included: response.included };
const globalPermissionGroup = this.serializer.deserialize<JGlobalPermissionGroup>(responseData);
const permissions = globalPermissionGroup.permissions;
this.currentPermissions = permissions;
this.permissions$.next(permissions);
return permissions;
})
Expand Down Expand Up @@ -129,4 +131,56 @@ export class PermissionService {
hasAllPermissions(checks: PermissionCheck[]): Observable<boolean> {
return this.hasPermissions(checks).pipe(map((results) => results.every((granted) => granted)));
}

// ----------- Synchronous Permission Methods -----------
/**
* Checks synchronously whether the user has a specific permission.
*
* @param resource - Permission resource key (e.g., 'Agent')
* @param type - CRUD type (e.g., 'READ')
* @returns `true` if permission is granted
*
* @example
* if (permissionService.hasPermissionSync('Agent', 'DELETE')) {
* this.initAgentDeleteFeature();
* }
*/
hasPermissionSync(resource: PermissionResource, type: PermissionType): boolean {
const key = Perm[resource]?.[type];
return !!this.currentPermissions?.[key];
}

/**
* Checks a list of permissions synchronously and returns individual results.
*
* @param checks - Array of {resource, type} objects
* @returns Array of booleans corresponding to each check
*
* @example
* const results = permissionService.hasPermissionsSync([
* { resource: 'Chunk', type: 'READ' },
* { resource: 'Agent', type: 'DELETE' }
* ]);
* console.log(results); // e.g., [true, false]
*/
hasPermissionsSync(checks: PermissionCheck[]): boolean[] {
return checks.map((c) => this.hasPermissionSync(c.resource, c.type));
}

/**
* Checks if all listed permissions are granted synchronously.
*
* @param checks - Array of {resource, type} objects
* @returns `true` if all permissions are granted
*
* @example
* const allowed = permissionService.hasAllPermissionsSync([
* { resource: 'Agent', type: 'READ' },
* { resource: 'User', type: 'UPDATE' }
* ]);
* if (allowed) this.initTable();
*/
hasAllPermissionsSync(checks: PermissionCheck[]): boolean {
return checks.every((c) => this.hasPermissionSync(c.resource, c.type));
}
}