Skip to content

Commit 4e79d3a

Browse files
committed
refactor(rbac): enhance permission checking logic
- Update hasPermission method to consider both appRole and dashboardRole - Modify isAdmin method to check dashboardRole directly - Improve documentation to reflect new logic and role separation
1 parent 753aaf9 commit 4e79d3a

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

lib/src/rbac/permission_service.dart

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,47 @@ import 'package:ht_shared/ht_shared.dart';
44
/// {@template permission_service}
55
/// Service responsible for checking if a user has a specific permission.
66
///
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.
1011
/// {@endtemplate}
1112
class PermissionService {
1213
/// {@macro permission_service}
1314
const PermissionService();
1415

1516
/// Checks if the given [user] has the specified [permission].
1617
///
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.
1920
///
2021
/// - [user]: The authenticated user.
2122
/// - [permission]: The permission string to check (e.g., `headline.read`).
2223
bool hasPermission(User user, String permission) {
2324
// Administrators implicitly have all permissions.
24-
if (user.roles.contains(UserRoles.admin)) {
25+
if (isAdmin(user)) {
2526
return true;
2627
}
2728

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);
3239
}
3340

34-
/// Checks if the given [user] has the 'admin' role.
41+
/// Checks if the given [user] has the `admin` dashboard role.
3542
///
3643
/// This is a convenience method for checks that are strictly limited
3744
/// to administrators, bypassing the permission map.
3845
///
3946
/// - [user]: The authenticated user.
4047
bool isAdmin(User user) {
41-
return user.roles.contains(UserRoles.admin);
48+
return user.dashboardRole == DashboardUserRole.admin;
4249
}
4350
}

0 commit comments

Comments
 (0)