user.js 2.61 KB
Newer Older
1
'use strict'
2

3 4 5
/* global db, lang */

const Mongoose = require('mongoose')
6 7 8
const Promise = require('bluebird')
const bcrypt = require('bcryptjs-then')
const _ = require('lodash')
9 10

/**
11
 * Users schema
12 13 14
 *
 * @type       {<Mongoose.Schema>}
 */
NGPixel's avatar
NGPixel committed
15
var userSchema = Mongoose.Schema({
16

17 18 19 20 21
  email: {
    type: String,
    required: true,
    index: true
  },
22

23 24 25 26
  provider: {
    type: String,
    required: true
  },
27

28 29 30
  providerId: {
    type: String
  },
31

32 33 34
  password: {
    type: String
  },
35

36 37 38
  name: {
    type: String
  },
39

40 41 42 43 44 45
  rights: [{
    role: String,
    path: String,
    exact: Boolean,
    deny: Boolean
  }]
46

47
}, { timestamps: {} })
48

49
userSchema.statics.processProfile = (profile) => {
50 51 52 53 54 55
  let primaryEmail = ''
  if (_.isArray(profile.emails)) {
    let e = _.find(profile.emails, ['primary', true])
    primaryEmail = (e) ? e.value : _.first(profile.emails).value
  } else if (_.isString(profile.email) && profile.email.length > 5) {
    primaryEmail = profile.email
56 57
  } else if (_.isString(profile.mail) && profile.mail.length > 5) {
    primaryEmail = profile.mail
58 59
  } else if (profile.user && profile.user.email && profile.user.email.length > 5) {
    primaryEmail = profile.user.email
60
  } else {
NGPixel's avatar
NGPixel committed
61
    return Promise.reject(new Error(lang.t('auth:errors.invaliduseremail')))
62
  }
63

64
  profile.provider = _.lowerCase(profile.provider)
65
  primaryEmail = _.toLower(primaryEmail)
66

67 68 69 70 71 72 73 74 75
  return db.User.findOneAndUpdate({
    email: primaryEmail,
    provider: profile.provider
  }, {
    email: primaryEmail,
    provider: profile.provider,
    providerId: profile.id,
    name: profile.displayName || _.split(primaryEmail, '@')[0]
  }, {
NGPixel's avatar
NGPixel committed
76
    new: true
77
  }).then((user) => {
78 79
    // Handle unregistered accounts
    if (!user && profile.provider !== 'local' && (appconfig.auth.defaultReadAccess || profile.provider === 'ldap' || profile.provider === 'azure')) {
NGPixel's avatar
NGPixel committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
      let nUsr = {
        email: primaryEmail,
        provider: profile.provider,
        providerId: profile.id,
        password: '',
        name: profile.displayName || profile.name || profile.cn,
        rights: [{
          role: 'read',
          path: '/',
          exact: false,
          deny: false
        }]
      }
      return db.User.create(nUsr)
    }
NGPixel's avatar
NGPixel committed
95
    return user || Promise.reject(new Error(lang.t('auth:errors:notyetauthorized')))
96 97
  })
}
98 99

userSchema.statics.hashPassword = (rawPwd) => {
100 101
  return bcrypt.hash(rawPwd)
}
102

103 104
userSchema.methods.validatePassword = function (rawPwd) {
  return bcrypt.compare(rawPwd, this.password).then((isValid) => {
NGPixel's avatar
NGPixel committed
105
    return (isValid) ? true : Promise.reject(new Error(lang.t('auth:errors:invalidlogin')))
106 107
  })
}
108

109
module.exports = Mongoose.model('User', userSchema)