-
Notifications
You must be signed in to change notification settings - Fork 1
Finish Up Restructuring User System #84
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| from firebase_admin import auth | ||
|
|
||
| from app import initialize_firebase | ||
|
|
||
| admin_email = "[email protected]" | ||
|
|
||
|
Comment on lines
+4
to
+6
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might be best to have this as an env variable to avoid hardcoded values |
||
|
|
||
| def update_all_users_role(role_name: str) -> None: | ||
| """ | ||
|
Comment on lines
+7
to
+9
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great job on implementing a way to update all roles! I realize that I should have probably better specified this in the ticket, but can we add another function to update only one user to admin or driver instead? This way, we won't have people changing other people's roles in the middle of development. Thanks! |
||
| Iterates through all Firebase users and sets a custom 'role' claim. | ||
| Always ensures the admin only has the role 'admin' | ||
| """ | ||
| initialize_firebase() | ||
| print(f"Starting update: Setting all non admin users to role: {role_name}") | ||
|
|
||
| # List all users (paginated) | ||
| page = auth.list_users() | ||
| count = 0 | ||
|
|
||
| while page: | ||
| for user in page.users: | ||
| try: | ||
| if user.email == admin_email: | ||
| auth.set_custom_user_claims(user.uid, {"role": "admin"}) | ||
| else: | ||
| # This overwrites existing claims, so be careful if you have other claims! | ||
| auth.set_custom_user_claims(user.uid, {"role": role_name}) | ||
|
|
||
| print(f"Updated UID: {user.uid} ({user.email})") | ||
| count += 1 | ||
| except Exception as e: | ||
| print(f"Failed to update {user.uid}: {e}") | ||
|
|
||
| # Get the next page of users | ||
| page = page.get_next_page() | ||
|
|
||
| print(f"\nSuccessfully updated {count} users.") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| # Change to new desired role | ||
| # NOTE: This overwrites preexisting roles so be careful! | ||
| new_role = "driver" | ||
| update_all_users_role(new_role) | ||
|
Comment on lines
+42
to
+44
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Once the update_your_user_role is implemented (with input param being your email). Please feel free to make that the default main function that runs, and make a variable your_email = "" to be used by the function! |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| """Add cascade delete to Driver and Admin | ||
|
|
||
| Revision ID: 01f342ea9ad6 | ||
| Revises: ba76119b3e4c | ||
| Create Date: 2026-02-08 00:31:30.165149 | ||
|
|
||
| """ | ||
| from alembic import op | ||
|
|
||
| # revision identifiers, used by Alembic. | ||
| revision = '01f342ea9ad6' | ||
| down_revision = 'ba76119b3e4c' | ||
| branch_labels = None | ||
| depends_on = None | ||
|
|
||
|
|
||
| def upgrade(): | ||
| # ### commands auto generated by Alembic - please adjust! ### | ||
| op.drop_constraint(op.f('admin_info_user_id_fkey'), 'admin_info', type_='foreignkey') | ||
| op.create_foreign_key(None, 'admin_info', 'users', ['user_id'], ['user_id'], ondelete='CASCADE') | ||
| op.drop_constraint(op.f('drivers_user_id_fkey'), 'drivers', type_='foreignkey') | ||
| op.create_foreign_key(None, 'drivers', 'users', ['user_id'], ['user_id'], ondelete='CASCADE') | ||
| # ### end Alembic commands ### | ||
|
|
||
|
|
||
| def downgrade(): | ||
| # ### commands auto generated by Alembic - please adjust! ### | ||
| op.drop_constraint(None, 'drivers', type_='foreignkey') | ||
| op.create_foreign_key(op.f('drivers_user_id_fkey'), 'drivers', 'users', ['user_id'], ['user_id']) | ||
| op.drop_constraint(None, 'admin_info', type_='foreignkey') | ||
| op.create_foreign_key(op.f('admin_info_user_id_fkey'), 'admin_info', 'users', ['user_id'], ['user_id']) | ||
| # ### end Alembic commands ### |
Uh oh!
There was an error while loading. Please reload this page.
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.
Just a quick food for thought, in our codebase, there are two roles drivers and admin, and some routes are admin only. This means that by default, admin should be able to access all routes, so it might not make sense to have a require driver.
This gives 2 solutions:
Admin ID bypasses all id checks or has access to all IDs
Lmk which one you would prefer and your thoughts!