diff --git a/src/index.ts b/src/index.ts index b5e32f9..35ebbc3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,118 +1,14 @@ -const readOnlyPermissions = [ - { - action: 'plugin::content-manager.explorer.read', - subject: 'plugin::users-permissions.user', - conditions: [], - properties: { - fields: [ - 'username', - 'email', - 'provider', - 'password', - 'resetPasswordToken', - 'confirmationToken', - 'confirmed', - 'blocked', - 'role', - ], - }, - }, - { - action: 'plugin::content-manager.explorer.read', - subject: 'api::category.category', - conditions: [], - properties: { - fields: ['Name', 'genre'], - }, - }, - { - action: 'plugin::content-manager.explorer.read', - subject: 'api::genre.genre', - conditions: [], - properties: { - fields: ['Name', 'categories'], - }, - }, - { - action: 'plugin::content-manager.explorer.read', - subject: 'api::live-channel.live-channel', - conditions: [], - properties: { - fields: [ - 'name', - 'input_type', - 'output_type', - 'thumbnail', - 'Live_to_vod', - 'catch_up', - 'genre', - 'category', - 'genre-category-relation', - 'vods', - ], - }, - }, - { - action: 'plugin::content-manager.explorer.read', - subject: 'api::vod.vod', - conditions: [], - properties: { - fields: [ - 'Name', - 'Thumbnails', - 'rotation_start', - 'rotation_end', - 'description', - 'live_channel', - 'genre-category-relation', - 'views', - 'genre', - 'category', - 'media_url', - 'highlighted', - ], - }, - }, -] - -const createReadOnlyRole = async () => { - const roleService = strapi.services["admin::role"]; - const data = await strapi.entityService.create('admin::role', { - data: { - name: 'Read-Only', - code: `read-only`, - description: 'Read only access for demo user', - }, - }); - await roleService.assignPermissions(data.id, readOnlyPermissions); -} - -const createReadOnlyRoleIfNotExists = async () => { - const data: any[] = await strapi.entityService.findMany("admin::role", { - filters: { code: {$eq: "read-only"}} - }) - if (data.length == 0) - await createReadOnlyRole(); -}; - +import { createReadOnlyRoleIfNotExists } from "./setup/readOnlyRole"; +import { setVODLayout, setLiveChannelLayout } from "./setup/layouts"; export default { -/** - * An asynchronous register function that runs before - * your application is initialized. - * - * This gives you an opportunity to extend code. - */ -register(/*{ strapi }*/) {}, +register({ strapi }) { + +}, -/** - * An asynchronous bootstrap function that runs before - * your application gets started. - * - * This gives you an opportunity to set up your data model, - * run jobs, or perform some special logic. - */ async bootstrap({ strapi }) { - createReadOnlyRoleIfNotExists(); + createReadOnlyRoleIfNotExists(strapi); + setVODLayout(strapi); + setLiveChannelLayout(strapi); }, }; diff --git a/src/setup/layouts.ts b/src/setup/layouts.ts new file mode 100644 index 0000000..9c25d70 --- /dev/null +++ b/src/setup/layouts.ts @@ -0,0 +1,19 @@ +import VODLayout from "./layouts/VOD.json" +import LiveChannelLayout from "./layouts/LiveChannel.json" + +export const setVODLayout = (strapi) => { + const contentTypeService = strapi.services["plugin::content-manager.content-types"]; + const contentType = contentTypeService.findContentType("api::vod.vod"); + const defaultConfig = contentTypeService.findConfiguration(contentType); + contentTypeService.updateConfiguration(contentType, {...defaultConfig, layouts: VODLayout.layouts}); +} + +export const setLiveChannelLayout = (strapi) => { + const contentTypeService = strapi.services["plugin::content-manager.content-types"]; + const contentType = contentTypeService.findContentType("api::live-channel.live-channel"); + const defaultConfig = contentTypeService.findConfiguration(contentType); + contentTypeService.updateConfiguration(contentType, {...defaultConfig, layouts: LiveChannelLayout.layouts}); +} + + + diff --git a/src/setup/layouts/LiveChannel.json b/src/setup/layouts/LiveChannel.json new file mode 100644 index 0000000..4d4f7f0 --- /dev/null +++ b/src/setup/layouts/LiveChannel.json @@ -0,0 +1,52 @@ +{ + "layouts": { + "edit": [ + [ + { + "name": "name", + "size": 6 + }, + { + "name": "thumbnail", + "size": 6 + } + ], + [ + { + "name": "input_type", + "size": 6 + }, + { + "name": "output_type", + "size": 6 + } + ], + [ + { + "name": "vods", + "size": 6 + }, + { + "name": "genre-category-relation", + "size": 6 + } + ], + [ + { + "name": "catch_up", + "size": 6 + }, + { + "name": "Live_to_vod", + "size": 6 + } + ] + ], + "list": [ + "thumbnail", + "name", + "genre", + "category" + ] + } +} \ No newline at end of file diff --git a/src/setup/layouts/VOD.json b/src/setup/layouts/VOD.json new file mode 100644 index 0000000..07a633e --- /dev/null +++ b/src/setup/layouts/VOD.json @@ -0,0 +1,60 @@ +{ + "layouts": { + "edit": [ + [ + { + "name": "Name", + "size": 6 + }, + { + "name": "Thumbnails", + "size": 6 + } + ], + [ + { + "name": "description", + "size": 6 + }, + { + "name": "genre-category-relation", + "size": 6 + } + ], + [ + { + "name": "media_url", + "size": 12 + } + ], + [ + { + "name": "rotation_start", + "size": 6 + }, + { + "name": "rotation_end", + "size": 6 + } + ], + [ + { + "name": "live_channel", + "size": 6 + }, + { + "name": "highlighted", + "size": 6 + } + ] + ], + "list": [ + "Thumbnails", + "Name", + "media_url", + "genre", + "category", + "views" + ] + } +} diff --git a/src/setup/readOnlyRole.ts b/src/setup/readOnlyRole.ts new file mode 100644 index 0000000..ceeb688 --- /dev/null +++ b/src/setup/readOnlyRole.ts @@ -0,0 +1,63 @@ +const readOnlyUsersPermissions = { + action: 'plugin::content-manager.explorer.read', + subject: 'plugin::users-permissions.user', + conditions: [], + properties: { + fields: [ + 'username', + 'email', + 'provider', + 'password', + 'resetPasswordToken', + 'confirmationToken', + 'confirmed', + 'blocked', + 'role', + ], + }, +} + +const getApiContentTypesFields = (contentTypeService) => { + const apiUIDs : string[] = contentTypeService.findAllContentTypes() + .map(ct => ct.uid) + .filter(uid => uid.startsWith("api::")); + const permissions = apiUIDs.map((uid) => { + const contentType = contentTypeService.findContentType(uid); + const fields = Object.keys(contentType.attributes).filter(key => key !== "id"); + return { + action: 'plugin::content-manager.explorer.read', + subject: uid, + conditions: [], + properties: { + fields, + }, + } + }); + return permissions +}; + + +const createReadOnlyRole = async (strapi) => { + const contentTypeService = strapi.services["plugin::content-manager.content-types"]; + const roleService = strapi.services["admin::role"]; + const permissions = [ + readOnlyUsersPermissions, + ...getApiContentTypesFields(contentTypeService), + ]; + const data = await strapi.entityService.create('admin::role', { + data: { + name: 'Read-Only', + code: `read-only`, + description: 'Read only access for demo user', + }, + }); + await roleService.assignPermissions(data.id, permissions); +} + +export const createReadOnlyRoleIfNotExists = async (strapi) => { + const data: any[] = await strapi.entityService.findMany("admin::role", { + filters: { code: {$eq: "read-only"}} + }) + if (data.length == 0) + await createReadOnlyRole(strapi); +}; \ No newline at end of file