Skip to content

Commit 0a83508

Browse files
committed
refactor: use shared models for permissions
- Use UserRole enum - Use Permission class
1 parent d4badaf commit 0a83508

File tree

2 files changed

+33
-82
lines changed

2 files changed

+33
-82
lines changed

lib/src/permissions.dart

Lines changed: 31 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'package:ht_shared/ht_shared.dart';
2+
13
// ignore_for_file: public_member_api_docs
24

35
/// Defines the roles and permissions used in the RBAC system.
@@ -6,94 +8,43 @@
68
/// Roles are defined as constants.
79
/// The `rolePermissions` map defines which permissions are granted to each role.
810
9-
library;
10-
11-
/// {@template role}
12-
/// Defines the available user roles in the system.
13-
/// {@endtemplate}
14-
abstract class Role {
15-
/// Administrator role with full access.
16-
static const String admin = 'admin';
17-
18-
/// Standard user role with limited access.
19-
static const String standardUser = 'standard_user';
20-
21-
// Add other roles here as needed.
22-
}
23-
24-
/// {@template permission}
25-
/// Defines the available permissions in the system.
26-
///
27-
/// Permissions follow the format `resource.action`.
28-
/// {@endtemplate}
29-
abstract class Permission {
30-
// Headline Permissions
31-
static const String headlineRead = 'headline.read';
32-
static const String headlineCreate = 'headline.create';
33-
static const String headlineUpdate = 'headline.update';
34-
static const String headlineDelete = 'headline.delete';
35-
36-
// Category Permissions
37-
static const String categoryRead = 'category.read';
38-
static const String categoryCreate = 'category.create';
39-
static const String categoryUpdate = 'category.update';
40-
static const String categoryDelete = 'category.delete';
41-
42-
// Source Permissions
43-
static const String sourceRead = 'source.read';
44-
static const String sourceCreate = 'source.create';
45-
static const String sourceUpdate = 'source.update';
46-
static const String sourceDelete = 'source.delete';
47-
48-
// Country Permissions
49-
static const String countryRead = 'country.read';
50-
static const String countryCreate = 'country.create';
51-
static const String countryUpdate = 'country.update';
52-
static const String countryDelete = 'country.delete';
53-
54-
// User Settings Permissions
55-
static const String userSettingsRead = 'user_settings.read';
56-
static const String userSettingsUpdate = 'user_settings.update';
57-
// Note: User settings delete is handled via account deletion, no separate permission needed here.
58-
59-
// Add other resource permissions here as needed.
60-
}
61-
6211
/// A map defining which permissions are granted to each role.
6312
///
6413
/// The key is the role string, and the value is a set of permission strings.
65-
final Map<String, Set<String>> rolePermissions = {
66-
Role.admin: {
14+
final Map<UserRole, Set<Permission>> rolePermissions = {
15+
UserRole.admin: {
6716
// Admins have all permissions. You might have a more
6817
// sophisticated way to represent this, but listing them explicitly is clear.
69-
Permission.headlineRead,
70-
Permission.headlineCreate,
71-
Permission.headlineUpdate,
72-
Permission.headlineDelete,
73-
Permission.categoryRead,
74-
Permission.categoryCreate,
75-
Permission.categoryUpdate,
76-
Permission.categoryDelete,
77-
Permission.sourceRead,
78-
Permission.sourceCreate,
79-
Permission.sourceUpdate,
80-
Permission.sourceDelete,
81-
Permission.countryRead,
82-
Permission.countryCreate,
83-
Permission.countryUpdate,
84-
Permission.countryDelete,
85-
Permission.userSettingsRead,
86-
Permission.userSettingsUpdate,
18+
const Permission(name: 'headlineRead'),
19+
const Permission(name: 'headlineCreate'),
20+
const Permission(name: 'headlineUpdate'),
21+
const Permission(name: 'headlineDelete'),
22+
const Permission(name: 'categoryRead'),
23+
const Permission(name: 'categoryCreate'),
24+
const Permission(name: 'categoryUpdate'),
25+
const Permission(name: 'categoryDelete'),
26+
const Permission(name: 'sourceRead'),
27+
const Permission(name: 'sourceCreate'),
28+
const Permission(name: 'sourceUpdate'),
29+
const Permission(name: 'sourceDelete'),
30+
const Permission(name: 'countryRead'),
31+
const Permission(name: 'countryCreate'),
32+
const Permission(name: 'countryUpdate'),
33+
const Permission(name: 'countryDelete'),
34+
const Permission(name: 'userSettingsRead'),
35+
const Permission(name: 'userSettingsUpdate'),
8736
// Add other admin permissions here.
8837
},
89-
Role.standardUser: {
38+
UserRole.standardUser: {
9039
// Standard users can read public data and manage their own settings.
91-
Permission.headlineRead,
92-
Permission.categoryRead,
93-
Permission.sourceRead,
94-
Permission.countryRead,
95-
Permission.userSettingsRead, // Can read their own settings
96-
Permission.userSettingsUpdate, // Can update their own settings
40+
const Permission(name: 'headlineRead'),
41+
const Permission(name: 'categoryRead'),
42+
const Permission(name: 'sourceRead'),
43+
const Permission(name: 'countryRead'),
44+
const Permission(name: 'userSettingsRead'), // Can read their own settings
45+
const Permission(
46+
name: 'userSettingsUpdate',
47+
), // Can update their own settings
9748
// Add other standard user permissions here.
9849
},
9950
// Add mappings for other roles here.

lib/src/services/authorization_service.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ class AuthorizationService {
1212
///
1313
/// Assumes the [User] model has a `role` property (String).
1414
/// Returns `true` if the user has the permission, `false` otherwise.
15-
bool hasPermission(User user, String permission) {
15+
bool hasPermission(User user, Permission permission) {
1616
// Admins always have permission.
1717
// Assuming user.role exists and 'admin' is the admin role string.
18-
if (user.role == Role.admin) {
18+
if (user.role == UserRole.admin) {
1919
return true;
2020
}
2121

0 commit comments

Comments
 (0)