Once a web app grows past a single admin role, frontend permission logic tends to sprawl into scattered conditionals. A document manager is a useful example: admins manage everything, editors create and modify documents but cannot delete them, and guests only read. The same reasoning extends to a recipe app or a private cloud service.

The pattern below stays framework agnostic — React, Vue, and plain vanilla JavaScript all work, and the same shape applies whether the data comes from a REST API, GraphQL, or server-side rendering. The central idea is that the data returned for the current user includes their role, so the data object itself tells the frontend what is allowed.

{
  id: 1,
  title: "My First Document",
  authorId: 742,
  accessLevel: "ADMIN",
  content: {...}
}

That accessLevel property carries the user's role and drives every check that follows. One caveat comes first: the frontend must never be the only checkpoint. Anyone can issue a request directly to the server without going through the UI, so the backend has to enforce permissions as well.

Constants and the action-role mapping

A good first step is a dedicated constants.js holding the app's actions and roles. This is optional but strongly recommended. With TypeScript, enums give a slightly cleaner syntax.

const actions = {
  MODIFY_FILE: "MODIFY_FILE",
  VIEW_FILE: "VIEW_FILE",
  DELETE_FILE: "DELETE_FILE",
  CREATE_FILE: "CREATE_FILE"
};

const roles = {
  ADMIN: "ADMIN",
  EDITOR: "EDITOR",
  GUEST: "GUEST"
};

export { actions, roles };

Centralizing these values pays off in three ways. It gives one source of truth that you edit when actions are added or removed, it removes hand-typed role names (with editor autocompletion and linting as a safety net), and it documents the available permissions for new team members, optionally with JSDoc. Importing the constants is straightforward:

import { actions } from "./constants.js";

console.log(actions.CREATE_FILE);

The actual rule set lives in permissions.js, built around a JavaScript Map called mappings. Each action is a key; the value is an array of the roles required for that action.

import { actions, roles } from "./constants.js";

const mappings = new Map();

mappings.set(actions.MODIFY_FILE, [roles.ADMIN, roles.EDITOR]);
mappings.set(actions.VIEW_FILE, [roles.ADMIN, roles.EDITOR, roles.GUEST]);
mappings.set(actions.DELETE_FILE, [roles.ADMIN]);
mappings.set(actions.CREATE_FILE, [roles.ADMIN, roles.EDITOR]);

The flow is simple: import the constants, create the Map, and add one entry per action. Objects or arrays would work too, but a Map offers handy methods such as .has() and .get(). Despite the inverted, unusual-looking shape — actions as keys rather than roles — the benefits hold up in large applications. There is a single place to look when you need to know who can edit a file, changing business logic is minimal (add the editor role to the DELETE_FILE entry and editors can delete from then on), and the mappings are easy to snapshot-test and review. This simple form can later be extended, for instance when different file types carry different role access.

A checker function for the UI

Start from the call site. Given a React component rendering a dropdown menu, guests should neither see nor click "Delete" or "Rename" but should see "Refresh"; editors see everything except "Delete." That suggests an API like the following.


function Dropdown() {
  return (
    <ul>
      <li><button type="button">Refresh</button><li>
      <li><button type="button">Rename</button><li>
      <li><button type="button">Duplicate</button><li>
      <li><button type="button">Delete</button><li>
    </ul>
  );
}
hasPermission(file, actions.DELETE_FILE);

The first argument is the file returned by the REST API, whose accessLevel is ADMIN, EDITOR, or GUEST; since a user can hold different rights on different files, it always has to be passed. The second is the action to perform. The function returns a boolean: true when the current user may perform the action, false otherwise.

import hasPermission from "./permissions.js";
import { actions } from "./constants.js";

function Dropdown() {
  return (
    <ul>
      {hasPermission(file, actions.VIEW_FILE) && (
        <li><button type="button">Refresh</button></li>
      )}
      {hasPermission(file, actions.MODIFY_FILE) && (
        <li><button type="button">Rename</button></li>
      )}
      {hasPermission(file, actions.CREATE_FILE) && (
        <li><button type="button">Duplicate</button></li>
      )}
      {hasPermission(file, actions.DELETE_FILE) && (
        <li><button type="button">Delete</button></li>
      )}
    </ul>
  );
}

A less verbose function name or another shape (currying, for instance) is a matter of taste; this version has scaled well in practice, including in applications with very complex permissions. The cost in JSX clutter is small compared with the clarity of applying the pattern consistently.

return (
  <ul>
    {['ADMIN', 'EDITOR', 'GUEST'].includes(file.accessLevel) && (
      <li><button type="button">Refresh</button></li>
    )}
    {['ADMIN', 'EDITOR'].includes(file.accessLevel) && (
      <li><button type="button">Rename</button></li>
    )}
    {['ADMIN', 'EDITOR'].includes(file.accessLevel) && (
      <li><button type="button">Duplicate</button></li>
    )}
    {file.accessLevel == "ADMIN" && (
      <li><button type="button">Delete</button></li>
    )}
  </ul>
);

Without the helper this particular example may still look manageable, but more logic — license checks, finer-grained permissions — is where that kind of code degrades quickly. The permission check on the always-visible "Refresh" button is deliberately kept: if a future role may not see it at all, only permissions.js changes, leaving the component untouched. That means cleaner commits and fewer chances to break things.

Implementing hasPermission

The implementation can live in its own file or inside permissions.js.

import mappings from "./permissions.js";

function hasPermission(file, action) {
  if (!file?.accessLevel) {
    return false;
  }

  if (mappings.has(action)) {
    return mappings.get(action).includes(file.accessLevel);
  }

  return false;
}

export default hasPermission;
export { actions, roles };

The steps, in order:

  1. hasPermission takes the file from the backend and the desired action, matching the signature chosen above.
  2. If the file is null or has no accessLevel, return false — a fail-safe against exposing information because of a glitch.
  3. If mappings holds the action, read its array of roles and check whether the current user's role is among them, returning true or false.
  4. If the action is missing from mappings (a coding mistake or a glitch), return false again for safety.
  5. The module exports hasPermission and re-exports the constants, so a single import line covers all utilities.
import hasPermission, { actions } from "./permissions.js";

Extending to licenses and entity types

The pattern generalizes beyond roles. Swapping the role for a license property on the user gives the same inputs, the same output, and an entirely different purpose.

import { actions, licenses } from "./constants.js";

const mappings = new Map();

mappings.set(actions.MODIFY_FILE, [licenses.PAID]);
mappings.set(actions.VIEW_FILE, [licenses.FREE, licenses.PAID]);
mappings.set(actions.DELETE_FILE, [licenses.FREE, licenses.PAID]);
mappings.set(actions.CREATE_FILE, [licenses.PAID]);

function hasLicense(user, action) {
  if (mappings.has(action)) {
    return mappings.get(action).includes(user.license);
  }

  return false;
}

When an app must check roles and licenses both together and separately, the approach is one function per check combined inside a wrapper such as a hasAccess util.

function hasAccess(file, user, action) {
  return hasPermission(file, action) && hasLicense(user, action);
}

Passing three arguments on every call is not ideal; currying or global state can remove that. With global stores holding user information, for example, the second argument disappears and the user is read from the store instead.

Entities can also branch by type. Here the values become a nested Map of entity types, offering the same .has() convenience — useful, say, to gate a paid Office exporter behind a license while keeping one action for several file types.

const mappings = new Map();

mappings.set(
  actions.EXPORT_FILE,
  new Map([
    [types.PDF, [licenses.FREE, licenses.PAID]],
    [types.DOCX, [licenses.PAID]],
    [types.XLSX, [licenses.PAID]],
    [types.PPTX, [licenses.PAID]]
  ])
);

hasLicense needs a small update to handle the added depth:

function hasLicense(user, file, action) {
  if (!user || !file) {
    return false;
  }

  if (mappings.has(action)) {
    const mapping = mappings.get(action);

    if (mapping.has(file.type)) {
      return mapping.get(file.type).includes(user.license);
    }
  }

  return false;
}

Testing

Code coverage matters for permission logic, since refactors and new features must not silently widen access. There are several complementary approaches:

  • Snapshot tests for mappings, actions, and types, easily written in Jest or another runner, catch unexpected changes in review. They can become tedious to update if permissions churn constantly.
  • Unit tests for hasPermission and hasLicense with hard-coded real-world cases verify the returned value.
  • Additional snapshot tests over the constants can cover every scenario; a team setup along these lines looks like the following.
Object.values(actions).forEach((action) => {
  describe(action.toLowerCase(), function() {
    Object.values(licenses).forEach((license) => {
      it(license.toLowerCase(), function() {
        expect(hasLicense({ type: 'PDF' }, { license }, action)).toMatchSnapshot();
        expect(hasLicense({ type: 'DOCX' }, { license }, action)).toMatchSnapshot();
        expect(hasLicense({ type: 'XLSX' }, { license }, action)).toMatchSnapshot();
        expect(hasLicense({ type: 'PPTX' }, { license }, action)).toMatchSnapshot();
      });
    });
  });
});

Why the pattern works

UI components need no complicated conditionals: they show and hide elements based on hasPermission's return value, which keeps business logic out of markup and the codebase maintainable. The mappings act as the one place to consult when you need to know who can see what, so extending or changing permissions often requires no markup changes at all. Centralized rules are also painless to test — snapshot, integration, or otherwise — and you get documentation and autocompletion without TypeScript, plus fewer typos and a quick way for teammates to see which actions and roles exist.