-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsd_init.js
89 lines (74 loc) · 2.56 KB
/
sd_init.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import {
DEBUG,
NOUSER_ID,
GROUP_TABLE, USER_TABLE, PERMISSION_TABLE,
TRANSACTION_TABLE,
SYSTEM_PAYMENT_ACCOUNT
} from './common.js';
import {
addUser
} from './helpers.js';
import {
DB_ROOT,
setItem,
} from './db_helpers.js';
import Perms from './permissions.js';
const ADMIN_EMAIL = 'cris@dosycorp.com';
// we could check these props are actually in the schema
// ie to catch changes or spelling errors
// ie txID when txId is meant
export const IndexProps = {
[USER_TABLE]: ["username", "email"],
[TRANSACTION_TABLE]: ["txId"]
};
export function init({_getTable, config}) {
try {
// config
config({root:DB_ROOT});
// basic tables
const utable = _getTable(USER_TABLE);
const ptable = _getTable(PERMISSION_TABLE);
const gtable = _getTable(GROUP_TABLE);
// add basic users 'no user', 'test user' (regular user), 'useradmin' (user admin),
// and 'globaladmin' (global admin)
const noUser = {
_owner: NOUSER_ID,
username: 'nouser',
email: 'no-one@nowhere.nothing',
salt: 0,
passwordHash: '0000000000000000',
groups:['nousers'],
stripeCustomerID: SYSTEM_PAYMENT_ACCOUNT,
verified: false
};
setItem({table:utable, id:NOUSER_ID, item:noUser});
gtable.put('nousers', {name:'nousers', users: [NOUSER_ID], description:'not logged in users'});
gtable.put('users', {name:'users', users: [], description:'regular users'});
gtable.put('useradmins', {name:'useradmins', users: [], description:'user administrators'});
gtable.put('globaladmins', {name:'globaladmins', users: [], description:'global administrators'});
try {
addUser({username:'test9', email:ADMIN_EMAIL, stripeCustomerID: SYSTEM_PAYMENT_ACCOUNT, password:'abc123', verified: true}, 'users');
} catch(e) {
DEBUG.INFO && console.info(e+'');
}
try {
addUser({username:'useradmin', email:ADMIN_EMAIL, stripeCustomerID: SYSTEM_PAYMENT_ACCOUNT, password:'abc123', verified: true}, 'users', 'useradmins');
} catch(e) {
DEBUG.INFO && console.info(e+'');
}
try {
addUser({username:'globaladmin', email:ADMIN_EMAIL, stripeCustomerID: SYSTEM_PAYMENT_ACCOUNT, password:'abc123', verified: true}, 'users', 'globaladmins');
} catch(e) {
DEBUG.INFO && console.info(e+'');
}
// add perms
DEBUG.INFO && console.log({Perms});
for( const [scope, perm] of Perms ) {
ptable.put(scope, perm);
}
console.log("Init called");
return new Map([...Object.entries(IndexProps)]);
} catch(e) {
console.log("Error on DB initialization", e);
}
}