Skip to content

Commit f0f6a92

Browse files
authored
Merge pull request #289 from dedis/fix/update-dbUtils-script-to-reflect-new-architecture
Fix: update db utils script to reflect new architecture
2 parents f87e328 + 33da8d3 commit f0f6a92

12 files changed

+129
-65
lines changed

.github/workflows/releases.yml

-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ jobs:
8383
cd ${{ steps.get_version.outputs.version_file }}
8484
cp -r ../build/* .
8585
cp -r ../node_modules .
86-
cp -r ../dbUtils.js .
8786
cp -r ../config.env.template .
8887
8988
- name: Create tar.gz

README.docker.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,4 @@ to delete the volumes (this will reset your instance).
5454

5555
1. run the script `DELA_REPLICAS=... init_dela.sh` to initialize the DELA network with `DELA_REPLICAS set to the same value as in .env`
5656
2. run `docker exec -it d-voting-backend-1 /bin/bash` to connect to the backend
57-
3. execute `node -e 'require("./dbUtils").addAdmin("./dvoting-users", <sciper>)'` with your Sciper to add yourself as admin
57+
3. execute `npx cli addAdmin --sciper 123455` with your SCIPER to add yourself as admin

deb-package/README.md

+2-4
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,10 @@ and be sure to *export* variables in `config.env` (i.e do `export xx=yy`).
2525
However it is recommended to use a service manager such as systemd to run the
2626
app.
2727

28-
4) Use dbUtils.js
29-
30-
You can manually update the rights with `dbUtils.js`:
28+
4) Use the CLI to set yourself up as an admin
3129

3230
```sh
33-
NODE_PATH=./node_modules node -e 'require("./dbUtils").listEls("../data/dvoting-users")'
31+
npx cli addAdmin --sciper 1234
3432
```
3533

3634
# Run the web frontend

web/backend/.eslintignore

-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
dist/
2-
dbUtils.js

web/backend/dbUtils.js

-45
This file was deleted.

web/backend/package-lock.json

+25-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

web/backend/package.json

+6-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
"prettier-check": "./node_modules/.bin/prettier --check .",
1414
"test": "echo \"Error: no test specified\" && exit 1"
1515
},
16+
"bin": {
17+
"cli": "./src/cli.ts"
18+
},
1619
"keywords": [],
1720
"author": "",
1821
"license": "ISC",
@@ -21,6 +24,7 @@
2124
"axios": "^0.24.0",
2225
"casbin": "^5.19.1",
2326
"casbin-sequelize-adapter": "^2.4.0",
27+
"commander": "^11.0.0",
2428
"cookie-parser": "^1.4.6",
2529
"crypto": "^1.0.1",
2630
"express": "^4.17.2",
@@ -29,8 +33,8 @@
2933
"lodash": "^4.17.21",
3034
"memorystore": "^1.6.7",
3135
"morgan": "^1.10.0",
32-
"xss": "^1.0.11",
33-
"pg": "^8.11.1"
36+
"pg": "^8.11.1",
37+
"xss": "^1.0.11"
3438
},
3539
"devDependencies": {
3640
"@types/cookie-parser": "^1.4.2",

web/backend/readme.md

+23
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,26 @@ func GenerateKey() {
2525
# Run the program
2626

2727
Once all the previous steps done, the project can be run using `npm start`
28+
29+
# CLI
30+
31+
To control the administration right of different users, the CLI tool can be used.
32+
For example,
33+
```sh
34+
$ npx cli addAdmin --sciper MY_SCIPER_NUMBER
35+
```
36+
37+
You can also consult the following command for more information
38+
```sh
39+
$ npx cli help
40+
Usage: cli [options] [command]
41+
42+
Options:
43+
-h, --help display help for command
44+
45+
Commands:
46+
addAdmin [options] Given a SCIPER number, the owner would gain full admin permissions
47+
listUserPermissions [options] Lists the permissions -if any- of the owner of a given SCIPER
48+
removeAdmin [options] Given a SCIPER number, the owner would lose all admin privileges -if any-
49+
help [command] display help for command
50+
```

web/backend/src/Server.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ async function initEnforcer() {
5555
database: 'casbin',
5656
});
5757

58-
return newEnforcer('model.conf', dbAdapter);
58+
return newEnforcer('src/model.conf', dbAdapter);
5959
}
6060

6161
const port = process.env.PORT || 5000;

web/backend/src/cli.ts

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/usr/bin/env ts-node
2+
3+
/*
4+
Backend CLI, currently providing 3 commands for user management:
5+
npx cli addAdmin --sciper 1234
6+
npx cli listUserPermissions --sciper 1234
7+
npx cli removeAdmin --sciper 1234
8+
*/
9+
10+
import { Command } from 'commander';
11+
import { SequelizeAdapter } from 'casbin-sequelize-adapter';
12+
import { newEnforcer } from 'casbin';
13+
14+
const program = new Command();
15+
16+
async function initEnforcer() {
17+
const dbAdapter = await SequelizeAdapter.newAdapter({
18+
dialect: 'postgres',
19+
host: process.env.DATABASE_HOST,
20+
port: parseInt(process.env.DATABASE_PORT || '5432', 10),
21+
username: process.env.DATABASE_USERNAME,
22+
password: process.env.DATABASE_PASSWORD,
23+
database: 'casbin',
24+
});
25+
26+
return newEnforcer('src/model.conf', dbAdapter);
27+
}
28+
29+
program
30+
.command('addAdmin')
31+
.description('Given a SCIPER number, the owner would gain full admin permissions')
32+
.requiredOption('-s, --sciper <char>', 'user SCIPER')
33+
.action(async ({ sciper }) => {
34+
const enforcer = await initEnforcer();
35+
const permissions = [
36+
[sciper, 'roles', 'add'],
37+
[sciper, 'roles', 'list'],
38+
[sciper, 'roles', 'remove'],
39+
[sciper, 'proxies', 'post'],
40+
[sciper, 'proxies', 'put'],
41+
[sciper, 'proxies', 'delete'],
42+
[sciper, 'election', 'create'],
43+
];
44+
await enforcer.addPolicies(permissions);
45+
console.log('Successfully imported permissions for user!');
46+
});
47+
48+
program
49+
.command('listUserPermissions')
50+
.description('Lists the permissions -if any- of the owner of a given SCIPER')
51+
.requiredOption('-s, --sciper <char>', 'user SCIPER')
52+
.action(async ({ sciper }) => {
53+
const enforcer = await initEnforcer();
54+
const userPermissions = await enforcer.getPermissionsForUser(sciper);
55+
console.log(userPermissions);
56+
});
57+
58+
program
59+
.command('removeAdmin')
60+
.description('Given a SCIPER number, the owner would lose all admin privileges -if any-')
61+
.requiredOption('-s, --sciper <char>', 'user SCIPER')
62+
.action(async ({ sciper }) => {
63+
const enforcer = await initEnforcer();
64+
await enforcer.deletePermissionsForUser(sciper);
65+
console.log('Permissions removed successfully!');
66+
});
67+
68+
program.parse();

web/backend/model.conf web/backend/src/model.conf

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ p = sub, obj, act
99
e = some(where (p.eft == allow))
1010

1111
[matchers]
12-
m = r.sub == p.sub && r.obj == p.obj && r.act == p.act
12+
m = r.sub == p.sub && r.obj == p.obj && r.act == p.act

web/backend/tsconfig.json

+2-3
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,16 @@
2525
}
2626
},
2727
"include": [
28-
"model.conf",
28+
"src/model.conf",
2929
"policy.csv",
30-
"src/Server.ts",
30+
"src/*.ts",
3131
"config.json"
3232
],
3333
"exclude": [
3434
"node_modules",
3535
"jest.config.js",
3636
".eslintrc",
3737
"dist",
38-
"dbUtils.js"
3938
],
4039
"typedocOptions": {
4140
"entryPoints": [

0 commit comments

Comments
 (0)