import type { Core } from '@strapi/strapi'; /** Read-only endpoints the public website needs. Nothing else is granted. */ const PUBLIC_READ_ACTIONS = ['article', 'category'].flatMap((type) => [ `api::${type}.${type}.find`, `api::${type}.${type}.findOne`, ]); /** * Grants the Public role read access to the blog content types, so a fresh * deployment serves content without anyone clicking through the admin panel. * Existing permissions are left untouched. */ async function grantPublicReadAccess(strapi: Core.Strapi) { const publicRole = await strapi .query('plugin::users-permissions.role') .findOne({ where: { type: 'public' } }); if (!publicRole) return; for (const action of PUBLIC_READ_ACTIONS) { const existing = await strapi .query('plugin::users-permissions.permission') .findOne({ where: { action, role: publicRole.id } }); if (!existing) { await strapi .query('plugin::users-permissions.permission') .create({ data: { action, role: publicRole.id } }); } } } /** * The site has no front-end accounts: only the administrators authoring content * in the admin panel. Self-registration is therefore closed, so nobody can * create an Authenticated user through the public API. */ async function disablePublicSignUp(strapi: Core.Strapi) { const store = strapi.store({ type: 'plugin', name: 'users-permissions', key: 'advanced' }); const advanced = ((await store.get({ key: 'advanced' })) ?? {}) as Record; if (advanced.allow_register !== false) { await store.set({ key: 'advanced', value: { ...advanced, allow_register: false } }); } } export default { register() {}, async bootstrap({ strapi }: { strapi: Core.Strapi }) { await grantPublicReadAccess(strapi); await disablePublicSignUp(strapi); }, };