generated from autonomic-cooperative/astro-payload-template
Configure user access
This commit is contained in:
parent
1dbb075cd8
commit
1ccf660f5b
12
payload/src/access/isAdmin.ts
Normal file
12
payload/src/access/isAdmin.ts
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import { Access, FieldAccess } from "payload/types";
|
||||||
|
import { User } from "../payload-types";
|
||||||
|
|
||||||
|
export const isAdmin: Access<any, User> = ({ req: { user } }) => {
|
||||||
|
// Return true or false based on if the user has an admin role
|
||||||
|
return Boolean(user?.roles?.includes('admin'));
|
||||||
|
}
|
||||||
|
|
||||||
|
export const isAdminFieldLevel: FieldAccess<{ id: string }, unknown, User> = ({ req: { user } }) => {
|
||||||
|
// Return true or false based on if the user has an admin role
|
||||||
|
return Boolean(user?.roles?.includes('admin'));
|
||||||
|
}
|
21
payload/src/access/isAdminOrSelf.ts
Normal file
21
payload/src/access/isAdminOrSelf.ts
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
import { Access } from "payload/config";
|
||||||
|
|
||||||
|
export const isAdminOrSelf: Access = ({ req: { user } }) => {
|
||||||
|
// Need to be logged in
|
||||||
|
if (user) {
|
||||||
|
// If user has role of 'admin'
|
||||||
|
if (user.roles?.includes('admin')) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If any other type of user, only provide access to themselves
|
||||||
|
return {
|
||||||
|
id: {
|
||||||
|
equals: user.id,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reject everyone else
|
||||||
|
return false;
|
||||||
|
}
|
@ -1,6 +1,6 @@
|
|||||||
import { CollectionConfig } from 'payload/types';
|
import { CollectionConfig } from 'payload/types';
|
||||||
|
import { isAdmin, isAdminFieldLevel } from '../access/isAdmin';
|
||||||
const isAdmin = ({ req: { user } }) => (user && user.role === 'admin');
|
import { isAdminOrSelf } from '../access/isAdminOrSelf';
|
||||||
|
|
||||||
const Users: CollectionConfig = {
|
const Users: CollectionConfig = {
|
||||||
slug: 'users',
|
slug: 'users',
|
||||||
@ -9,11 +9,14 @@ const Users: CollectionConfig = {
|
|||||||
useAsTitle: 'email',
|
useAsTitle: 'email',
|
||||||
},
|
},
|
||||||
access: {
|
access: {
|
||||||
read: isAdmin,
|
create: isAdmin,
|
||||||
|
read: isAdminOrSelf,
|
||||||
|
update: isAdminOrSelf,
|
||||||
|
delete: isAdmin,
|
||||||
},
|
},
|
||||||
fields: [
|
fields: [
|
||||||
{
|
{
|
||||||
name: 'role',
|
name: 'roles',
|
||||||
type: 'select',
|
type: 'select',
|
||||||
options: [
|
options: [
|
||||||
{ label: 'ssg', value: 'ssg' }, //cRud
|
{ label: 'ssg', value: 'ssg' }, //cRud
|
||||||
@ -23,14 +26,14 @@ const Users: CollectionConfig = {
|
|||||||
],
|
],
|
||||||
required: true,
|
required: true,
|
||||||
defaultValue: "user",
|
defaultValue: "user",
|
||||||
|
// JWT so that role is accessible from 'req.user'
|
||||||
|
saveToJWT: true,
|
||||||
|
hasMany: true,
|
||||||
access: {
|
access: {
|
||||||
create: isAdmin,
|
create: isAdminFieldLevel,
|
||||||
read: isAdmin,
|
read: () => true,
|
||||||
update: isAdmin,
|
update: isAdminFieldLevel,
|
||||||
},
|
},
|
||||||
admin: {
|
|
||||||
readOnly: !isAdmin
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'name',
|
name: 'name',
|
||||||
|
Loading…
Reference in New Issue
Block a user