Skip to content

๐Ÿš€ Open source plugins for Strapi - Node.js Headless CMS

License

Notifications You must be signed in to change notification settings

surgeharb/strapi-plugins

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

10 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿš€ Cool Strapi Plugins

Amazing Plugin strapi-plugin-graphs-builder for Strapi v4, check it out now!

Getting Started

First clone the project by running:

git clone https://github.com/surgeharb/strapi-plugins.git

Installation

Once the project is cloned locally, navigate to the main directory:

cd strapi-plugins

Make sure that yarn is installed on your machine and run:

yarn install

To start the project in development mode:

yarn develop

If you want to tinker with plugins and watch changes without rebuilding the project multiple times:

yarn develop --watch-admin

Finally, for production make sure to:

# build the project -- production
NODE_ENV=production yarn build

# start Strapi -- production
NODE_ENV=production yarn start

Plugin: Admin Access Rights

Keep in mind that this is a temporary solution as the core team of Strapi is in the latest stages of achieving this the good way in the system.
I made use of the built in Strapi "admin customization" feature.

This feature is available directly when you login as an admin user > Access Rights under Plugins in the Left Panel.

Make it possible in your own Strapi Project!

First, you will have 3 default roles admin, author, editor as a start.

Step 1 - Modify admin database model

Add these roles to the Administrator database model.

From this repo, copy the admin extension into your project's extensions:

cp -R ./extensions/admin ~/your-project/extensions

You can notice a script to override Administrator model. To apply it on every run, edit your package.json scripts as follow:

{
  "scripts": {
    "admin-override": "node ./extensions/admin/override-script.js",
    "develop": "npm run admin-override && strapi develop",
    "start": "npm run admin-override && strapi start",
    "build": "npm run admin-override && strapi build",
    "strapi": "strapi"
  },
}

Make sure the default admin url is not /admin as it admin prefix causes issues in this modification. Issue #1. For this project I changed the url to /dashboard as follows:

// ./config/server.js
module.exports = ({ env }) => ({
  host: env('HOST', '0.0.0.0'),
  port: env.int('PORT', 1337),
  admin: {
    url: '/dashboard'
  }
});

Step 2 - Override admin CRUD controllers

Strapi administrator controllers restrict us from adding a new field while creating/updating admins.

For this I wrote a hook that overrides the controller behavior into letting us add new role on create/update admin.

# if you don't have hooks folder
mkdir ~/your-project/hooks

# copy admin-roles custom hook
cp -R ./hooks/admin-roles ~/your-project/hooks

# enable the hook on Strapi startup
cp ./config/hook.js ~/your-project/config

Kill the process, and restart Strapi instance, then navigate to Manage administrators in the top right menu, and create a new administrator.

Now give new admin a role from the new dropdown that we injected in the model.

P.S. Make sure to keep at least 1 admin role on an administrator!!!

Step 3 - Create admin-access model and components

To manage roles access, create a database model linking plugin uid with the administrator role.

# copy new API models and controllers
cp -R ./api/admin-access ~/your-project/api

# prepare components general folder
mkdir -p ~/your-project/components/general

# copy custom plugin components used by the API
cp ./components/general/plugin-access.json ~/your-project/components/general

Step 4 - Restrict access for roles

Restrict plugin API fetching backend-side

# copy modified content-manager plugin controller
cp -R ./extensions/content-manager ~/your-project/extensions

Remove frontend elements according to provided Roles Manage administrators button and Left Panel Plugins

# copy admin frontend modification in the root of your project
cp -R ./admin ~/your-project

Step 5 - Add "access rights" plugin

# create plugins folder if not exists
mkdir ~/your-project/plugins

# add the newly crafter plugin for access rights
cp -R ./plugins/access-rights ~/your-project/plugins

Optional Step - Add more roles

Add roles in enum array inside extensions/admin/models/Administrator.settings.json
Don't remove first role - admin, it is necessary for the plugin to work properly!

Apply same additions also inside plugins/access-rights/admin/src/containers/HomePage/index.js

// modify this array - do not include first role 'admin'
const [roles, setRoles] = useState(['author', 'editor']);

Voilร ! Now kill the process, yarn build and restart Strapi. Login using your main admin user and manage other administrators access.



Do not hesitate to open issues if you encounter any, submit a pull request to improve any bit of the code, or contact me [email protected]