1 | /**
|
2 | * vim:set sw=2 ts=2 sts=2 ft=javascript expandtab:
|
3 | *
|
4 | * # Group 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 |
|
26 | module.exports = (function () {
|
27 | ;
|
28 |
|
29 | // Dependencies
|
30 | var ld = require('lodash');
|
31 | var hashPassword = require('./common.js').hashPassword;
|
32 |
|
33 | /**
|
34 | * ## Description
|
35 | *
|
36 | * This module regroups shared functions for group and pad models.
|
37 | */
|
38 |
|
39 | var commonGroupPad = {};
|
40 |
|
41 | /**
|
42 | * ### handlePassword
|
43 | *
|
44 | * `handlePassword` is a function that ensures if `visibility` is *private*, a
|
45 | * password has been filled. Also, it encrypts the given password with a salt,
|
46 | * using `common.hashPassword`. It takes :
|
47 | *
|
48 | * - `params` group object
|
49 | * - `callback` function, called with *null* if `visibility` isn't *private* or
|
50 | * if the password is an object, or with an error or *null* and the *password*
|
51 | * object
|
52 | */
|
53 |
|
54 | commonGroupPad.handlePassword = function (params, callback) {
|
55 | if ((params.visibility !== 'private') || ld.isObject(params.password)) {
|
56 | return callback(null);
|
57 | }
|
58 | if (!ld.isString(params.password) || ld.isEmpty(params.password)) {
|
59 | var err = new Error('BACKEND.ERROR.AUTHENTICATION.PASSWORD_INCORRECT');
|
60 | return callback(err);
|
61 | }
|
62 | hashPassword(undefined, params.password, function (err, res) {
|
63 | if (err) { return callback(err); }
|
64 | callback(null, res);
|
65 | });
|
66 | };
|
67 |
|
68 | return commonGroupPad;
|
69 |
|
70 |
|
71 | }).call(this);
|