Access Control Tree

Hierarchical permissions

Design a tree of resources, like folders and files, where a permission granted on a folder applies to everything inside it unless a lower node says otherwise.

Overview

Picture a shared drive, where folders hold files and other folders. You grant a team WRITE on the folder /eng, and every file under /eng is writable by that team without a grant on each file. Then one file holds secrets, so you block the team there while one person keeps READ. The system must answer one question fast and correctly: what may this user do on this node?

The problem tests three things. First, a clear resolution rule: when grants at different levels, or for a user and a group, disagree, which one wins? Say the rule in one sentence before you write code. Second, the tree itself: finding a node by path, creating missing folders like mkdir -p, and moving a subtree without creating a cycle. Third, the cost of a check: walking up the tree on every check is simple and always correct; caching is faster but must be cleared when anything changes.

A strong answer states the rule, draws the tree with a worked example, writes the walk-up resolver, and then discusses caching, groups and persistence as follow-ups.

Scoping the problem

Ask a few questions before you write code, because each answer changes the resolver. The usual answers are the ones this design builds on.

  • Are permissions ordered levels, or independent rights? Levels compare with >=; independent rights need a set or bit flags. The usual answer is levels: NONE, READ, WRITE, DELETE, ADMIN.
  • Can a lower node take away what a folder granted? That needs an explicit block, not just a missing entry. Usually yes: an explicit NONE blocks.
  • Are there groups, and what if a user and their group disagree on one node? This decides the tie-break at one node. Usually there are groups, and the user's own entry wins.
  • What if two of the user's groups disagree? You can pick the most permissive or the least permissive. Usually the highest level wins.
  • How often are grants changed compared with checks? This decides between walking up and caching. Checks far outnumber changes.
  • Can subtrees be moved? A move changes what every node below inherits. Usually yes, with a cycle check.

Functional requirements

  • Create nodes by path, creating missing parents like mkdir -p
  • Grant or revoke a permission level for a user or a group on any node
  • Resolve the effective permission: the nearest node with a matching entry decides; there the user entry beats groups, and the highest group level wins
  • An explicit NONE on a lower node blocks a grant from above
  • can(path, user, needed) answers effective >= needed
  • List the children of a node that a user may read
  • Move a subtree to a new parent, refusing moves of the root and moves under itself

Non-functional requirements

  • A check costs O(d) for depth d, or O(1) when the cached result is still valid
  • Every change clears the cache, so a check never sees a stale result
  • Safe with many threads: one lock guards the tree, entries, groups and cache
  • Adding a permission level is one enum member; the comparison code does not change

Interview questions

  • Why ordered levels instead of separate rights?
  • User and group entries disagree. Which one wins, and what do real systems do?
  • Checks walk up the tree every time. How would you make them faster?
  • How do you prevent a cycle when a folder is moved?
  • How would you store this in a database?
  • One lock guards everything. When is that not enough?

Sign up free to read the full topic

More Low-Level Design topics

Start free on Cariara · Coding problems · System design studio