Skip to content

Commit

Permalink
chore: Implement access control for collections
Browse files Browse the repository at this point in the history
  • Loading branch information
m453h committed Oct 18, 2024
1 parent 9167015 commit 5809930
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 0 deletions.
11 changes: 11 additions & 0 deletions apps/civicsignalblog/src/payload/access/isAdminOrEditor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { ROLE_ADMIN, ROLE_EDITOR } from "./roles";

const isAdminOrEditor = ({ req: { user } }) => {
// Return true or false based on if the user has an admin or editor role
return (
Boolean(user?.roles?.includes(ROLE_ADMIN)) ||
Boolean(user?.roles?.includes(ROLE_EDITOR))
);
};

export default isAdminOrEditor;
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { slateEditor } from "@payloadcms/richtext-slate";

import canRead from "#civicsignalblog/payload/access/applications/main";
import isAdminOrEditor from "#civicsignalblog/payload/access/isAdminOrEditor";
import document from "#civicsignalblog/payload/fields/document";
import image from "#civicsignalblog/payload/fields/image";
import richText from "#civicsignalblog/payload/fields/richText";
Expand All @@ -9,6 +10,9 @@ const MediaData = {
slug: "media-data",
access: {
read: canRead,
update: isAdminOrEditor,
create: isAdminOrEditor,
delete: isAdminOrEditor,
},
admin: {
defaultColumns: ["title", "updatedAt"],
Expand Down
4 changes: 4 additions & 0 deletions apps/civicsignalblog/src/payload/collections/Main/Pages.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import canRead from "#civicsignalblog/payload/access/applications/main";
import isAdminOrEditor from "#civicsignalblog/payload/access/isAdminOrEditor";
import PageHeader from "#civicsignalblog/payload/blocks/PageHeader";
import { MAIN } from "#civicsignalblog/payload/lib/data/common/applications";
import pages from "#civicsignalblog/payload/utils/createPagesCollection";
Expand All @@ -11,6 +12,9 @@ const Pages = pages({
blocks: [PageHeader],
access: {
read: canRead,
update: isAdminOrEditor,
create: isAdminOrEditor,
delete: isAdminOrEditor,
},
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import canRead from "#civicsignalblog/payload/access/applications/research";
import isAdminOrEditor from "#civicsignalblog/payload/access/isAdminOrEditor";

const Authors = {
slug: "author",
access: {
read: canRead,
update: isAdminOrEditor,
create: isAdminOrEditor,
delete: isAdminOrEditor,
},
admin: {
defaultColumns: ["fullName", "updatedAt"],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import isAdminOrEditor from "#civicsignalblog/payload/access/isAdminOrEditor";

const Media = {
slug: "media",
admin: {
Expand All @@ -8,6 +10,9 @@ const Media = {
},
access: {
read: () => true, // Everyone can read Media
update: isAdminOrEditor,
create: isAdminOrEditor,
delete: isAdminOrEditor,
},
upload: {
staticURL: "/media",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import canRead from "#civicsignalblog/payload/access/applications/research";
import isAdminOrEditor from "#civicsignalblog/payload/access/isAdminOrEditor";
import CustomPageHeader from "#civicsignalblog/payload/blocks/CustomPageHeader";
import Error from "#civicsignalblog/payload/blocks/Error";
import FeaturedStories from "#civicsignalblog/payload/blocks/FeaturedStories";
Expand All @@ -23,6 +24,9 @@ const Pages = pages({
],
access: {
read: canRead,
update: isAdminOrEditor,
create: isAdminOrEditor,
delete: isAdminOrEditor,
},
adminOptions: {
description: "Research",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import canRead from "#civicsignalblog/payload/access/applications/research";
import isAdminOrEditor from "#civicsignalblog/payload/access/isAdminOrEditor";
import authors from "#civicsignalblog/payload/fields/authors";
import content from "#civicsignalblog/payload/fields/content";
import image from "#civicsignalblog/payload/fields/image";
Expand All @@ -19,6 +20,9 @@ const Posts = {
},
access: {
read: canRead,
update: isAdminOrEditor,
create: isAdminOrEditor,
delete: isAdminOrEditor,
},
admin: {
defaultColumns: ["title", "authors", "publishedOn"],
Expand Down
4 changes: 4 additions & 0 deletions apps/civicsignalblog/src/payload/collections/Research/Tags.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import canRead from "#civicsignalblog/payload/access/applications/research";
import isAdminOrEditor from "#civicsignalblog/payload/access/isAdminOrEditor";
import slug from "#civicsignalblog/payload/fields/slug/index";

const Tags = {
Expand All @@ -11,6 +12,9 @@ const Tags = {
},
access: {
read: canRead,
update: isAdminOrEditor,
create: isAdminOrEditor,
delete: isAdminOrEditor,
},
fields: [
{
Expand Down

0 comments on commit 5809930

Please sign in to comment.