Role-based access control pattern in Firebase - Time & Space Complexity
When using role-based access control in Firebase, we want to know how the time to check permissions changes as more users or roles are involved.
We ask: How does the number of permission checks grow when the system scales?
Analyze the time complexity of checking user roles to allow access.
// Example Firebase security rule snippet
match /documents/{docId} {
allow read: if get(/databases/$(database)/documents/users/$(request.auth.uid)).data.role == 'admin';
}
This checks if the requesting user has the 'admin' role before allowing read access to a document.
Look at what happens each time a user tries to access a document.
- Primary operation: Reading the user's role data from the database.
- How many times: Once per access request.
Each access request triggers one role check, no matter how many users or documents exist.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 10 role checks |
| 100 | 100 role checks |
| 1000 | 1000 role checks |
Pattern observation: The number of role checks grows directly with the number of access requests.
Time Complexity: O(n)
This means the time to check roles grows linearly with the number of access requests.
[X] Wrong: "Checking roles happens once and applies to all requests."
[OK] Correct: Each request independently checks the user's role, so the checks add up as requests increase.
Understanding how access checks scale helps you design secure and efficient systems, a key skill in cloud roles.
"What if roles were cached on the client side? How would the time complexity change?"
Practice
Solution
Step 1: Understand RBAC concept
RBAC assigns permissions to users based on their roles, controlling what they can access.Step 2: Identify RBAC purpose in Firebase
In Firebase, RBAC is used in security rules to allow or deny access depending on user roles.Final Answer:
To manage user permissions based on their assigned roles -> Option BQuick Check:
RBAC controls permissions by roles [OK]
- Confusing RBAC with data storage functions
- Thinking RBAC creates authentication tokens
- Assuming RBAC handles backups
request.auth.token.role?Solution
Step 1: Identify correct token path
The user's role is stored inrequest.auth.token.role, so the check must use this exact path.Step 2: Match syntax for role check
The correct syntax comparesrequest.auth.token.roleto the string 'admin' using ==.Final Answer:
allow read: if request.auth.token.role == 'admin'; -> Option AQuick Check:
Use full path request.auth.token.role == 'admin' [OK]
- Using request.auth.role instead of request.auth.token.role
- Omitting 'token' in the path
- Comparing token object directly to string
allow write: if request.auth.token.role == 'editor';
What happens if a user with role 'viewer' tries to write data?
Solution
Step 1: Analyze the rule condition
The rule allows write only ifrequest.auth.token.roleequals 'editor'.Step 2: Check user's role against condition
The user has role 'viewer', which does not match 'editor', so the condition fails.Final Answer:
The write is denied because the role is not 'editor' -> Option CQuick Check:
Role mismatch denies write [OK]
- Assuming any authenticated user can write
- Confusing roles with authentication status
- Ignoring role check in rules
allow read: if request.auth.token.role = 'admin';
Solution
Step 1: Check comparison operator
The rule uses '=' which is an assignment operator, not a comparison operator.Step 2: Correct operator for equality check
Firebase rules require '==' to compare values, so '=' causes a syntax error.Final Answer:
Using single equals '=' instead of double equals '==' for comparison -> Option AQuick Check:
Use '==' for comparisons in rules [OK]
- Using '=' instead of '==' in conditions
- Confusing property names in request.auth
- Ignoring syntax errors in rules
Solution
Step 1: Understand role conditions for delete
Users with role 'admin' or 'moderator' can delete, so use 'in' operator with list ['admin', 'moderator'] for clarity and correctness.Step 2: Understand role condition for update
Only 'admin' can update, so check equality with 'admin' for update permission.Step 3: Evaluate options
allow delete: if request.auth.token.role == 'admin' || 'moderator'; allow update: if request.auth.token.role == 'admin'; has syntax error ('|| 'moderator'' invalid); allow delete, update: if request.auth.token.role == 'admin' || 'moderator'; has syntax error with '|| 'moderator''; allow delete: if request.auth.token.role == 'admin' && request.auth.token.role == 'moderator'; allow update: if request.auth.token.role == 'admin'; uses '&&' which requires both roles simultaneously (impossible). allow delete: if request.auth.token.role in ['admin', 'moderator']; allow update: if request.auth.token.role == 'admin'; correctly uses 'in' and separate rules.Final Answer:
allow delete: if request.auth.token.role in ['admin', 'moderator']; allow update: if request.auth.token.role == 'admin'; -> Option DQuick Check:
Use 'in' for multiple roles, '==' for single role [OK]
- Using '&&' instead of '||' for multiple roles
- Incorrect syntax with '||' and strings
- Trying to check multiple roles with '==' operator
Ready to prove it?
Pass the test and this pattern moves unknown
