Commit | Line | Data |
---|---|---|
e99c53fb BA |
1 | const UserEntity = require("../entities/user"); |
2 | const params = require("../config/parameters"); | |
3 | ||
4 | const UserModel = | |
5 | { | |
6 | create: function(newUser, callback) | |
7 | { | |
b3540dbb | 8 | // Determine initials from name parts |
e99c53fb | 9 | let nameParts = newUser.name.split(/[ -]+/); |
b3540dbb | 10 | let initials = nameParts.map( n => { return n.charAt(0).toLowerCase(); }).join(""); |
e99c53fb BA |
11 | // First retrieve all users with similar prefix initials |
12 | UserEntity.getInitialsByPrefix(initials, (err,userArray) => { | |
13 | if (!!userArray && userArray.length == 1) | |
14 | initials = initials + "2"; //thus number == users count for this hash | |
15 | else if (!!userArray && userArray.length > 1) | |
16 | { | |
17 | // Pick the highest number after initials (if any), and increment | |
18 | let numbers = userArray.map( u => { | |
19 | let digitMatch = u.initials.match(/[0-9]/); | |
20 | if (!digitMatch) | |
21 | return 1; //irrelevant | |
22 | let firstDigit = digitMatch.index; | |
23 | return parseInt(u.initials.slice(digitMatch.index)); | |
24 | }); | |
25 | initials = initials + (Math.max(...numbers)+1); | |
26 | } | |
27 | Object.assign(newUser, {initials: initials}); | |
28 | UserEntity.insert(newUser, callback); | |
29 | }); | |
30 | }, | |
31 | ||
32 | whitelistCheck: function(email) | |
33 | { | |
34 | if (params.whitelist.length == 0) | |
35 | return true; //no whitelist, everyone allowed | |
36 | for (let w of params.whitelist) | |
37 | { | |
38 | if ((w.indexOf('@') >= 0 && w==email) || !!email.match(new RegExp(w+"$"))) | |
39 | return true; | |
40 | } | |
41 | return false; | |
42 | }, | |
43 | ||
44 | logout: function(uid, token, cb) | |
45 | { | |
46 | UserEntity.removeToken(uid, token, cb); | |
47 | }, | |
48 | ||
49 | cleanUsersDb: function() | |
50 | { | |
51 | UserEntity.getUnlogged( (err,unlogged) => { | |
52 | UserEntity.remove(unlogged); | |
53 | }); | |
54 | }, | |
55 | } | |
56 | ||
57 | module.exports = UserModel; |