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(),
117 setSessionToken: function(token
, uid
, cb
)
119 // Also empty the login token to invalidate future attempts
123 $set: { loginToken: {} },
124 $push: { sessionTokens: {
126 $slice: -7 //only allow 7 simultaneous connections per user (TODO?)
133 removeToken: function(uid
, token
, cb
)
137 { $pull: {sessionTokens: token
} },
142 // TODO: later, allow account removal
143 remove: function(uids
)
145 db
.users
.remove({_id: uids
});
148 /////////////////////
149 // ADVANCED FUNCTIONS
151 create: function(newUser
, callback
)
153 // Determine initials from name parts
154 let nameParts
= newUser
.name
.split(/[ -]+/);
155 let initials
= nameParts
.map( n
=> { return n
.charAt(0).toLowerCase(); }).join("");
156 // First retrieve all users with similar prefix initials
157 UserModel
.getInitialsByPrefix(initials
, (err
,userArray
) => {
158 if (!!userArray
&& userArray
.length
== 1)
159 initials
= initials
+ "2"; //thus number == users count for this hash
160 else if (!!userArray
&& userArray
.length
> 1)
162 // Pick the highest number after initials (if any), and increment
163 let numbers
= userArray
.map( u
=> {
164 let digitMatch
= u
.initials
.match(/[0-9]/);
166 return 1; //irrelevant
167 let firstDigit
= digitMatch
.index
;
168 return parseInt(u
.initials
.slice(digitMatch
.index
));
170 initials
= initials
+ (Math
.max(...numbers
)+1);
172 Object
.assign(newUser
, {initials: initials
});
173 UserModel
.insert(newUser
, callback
);
177 whitelistCheck: function(email
)
179 if (params
.whitelist
.length
== 0)
180 return true; //no whitelist, everyone allowed
181 for (let w
of params
.whitelist
)
183 if ((w
.indexOf('@') >= 0 && w
==email
) || !!email
.match(new RegExp(w
+"$")))
189 cleanUsersDb: function()
191 UserModel
.getUnlogged( (err
,unlogged
) => {
192 UserModel
.remove(unlogged
);
197 module
.exports
= UserModel
;