44
loading...
This website collects cookies to deliver better user experience
role
fieldbeforeChange
hook to save which user created the order to a createdBy
fieldadmin
roles or the creator of the ordercreate-payload-app
to build out the initial project.npx create-payload-app payload-rbac
javascript
for languageblank
for our template├─ payload.config.js
└─ collections/
└─ Users.js
└─ Orders.js
role
field to our Users collection with 2 options: admin
and user
.const Users = {
slug: 'users',
auth: true,
admin: {
useAsTitle: 'email',
},
fields: [
{
name: 'role',
type: 'select',
options: [
{ label: 'Admin', value: 'admin' },
{ label: 'User', value: 'user' },
],
required: true,
defaultValue: 'user',
},
],
};
export default Users;
Orders.js
collection in our collections/
directory and scaffold out basic fields and values - including the createdBy
relationship to the user.const Orders = {
slug: 'orders',
fields: [
{
name: 'items',
type: 'array',
fields: [
{
name: 'item',
type: 'text',
}
]
},
{
name: 'createdBy',
type: 'relationship',
relationTo: 'users',
access: {
update: () => false,
},
admin: {
readOnly: true,
position: 'sidebar',
condition: data => Boolean(data?.createdBy)
},
},
]
}
export default Orders;
array
field for items and a createdBy
field which is a relationship to our Users
collection. The createdBy
field will feature a strict update
access control function so that it can never be changed.condition
function under the createdBy
field's access. This will hide createdBy
until it has a value.beforeChange
hook to our collection definition.const Orders = {
slug: 'orders',
fields: [
// Collapsed
],
hooks: {
beforeChange: [
({ req, operation, data }) => {
if (operation === 'create') {
if (req.user) {
data.createdBy = req.user.id;
return data;
}
}
},
],
},
}
createdBy
field to be the current user's id
value, only if it is on a create
operation. This will create a relationship between an order and the user who created it.boolean
value to allow/disallow access or it returns a query constraint that filters the data.const isAdminOrCreatedBy = ({ req: { user } }) => {
// Scenario #1 - Check if user has the 'admin' role
if (user && user.role === 'admin') {
return true;
}
// Scenario #2 - Allow only documents with the current user set to the 'createdBy' field
if (user) {
// Will return access for only documents that were created by the current user
return {
createdBy: {
equals: user.id,
},
};
}
// Scenario #3 - Disallow all others
return false;
};
access
property of the collection definition:const Orders = {
slug: 'orders',
fields: [
// Collapsed
],
access: {
read: isAdminOrCreatedBy,
update: isAdminOrCreatedBy,
delete: isAdminOrCreatedBy,
},
hooks: {
// Collapsed
},
}
read
, update
, and delete
access properties, the function will run whenever these operations are attempted on the collection.Note: Access functions default to requiring a logged-in user. This is why create
does not need to be defined for this example, since we want any logged in user to be able to create an order.
payload.config.js
import { buildConfig } from 'payload/config';
import Orders from './collections/Orders';
import Users from './collections/Users';
export default buildConfig({
serverURL: 'http://localhost:3000',
admin: {
user: Users.slug,
},
collections: [
Users,
Orders,
],
});
npm run dev
or yarn dev
and navigate to http://localhost:3000/admin
paymentID
field only for Admin users. Create an isAdmin
function that checks the role as we did earlier.const isAdmin = ({ req: { user } }) => (user && user.role === 'admin');
create
, read
or update
access calls to use the isAdmin function.const Orders = {
slug: 'orders',
fields: [
// Collapsed
{
name: 'paymentId',
type: 'text',
access: {
create: isAdmin,
read: isAdmin,
update: isAdmin,
},
}
],
// Collapsed
}
editor
role which allows reading and editing, but disallows creating. This all can be customized specifically to your needs.