@@ -29,39 +29,42 @@ class DefaultUserPreferenceLimitService implements UserPreferenceLimitService {
29
29
final appConfig = await _appConfigRepository.read (id: _appConfigId);
30
30
final limits = appConfig.userPreferenceLimits;
31
31
32
- // 2. Determine the limit based on user role and item type
32
+ // Admins have no limits.
33
+ if (user.roles.contains (UserRoles .admin)) {
34
+ return ;
35
+ }
36
+
37
+ // 2. Determine the limit based on the user's highest role.
33
38
int limit;
34
- switch (user.role) {
35
- case UserRole .guestUser:
36
- if (itemType == 'headline' ) {
37
- limit = limits.guestSavedHeadlinesLimit;
38
- } else {
39
- // Applies to countries, sources, categories
40
- limit = limits.guestFollowedItemsLimit;
41
- }
42
- case UserRole .standardUser:
43
- if (itemType == 'headline' ) {
44
- limit = limits.authenticatedSavedHeadlinesLimit;
45
- } else {
46
- // Applies to countries, sources, categories
47
- limit = limits.authenticatedFollowedItemsLimit;
48
- }
49
- case UserRole .premiumUser:
50
- if (itemType == 'headline' ) {
51
- limit = limits.premiumSavedHeadlinesLimit;
52
- } else {
53
- limit = limits.premiumFollowedItemsLimit;
54
- }
55
- case UserRole .admin:
56
- // Admins have no limits
57
- return ;
39
+ String accountType;
40
+
41
+ if (user.roles.contains (UserRoles .premiumUser)) {
42
+ accountType = 'premium' ;
43
+ limit = (itemType == 'headline' )
44
+ ? limits.premiumSavedHeadlinesLimit
45
+ : limits.premiumFollowedItemsLimit;
46
+ } else if (user.roles.contains (UserRoles .standardUser)) {
47
+ accountType = 'standard' ;
48
+ limit = (itemType == 'headline' )
49
+ ? limits.authenticatedSavedHeadlinesLimit
50
+ : limits.authenticatedFollowedItemsLimit;
51
+ } else if (user.roles.contains (UserRoles .guestUser)) {
52
+ accountType = 'guest' ;
53
+ limit = (itemType == 'headline' )
54
+ ? limits.guestSavedHeadlinesLimit
55
+ : limits.guestFollowedItemsLimit;
56
+ } else {
57
+ // Fallback for users with unknown or no roles.
58
+ throw const ForbiddenException (
59
+ 'Cannot determine preference limits for this user account.' ,
60
+ );
58
61
}
59
62
60
63
// 3. Check if adding the item would exceed the limit
61
64
if (currentCount >= limit) {
62
65
throw ForbiddenException (
63
66
'You have reached the maximum number of $itemType items allowed '
64
- 'for your account type (${ user . role . name } ).' ,
67
+ 'for your account type ($accountType ).' ,
65
68
);
66
69
}
67
70
} on HtHttpException {
@@ -86,48 +89,58 @@ class DefaultUserPreferenceLimitService implements UserPreferenceLimitService {
86
89
final appConfig = await _appConfigRepository.read (id: _appConfigId);
87
90
final limits = appConfig.userPreferenceLimits;
88
91
89
- // 2. Determine limits based on user role
92
+ // Admins have no limits.
93
+ if (user.roles.contains (UserRoles .admin)) {
94
+ return ;
95
+ }
96
+
97
+ // 2. Determine limits based on the user's highest role.
90
98
int followedItemsLimit;
91
99
int savedHeadlinesLimit;
100
+ String accountType;
92
101
93
- switch (user.role) {
94
- case UserRole .guestUser:
95
- followedItemsLimit = limits.guestFollowedItemsLimit;
96
- savedHeadlinesLimit = limits.guestSavedHeadlinesLimit;
97
- case UserRole .standardUser:
98
- followedItemsLimit = limits.authenticatedFollowedItemsLimit;
99
- savedHeadlinesLimit = limits.authenticatedSavedHeadlinesLimit;
100
- case UserRole .premiumUser:
101
- followedItemsLimit = limits.premiumFollowedItemsLimit;
102
- savedHeadlinesLimit = limits.premiumSavedHeadlinesLimit;
103
- case UserRole .admin:
104
- // Admins have no limits
105
- return ;
102
+ if (user.roles.contains (UserRoles .premiumUser)) {
103
+ accountType = 'premium' ;
104
+ followedItemsLimit = limits.premiumFollowedItemsLimit;
105
+ savedHeadlinesLimit = limits.premiumSavedHeadlinesLimit;
106
+ } else if (user.roles.contains (UserRoles .standardUser)) {
107
+ accountType = 'standard' ;
108
+ followedItemsLimit = limits.authenticatedFollowedItemsLimit;
109
+ savedHeadlinesLimit = limits.authenticatedSavedHeadlinesLimit;
110
+ } else if (user.roles.contains (UserRoles .guestUser)) {
111
+ accountType = 'guest' ;
112
+ followedItemsLimit = limits.guestFollowedItemsLimit;
113
+ savedHeadlinesLimit = limits.guestSavedHeadlinesLimit;
114
+ } else {
115
+ // Fallback for users with unknown or no roles.
116
+ throw const ForbiddenException (
117
+ 'Cannot determine preference limits for this user account.' ,
118
+ );
106
119
}
107
120
108
121
// 3. Check if proposed preferences exceed limits
109
122
if (updatedPreferences.followedCountries.length > followedItemsLimit) {
110
123
throw ForbiddenException (
111
124
'You have reached the maximum number of followed countries allowed '
112
- 'for your account type (${ user . role . name } ).' ,
125
+ 'for your account type ($accountType ).' ,
113
126
);
114
127
}
115
128
if (updatedPreferences.followedSources.length > followedItemsLimit) {
116
129
throw ForbiddenException (
117
130
'You have reached the maximum number of followed sources allowed '
118
- 'for your account type (${ user . role . name } ).' ,
131
+ 'for your account type ($accountType ).' ,
119
132
);
120
133
}
121
134
if (updatedPreferences.followedCategories.length > followedItemsLimit) {
122
135
throw ForbiddenException (
123
136
'You have reached the maximum number of followed categories allowed '
124
- 'for your account type (${ user . role . name } ).' ,
137
+ 'for your account type ($accountType ).' ,
125
138
);
126
139
}
127
140
if (updatedPreferences.savedHeadlines.length > savedHeadlinesLimit) {
128
141
throw ForbiddenException (
129
142
'You have reached the maximum number of saved headlines allowed '
130
- 'for your account type (${ user . role . name } ).' ,
143
+ 'for your account type ($accountType ).' ,
131
144
);
132
145
}
133
146
} on HtHttpException {
0 commit comments