forked from apostrophecms-legacy/apostrophe-personas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
286 lines (246 loc) · 9.65 KB
/
index.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
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
var _ = require('@sailshq/lodash');
module.exports = {
moogBundle: {
directory: 'lib/modules',
modules: [
'apostrophe-personas-areas',
'apostrophe-personas-custom-pages',
'apostrophe-personas-pages',
'apostrophe-personas-widgets',
'apostrophe-personas-doc-type-manager',
'apostrophe-personas-pieces'
]
},
afterConstruct: function(self) {
self.composePersonas();
self.addMultiplePersonasMigration();
},
construct: function(self, options) {
require('./lib/browser.js')(self, options);
self.composePersonas = function() {
_.each(self.options.personas, function(persona) {
if (!persona.label) {
persona.label = persona.name;
}
if (!persona.prefix) {
persona.prefix = '/' + persona.name;
}
});
self.personas = self.options.personas;
};
self.modulesReady = function() {
var workflow = self.apos.modules['apostrophe-workflow'];
var inferredAll = false;
if (!workflow) {
return;
}
_.each(self.personas, function(persona) {
if (!persona.prefixes) {
persona.prefixes = {};
self.apos.utils.warn('Warning: workflow module is in use and the prefixes option is not configured for the ' + persona.name + ' persona, falling back to ' + persona.prefix + ' which will not be translated');
inferredAll = true;
}
_.each(workflow.locales, function(locale, name) {
if (name.match(/-draft$/)) {
return;
}
if (!persona.prefixes[name]) {
persona.prefixes[name] = persona.prefix || ('/' + persona.name);
if (!inferredAll) {
self.apos.utils.warn('Warning: workflow module is in use and the prefixes option for the ' + persona.name + ' persona has no setting for the ' + name + ' locale, falling back to ' + persona.prefix + ' which will not be translated');
}
}
});
});
};
self.addHelpers({
personas: function() {
return self.personas;
}
});
// Set `req.persona` if appropriate
self.expressMiddleware = {
before: 'apostrophe-global',
middleware: function(req, res, next) {
if (req.method !== 'GET') {
return next();
}
var workflow = self.apos.modules['apostrophe-workflow'];
if (req.query.persona === 'none') {
delete req.session.persona;
}
// If a user clicks on the official persona switcher, this always
// changes their cookie immediately, no matter what the old setting was.
// After that the persona prefix is sufficient so redirect to get
// rid of the query
if ((req.query.persona && _.find(self.personas, { name: req.query.persona })) || req.query.persona === 'none') {
if (req.query.persona !== 'none') {
req.session.nextPersona = req.query.persona;
}
req.url = req.url.replace(/(\?)?(&)?(persona=[^&]+)/, '$1');
req.url = req.url.replace(/\?$/, '');
req.url = self.addPrefix(req, req.query.persona, req.url);
return res.redirect(req.url);
}
if (req.session.nextPersona) {
if (req.session.nextPersona !== req.session.persona) {
req.session.persona = req.session.nextPersona;
req.data.personaSwitched = true;
}
delete req.session.nextPersona;
}
// A session could outlive a persona
if (req.session.persona && (!_.find(self.personas, { name: req.session.persona }))) {
delete req.session.persona;
}
// Find the persona suggested by the URL prefix and adjust req.url
// after capturing that information.
var addSlash = false;
var urlPersona = _.find(self.personas, function(persona) {
var prefix;
var liveLocale = workflow && workflow.liveify(req.locale);
var workflowPrefix = '';
if (workflow) {
prefix = persona.prefixes[liveLocale];
workflowPrefix = (workflow.prefixes && workflow.prefixes[liveLocale]) || '';
} else {
prefix = persona.prefix;
}
if (req.url.substr(0, workflowPrefix.length + 1) !== (workflowPrefix + '/')) {
// Unprefixed route like /login
workflowPrefix = '';
}
if (prefix && (req.url.substr(workflowPrefix.length) === prefix)) {
// handle /en/car as a full URL gracefully
req.url += '/';
addSlash = true;
return true;
}
if (prefix &&
(req.url.substr(workflowPrefix.length, prefix.length + 1) === (prefix + '/'))) {
// The workflow prefix is really in the slug, but the
// persona prefix is not. So snip it out to let
// apostrophe find it in the database:
//
// /fr/auto/driving becomes /fr/driving
//
// If there is no workflow prefix the default empty
// string just turns /auto/driving into /driving which
// is also what we want
req.url = req.url.substr(0, workflowPrefix.length) + req.url.substr(workflowPrefix.length + prefix.length);
return true;
}
});
urlPersona = urlPersona && urlPersona.name;
req.urlPersona = urlPersona;
if (addSlash) {
return res.redirect(req.url);
}
// Arriving at a generic page with a persona prefix will set the persona of
// the user immediately
if (req.session.persona !== urlPersona) {
req.session.persona = urlPersona;
req.data.personaSwitched = !!urlPersona;
}
req.data.urlPersona = urlPersona;
if (req.session.persona) {
req.persona = req.session.persona;
req.data.persona = req.persona;
}
return next();
}
};
// Add the prefix for the given persona name to the given URL.
// In the presence of workflow, the persona "prefix" falls between
// the workflow prefix (already in the URL) and the rest of the URL,
// and varies based on locale.
//
// If the URL already has a persona prefix it is replaced with
// one appropriate to the given persona name.
self.addPrefix = function(req, persona, url) {
var workflow = self.apos.modules['apostrophe-workflow'];
var personas = self.apos.modules['apostrophe-personas'];
var liveLocale = workflow && workflow.liveify(req.locale);
var workflowPrefix = (liveLocale && workflow.prefixes && workflow.prefixes[liveLocale]) || '';
if (require('url').parse(url).pathname.substr(0, workflowPrefix.length) !== workflowPrefix) {
// Workflow prefix is not actually present, probably a route like /login
workflowPrefix = '';
}
var personaInfo = (persona === 'none') ? 'none' : _.find(personas.personas, { name: persona });
var prefix;
if (personaInfo === 'none') {
prefix = '';
} else {
prefix = workflow ? personaInfo.prefixes[liveLocale] : personaInfo.prefix;
}
if (url.match(/^(https?:)?\/\//)) {
// Turn on the "slashes denote host" option
var parsed = require('url').parse(url, false, true);
parsed.pathname = prepend(parsed.pathname);
return require('url').format(parsed);
} else {
var result = prepend(url);
return result;
}
function prepend(path) {
path = path.substr(workflowPrefix.length);
var existingPersonaPrefix = _.find(self.getAllPrefixes(req), function(prefix) {
return path.substr(0, prefix.length + 1) === (prefix + '/');
});
if (existingPersonaPrefix) {
path = path.substr(existingPersonaPrefix.length);
}
if (path.length) {
return workflowPrefix + prefix + path;
} else {
return workflowPrefix + prefix + '/';
}
}
};
self.getAllPrefixes = function(req) {
var workflow = self.apos.modules['apostrophe-workflow'];
var prefixes = [];
_.each(self.personas, function(persona) {
if (workflow) {
prefixes.push(persona.prefixes[workflow.liveify(req.locale)]);
} else if (persona.prefix) {
prefixes.push(persona.prefix);
}
});
return prefixes;
};
// Should return true if the user is an editor
// should bypass the normal restrictions on whether they
// can see widgets and pieces for other personas, for
// editing purposes. If this definition ("anyone who is
// logged in is a potential editor") is not fine-grained
// enough for your purposes, override this method at
// project level
self.userIsEditor = function(req) {
return req.user;
};
self.addMultiplePersonasMigration = function() {
self.apos.migrations.add('addMultiplePersonas', function(callback) {
return self.apos.migrations.eachWidget({}, function(doc, widget, dotPath, callback) {
if (!widget.personas) {
if (widget.persona) {
widget.personas = [ widget.persona ];
} else {
widget.personas = [];
}
delete widget.persona;
var update = {};
update[dotPath + '.personas'] = widget.personas;
update[dotPath + '.persona'] = null;
return self.apos.docs.db.update({
_id: doc._id
}, { $set: update }, callback);
} else {
return setImmediate(callback);
}
}, callback);
}, { safe: true });
};
self.apos.define('apostrophe-cursor', require('./lib/cursor.js'));
}
};