KeystoneJS Access Control Setup

Our company is engaged in the development, support and maintenance of sites of any complexity. From simple one-page sites to large-scale cluster systems built on micro services. Experience of developers is confirmed by certificates from vendors.
Development and maintenance of all types of websites:
Informational websites or web applications
Business card websites, landing pages, corporate websites, online catalogs, quizzes, promo websites, blogs, news resources, informational portals, forums, aggregators
E-commerce websites or web applications
Online stores, B2B portals, marketplaces, online exchanges, cashback websites, exchanges, dropshipping platforms, product parsers
Business process management web applications
CRM systems, ERP systems, corporate portals, production management systems, information parsers
Electronic service websites or web applications
Classified ads platforms, online schools, online cinemas, website builders, portals for electronic services, video hosting platforms, thematic portals

These are just some of the technical types of websites we work with, and each of them can have its own specific features and functionality, as well as be customized to meet the specific needs and goals of the client.

Showing 1 of 1 servicesAll 2065 services
KeystoneJS Access Control Setup
Medium
from 1 business day to 3 business days
FAQ
Our competencies:
Development stages
Latest works
  • image_website-b2b-advance_0.png
    B2B ADVANCE company website development
    1212
  • image_web-applications_feedme_466_0.webp
    Development of a web application for FEEDME
    1161
  • image_websites_belfingroup_462_0.webp
    Website development for BELFINGROUP
    852
  • image_ecommerce_furnoro_435_0.webp
    Development of an online store for the company FURNORO
    1041
  • image_crm_enviok_479_0.webp
    Development of a web application for Enviok
    822
  • image_bitrix-bitrix-24-1c_fixper_448_0.png
    Website development for FIXPER company
    815

Access Control Setup in KeystoneJS

KeystoneJS provides multi-level access management: at operation level (CRUD), individual items, and specific fields. Granularity allows implementing any role model without external libraries.

Access Levels

Operation Access — allow/deny operation entirely (before fetching from DB):

access: {
  operation: {
    query: ({ session }) => !!session, // only authorized
    create: ({ session }) => session?.data?.role === 'editor',
    update: ({ session }) => ['editor', 'admin'].includes(session?.data?.role),
    delete: ({ session }) => session?.data?.role === 'admin',
  },
},

Filter Access — restrict visible data via automatic WHERE filter:

access: {
  filter: {
    // Authors see only their posts, admin — all
    query: ({ session }) => {
      if (session?.data?.role === 'admin') return true;
      return { author: { id: { equals: session?.data?.id } } };
    },
    update: ({ session }) => {
      if (session?.data?.role === 'admin') return true;
      return { author: { id: { equals: session?.data?.id } } };
    },
  },
},

Item Access — check for specific item (after fetching):

access: {
  item: {
    update: async ({ session, item }) => {
      if (session?.data?.role === 'admin') return true;
      // Can edit only drafts
      return item.status === 'draft' && item.authorId === session?.data?.id;
    },
    delete: async ({ session, item }) => {
      return session?.data?.role === 'admin' || item.authorId === session?.data?.id;
    },
  },
},

Field-Level Access Control

fields: {
  title: text(),
  // Regular editors don't see internal notes
  internalNotes: text({
    access: {
      read: ({ session }) => session?.data?.role === 'admin',
      create: ({ session }) => session?.data?.role === 'admin',
      update: ({ session }) => session?.data?.role === 'admin',
    },
  }),
  // Salary — only HR and admin
  salary: integer({
    access: {
      read: ({ session }) => ['admin', 'hr'].includes(session?.data?.role),
      update: ({ session }) => session?.data?.role === 'admin',
    },
  }),
},

Role Model via Database

Instead of hardcoding roles — store permissions in DB:

// lists/Role.ts
export const Role = list({
  access: {
    operation: {
      query: allowAll,
      create: ({ session }) => session?.data?.role === 'admin',
      update: ({ session }) => session?.data?.role === 'admin',
      delete: ({ session }) => session?.data?.role === 'admin',
    },
  },
  fields: {
    name: text({ validation: { isRequired: true }, isIndexed: 'unique' }),
    canManagePosts: checkbox({ defaultValue: false }),
    canManageUsers: checkbox({ defaultValue: false }),
    canManageRoles: checkbox({ defaultValue: false }),
    canPublish: checkbox({ defaultValue: false }),
    users: relationship({ ref: 'User.role', many: true }),
  },
});

// lists/Post.ts — use role permissions from DB
access: {
  operation: {
    create: ({ session }) => !!session?.data?.role?.canManagePosts,
    update: ({ session }) => !!session?.data?.role?.canManagePosts,
    delete: ({ session }) => !!session?.data?.role?.canManagePosts,
  },
},

Include needed role fields in sessionData:

// auth.ts
sessionData: 'id name email role { canManagePosts canManageUsers canPublish }',

Public API for Frontend

Part of data should be publicly accessible for headless:

// Helper for mixed access
const isSignedIn = ({ session }) => !!session;
const isAdmin = ({ session }) => session?.data?.role === 'admin';
const isPublicOrSignedIn = ({ session }) => true; // open to all

export const Article = list({
  access: {
    operation: {
      query: isPublicOrSignedIn, // articles read by all
      create: isSignedIn,
      update: isAdmin,
      delete: isAdmin,
    },
    filter: {
      query: ({ session }) => {
        if (session?.data?.role === 'admin') return true;
        return { status: { equals: 'published' } }; // guests and users — only published
      },
    },
  },
});

Typical role model setup (3–4 roles, 5–10 Lists) takes 2–4 days, including testing access scenarios.