Skip to content

Commit f1468ca

Browse files
author
Ahmed Elghareeb
committed
fix: rewrite the script to target postgres instead of lmdb
1 parent d552e99 commit f1468ca

File tree

1 file changed

+43
-34
lines changed

1 file changed

+43
-34
lines changed

web/backend/src/dbUtils.js

+43-34
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,54 @@
1-
// This file provides utility functions to handle an lmdb database. You can use
2-
// the node CLI to call those functions:
1+
// This file provides utility functions to for managing admin users
32
//
4-
// node -e 'require("./dbUtils").addAdmin("./dvoting-users", 1234)'
5-
// node -e 'require("./dbUtils").listEls("./dvoting-users")'
6-
// node -e 'require("./dbUtils").removeEl("./dvoting-users", 1234)'
3+
// node -e 'require("./dbUtils").addAdmin("1234")'
4+
// node -e 'require("./dbUtils").listUserPermissions("1234")'
5+
// node -e 'require("./dbUtils").removeAdmin("1234")'
76
//
87
// If your are running this script outside of this module, specify NODE_PATH=
98

10-
const lmdb = require('lmdb');
9+
import { SequelizeAdapter } from 'casbin-sequelize-adapter';
10+
import { newEnforcer } from 'casbin';
11+
12+
async function initEnforcer() {
13+
const dbAdapter = await SequelizeAdapter.newAdapter({
14+
dialect: 'postgres',
15+
host: process.env.DATABASE_HOST,
16+
port: parseInt(process.env.DATABASE_PORT || '5432', 10),
17+
username: process.env.DATABASE_USERNAME,
18+
password: process.env.DATABASE_PASSWORD,
19+
database: 'casbin',
20+
});
1121

12-
const addAdmin = (dbPath, sciper) => {
13-
const usersDB = lmdb.open({ path: dbPath });
22+
return newEnforcer('../model.conf', dbAdapter);
23+
}
1424

15-
usersDB
16-
.put(String(sciper), 'admin')
17-
.then(() => {
18-
console.log('ok');
19-
})
20-
.catch((error) => {
21-
console.log(error);
22-
});
23-
};
2425

25-
const listEls = (dbPath) => {
26-
const db = lmdb.open({ path: dbPath });
26+
async function addAdmin(sciper) {
27+
const enforcer = await initEnforcer();
28+
const permissions = [
29+
[sciper, 'roles', 'add'],
30+
[sciper, 'roles', 'list'],
31+
[sciper, 'roles', 'remove'],
32+
[sciper, 'proxies', 'post'],
33+
[sciper, 'proxies', 'put'],
34+
[sciper, 'proxies', 'delete'],
35+
[sciper, 'election', 'create'],
36+
]
37+
await enforcer.addPolicies(permissions);
38+
console.log("Successfully imported permissions for user!")
39+
}
2740

28-
db.getRange({}).forEach(({ key, value }) => {
29-
console.log(`'${key}' => '${value}' \t | \t (${typeof key}) => (${typeof key})`);
30-
});
31-
};
3241

33-
const removeEl = (dbPath, key) => {
34-
const db = lmdb.open({ path: dbPath });
42+
async function listUserPermissions(userID) {
43+
const enforcer = await initEnforcer();
44+
const userPermissions = await enforcer.getPermissionsForUser(userID)
45+
console.log(userPermissions)
46+
}
47+
3548

36-
db.remove(String(key))
37-
.then(() => {
38-
console.log(`key '${key}' removed`);
39-
})
40-
.catch((error) => {
41-
console.log(`error: ${error}`);
42-
});
43-
};
49+
async function removeAdmin(sciper) {
50+
const enforcer = await initEnforcer();
51+
await enforcer.deletePermissionsForUser(sciper)
52+
}
4453

45-
module.exports = { addAdmin, listEls, removeEl };
54+
module.exports = { addAdmin, listUserPermissions, removeAdmin };

0 commit comments

Comments
 (0)