How to registered routes in express
Assign the permission to another role
This is a Node.js module available through the npm registry.
This Package is only for mongoDB
Node.js 0.10 or higher is required.
Installation is done using the npm install command:
$ npm install roles-and-permission-node
this package is generate three table automatically
1.roles
2.permission
3.permission-role
all permissions are assign to super admin automatically.
DB_CONNECTION=mongodb
DB_HOST=localhost
DB_PORT=27017
DB_DATABASE=knovator
const { store } = require('roles-and-permission-node');
const express = require('express');
var api = require('express-list-endpoints-descriptor')(express);
const app = express();
const routes = require('./routes');
//api routes
app.use('/api', routes);
store(api.listEndpoints(app));
const express = require('express');
const routes = express.Router({ strict: true });
routes.route('/').get((req, res) => {
res.send('ok in api')
}).descriptor('setting.get');
routes.route('/').put((req, res) => {
res.send('ok in api')
}).descriptor('setting.update');
routes.route('/').post((req, res) => {
res.send('ok in api')
}).descriptor('setting.create');
routes.route('/:id').get((req, res) => {
res.send('ok in api')
}).descriptor('setting.show');
module.exports = routes;
const { permit } = require('roles-and-permission-node');
async function auth(req, res, next) {
auth...
//add roleIds into req object
req.roleIds = ["5fec604cd88d5b57ccfc8561", "5fec604cd88d5b57ccfc8562"]
next();
};
async function checkPermission(req, res, next) {
try {
const result = await permit(req)
if (result) {
next();
} else {
return res.status(403).send({ message: 'Unauthorized permission !' });
}
}
catch (err) {
console.log(err);
}
};
module.exports = {
auth: auth,
checkPermission: checkPermission
}
const { getRole } = require('roles-and-permission-node');
console.log(await getRole())
you can also find the roles by codes
const codes=['SUPER_ADMIN','ADMIN'];
console.log(await getRole(codes))
const { createRole } = require('roles-and-permission-node');
const data = {
name: "test",
code: "TESTER",
weight: 3
}
console.log(await createRole(data))
const { editRole } = require('roles-and-permission-node');
const data = {
id: "5ffc312839e95643781f933e",
name: "test",
code: "ADMIN",
weight: 3
}
console.log(await editRole(data))
const { deleteRole } = require('roles-and-permission-node');
you have to pass the id of role
## you can't delete the super admin role
const id=roleId
console.log(await createRole(id))
const { createOrUpdatePermission } = require('roles-and-permission-node');
const permissions: [
{
"roleId": 3,
"permissionId": 1
},
{
"roleId": 3,
"permissionId": 2
},
]
await createOrUpdatePermission(roleId,permissions);
const { permissionRole } = require('roles-and-permission-node');
const roleid = 5ffbda31f87d4b7e3ac82984"
console.log(await permissionRole(roleid));
const { rolePermissions } = require('roles-and-permission-node');
const codes = ['SUPER_ADMIN','ADMIN'];
console.log(await rolePermissions(codes));