passes entire operation through to policy instead of just user
This commit is contained in:
@@ -1,85 +1,85 @@
|
||||
const checkRole = require('../policies/checkRole');
|
||||
|
||||
module.exports = {
|
||||
slug: 'categories',
|
||||
labels: {
|
||||
singular: 'Category',
|
||||
plural: 'Categories',
|
||||
},
|
||||
useAsTitle: 'title',
|
||||
policies: {
|
||||
create: user => checkRole(['user', 'admin'], user),
|
||||
read: () => true,
|
||||
update: user => checkRole(['user', 'admin'], user),
|
||||
destroy: user => checkRole(['user', 'admin'], user),
|
||||
},
|
||||
fields: [
|
||||
{
|
||||
name: 'title',
|
||||
label: 'Title',
|
||||
type: 'text',
|
||||
maxLength: 100,
|
||||
required: true,
|
||||
unique: true,
|
||||
localized: true,
|
||||
},
|
||||
{
|
||||
name: 'description',
|
||||
label: 'Description',
|
||||
type: 'textarea',
|
||||
height: 100,
|
||||
required: true,
|
||||
localized: true,
|
||||
},
|
||||
{
|
||||
name: 'post',
|
||||
label: 'Post',
|
||||
type: 'relationship',
|
||||
relationTo: ['posts', 'categories'],
|
||||
localized: false,
|
||||
hasMany: false,
|
||||
},
|
||||
{
|
||||
name: 'demoSelect',
|
||||
label: 'Demo Select',
|
||||
type: 'select',
|
||||
options: [{
|
||||
value: 'Option 1',
|
||||
label: 'Here is a label for Option 1',
|
||||
}, {
|
||||
value: 'Option 2',
|
||||
label: 'Option 2 Label',
|
||||
}, {
|
||||
value: 'Option 3',
|
||||
label: 'Option 3 Label',
|
||||
}, {
|
||||
value: 'Option 4',
|
||||
label: 'Option 4 Label',
|
||||
}],
|
||||
defaultValue: 'Option 1',
|
||||
required: true,
|
||||
},
|
||||
{
|
||||
name: 'demoSelectMany',
|
||||
label: 'Demo Select w/ hasMany',
|
||||
type: 'select',
|
||||
options: [{
|
||||
value: 'Option 1',
|
||||
label: 'Here is a label for Option 1',
|
||||
}, {
|
||||
value: 'Option 2',
|
||||
label: 'Option 2 Label',
|
||||
}, {
|
||||
value: 'Option 3',
|
||||
label: 'Option 3 Label',
|
||||
}, {
|
||||
value: 'Option 4',
|
||||
label: 'Option 4 Label',
|
||||
}],
|
||||
defaultValue: 'Option 1',
|
||||
required: true,
|
||||
hasMany: true,
|
||||
},
|
||||
],
|
||||
timestamps: true,
|
||||
slug: 'categories',
|
||||
labels: {
|
||||
singular: 'Category',
|
||||
plural: 'Categories',
|
||||
},
|
||||
useAsTitle: 'title',
|
||||
policies: {
|
||||
create: ({ user }) => checkRole(['user', 'admin'], user),
|
||||
read: () => true,
|
||||
update: ({ user }) => checkRole(['user', 'admin'], user),
|
||||
destroy: ({ user }) => checkRole(['user', 'admin'], user),
|
||||
},
|
||||
fields: [
|
||||
{
|
||||
name: 'title',
|
||||
label: 'Title',
|
||||
type: 'text',
|
||||
maxLength: 100,
|
||||
required: true,
|
||||
unique: true,
|
||||
localized: true,
|
||||
},
|
||||
{
|
||||
name: 'description',
|
||||
label: 'Description',
|
||||
type: 'textarea',
|
||||
height: 100,
|
||||
required: true,
|
||||
localized: true,
|
||||
},
|
||||
{
|
||||
name: 'post',
|
||||
label: 'Post',
|
||||
type: 'relationship',
|
||||
relationTo: ['posts', 'categories'],
|
||||
localized: false,
|
||||
hasMany: false,
|
||||
},
|
||||
{
|
||||
name: 'demoSelect',
|
||||
label: 'Demo Select',
|
||||
type: 'select',
|
||||
options: [{
|
||||
value: 'Option 1',
|
||||
label: 'Here is a label for Option 1',
|
||||
}, {
|
||||
value: 'Option 2',
|
||||
label: 'Option 2 Label',
|
||||
}, {
|
||||
value: 'Option 3',
|
||||
label: 'Option 3 Label',
|
||||
}, {
|
||||
value: 'Option 4',
|
||||
label: 'Option 4 Label',
|
||||
}],
|
||||
defaultValue: 'Option 1',
|
||||
required: true,
|
||||
},
|
||||
{
|
||||
name: 'demoSelectMany',
|
||||
label: 'Demo Select w/ hasMany',
|
||||
type: 'select',
|
||||
options: [{
|
||||
value: 'Option 1',
|
||||
label: 'Here is a label for Option 1',
|
||||
}, {
|
||||
value: 'Option 2',
|
||||
label: 'Option 2 Label',
|
||||
}, {
|
||||
value: 'Option 3',
|
||||
label: 'Option 3 Label',
|
||||
}, {
|
||||
value: 'Option 4',
|
||||
label: 'Option 4 Label',
|
||||
}],
|
||||
defaultValue: 'Option 1',
|
||||
required: true,
|
||||
hasMany: true,
|
||||
},
|
||||
],
|
||||
timestamps: true,
|
||||
};
|
||||
|
||||
@@ -4,39 +4,39 @@ const Quote = require('../../content-blocks/Quote');
|
||||
const NumberBlock = require('../../content-blocks/Number');
|
||||
|
||||
module.exports = {
|
||||
slug: 'layouts',
|
||||
labels: {
|
||||
singular: 'Layout',
|
||||
plural: 'Layouts',
|
||||
},
|
||||
useAsTitle: 'title',
|
||||
policies: {
|
||||
// options: create, read, update, delete
|
||||
// null or undefined policies will default to requiring auth
|
||||
// any policy can use req.user to see that the user is logged
|
||||
create: null,
|
||||
read: () => true,
|
||||
update: user => checkRole(['user', 'admin'], user),
|
||||
destroy: user => checkRole(['user', 'admin'], user),
|
||||
},
|
||||
fields: [
|
||||
{
|
||||
name: 'title',
|
||||
label: 'Page Title',
|
||||
type: 'text',
|
||||
unique: true,
|
||||
localized: true,
|
||||
maxLength: 100,
|
||||
required: true,
|
||||
},
|
||||
{
|
||||
name: 'layout',
|
||||
label: 'Layout Blocks',
|
||||
singularLabel: 'Block',
|
||||
type: 'flexible',
|
||||
blocks: [Email, NumberBlock, Quote],
|
||||
localized: true,
|
||||
},
|
||||
],
|
||||
timestamps: true,
|
||||
slug: 'layouts',
|
||||
labels: {
|
||||
singular: 'Layout',
|
||||
plural: 'Layouts',
|
||||
},
|
||||
useAsTitle: 'title',
|
||||
policies: {
|
||||
// options: create, read, update, delete
|
||||
// null or undefined policies will default to requiring auth
|
||||
// any policy can use req.user to see that the user is logged
|
||||
create: null,
|
||||
read: () => true,
|
||||
update: ({ user }) => checkRole(['user', 'admin'], user),
|
||||
destroy: ({ user }) => checkRole(['user', 'admin'], user),
|
||||
},
|
||||
fields: [
|
||||
{
|
||||
name: 'title',
|
||||
label: 'Page Title',
|
||||
type: 'text',
|
||||
unique: true,
|
||||
localized: true,
|
||||
maxLength: 100,
|
||||
required: true,
|
||||
},
|
||||
{
|
||||
name: 'layout',
|
||||
label: 'Layout Blocks',
|
||||
singularLabel: 'Block',
|
||||
type: 'flexible',
|
||||
blocks: [Email, NumberBlock, Quote],
|
||||
localized: true,
|
||||
},
|
||||
],
|
||||
timestamps: true,
|
||||
};
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
const path = require('path');
|
||||
const checkRole = require('../../policies/checkRole');
|
||||
const Quote = require('../../content-blocks/Quote');
|
||||
const CallToAction = require('../../content-blocks/CallToAction');
|
||||
@@ -16,8 +15,8 @@ module.exports = {
|
||||
// any policy can use req.user to see that the user is logged
|
||||
create: null,
|
||||
read: () => true,
|
||||
update: user => checkRole(['user', 'admin'], user),
|
||||
destroy: user => checkRole(['user', 'admin'], user),
|
||||
update: ({ user }) => checkRole(['user', 'admin'], user),
|
||||
destroy: ({ user }) => checkRole(['user', 'admin'], user),
|
||||
},
|
||||
fields: [
|
||||
{
|
||||
@@ -159,9 +158,9 @@ module.exports = {
|
||||
},
|
||||
],
|
||||
// components: {
|
||||
// views: {
|
||||
// List: path.resolve(__dirname, 'components/List/index.js'),
|
||||
// },
|
||||
// views: {
|
||||
// List: path.resolve(__dirname, 'components/List/index.js'),
|
||||
// },
|
||||
// },
|
||||
timestamps: true,
|
||||
};
|
||||
|
||||
@@ -10,24 +10,24 @@ module.exports = {
|
||||
},
|
||||
useAsTitle: 'title',
|
||||
policies: {
|
||||
create: user => checkRole(['user', 'admin'], user),
|
||||
create: ({ user }) => checkRole(['user', 'admin'], user),
|
||||
read: () => true,
|
||||
update: user => checkRole(['user', 'admin'], user),
|
||||
delete: user => checkRole(['user', 'admin'], user),
|
||||
update: ({ user }) => checkRole(['user', 'admin'], user),
|
||||
delete: ({ user }) => checkRole(['user', 'admin'], user),
|
||||
},
|
||||
hooks: {
|
||||
beforeCreate: options => options,
|
||||
beforeRead: options => options,
|
||||
beforeUpdate: options => options,
|
||||
beforeDelete: (options) => {
|
||||
console.log(`About to delete ${options.query._id}`);
|
||||
return options;
|
||||
beforeCreate: operation => operation,
|
||||
beforeRead: operation => operation,
|
||||
beforeUpdate: operation => operation,
|
||||
beforeDelete: (operation) => {
|
||||
console.log(`About to delete ${operation.id}`);
|
||||
return operation;
|
||||
},
|
||||
afterCreate: (options, value) => value,
|
||||
afterRead: (options, value) => value,
|
||||
afterUpdate: (options, value) => value,
|
||||
afterDelete: (options, value) => {
|
||||
console.log(`Deleted ${options.query._id}`);
|
||||
afterCreate: (operation, value) => value,
|
||||
afterRead: (operation, value) => value,
|
||||
afterUpdate: (operation, value) => value,
|
||||
afterDelete: (operation, value) => {
|
||||
console.log(`Deleted ${operation.query._id}`);
|
||||
console.log(`Deleted record: ${JSON.stringify(value)}`);
|
||||
return value;
|
||||
},
|
||||
|
||||
@@ -8,10 +8,10 @@ module.exports = {
|
||||
},
|
||||
useAsTitle: 'filename',
|
||||
policies: {
|
||||
create: user => checkRole(['user', 'admin'], user),
|
||||
read: user => checkRole(['user', 'admin'], user),
|
||||
update: user => checkRole(['user', 'admin'], user),
|
||||
destroy: user => checkRole(['user', 'admin'], user),
|
||||
create: ({ user }) => checkRole(['user', 'admin'], user),
|
||||
read: ({ user }) => checkRole(['user', 'admin'], user),
|
||||
update: ({ user }) => checkRole(['user', 'admin'], user),
|
||||
destroy: ({ user }) => checkRole(['user', 'admin'], user),
|
||||
},
|
||||
fields: [
|
||||
{
|
||||
|
||||
@@ -9,10 +9,10 @@ module.exports = {
|
||||
},
|
||||
useAsTitle: 'email',
|
||||
policies: {
|
||||
create: user => checkRole(['admin', 'user'], user),
|
||||
create: ({ user }) => checkRole(['admin', 'user'], user),
|
||||
read: null,
|
||||
update: user => checkRole(['admin', 'user'], user),
|
||||
destroy: user => checkRole(['admin', 'user'], user),
|
||||
update: ({ user }) => checkRole(['admin', 'user'], user),
|
||||
destroy: ({ user }) => checkRole(['admin', 'user'], user),
|
||||
},
|
||||
auth: {
|
||||
passwordIndex: 1,
|
||||
|
||||
@@ -4,7 +4,7 @@ module.exports = {
|
||||
slug: 'footer',
|
||||
label: 'Footer',
|
||||
policies: {
|
||||
upsert: user => checkRole(['admin', 'user'], user),
|
||||
upsert: ({ user }) => checkRole(['admin', 'user'], user),
|
||||
read: () => true,
|
||||
},
|
||||
fields: [
|
||||
|
||||
@@ -6,7 +6,7 @@ module.exports = {
|
||||
slug: 'header',
|
||||
label: 'Header',
|
||||
policies: {
|
||||
upsert: user => checkRole(['admin', 'user'], user),
|
||||
upsert: ({ user }) => checkRole(['admin', 'user'], user),
|
||||
read: () => true,
|
||||
},
|
||||
fields: [
|
||||
|
||||
@@ -7,7 +7,7 @@ const create = async (args) => {
|
||||
// 1. Retrieve and execute policy
|
||||
// /////////////////////////////////////
|
||||
|
||||
await executePolicy(args.user, args.config.policies.create);
|
||||
await executePolicy(args, args.config.policies.create);
|
||||
|
||||
// Await validation here
|
||||
|
||||
|
||||
@@ -7,10 +7,10 @@ const deleteQuery = async (args) => {
|
||||
// 1. Retrieve and execute policy
|
||||
// /////////////////////////////////////
|
||||
|
||||
await executePolicy(args.user, args.config.policies.delete);
|
||||
await executePolicy(args, args.config.policies.delete);
|
||||
|
||||
let options = {
|
||||
query: { _id: args.id },
|
||||
id: args.id,
|
||||
Model: args.Model,
|
||||
config: args.config,
|
||||
locale: args.locale,
|
||||
@@ -35,12 +35,12 @@ const deleteQuery = async (args) => {
|
||||
|
||||
const {
|
||||
Model,
|
||||
query,
|
||||
id,
|
||||
locale,
|
||||
fallbackLocale,
|
||||
} = options;
|
||||
|
||||
let result = await Model.findOneAndDelete(query);
|
||||
let result = await Model.findOneAndDelete({ _id: id });
|
||||
|
||||
if (!result) throw new NotFound();
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ const find = async (args) => {
|
||||
// 1. Retrieve and execute policy
|
||||
// /////////////////////////////////////
|
||||
|
||||
await executePolicy(args.user, args.config.policies.read);
|
||||
await executePolicy(args, args.config.policies.read);
|
||||
|
||||
const queryToBuild = {};
|
||||
if (args.where) queryToBuild.where = args.where;
|
||||
|
||||
@@ -9,7 +9,7 @@ const findByID = async (args) => {
|
||||
// /////////////////////////////////////
|
||||
|
||||
const policy = args.config && args.config.policies && args.config.policies.read;
|
||||
await executePolicy(args.user, policy);
|
||||
await executePolicy(args, policy);
|
||||
|
||||
let options = {
|
||||
query: { _id: args.id },
|
||||
|
||||
@@ -8,7 +8,7 @@ const update = async (args) => {
|
||||
// 1. Retrieve and execute policy
|
||||
// /////////////////////////////////////
|
||||
|
||||
await executePolicy(args.user, args.config.policies.update);
|
||||
await executePolicy(args, args.config.policies.update);
|
||||
|
||||
// Await validation here
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ const findOne = async (args) => {
|
||||
// 1. Retrieve and execute policy
|
||||
// /////////////////////////////////////
|
||||
|
||||
await executePolicy(args.user, args.config.policies.read);
|
||||
await executePolicy(args, args.config.policies.read);
|
||||
|
||||
let options = { ...args };
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ const upsert = async (args) => {
|
||||
// 1. Retrieve and execute policy
|
||||
// /////////////////////////////////////
|
||||
|
||||
await executePolicy(args.user, args.config.policies.upsert);
|
||||
await executePolicy(args, args.config.policies.upsert);
|
||||
|
||||
let options = { ...args };
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
const { Forbidden } = require('../errors');
|
||||
|
||||
const executePolicy = async (user, policy) => {
|
||||
const executePolicy = async (operation, policy) => {
|
||||
if (policy) {
|
||||
const result = await policy(user);
|
||||
const result = await policy(operation);
|
||||
|
||||
if (!result) {
|
||||
throw new Forbidden();
|
||||
@@ -11,7 +11,7 @@ const executePolicy = async (user, policy) => {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (user) {
|
||||
if (operation.user) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ const register = async (args) => {
|
||||
// /////////////////////////////////////
|
||||
|
||||
if (!args.overridePolicy) {
|
||||
await executePolicy(args.user, args.config.policies.register);
|
||||
await executePolicy(args, args.config.policies.register);
|
||||
}
|
||||
|
||||
// Await validation here
|
||||
|
||||
@@ -8,7 +8,7 @@ const update = async (args) => {
|
||||
// 1. Retrieve and execute policy
|
||||
// /////////////////////////////////////
|
||||
|
||||
await executePolicy(args.user, args.config.policies.update);
|
||||
await executePolicy(args, args.config.policies.update);
|
||||
|
||||
// Await validation here
|
||||
|
||||
|
||||
Reference in New Issue
Block a user