user.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. 'use strict'
  2. /* global db, lang */
  3. const Mongoose = require('mongoose')
  4. const Promise = require('bluebird')
  5. const bcrypt = require('bcryptjs-then')
  6. const _ = require('lodash')
  7. /**
  8. * Users schema
  9. *
  10. * @type {<Mongoose.Schema>}
  11. */
  12. var userSchema = Mongoose.Schema({
  13. email: {
  14. type: String,
  15. required: true,
  16. index: true
  17. },
  18. provider: {
  19. type: String,
  20. required: true
  21. },
  22. providerId: {
  23. type: String
  24. },
  25. password: {
  26. type: String
  27. },
  28. name: {
  29. type: String
  30. },
  31. rights: [{
  32. role: String,
  33. path: String,
  34. exact: Boolean,
  35. deny: Boolean
  36. }]
  37. }, { timestamps: {} })
  38. userSchema.statics.processProfile = (profile) => {
  39. let primaryEmail = ''
  40. let name = ''
  41. if (_.isArray(profile.emails)) {
  42. let e = _.find(profile.emails, ['primary', true])
  43. primaryEmail = (e) ? e.value : _.first(profile.emails).value
  44. } else if (_.isString(profile.email) && profile.email.length > 5) {
  45. primaryEmail = profile.email
  46. } else if (_.isString(profile.mail) && profile.mail.length > 5) {
  47. primaryEmail = profile.mail
  48. } else if (profile.user && profile.user.email && profile.user.email.length > 5) {
  49. primaryEmail = profile.user.email
  50. } else if (_.isString(profile.unique_name) && profile.unique_name.length > 5) {
  51. primaryEmail = profile.unique_name
  52. } else {
  53. return Promise.reject(new Error(lang.t('auth:errors.invaliduseremail')))
  54. }
  55. profile.provider = _.lowerCase(profile.provider)
  56. primaryEmail = _.toLower(primaryEmail)
  57. if (_.has(profile, 'displayName')) {
  58. name = profile.displayName
  59. } else if (_.has(profile, 'name')) {
  60. name = profile.name
  61. } else if (_.has(profile, 'cn')) {
  62. name = profile.cn
  63. } else {
  64. name = _.split(primaryEmail, '@')[0]
  65. }
  66. return db.User.findOneAndUpdate({
  67. email: primaryEmail,
  68. provider: profile.provider
  69. }, {
  70. email: primaryEmail,
  71. provider: profile.provider,
  72. providerId: profile.id,
  73. name
  74. }, {
  75. new: true
  76. }).then((user) => {
  77. // Handle unregistered accounts
  78. if (!user && profile.provider !== 'local' && (appconfig.auth.defaultReadAccess || profile.provider === 'ldap' || profile.provider === 'azure')) {
  79. let nUsr = {
  80. email: primaryEmail,
  81. provider: profile.provider,
  82. providerId: profile.id,
  83. password: '',
  84. name,
  85. rights: [{
  86. role: 'read',
  87. path: '/',
  88. exact: false,
  89. deny: false
  90. }]
  91. }
  92. return db.User.create(nUsr)
  93. }
  94. return user || Promise.reject(new Error(lang.t('auth:errors:notyetauthorized')))
  95. })
  96. }
  97. userSchema.statics.hashPassword = (rawPwd) => {
  98. return bcrypt.hash(rawPwd)
  99. }
  100. userSchema.methods.validatePassword = function (rawPwd) {
  101. return bcrypt.compare(rawPwd, this.password).then((isValid) => {
  102. return (isValid) ? true : Promise.reject(new Error(lang.t('auth:errors:invalidlogin')))
  103. })
  104. }
  105. module.exports = Mongoose.model('User', userSchema)