-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap.js
136 lines (104 loc) · 2.96 KB
/
bootstrap.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
const USER_SESSIONS = new Classes.WeakValueSetMap();
alchemy.requirePlugin('form');
// Create the user settings group
const USER_SETTINGS = new Classes.Alchemy.Setting.Group('user', null);
Plugin.USER_SETTINGS = USER_SETTINGS;
// Create an acl log function
log.acl = log.verbose;
// If the i18n translation function does not exist, create a dummy one
if (!global.__) {
global.__ = function __(domain, name) {
if (typeof name == 'undefined') {
name = domain;
}
return name;
};
}
/**
* Load all permissions before starting the server
*
* @author Jelle De Loecker <jelle@elevenways.be>
* @since 0.1.0
* @version 0.9.0
*/
STAGES.getStage('server.start').addPreTask(async function beforeStartServer() {
const PermissionGroup = Model.get('Acl.PermissionGroup'),
User = Model.get('User');
let superuser_group = await PermissionGroup.findByValues({slug: 'superuser'});
if (!superuser_group) {
superuser_group = PermissionGroup.createDocument();
superuser_group.title = 'Superuser';
superuser_group.slug = 'superuser';
superuser_group.permissions = [{permission: '*', value: true}];
await superuser_group.save();
}
await PermissionGroup.loadAllGroups();
if (!Plugin.has_proteus) {
let super_user_id = alchemy.settings.plugins.acl.model.super_user_id;
await User.ensureIds({
_id : super_user_id,
username : 'admin',
name : 'Superuser',
password : 'admin',
permissions : [{permission: 'group.superuser', value: true}],
});
}
});
/**
* Set the user data for the given session
*
* @author Jelle De Loecker <jelle@elevenways.be>
* @since 0.9.0
* @version 0.9.0
*
* @param {Conduit} conduit
* @param {User} user
*/
Plugin.addUserDataToSession = function addUserDataToSession(conduit, user) {
let user_id = '' + user.$pk;
conduit.session('UserData', user);
USER_SESSIONS.add(user_id, conduit.getSession());
};
/**
* Update sessions for the given usre data
*
* @author Jelle De Loecker <jelle@elevenways.be>
* @since 0.9.0
* @version 0.9.0
*
* @param {User} user
*/
Plugin.updateUserDataSessions = function updateUserDataSessions(user) {
let user_id = '' + user.$pk;
let sessions = USER_SESSIONS.get(user_id);
if (!sessions) {
return;
}
for (let session of sessions) {
let existing_data = session.get('UserData');
// If this session no longer has existing data, skip it
if (!existing_data) {
sessions.delete(session);
continue;
}
session.set('UserData', JSON.clone(user));
}
};
// Send the user info to the client
alchemy.hawkejs.on({type: 'renderer', status: 'begin'}, function onBegin(renderer) {
if (!renderer.conduit) {
return;
}
let data = renderer.conduit.session('UserData');
if (data) {
data = data.User;
}
if (data && data.username) {
let user = Object.assign({}, data);
delete user.password;
if (user.permissions) {
user.permissions = user.permissions.flattened();
}
renderer.expose('acl-user-data', user);
}
});