@@ -4,40 +4,47 @@ import 'package:ht_shared/ht_shared.dart';
4
4
/// {@template permission_service}
5
5
/// Service responsible for checking if a user has a specific permission.
6
6
///
7
- /// This service uses the predefined [rolePermissions] map to determine
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.
7
+ /// This service uses the predefined [rolePermissions] map to determine a user's
8
+ /// access rights based on their `appRole` and `dashboardRole` . It also
9
+ /// includes an explicit check for the `admin` role, granting them all
10
+ /// permissions.
10
11
/// {@endtemplate}
11
12
class PermissionService {
12
13
/// {@macro permission_service}
13
14
const PermissionService ();
14
15
15
16
/// Checks if the given [user] has the specified [permission] .
16
17
///
17
- /// Returns `true` if the user's role grants the permission, or if the user
18
- /// is an administrator. Returns `false` otherwise.
18
+ /// Returns `true` if the user's combined roles grant the permission, or if
19
+ /// the user is an administrator. Returns `false` otherwise.
19
20
///
20
21
/// - [user] : The authenticated user.
21
22
/// - [permission] : The permission string to check (e.g., `headline.read` ).
22
23
bool hasPermission (User user, String permission) {
23
24
// Administrators implicitly have all permissions.
24
- if (user.roles. contains ( UserRoles .admin )) {
25
+ if (isAdmin (user )) {
25
26
return true ;
26
27
}
27
28
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
- );
29
+ // Get the permission sets for the user's app and dashboard roles.
30
+ final appPermissions = rolePermissions[user.appRole] ?? const < String > {};
31
+ final dashboardPermissions =
32
+ rolePermissions[user.dashboardRole] ?? const < String > {};
33
+
34
+ // Combine the permissions from both roles.
35
+ final totalPermissions = {...appPermissions, ...dashboardPermissions};
36
+
37
+ // Check if the combined set contains the required permission.
38
+ return totalPermissions.contains (permission);
32
39
}
33
40
34
- /// Checks if the given [user] has the ' admin' role.
41
+ /// Checks if the given [user] has the ` admin` dashboard role.
35
42
///
36
43
/// This is a convenience method for checks that are strictly limited
37
44
/// to administrators, bypassing the permission map.
38
45
///
39
46
/// - [user] : The authenticated user.
40
47
bool isAdmin (User user) {
41
- return user.roles. contains ( UserRoles . admin) ;
48
+ return user.dashboardRole == DashboardUserRole . admin;
42
49
}
43
50
}
0 commit comments