Skip to content

Commit c9ce461

Browse files
authored
Merge pull request #123 from CodeForBaltimore/revjtanton/issue-100
Adding Casbin for User Roles
2 parents d693cfd + 54fca93 commit c9ce461

28 files changed

+1188
-607
lines changed

package-lock.json

Lines changed: 97 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,14 @@
3737
"dependencies": {
3838
"@babel/core": "7.9.0",
3939
"@babel/node": "7.8.7",
40+
"casbin": "4.3.1",
41+
"casbin-sequelize-adapter": "2.0.1",
4042
"chai": "4.2.0",
4143
"cors": "2.8.5",
4244
"crypto": "1.0.1",
4345
"dotenv": "8.2.0",
4446
"express": "4.17.1",
47+
"express-rate-limit": "5.1.1",
4548
"express-request-id": "1.4.1",
4649
"helmet": "3.22.0",
4750
"json2csv": "^5.0.0",

sequelize/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ This repo has simplified a few of the Sequelize CLI options that would be common
2020
- `npm run db-seed` will run all seeders and populate all data.
2121
- `npm run db-unseed` will delete all data in all tables and revert all seeders.
2222

23+
# Casbin
24+
User role management is being handled by [Casbin](https://casbin.org/). To facilitate this our migrations and seeding will replicate the `casbin_rule` table that would be generated by the casbin-sequelize package. We are then seeding as normal, however the field names are not straight-forward. They are defined as follows:
25+
- `role` = The plain-text name of the role. _Example:_ `admin`
26+
- `path` = The path the role has access to. _Example:_ `/user`
27+
- `method` = The http method that can be hit. _Example_ `GET`
28+
29+
For use and management of roles after seeding, please refer to the API spec or Casbin Node.js documentation.
30+
2331
# Links and more information
2432
To create new models, migrations, and seeders you _must_ use the Sequelize CLI commands. Full documentation is here https://sequelize.org/master/manual/migrations.html but here are a few useful commands:
2533
- `npx sequelize-cli model:generate --name User --attributes firstName:string,lastName:string,email:string` - Creates a model under `/src/models` and a migration script.

sequelize/data/user-role.json

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,73 @@
11
[
22
{
33
"role": "admin",
4-
"description": "This is a test admin role."
4+
"path": "/*",
5+
"method": "GET"
6+
},
7+
{
8+
"role": "admin",
9+
"path": "/*",
10+
"method": "POST"
11+
},
12+
{
13+
"role": "admin",
14+
"path": "/*",
15+
"method": "PUT"
16+
},
17+
{
18+
"role": "admin",
19+
"path": "/*",
20+
"method": "DELETE"
21+
},
22+
23+
{
24+
"role": "user",
25+
"path": "/entity",
26+
"method": "GET"
27+
},
28+
{
29+
"role": "user",
30+
"path": "/entity",
31+
"method": "POST"
32+
},
33+
{
34+
"role": "user",
35+
"path": "/entity",
36+
"method": "PUT"
37+
},
38+
{
39+
"role": "user",
40+
"path": "/contact",
41+
"method": "GET"
42+
},
43+
{
44+
"role": "user",
45+
"path": "/contact",
46+
"method": "POST"
47+
},
48+
{
49+
"role": "user",
50+
"path": "/contact",
51+
"method": "PUT"
52+
},
53+
{
54+
"role": "user",
55+
"path": "/csv/Entity",
56+
"method": "GET"
57+
},
58+
{
59+
"role": "user",
60+
"path": "/csv/Contact",
61+
"method": "GET"
62+
},
63+
{
64+
"role": "user",
65+
"path": "/user/*",
66+
"method": "GET"
67+
},
68+
{
69+
"role": "user",
70+
"path": "/user/*",
71+
"method": "PUT"
572
}
673
]

sequelize/data/user.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
{
33
"email": "[email protected]",
44
"password": "donuts",
5-
"roles": [1]
5+
"roles": ["admin"]
66
},
77
{
88
"email": "[email protected]",
99
"password": "test",
10-
"roles": [1]
10+
"roles": ["user"]
1111
}
1212
]

sequelize/migrations/01-create-demo-user.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ module.exports = {
2323
token: {
2424
type: Sequelize.STRING
2525
},
26-
roles: {
27-
type: Sequelize.JSON
28-
},
2926
displayName: {
3027
type: Sequelize.STRING
3128
},
Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,37 @@
11
'use strict';
22
module.exports = {
33
up: (queryInterface, Sequelize) => {
4-
return queryInterface.createTable('UserRoles', {
4+
return queryInterface.createTable('casbin_rule', {
55
id: {
66
allowNull: false,
77
autoIncrement: true,
88
primaryKey: true,
99
type: Sequelize.INTEGER
1010
},
11-
role: {
12-
allowNull: false,
13-
unique: true,
11+
ptype: {
1412
type: Sequelize.STRING
1513
},
16-
description: {
14+
v0: {
1715
type: Sequelize.STRING
1816
},
19-
createdAt: {
20-
allowNull: false,
21-
type: Sequelize.DATE
17+
v1: {
18+
type: Sequelize.STRING
19+
},
20+
v2: {
21+
type: Sequelize.STRING
22+
},
23+
v3: {
24+
type: Sequelize.STRING
25+
},
26+
v4: {
27+
type: Sequelize.STRING
28+
},
29+
v5: {
30+
type: Sequelize.STRING
2231
},
23-
updatedAt: {
24-
allowNull: false,
25-
type: Sequelize.DATE
26-
}
2732
});
2833
},
2934
down: queryInterface => {
30-
return queryInterface.dropTable('UserRoles');
35+
return queryInterface.dropTable('casbin_rule');
3136
}
3237
};

sequelize/seeders/01-demo-role.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
1+
12
const userRoles = require('../data/user-role.json');
23

34
module.exports = {
45
up: queryInterface => {
6+
const roles = [];
57
for (const element of userRoles) {
6-
element.createdAt = new Date();
7-
element.updatedAt = new Date();
8+
roles.push({
9+
ptype: "p",
10+
v0: element.role,
11+
v1: element.path,
12+
v2: element.method
13+
});
814
}
915

10-
return queryInterface.bulkInsert('UserRoles', userRoles);
16+
return queryInterface.bulkInsert('casbin_rule', roles);
1117
},
1218
down: queryInterface => {
13-
return queryInterface.bulkDelete('UserRoles', null, {});
19+
return queryInterface.bulkDelete('casbin_rule', null, {});
1420
}
1521
};

0 commit comments

Comments
 (0)