Skip to content

Commit 894cb6d

Browse files
committed
refactor(api): update PermissionService and role map for multi-role support
1 parent 578a2d9 commit 894cb6d

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

lib/src/rbac/permission_service.dart

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import 'package:ht_shared/ht_shared.dart';
55
/// Service responsible for checking if a user has a specific permission.
66
///
77
/// This service uses the predefined [rolePermissions] map to determine
8-
/// a user's access rights based on their [UserRole]. It also includes
9-
/// an explicit check for the [UserRole.admin], granting them all permissions.
8+
/// a user's access rights based on their roles. It also includes
9+
/// an explicit check for the 'admin' role, granting them all permissions.
1010
/// {@endtemplate}
1111
class PermissionService {
1212
/// {@macro permission_service}
@@ -20,22 +20,24 @@ class PermissionService {
2020
/// - [user]: The authenticated user.
2121
/// - [permission]: The permission string to check (e.g., `headline.read`).
2222
bool hasPermission(User user, String permission) {
23-
// Administrators have all permissions
24-
if (user.role == UserRole.admin) {
23+
// Administrators implicitly have all permissions.
24+
if (user.roles.contains(UserRoles.admin)) {
2525
return true;
2626
}
2727

28-
// Check if the user's role is in the map and has the permission
29-
return rolePermissions[user.role]?.contains(permission) ?? false;
28+
// Check if any of the user's roles grant the required permission.
29+
return user.roles.any(
30+
(role) => rolePermissions[role]?.contains(permission) ?? false,
31+
);
3032
}
3133

32-
/// Checks if the given [user] has the [UserRole.admin] role.
34+
/// Checks if the given [user] has the 'admin' role.
3335
///
3436
/// This is a convenience method for checks that are strictly limited
3537
/// to administrators, bypassing the permission map.
3638
///
3739
/// - [user]: The authenticated user.
3840
bool isAdmin(User user) {
39-
return user.role == UserRole.admin;
41+
return user.roles.contains(UserRoles.admin);
4042
}
4143
}

0 commit comments

Comments
 (0)