The short answer
- A user can be in many teams and a team has many users, so add a Membership table.
- Hold the role on the membership, not on the user.
- Write the permission rules in plain words and test each against the tables.
A finished example
This is what the Accounts and permissions database template can look like once it is filled in. This one is for a small app where people work in teams.
Read the example as words
- On the board
- User: id, number, main ID; name, text; email, text; created, date
- Membership: id, number, main ID; user_id, number, links to another table; team_id, number, links to another table; role_id, number, links to another table; joined, date
- Team: id, number, main ID; name, text; plan, text
- Role: id, number, main ID; name, text; can_edit, yes/no; can_invite, yes/no
- Who can do what
- Only an Admin may invite
- Every team keeps at least one Admin
- A person can be in two teams with different roles
- How the boxes are joined
- User has Membership, one to many
- Team has Membership, one to many
- Role is given to Membership, one to many
Who can do what is a data question
Almost every app that more than one person uses has to answer the same question. Who is allowed to do what?
It sounds like a question about screens and buttons. It is really a question about data. The app can only check a rule if the facts behind the rule are stored somewhere. Take the rule "Only an admin of this team may invite people". The app must know who the person is, which team they are in and their role there.
If the tables are wrong, the rules are hard or impossible to check, and they are painful to change once real accounts exist. A person who should have been in two teams ends up with two logins. A role that should have belonged to one team applies everywhere.
So this is worth an hour at a whiteboard with the people who know how customers will use the product. You are not choosing technology. You are agreeing what a user, a team and a role mean.
Users, teams and the table between them
Start with two tables. A User is a person who can sign in. A Team is a group that shares work, and usually the thing that pays.
Now ask the two questions that fit every line on a data model. Can one user be in many teams? Can one team have many users?
The second answer is always yes. The first is the one that catches people out. A freelancer works with three clients. A manager sits in two departments. If you say no today, you will probably be changing it within a year.
When both answers are yes, the link is many-to-many, and two tables cannot hold it on their own. You need a third table between them. Here it is called Membership. One row of Membership says "this user is in this team".
On the board, User joins to Membership as one to many, and so does Team. Membership holds two fields that point outward: user_id and team_id.
The table in the middle turns out to be useful. Facts about the link itself live there, such as the date the person joined. The most important of those facts is the role.
Roles belong to the membership
It is tempting to put a role field on the User table. Resist it. A person may be an admin in one team and a viewer in another. The role is a fact about the person in that team, so it belongs on Membership.
The template keeps roles in a small table of their own: Role, with a name and a few yes or no fields such as can_edit and can_invite. Membership points at it with role_id. Adding a new role later is then a new row, not a change to the app.
Now write the rules in the frame at the side, in plain words, and test each one against the tables.
- Only an Admin may invite. Can the app find the person's role in this team? Yes, through Membership and Role.
- Every team keeps one Admin or more. The tables allow a team with none, so the app must refuse to remove the last one. Write that down.
- A person who leaves keeps their own account. Removing the Membership row does that, and leaves the User row alone.
Keep the number of roles small. Three that people understand are better than nine that nobody can explain. And store as little about each user as the product needs.
Using the template on doodleboard
The Accounts and permissions database template on doodleboard starts with four data tables: User, Team, Membership and Role. The lines between them have their "one" and "many" ends set, and each has a short label so you can read it aloud.
Double-click a table to rename it or change its fields. Each field has a name, a type such as text, number or date, and a key. Main ID (PK) marks the field that identifies each row. Links to a table (FK) marks a field that points at another table.
To add something of your own, such as an Invite table, choose Data table from the More menu. Draw a connector from one of its fields to the table it belongs to, then select the connector to set the ends.
Walk a few real people through the drawing. A new starter who has been invited but has not yet signed up. Someone who moves from one team to another. An owner who wants to hand over to a colleague. Each should have a clear place in the tables.
When the team agrees, select the tables and choose Copy as code to take the model out as SQL or as Mermaid. If the product already has a database, paste its SQL onto the board first and start from what is really there.
Questions people ask
Why not put the team on the user's row?
Because then a person can only ever be in one team. It works on day one and breaks the first time someone needs to belong to two. A table between users and teams costs little now and saves a hard change later.
How many roles should we start with?
As few as the product needs, often two or three, such as Admin, Member and Viewer. Each should be easy to explain in one sentence. Add another only when a real customer need cannot be met by the ones you have.
Does this cover signing in with Google or Microsoft?
Not by itself. This model is about who belongs where and what they may do. How a person proves who they are is a separate matter. It usually needs one more table, joining a User to each way they sign in.
