mirror of
https://github.com/strapi/strapi.git
synced 2025-11-01 10:23:34 +00:00
[content-manager] types for policies (#18846)
This commit is contained in:
parent
ed636a7ccf
commit
fc0ebf30f1
@ -26,28 +26,28 @@ describe('hasDraftAndPublish policy', () => {
|
||||
});
|
||||
|
||||
test('It should succeed when the model has draft & publish enabled', () => {
|
||||
const ctx = { params: { model: 'foo' } };
|
||||
const ctx = { params: { model: 'foo' } } as any;
|
||||
const res = hasDraftAndPublish(ctx, {}, { strapi: global.strapi });
|
||||
|
||||
expect(res).toBe(true);
|
||||
});
|
||||
|
||||
test(`It should fail when the model has draft & publish disabled`, () => {
|
||||
const ctx = { params: { model: 'bar' } };
|
||||
const ctx = { params: { model: 'bar' } } as any;
|
||||
|
||||
const res = hasDraftAndPublish(ctx, {}, { strapi: global.strapi });
|
||||
expect(res).toBe(false);
|
||||
});
|
||||
|
||||
test(`It should fail when the model doesn't exists`, () => {
|
||||
const ctx = { params: { model: 'foobar' } };
|
||||
const ctx = { params: { model: 'foobar' } } as any;
|
||||
|
||||
const res = hasDraftAndPublish(ctx, {}, { strapi: global.strapi });
|
||||
expect(res).toBe(false);
|
||||
});
|
||||
|
||||
test(`It should fail when params.model isn't provided`, () => {
|
||||
const ctx = { params: {} };
|
||||
const ctx = { params: {} } as any;
|
||||
|
||||
const res = hasDraftAndPublish(ctx, {}, { strapi: global.strapi });
|
||||
expect(res).toBe(false);
|
||||
|
||||
@ -1,9 +1,11 @@
|
||||
import type { Context } from 'koa';
|
||||
import { contentTypes } from '@strapi/utils';
|
||||
import { Strapi, UID } from '@strapi/types';
|
||||
|
||||
const { hasDraftAndPublish } = contentTypes;
|
||||
|
||||
export default (ctx: any, config: any, { strapi }: any) => {
|
||||
const { model: modelUID } = ctx.params;
|
||||
export default (ctx: Context, config: any, { strapi }: { strapi: Strapi }) => {
|
||||
const { model: modelUID }: { model: UID.ContentType } = ctx.params;
|
||||
|
||||
const model = strapi.contentTypes[modelUID];
|
||||
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
import type { Context } from 'koa';
|
||||
import { policy } from '@strapi/utils';
|
||||
import { validateHasPermissionsInput } from '../validation/policies/hasPermissions';
|
||||
|
||||
@ -6,17 +7,16 @@ const { createPolicy } = policy;
|
||||
export default createPolicy({
|
||||
name: 'plugin::content-manager.hasPermissions',
|
||||
validator: validateHasPermissionsInput,
|
||||
handler(ctx, config = {}) {
|
||||
const { actions = [], hasAtLeastOne = false } = config;
|
||||
handler(ctx: Context, config = {}) {
|
||||
const { actions = [], hasAtLeastOne = false }: { actions: string[]; hasAtLeastOne: boolean } =
|
||||
config;
|
||||
|
||||
const {
|
||||
state: { userAbility },
|
||||
params: { model },
|
||||
} = ctx;
|
||||
const { userAbility } = ctx.state;
|
||||
const { model }: { model: string } = ctx.params;
|
||||
|
||||
const isAuthorized = hasAtLeastOne
|
||||
? actions.some((action: any) => userAbility.can(action, model))
|
||||
: actions.every((action: any) => userAbility.can(action, model));
|
||||
? actions.some((action) => userAbility.can(action, model))
|
||||
: actions.every((action) => userAbility.can(action, model));
|
||||
|
||||
return isAuthorized;
|
||||
},
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user