user.js 3.1 KB

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