Restricted the Admin Pages from users #59
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description of your changes:
This PR introduces a role-based access control (RBAC) mechanism for the app, ensuring that only users with the correct roles (i.e., "admin") are allowed to access certain routes and pages. The changes include the enhancement of the
role_required
decorator to check the user's role and redirect non-admin users to the403.html
page if they attempt to access admin-specific pages. Additionally, improvements were made to handle cases where the user session might beNone
, preventingAttributeError
exceptions when accessing user data.Changes:
role_required
Decorator:role_required
decorator was modified to first check for thegoogle_id
in the session. If present, it checks whether the user is an admin via theis_admin
function.google_id
is not in the session, the decorator checks for the regular user session (username
) and verifies their role by fetching user data from the database.403.html
page.Session Handling:
get_currentuser_from_session()
function returnsNone
(i.e., when no valid user data is found in the session). This prevents errors when accessing attributes likerole
of a non-existent user.403.html
page to indicate a forbidden access attempt.Improved Admin Access Flow:
admin
to be accessed. Non-admin users will be redirected to a403
page.Testing:
admin
can access admin pages.403.html
page when attempting to access restricted areas.AttributeError: 'NoneType' object has no attribute 'get'
.Why this change is necessary:
This update ensures secure access to admin routes, preventing unauthorized users from accessing administrative functionality and resources. It also handles session inconsistencies more gracefully, improving the overall robustness and security of the app.
Checklist: