UNPKG

4.33 kBJavaScriptView Raw
1/**
2* vim:set sw=2 ts=2 sts=2 ft=javascript expandtab:
3*
4* # User Model
5*
6* ## License
7*
8* Licensed to the Apache Software Foundation (ASF) under one
9* or more contributor license agreements. See the NOTICE file
10* distributed with this work for additional information
11* regarding copyright ownership. The ASF licenses this file
12* to you under the Apache License, Version 2.0 (the
13* "License"); you may not use this file except in compliance
14* with the License. You may obtain a copy of the License at
15*
16* http://www.apache.org/licenses/LICENSE-2.0
17*
18* Unless required by applicable law or agreed to in writing,
19* software distributed under the License is distributed on an
20* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21* KIND, either express or implied. See the License for the
22* specific language governing permissions and limitations
23* under the License.
24*/
25
26module.exports = (function () {
27 'use strict';
28
29 // Dependencies
30 require('../helpers.js'); // Helpers auto-init
31 var ld = require('lodash');
32 var conf = require('../configuration.js');
33 var storage = require('../storage.js');
34 var UPREFIX = storage.DBPREFIX.USER;
35
36 /**
37 * ## Description
38 *
39 * The `userCache` is the masterpiece of the MyPads plugin.
40 *
41 * It initially contains :
42 *
43 * - `logins`, an in memory object to map `_id` to `login` field and ensures
44 * uniqueness of logins
45 * - `emails`, which have the same purpose for emails
46 * - `firstname`, to ease search with firstname
47 * - `lastname`, to ease search with lastname
48 */
49
50 var userCache = { logins: {}, emails: {}, firstname: {}, lastname: {}, userCacheReady: false };
51
52 userCache.fn = {};
53
54 /**
55 * ## Public Functions
56 *
57 * ### init
58 *
59 * `init` is a function that is called once at the initialization of mypads
60 * and loops over all users to map their *login* to their *_id* and then
61 * ensures uniqueness.
62 *
63 * It takes a callback function which is returned with *null* when finished.
64 */
65
66 userCache.init = function (callback) {
67 storage.db.findKeys(UPREFIX + '*', null, function (err, keys) {
68 if (err) { return callback(err); }
69 // If you want to delay the user cache readyness (to test #141 for example),
70 // put storage.fn.getKeys function below in
71 // setTimeout(function() { }, 30000);
72 // (NB: won't work with mockupserver since it waits for cache to be
73 // ready before providing the web interface)
74 storage.fn.getKeys(keys, function (err, results) {
75 if (results) {
76 var memo = ld.reduce(results, function (memo, val, key) {
77 if (val) {
78 var k = key.replace(UPREFIX, '');
79 memo.logins[val.login] = k;
80 var email = (conf.get('insensitiveMailMatch')) ? val.email.toLowerCase() : val.email;
81 memo.emails[email] = k;
82 memo.firstname[k] = val.firstname;
83 memo.lastname[k] = val.lastname;
84 }
85 return memo;
86 }, { logins: {}, emails: {}, firstname: {}, lastname: {} });
87 memo.userCacheReady = true;
88 ld.assign(userCache, memo);
89 }
90 callback(null);
91 });
92 });
93 };
94
95 /**
96 * ### getIdsFromLoginsOrEmails
97 *
98 * `getIdsFromLoginsOrEmails` is a private synchronous function that checks
99 * if given data, users or admins logins or emails, are correct and transforms
100 * it to expected values : unique identifiers, before saving it to database.
101 *
102 * It takes an array of users `logins` and `emails`.
103 * It returns an object with :
104 *
105 * - `uids` for found users
106 * - `present` users logins or emails
107 * - `absent` users logins or emails
108 */
109
110 userCache.fn.getIdsFromLoginsOrEmails = function (loginsMails) {
111 if (!ld.isArray(loginsMails)) {
112 throw new TypeError('BACKEND.ERROR.TYPE.LOGINS_ARR');
113 }
114 return ld.reduce(loginsMails, function (memo, lm) {
115 var email = (conf.get('insensitiveMailMatch')) ? lm.toLowerCase() : lm;
116 var uid = userCache.logins[lm] || userCache.emails[email];
117 if (uid) {
118 memo.uids.push(uid);
119 memo.present.push(lm);
120 } else {
121 memo.absent.push(lm);
122 }
123 return memo;
124 }, { uids: [], present: [], absent: [] });
125 };
126
127 return userCache;
128
129}).call(this);