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