Repository navigation
Expand file tree
/
Copy pathmodel.js
More file actions
393 lines (371 loc) · 13.9 KB
/
Copy pathmodel.js
File metadata and controls
393 lines (371 loc) · 13.9 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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/////////////////////////////
// Apps model
Apps = new Meteor.Collection("apps");
Apps.allow ({
insert: function (userId, doc) {
return (userId)
},
update: function (userId, doc) {
if (userId && Roles.userIsInRole(userId, ['admin']) )
return true;
return (userId && _.contains(doc.owner, Meteor.user().emails[0].address) )
},
remove: function (userId, doc) {
// limite par role
if ( Roles.userIsInRole(userId, ['admin']) === false ) {
throw new Meteor.Error(404, "Not admin");
return false;
}
return true;
},
});
/////////////////////////////
// Hooks
if (Meteor.isServer) {
// protect admin fields
Apps.before.update(function(userId, doc, fieldNames, modifier, options) {
if ( (_.contains(fieldNames, 'compte') || _.contains(fieldNames, 'serveurs') )
&& Roles.userIsInRole(userId, ['admin']) === false) {
return false;
}
});
// send mail after insert
Apps.after.insert(function(userId, doc) {
var adminmails =
_.map(_.pluck(Roles.getUsersInRole('admin').fetch(), 'emails'),
function(user){ return _.pluck(user,'address'); }).join(',');
if (Meteor.settings.environment === 'prod') {
Email.send({
to: adminmails + ',' + Meteor.user().emails[0].address,
from: Meteor.settings.mail.from,
subject: "["+ Meteor.settings.public.appName +"] Nouvelle demande",
text: Meteor.user().emails[0].address + " a créé une demande pour " + doc.nom +
"\n\nVous pouvez suivre cette demande ici : " + Router.routes['appInfo'].url({_id: doc._id}) });
}
else {
console.log("=====> Send mail by ",Meteor.user().emails[0].address);
console.log("=====> Send mail about ",Router.routes['appInfo'].url({_id: doc._id}));
};
console.log("=====> Send mail for ",adminmails + ',' + Meteor.user().emails[0].address);
});
// new request on ip or data perso change
Apps.after.update(function(userId, doc, fieldNames, modifier, options) {
if (_.contains(fieldNames, 'ips') && !_.isEqual(this.previous.ips,modifier.$set.ips)) {
// set status 0, send mail
var chg = [];
chg = _.difference(modifier.$set.ips,this.previous.ips);
if (chg.length === 0)
chg = _.difference(this.previous.ips,this.previous.ips);
if (chg.length === 0)
chg = modifier.$set.ips; // XXX don't find diff on removed ips !
Apps.update({'_id':doc._id},{ $set: {'status': 0, '_chgips': chg }});
var adminmails =
_.map(_.pluck(Roles.getUsersInRole('admin').fetch(), 'emails'),
function(user){ return _.pluck(user,'address'); }).join(',');
if (Meteor.settings.environment === 'prod') {
Email.send({
to: adminmails + ',' + Meteor.user().emails[0].address,
from: Meteor.settings.mail.from,
subject: "["+ Meteor.settings.public.appName +"] Modification IPs pour " + doc.nom,
text: Meteor.user().emails[0].address + " a modifié les IPs pour " + doc.nom +
"\n\nVous pouvez suivre cette demande ici : " + Router.routes['appInfo'].url({_id: doc._id}) });
}
else {
console.log("=====> prev values ", this.previous.ips);
console.log("=====> values ", modifier);
};
};
if (_.contains(fieldNames, 'dataperso') && !_.isEqual(this.previous.dataperso,modifier.$set.dataperso)) {
// set status 0, send mail
Apps.update({'_id':doc._id},{ $set: {'status': 0 }});
var adminmails =
_.map(_.pluck(Roles.getUsersInRole('admin').fetch(), 'emails'),
function(user){ return _.pluck(user,'address'); }).join(',');
if (Meteor.settings.environment === 'prod') {
Email.send({
to: adminmails + ',' + Meteor.user().emails[0].address,
from: Meteor.settings.mail.from,
subject: "["+ Meteor.settings.public.appName +"] Modification Data Perso pour " + doc.nom,
text: Meteor.user().emails[0].address + " a modifié les Data Perso pour " + doc.nom +
"\n\nVous pouvez suivre cette demande ici : " + Router.routes['appInfo'].url({_id: doc._id}) });
}
else {
console.log("=====> prev values ", this.previous.dataperso);
console.log("=====> values ", modifier);
};
}
;
if ( _.contains(fieldNames, "status") ) {
// send mail on status change ?
console.log("=====> value ",modifier.$set.status);
};
});
};
/////////////////////////////
// Schema
var ipv46test = /(^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))$)|(^((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?$)/;
var statusLabel = {
0: i18n('Request'),
1: i18n('Account'),
2: i18n('NetworkAccess'),
3: i18n('FieldAccess'),
4: i18n('WaitOnValid'),
5: "Ok",
6: i18n('ToRemove')
};
var Schemas = {};
Schemas.app = new SimpleSchema({
nom: {
type: String,
label: i18n('AppName') + "*",
max: 200,
unique: true
},
owner:{
type: [String],
label: "Contact*",
optional: true,
minCount: 1,
min: 4,
regEx: SimpleSchema.RegEx.Email,
autoValue: function () {
if (this.isInsert) {
if (!this.operator) {
return [Meteor.user().emails[0].address]
}
else {
return {$push: Meteor.user().emails[0].address};
}
}
}
},
content: {
type: String,
label: "Description",
max: 2000,
optional: true,
autoform: {
rows: 3 // for textarea field
}
},
domaine: {
type: String,
label: "Domaine",
allowedValues: ['Recherche','Pédagogie','ENT','DDE','DN SIG','DN Infra','DN UN','DN SU'],
autoform: {
options: "allowed",
firstOption: i18n('SelectOneLabel')
},
optional: true
},
usage: {
type: String,
label: "Usage Ldap+",
allowedValues: ['Autorisation', 'Authentification','Lecture profil','Traitement de données'],
autoform: {
options: "allowed",
firstOption: i18n('SelectOneLabel')
},
optional: true
},
filtre: {
type: String,
label: "Filtres",
max: 2000,
optional: true,
autoform: {
rows: 2
}
},
dataperso: {
type: [String],
label: "Données perso+",
optional: true,
autoform: {
options: "allowed", // autoform need to respect allowedValues
noselect: true // for checkboxes
},
allowedValues: ["telephoneNumber","supannEtuID","supannEmpID","supannMailPerso","schacDateOfBirth"]
},
ips: {
type: [String],
label: "IPs sources*",
regEx: ipv46test,
optional: true,
defaultValue: [],
minCount: 1,
},
_chgips: { // as ips are mandatory _chgips allow to show ips
type: [String], // changed requests in history
label: "IPs changed",
regEx: ipv46test,
optional: true,
defaultValue: []
},
compte: { // only for admins
type: String,
label: "Compte",
optional: true,
custom: function() {
if ( this.isSet && Roles.userIsInRole(this.userId, ['admin']) === false ) {
return "notAllowedField"; // set message, protection is server side
}
}
},
serveurs: { // only for admins
type: String,
label: "Serveurs",
optional: true,
custom: function() {
if ( this.isSet && Roles.userIsInRole(this.userId, ['admin']) === false ) {
return "notAllowedField"; // set message, protection is server side
}
}
},
created: {
type: Date,
label: "Created Date",
autoValue: function () {
if (this.isInsert) {
return new Date;
} else {
this.unset();
}
}
},
createdBy: {
type: String,
label: "Created By",
autoValue: function () {
if (this.isInsert) {
return Meteor.user().emails[0].address;
} else {
this.unset();
}
}
},
modified: {
type: Date,
label: "Modified Date",
optional: true,
autoValue: function () {
if (this.isUpdate) {
return new Date;
} else {
this.unset();
}
}
},
modifiedBy: {
type: String,
label: "Modified By",
optional: true,
autoValue: function () {
if (this.isUpdate) {
return Meteor.user().emails[0].address;
} else {
this.unset();
}
}
},
status: { // some values for admins
type: Number,
optional: true,
custom: function() {
if (this.isSet && (!this.operator || this.operator === "$set") ) {
if ( !_.contains([0, 1, 2, 3, 4, 5, 6], this.value) )
return "notAllowed";
if ( Roles.userIsInRole(this.userId, ['admin']) === false && _.contains([ 1, 2, 3, 4], this.value))
return "notAllowedStatus";
}
},
autoform: {
options: [
{label: statusLabel[0], value: 0},
{label: statusLabel[1], value: 1},
{label: statusLabel[2], value: 2},
{label: statusLabel[3], value: 3},
{label: statusLabel[4], value: 4},
{label: statusLabel[5], value: 5},
{label: statusLabel[6], value: 6}
]
}
},
updatesHistory: {
type: [Object],
optional: true,
autoValue: function() {
var ips = this.field("_chgips");
var owner = this.field("owner");
var status = this.field("status");
var dataperso = this.field("dataperso");
if (status.isSet || dataperso.isSet) {
if (this.isInsert) {
return [{
date: new Date,
by: Meteor.user().emails[0].address,
content: ips.value.join(', ') + ((dataperso.isSet)?dataperso.value.join(', '): '')
}];
} else {
var text = (statusLabel[status.value] || '');
text = text + ' ' + ((dataperso.isSet && dataperso.value)?dataperso.value.join(', '): '');
text = text + ' ' + ((ips.isSet)?ips.value.join(', '): '');
return {
$push: {
date: new Date,
by: Meteor.user().emails[0].address,
content: text
}
};
}
} else {
this.unset();
}
}
},
'updatesHistory.$.date': {
type: Date,
optional: true
},
'updatesHistory.$.content': {
type: String,
optional: true
},
'updatesHistory.$.by': {
type: String,
optional: true
},
});
/////////////////////////////
// custom message
Schemas.app.messages({
"regEx ips": "[label] ip non valide",
'regEx owner': "[label] l'adresse e-mail n'est pas valide",
'regEx.0 owner': "[label] l'adresse e-mail n'est pas valide",
'regEx.1 owner': "[label] l'adresse e-mail n'est pas valide",
'regEx.2 owner': "[label] l'adresse e-mail n'est pas valide",
'regEx.3 owner': "[label] l'adresse e-mail n'est pas valide",
'regEx.4 owner': "[label] l'adresse e-mail n'est pas valide",
required: "[label] est obligatoire",
minString: "[label] must be at least [min] characters",
maxString: "[label] cannot exceed [max] characters",
minNumber: "[label] must be at least [min]",
maxNumber: "[label] cannot exceed [max]",
minDate: "[label] must be on or before [min]",
maxDate: "[label] cannot be after [max]",
minCount: "Vous devez mettre au minimum [minCount] valeure",
maxCount: "You cannot specify more than [maxCount] values",
noDecimal: "[label] must be an integer",
noUnique: "[label] doit être unique",
notAllowed: "[value] is not an allowed value",
notAllowedStatus: "Status réservé aux admins",
notAllowedField: "Champs réservé aux admins",
expectedString: "[label] must be a string",
expectedNumber: "[label] must be a number",
expectedBoolean: "[label] must be a boolean",
expectedArray: "[label] must be an array",
expectedObject: "[label] must be an object",
expectedConstructor: "[label] must be a [type]",
regEx: "[label] failed regular expression validation"
});
Apps.attachSchema(Schemas.app);