@@ -5,8 +5,8 @@ import 'package:ht_shared/ht_shared.dart';
5
5
/// Service responsible for checking if a user has a specific permission.
6
6
///
7
7
/// 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.
10
10
/// {@endtemplate}
11
11
class PermissionService {
12
12
/// {@macro permission_service}
@@ -20,22 +20,24 @@ class PermissionService {
20
20
/// - [user] : The authenticated user.
21
21
/// - [permission] : The permission string to check (e.g., `headline.read` ).
22
22
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) ) {
25
25
return true ;
26
26
}
27
27
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
+ );
30
32
}
31
33
32
- /// Checks if the given [user] has the [UserRole. admin] role.
34
+ /// Checks if the given [user] has the ' admin' role.
33
35
///
34
36
/// This is a convenience method for checks that are strictly limited
35
37
/// to administrators, bypassing the permission map.
36
38
///
37
39
/// - [user] : The authenticated user.
38
40
bool isAdmin (User user) {
39
- return user.role == UserRole . admin;
41
+ return user.roles. contains ( UserRoles . admin) ;
40
42
}
41
43
}
0 commit comments