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

3 4 5
const Promise = require('bluebird')
const bcrypt = require('bcryptjs-then')
const _ = require('lodash')
6 7 8 9 10 11

/**
 * Region schema
 *
 * @type       {<Mongoose.Schema>}
 */
NGPixel's avatar
NGPixel committed
12
var userSchema = Mongoose.Schema({
13

14 15 16 17 18
  email: {
    type: String,
    required: true,
    index: true
  },
19

20 21 22 23
  provider: {
    type: String,
    required: true
  },
24

25 26 27
  providerId: {
    type: String
  },
28

29 30 31
  password: {
    type: String
  },
32

33 34 35
  name: {
    type: String
  },
36

37 38 39 40 41 42
  rights: [{
    role: String,
    path: String,
    exact: Boolean,
    deny: Boolean
  }]
43

44
}, { timestamps: {} })
45

46
userSchema.statics.processProfile = (profile) => {
47 48 49 50 51 52
  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
53 54
  } else if (_.isString(profile.mail) && profile.mail.length > 5) {
    primaryEmail = profile.mail
55 56
  } else if (profile.user && profile.user.email && profile.user.email.length > 5) {
    primaryEmail = profile.user.email
57 58 59
  } else {
    return Promise.reject(new Error('Invalid User Email'))
  }
60

61
  profile.provider = _.lowerCase(profile.provider)
62
  primaryEmail = _.toLower(primaryEmail)
63

64 65 66 67 68 69 70 71 72
  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
73
    new: true
74
  }).then((user) => {
75 76
    // Handle unregistered accounts
    if (!user && profile.provider !== 'local' && (appconfig.auth.defaultReadAccess || profile.provider === 'ldap' || profile.provider === 'azure')) {
NGPixel's avatar
NGPixel committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
      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
92
    return user || Promise.reject(new Error('You have not been authorized to login to this site yet.'))
93 94
  })
}
95 96

userSchema.statics.hashPassword = (rawPwd) => {
97 98
  return bcrypt.hash(rawPwd)
}
99

100 101 102 103 104
userSchema.methods.validatePassword = function (rawPwd) {
  return bcrypt.compare(rawPwd, this.password).then((isValid) => {
    return (isValid) ? true : Promise.reject(new Error('Invalid Login'))
  })
}
105

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