Implementing Permission Systems

1. Defining Permission Schema

ConventionExample
resource:actionorders:read, orders:write
domain.action.resourcebilling.view.invoice
CRUDScreate, read, update, delete, share
Conditionalorders:delete:own

2. Creating Permission Checks

Example: Centralized check helper

public class PermissionChecker {
  public boolean can(User user, String permission, Object resource) {
    if (user.hasPermission(permission)) return true;
    if (permission.endsWith(":own") && isOwner(user, resource)) return true;
    return false;
  }
}

3. Implementing Resource-Based Permissions

PatternDetail
Owner checkresource.owner_id == user.id
Tenant scoperesource.tenant_id == user.tenant_id
Shared resourceACL lookup
Row-level securityDB-enforced (Postgres RLS)

4. Using Permission Wildcards

PatternMatches
orders:*All actions on orders
*:readRead on any resource
*Superuser (use sparingly)
Hierarchicalbilling.* matches billing.view.invoice

5. Implementing Permission Inheritance

DirectionDetail
Role hierarchyParent role inherits child's perms
Resource treeFolder grant → file inheritance
Group nestingGroup A in Group B → A's members get B's perms

6. Combining Multiple Permissions

OperatorUse
Any (OR)Has any of [a, b, c]
All (AND)Has all of [a, b, c]
Not (NOT)Does not have x
Conditionalhas(x) AND env.mfa == true

7. Implementing Conditional Permissions

Example: Time-bound permission

function canAccess(user, resource, env) {
  const grant = user.permissions.find(p => p.action === "approve");
  if (!grant) return false;
  if (grant.condition?.business_hours && !isBusinessHours(env.time)) return false;
  if (grant.condition?.max_amount && resource.amount > grant.condition.max_amount) return false;
  return true;
}

8. Caching Permission Checks

LayerTTL
Request-scopedMemoize within single request
User session30 sec–5 min
DistributedRedis with invalidation events
Invalidate onRole/permission change, user disable

9. Implementing Permission Delegation

AspectDetail
GrantUser A delegates subset of perms to user B
ConstraintCannot delegate more than self has
Time-boundAuto-expire (vacation coverage)
AuditLog delegator + delegatee + actions
RevocableBy delegator or admin

10. Auditing Permission Changes

EventLogged
grantactor, subject, permission, resource, timestamp
revokeactor, subject, permission, reason
role create/updatediff of permissions
policy changebefore/after rule