f3acd1c5e5898cfa1e4008f4d2bb8938a95efcc5
1 const params
= require("../config/parameters");
2 const db
= require("../utils/database");
9 * ** Strings, identification informations:
12 * initials : computed, Benjamin Auder --> ba ...etc
15 * timestamp: datetime (validity)
16 * ip: address of requesting machine
18 * sessionTokens (array): cookie identification
24 getInitialsByPrefix: function(prefix
, cb
)
27 { initials: new RegExp("^" + prefix
) },
28 { initials: 1, _id: 0 },
33 insert: function(newUser
, cb
)
35 db
.users
.insert(Object
.assign({},
45 getByLoginToken: function(token
, cb
)
48 { "loginToken.value": token
},
53 getBySessionToken: function(token
, cb
)
56 { sessionTokens: token
},
61 getById: function(uid
, cb
)
69 getByEmail: function(email
, cb
)
77 getByInitials: function(initials
, cb
)
80 { initials: initials
},
85 getUnlogged: function(cb
)
87 var tsNow
= new Date().getTime();
88 // 86400000 = 24 hours in milliseconds
90 db
.users
.find({}, (err
,userArray
) => {
91 let unlogged
= userArray
.filter( u
=> {
92 return u
.sessionTokens
.length
==0 && u
._id
.getTimestamp().getTime() + day
< tsNow
;
100 db
.users
.find({}, cb
);
103 setLoginToken: function(token
, uid
, ip
, cb
)
107 { $set: { loginToken: {
109 timestamp: new Date().getTime(),
116 setSessionToken: function(token
, uid
, cb
)
118 // Also empty the login token to invalidate future attempts
122 $set: { loginToken: {} },
123 $push: { sessionTokens: {
125 $slice: -7 //only allow 7 simultaneous connections per user (TODO?)
132 removeToken: function(uid
, token
, cb
)
136 { $pull: {sessionTokens: token
} },
141 // TODO: later, allow account removal
142 remove: function(uids
)
144 db
.users
.remove({_id: uids
});
147 /////////////////////
148 // ADVANCED FUNCTIONS
150 create: function(newUser
, callback
)
152 // Determine initials from name parts
153 let nameParts
= newUser
.name
.split(/[ -]+/);
154 let initials
= nameParts
.map( n
=> { return n
.charAt(0).toLowerCase(); }).join("");
155 // First retrieve all users with similar prefix initials
156 UserModel
.getInitialsByPrefix(initials
, (err
,userArray
) => {
157 if (!!userArray
&& userArray
.length
== 1)
158 initials
= initials
+ "2"; //thus number == users count for this hash
159 else if (!!userArray
&& userArray
.length
> 1)
161 // Pick the highest number after initials (if any), and increment
162 let numbers
= userArray
.map( u
=> {
163 let digitMatch
= u
.initials
.match(/[0-9]/);
165 return 1; //irrelevant
166 let firstDigit
= digitMatch
.index
;
167 return parseInt(u
.initials
.slice(digitMatch
.index
));
169 initials
= initials
+ (Math
.max(...numbers
)+1);
171 Object
.assign(newUser
, {initials: initials
});
172 UserModel
.insert(newUser
, callback
);
176 whitelistCheck: function(email
)
178 if (params
.whitelist
.length
== 0)
179 return true; //no whitelist, everyone allowed
180 for (let w
of params
.whitelist
)
182 if ((w
.indexOf('@') >= 0 && w
==email
) || !!email
.match(new RegExp(w
+"$")))
188 cleanUsersDb: function()
190 UserModel
.getUnlogged( (err
,unlogged
) => {
191 UserModel
.remove(unlogged
);
196 module
.exports
= UserModel
;