-
Notifications
You must be signed in to change notification settings - Fork 0
Refactor migrate user role to multi role system #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Summary of Changes
Hello @fulleni, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
This pull request significantly refactors the application's user role management system, transitioning from a single-role assignment to a robust multi-role architecture. This change enhances the flexibility and granularity of access control, allowing users to possess multiple roles simultaneously and enabling more sophisticated permission management across various services and features.
Highlights
- Multi-Role System Implementation: The core user role management has been refactored from a single
UserRole
enum to a flexible system supporting multiple roles per user. This involves changing user objects to store a list of roles (e.g.,roles: [UserRoles.standardUser, UserRoles.publisher]
) instead of a singlerole
. - Enhanced Role-Based Access Control (RBAC): The
PermissionService
has been updated to leverage the new multi-role system. It now checks if a user has a specific permission by iterating through all of their assigned roles, rather than just a single one. A newpublisher
role has also been introduced with specific content creation permissions. - Dynamic User Provisioning: The
AuthService
now supports conditional role assignment during user creation and account linking. For instance, new users signing up from a 'dashboard' client can automatically be granted bothstandardUser
andpublisher
roles, enabling more granular initial access. - JWT Token Structure Update: JSON Web Tokens (JWTs) issued by the
JwtAuthTokenService
now include a list of user roles in their claims, replacing the previous single role string. This ensures that client applications receive comprehensive role information for authorization. - Adaptive User Preference Limits: The
DefaultUserPreferenceLimitService
has been adapted to the multi-role model. User preference limits (e.g., number of saved headlines) are now determined based on the most permissive role a user holds, ensuring that limits are correctly applied in the new system. - Documentation Update: The
README.md
has been updated to reflect the new 'Flexible Role-Based Access Control (RBAC)' feature, highlighting the ability to assign multiple roles for precise control over API features and data management.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command>
or @gemini-code-assist <command>
. Below is a summary of the supported commands.
Feature | Command | Description |
---|---|---|
Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/
folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request refactors the user authentication and authorization system to support multiple roles. The changes are well-structured and consistent. I suggested defining constants for client types and refactoring duplicated logic for determining user preference limits into a shared helper method to improve maintainability.
final roles = (clientType == 'dashboard') | ||
? [UserRoles.standardUser, UserRoles.publisher] | ||
: [UserRoles.standardUser]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider defining constants for the clientType
values instead of using a magic string1. This improves maintainability and reduces the risk of typos.
const String dashboardClient = 'dashboard';
const String mobileAppClient = 'mobile_app';
final roles = (clientType == dashboardClient)
? [UserRoles.standardUser, UserRoles.publisher]
: [UserRoles.standardUser];
Style Guide References
Footnotes
// Admins have no limits. | ||
if (user.roles.contains(UserRoles.admin)) { | ||
return; | ||
} | ||
|
||
// 2. Determine the limit based on the user's highest role. | ||
int limit; | ||
switch (user.role) { | ||
case UserRole.guestUser: | ||
if (itemType == 'headline') { | ||
limit = limits.guestSavedHeadlinesLimit; | ||
} else { | ||
// Applies to countries, sources, categories | ||
limit = limits.guestFollowedItemsLimit; | ||
} | ||
case UserRole.standardUser: | ||
if (itemType == 'headline') { | ||
limit = limits.authenticatedSavedHeadlinesLimit; | ||
} else { | ||
// Applies to countries, sources, categories | ||
limit = limits.authenticatedFollowedItemsLimit; | ||
} | ||
case UserRole.premiumUser: | ||
if (itemType == 'headline') { | ||
limit = limits.premiumSavedHeadlinesLimit; | ||
} else { | ||
limit = limits.premiumFollowedItemsLimit; | ||
} | ||
case UserRole.admin: | ||
// Admins have no limits | ||
return; | ||
String accountType; | ||
|
||
if (user.roles.contains(UserRoles.premiumUser)) { | ||
accountType = 'premium'; | ||
limit = (itemType == 'headline') | ||
? limits.premiumSavedHeadlinesLimit | ||
: limits.premiumFollowedItemsLimit; | ||
} else if (user.roles.contains(UserRoles.standardUser)) { | ||
accountType = 'standard'; | ||
limit = (itemType == 'headline') | ||
? limits.authenticatedSavedHeadlinesLimit | ||
: limits.authenticatedFollowedItemsLimit; | ||
} else if (user.roles.contains(UserRoles.guestUser)) { | ||
accountType = 'guest'; | ||
limit = (itemType == 'headline') | ||
? limits.guestSavedHeadlinesLimit | ||
: limits.guestFollowedItemsLimit; | ||
} else { | ||
// Fallback for users with unknown or no roles. | ||
throw const ForbiddenException( | ||
'Cannot determine preference limits for this user account.', | ||
); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The logic for determining user limits based on roles is duplicated in checkAddItem
and checkUpdatePreferences
. Refactor this into a private helper method to reduce redundancy and improve maintainability.1
int _getLimitForUser(User user, String itemType, UserPreferenceLimits limits) {
if (user.roles.contains(UserRoles.premiumUser)) {
return (itemType == 'headline')
? limits.premiumSavedHeadlinesLimit
: limits.premiumFollowedItemsLimit;
} else if (user.roles.contains(UserRoles.standardUser)) {
return (itemType == 'headline')
? limits.authenticatedSavedHeadlinesLimit
: limits.authenticatedFollowedItemsLimit;
} else if (user.roles.contains(UserRoles.guestUser)) {
return (itemType == 'headline')
? limits.guestSavedHeadlinesLimit
: limits.guestFollowedItemsLimit;
} else {
throw const ForbiddenException(
'Cannot determine preference limits for this user account.');
}
}
Style Guide References
Footnotes
// Fallback for users with unknown or no roles. | ||
throw const ForbiddenException( | ||
'Cannot determine preference limits for this user account.', | ||
); | ||
} | ||
|
||
// 3. Check if adding the item would exceed the limit | ||
if (currentCount >= limit) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
throw ForbiddenException( | ||
'You have reached the maximum number of followed countries allowed ' | ||
'for your account type (${user.role.name}).', | ||
'for your account type ($accountType).', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (updatedPreferences.followedSources.length > followedItemsLimit) { | ||
throw ForbiddenException( | ||
'You have reached the maximum number of followed sources allowed ' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (updatedPreferences.followedCategories.length > followedItemsLimit) { | ||
throw ForbiddenException( | ||
'You have reached the maximum number of followed categories allowed ' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
throw ForbiddenException( | ||
'You have reached the maximum number of saved headlines allowed ' | ||
'for your account type (${user.role.name}).', | ||
'for your account type ($accountType).', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Status
READY/IN DEVELOPMENT/HOLD
Description
Type of Change