[content-manager] types for policies (#18846)

This commit is contained in:
Jamie Howard 2023-11-22 09:45:34 +00:00 committed by GitHub
parent ed636a7ccf
commit fc0ebf30f1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 14 deletions

View File

@ -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);

View File

@ -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];

View File

@ -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;
},