|
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 |
3 | 2 | //
|
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")' |
7 | 6 | //
|
8 | 7 | // If your are running this script outside of this module, specify NODE_PATH=
|
9 | 8 |
|
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 | + }); |
11 | 21 |
|
12 |
| -const addAdmin = (dbPath, sciper) => { |
13 |
| - const usersDB = lmdb.open({ path: dbPath }); |
| 22 | + return newEnforcer('../model.conf', dbAdapter); |
| 23 | +} |
14 | 24 |
|
15 |
| - usersDB |
16 |
| - .put(String(sciper), 'admin') |
17 |
| - .then(() => { |
18 |
| - console.log('ok'); |
19 |
| - }) |
20 |
| - .catch((error) => { |
21 |
| - console.log(error); |
22 |
| - }); |
23 |
| -}; |
24 | 25 |
|
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 | +} |
27 | 40 |
|
28 |
| - db.getRange({}).forEach(({ key, value }) => { |
29 |
| - console.log(`'${key}' => '${value}' \t | \t (${typeof key}) => (${typeof key})`); |
30 |
| - }); |
31 |
| -}; |
32 | 41 |
|
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 | + |
35 | 48 |
|
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 | +} |
44 | 53 |
|
45 |
| -module.exports = { addAdmin, listEls, removeEl }; |
| 54 | +module.exports = { addAdmin, listUserPermissions, removeAdmin }; |
0 commit comments