1
+ import 'package:ht_api/src/rbac/permission_service.dart' ;
2
+ import 'package:ht_api/src/rbac/permissions.dart' ;
1
3
import 'package:ht_api/src/services/auth_token_service.dart' ;
2
4
import 'package:ht_api/src/services/verification_code_storage_service.dart' ;
3
5
import 'package:ht_data_repository/ht_data_repository.dart' ;
@@ -21,12 +23,14 @@ class AuthService {
21
23
required HtEmailRepository emailRepository,
22
24
required HtDataRepository <UserAppSettings > userAppSettingsRepository,
23
25
required HtDataRepository <UserContentPreferences >
24
- userContentPreferencesRepository,
26
+ userContentPreferencesRepository,
27
+ required PermissionService permissionService,
25
28
required Uuid uuidGenerator,
26
29
required Logger log,
27
30
}) : _userRepository = userRepository,
28
31
_authTokenService = authTokenService,
29
32
_verificationCodeStorageService = verificationCodeStorageService,
33
+ _permissionService = permissionService,
30
34
_emailRepository = emailRepository,
31
35
_userAppSettingsRepository = userAppSettingsRepository,
32
36
_userContentPreferencesRepository = userContentPreferencesRepository,
@@ -39,7 +43,8 @@ class AuthService {
39
43
final HtEmailRepository _emailRepository;
40
44
final HtDataRepository <UserAppSettings > _userAppSettingsRepository;
41
45
final HtDataRepository <UserContentPreferences >
42
- _userContentPreferencesRepository;
46
+ _userContentPreferencesRepository;
47
+ final PermissionService _permissionService;
43
48
final Logger _log;
44
49
final Uuid _uuid;
45
50
@@ -77,13 +82,13 @@ class AuthService {
77
82
);
78
83
}
79
84
80
- final hasRequiredRole =
81
- user.dashboardRole == DashboardUserRole .admin ||
82
- user.dashboardRole == DashboardUserRole .publisher;
83
-
84
- if ( ! hasRequiredRole ) {
85
+ // Use the PermissionService to check for the specific dashboard login permission.
86
+ if ( ! _permissionService. hasPermission (
87
+ user,
88
+ Permissions .dashboardLogin,
89
+ ) ) {
85
90
_log.warning (
86
- 'Dashboard login failed: User ${user .id } lacks required roles .' ,
91
+ 'Dashboard login failed: User ${user .id } lacks required permission (${ Permissions . dashboardLogin }) .' ,
87
92
);
88
93
throw const ForbiddenException (
89
94
'Your account does not have the required permissions to sign in.' ,
@@ -157,6 +162,24 @@ class AuthService {
157
162
final existingUser = await _findUserByEmail (email);
158
163
if (existingUser != null ) {
159
164
user = existingUser;
165
+ // If this is a dashboard login, re-verify the user's dashboard role.
166
+ // This closes the loophole where a non-admin user could request a code
167
+ // via the app flow and then use it to log into the dashboard.
168
+ if (isDashboardLogin) {
169
+ if (! _permissionService.hasPermission (
170
+ user,
171
+ Permissions .dashboardLogin,
172
+ )) {
173
+ _log.warning (
174
+ 'Dashboard login failed: User ${user .id } lacks required permission '
175
+ 'during code verification.' ,
176
+ );
177
+ throw const ForbiddenException (
178
+ 'Your account does not have the required permissions to sign in.' ,
179
+ );
180
+ }
181
+ _log.info ('Dashboard user ${user .id } re-verified successfully.' );
182
+ }
160
183
} else {
161
184
// User not found.
162
185
if (isDashboardLogin) {
0 commit comments