This repository was archived by the owner on Jun 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrap.js
More file actions
252 lines (197 loc) · 5.84 KB
/
bootstrap.js
File metadata and controls
252 lines (197 loc) · 5.84 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// 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;
};
}
// Define the default options
var options = {
// The model to use
model: 'User',
// The username field
username: 'username',
// The password field
password: 'password',
// The default url to redirect to
redirect: '/',
// The amount of rounds to process the salt
rounds: 10,
// The name of the base layout
baselayout: 'layouts/acl_base',
// The name of the body layout
bodylayout: 'layouts/acl_body',
// The main layout
mainlayout: 'layouts/acl_main',
// The name of the body block
bodyblock: 'acl-base',
// The name of the main block
mainblock: 'acl-main',
// The name of the content block
contentblock: 'acl-content',
// Placeholder variables to use in certain strings
placeholders: {},
// User model extra fields
userModelFields: [
['first_name', 'String'],
['last_name', 'String']
],
// The everyone group id
EveryoneGroupId: alchemy.ObjectId('52efff0000A1C00001000000'),
// The logged in user group id
LoggedInGroupId: alchemy.ObjectId('52efff0000A1C00001000003'),
// The super user group id
SuperUserGroupId: alchemy.ObjectId('52efff0000A1C00001000001'),
// The super user id
SuperUserId: alchemy.ObjectId('52efff0000A1C00000000000')
};
// Inject the user-overridden options
alchemy.plugins.acl = Object.assign(options, alchemy.plugins.acl);
// Make sure the model name is correct
options.model = options.model.modelName();
// Ensure these groups exist
var ensureGroups = [];
// The everyone group
ensureGroups[ensureGroups.length] = {
_id: options.EveryoneGroupId,
name: 'Everyone',
special: true,
special_command: 'everyone',
forfeit_to_group_id: options.LoggedInGroupId,
weight: 1
};
// The logged in user group
ensureGroups[ensureGroups.length] = {
_id: options.LoggedInGroupId,
name: 'Logged in',
special: true,
special_command: 'loggedin',
forfeit_to_group_id: options.SuperUserGroupId,
weight: 5
};
// The super user group
ensureGroups[ensureGroups.length] = {
_id: options.SuperUserGroupId,
name: 'Superuser',
root: true,
weight: 10001
};
setTimeout(function ensureData() {
var AclGroup = Model.get('AclGroup'),
User = Model.get('User'),
SuperUserGroupId = alchemy.plugins.acl.SuperUserGroupId,
SuperUserId = alchemy.plugins.acl.SuperUserId;
// Make sure the required ACL groups exist
AclGroup.ensureIds(ensureGroups);
// Make sure the super user exists
User.ensureIds({
_id: SuperUserId,
username: 'admin',
name: 'Superuser',
password: '$2a$10$sTLrARZ6hEJwnof6f6ZLDO2L.i.oumyWFC2jC4FB2k3fdkfszYzZC', // "admin"
acl_group_id: [SuperUserGroupId]
});
}, 4);
// Get the view settings
var viewSettings = {
baselayout: options.baselayout,
bodylayout: options.bodylayout,
mainlayout: options.mainlayout,
bodyblock: options.bodyblock,
mainblock: options.mainblock,
contentblock: options.contentblock,
username: options.username,
password: options.password
};
/**
* Look for persistent login cookies
*
* @author Jelle De Loecker <jelle@kipdola.be>
* @since 1.0.0
* @version 1.0.0
*/
Router.use(function persistentLoginCheck(req, res, next) {
var acpl,
conduit = req.conduit,
Persistent;
// Do nothing if userdata is already set
if (conduit.session('UserData')) {
return next();
}
// Get the persistent cookie
acpl = conduit.cookie('acpl');
if (acpl) {
Persistent = conduit.getModel('AclPersistentCookie');
Persistent.find('first', {conditions: {identifier: acpl.i, token: acpl.t}}, function gotCookie(err, cookie) {
if (!err && cookie.length && cookie[0].User) {
conduit.getModel('User').find('first', {conditions: {_id: cookie[0].User._id}}, function gotUser(err, user) {
conduit.session('UserData', user);
});
}
next();
});
} else {
next();
}
}, {weight: 99999});
// Send the acl layout options to the client
alchemy.hawkejs.on({type: 'viewrender', status: 'begin', client: false}, function onBegin(viewRender) {
// Expose the viewsettings only once (they don't change)
viewRender.expose('acl-view-setting', viewSettings);
});
// Send the user info to the client
alchemy.hawkejs.on({type: 'viewrender', status: 'begin'}, function onBegin(viewRender) {
var data,
user;
if (!viewRender.conduit) {
return;
}
data = viewRender.conduit.session('UserData');
if (data) {
data = data.User;
}
if (data && data.username) {
user = Object.assign({}, data);
delete user.password;
viewRender.expose('acl-user-data', user);
}
});
return;
// Get the view settings
var viewSettings = {
baselayout: alchemy.layoutify(options.baselayout),
bodylayout: alchemy.layoutify(options.bodylayout),
mainlayout: alchemy.layoutify(options.mainlayout),
bodyblock: options.bodyblock,
mainblock: options.mainblock,
contentblock: options.contentblock,
username: options.username,
password: options.password
};
// Add the middleware to intercept the routes
alchemy.addMiddleware(99, 'acl-routes', function(req, res, next){
Model.get('AclPermission').checkRequest(req, res, next);
});
// Send the acl layout options to the client
alchemy.on('render.callback', function(render, callback) {
var user = render.req.session.user,
display = __('acl', 'Unnamed User');
// Only send this data on the initial pageload
if (!render.ajax) {
render.store('acl-view-setting', viewSettings);
}
if(user){
if(user.first_name && user.last_name){
user.fullname = user.first_name + ' ' + user.last_name;
}
display = user.fullname || user.name || user.username || user.email;
render.viewVars.UserFullName = display;
render.viewVars.UserFirstName = user.first_name || user.username || '';
render.viewVars.UserLastName = user.last_name || '';
}
callback();
});