Add account member management to a Phoenix 1.8 app that already has multi-tenant accounts set up via /bootstrap-accounts. Provides a LiveView for listing members, inviting users by email, role assignment, and member removal with safety guards.
Prerequisites
Verify before starting:
/bootstrap-accountshas been run (accounts table, account_users table, Account schema, AccountUser schema, Accounts context, AccountContext module, LoadAccount plug, account-scoped router all exist)phx.gen.authcompleted (User schema, UserAuth module exist)- DaisyUI installed
App Name Detection
Detect the app module name from mix.exs (e.g., MyApp) and the OTP app name (e.g., my_app). Replace all occurrences of MyApp/my_app/MyAppWeb/my_app_web in reference templates with the actual names.
Implementation Order
Execute in this exact order. Read the referenced file for each step's code templates.
Phase 1: Context Functions
- Add member management functions to the
Accountscontext:list_account_members/1,get_account_member/2,invite_user_to_account/3,delete_account_member/1,count_account_owners/1,authorize_role_assignment/2,parse_member_role/1
Phase 2: Display Helpers
- Create
MemberDisplayHelpersmodule for shared role and date formatting
Phase 3: LiveView & Form Component
- Create
AccountUsersLiveLiveView with table display and delete action - Create
AccountUserFormComponentfor the invite modal
Phase 4: Routes
Read references/routes.md
- Add routes for the user management pages inside the existing account-scoped
live_session - Add email notification function for invited users
Phase 5: Verify
- Run
mix compile --warnings-as-errorsand fix any issues - Verify the routes exist:
mix phx.routes | grep users
Key Patterns
Role hierarchy:
:owner— full access, can assign any role including owner:admin— can invite/remove users, cannot assign owner role:member— basic access, no management permissions
Safety guards:
- Cannot remove yourself
- Cannot remove the last owner
- Only admins/owners see the Add User button and Remove links
- Only owners can assign the owner role
- Duplicate member detection with friendly error message
LiveView assigns in mount:
:active_nav— set to:account_users:current_account_user— the current user's membership in this account:breadcrumbs— navigation breadcrumbs:page_title— browser tab title