You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
3.1 KiB

  1. "use strict";
  2. var modb = require('mongoose');
  3. var bcrypt = require('bcryptjs-then');
  4. var Promise = require('bluebird');
  5. var _ = require('lodash');
  6. /**
  7. * User Schema
  8. *
  9. * @type {Object}
  10. */
  11. var userSchema = modb.Schema({
  12. email: {
  13. type: String,
  14. required: true,
  15. index: true,
  16. minlength: 6
  17. },
  18. password: {
  19. type: String,
  20. required: true
  21. },
  22. firstName: {
  23. type: String,
  24. required: true,
  25. minlength: 1
  26. },
  27. lastName: {
  28. type: String,
  29. required: true,
  30. minlength: 1
  31. },
  32. timezone: {
  33. type: String,
  34. required: true,
  35. default: 'UTC'
  36. },
  37. lang: {
  38. type: String,
  39. required: true,
  40. default: 'en'
  41. },
  42. rights: [{
  43. type: String,
  44. required: true
  45. }]
  46. },
  47. {
  48. timestamps: {}
  49. });
  50. /**
  51. * VIRTUAL - Full Name
  52. */
  53. userSchema.virtual('fullName').get(function() {
  54. return this.firstName + ' ' + this.lastName;
  55. });
  56. /**
  57. * INSTANCE - Validate password against hash
  58. *
  59. * @param {string} uPassword The user password
  60. * @return {Promise<Boolean>} Promise with valid / invalid boolean
  61. */
  62. userSchema.methods.validatePassword = function(uPassword) {
  63. let self = this;
  64. return bcrypt.compare(uPassword, self.password);
  65. };
  66. /**
  67. * MODEL - Generate hash from password
  68. *
  69. * @param {string} uPassword The user password
  70. * @return {Promise<String>} Promise with generated hash
  71. */
  72. userSchema.statics.generateHash = function(uPassword) {
  73. return bcrypt.hash(uPassword, 10);
  74. };
  75. /**
  76. * MODEL - Create a new user
  77. *
  78. * @param {Object} nUserData User data
  79. * @return {Promise} Promise of the create operation
  80. */
  81. userSchema.statics.new = function(nUserData) {
  82. let self = this;
  83. return self.generateHash(nUserData.password).then((passhash) => {
  84. return this.create({
  85. _id: db.ObjectId(),
  86. email: nUserData.email,
  87. firstName: nUserData.firstName,
  88. lastName: nUserData.lastName,
  89. password: passhash,
  90. rights: ['admin']
  91. });
  92. });
  93. };
  94. /**
  95. * MODEL - Edit a user
  96. *
  97. * @param {String} userId The user identifier
  98. * @param {Object} data The user data
  99. * @return {Promise} Promise of the update operation
  100. */
  101. userSchema.statics.edit = function(userId, data) {
  102. let self = this;
  103. // Change basic info
  104. let fdata = {
  105. email: data.email,
  106. firstName: data.firstName,
  107. lastName: data.lastName,
  108. timezone: data.timezone,
  109. lang: data.lang,
  110. rights: data.rights
  111. };
  112. let waitTask = null;
  113. // Change password?
  114. if(!_.isEmpty(data.password) && _.trim(data.password) !== '********') {
  115. waitTask = self.generateHash(data.password).then((passhash) => {
  116. fdata.password = passhash;
  117. return fdata;
  118. });
  119. } else {
  120. waitTask = Promise.resolve(fdata);
  121. }
  122. // Update user
  123. return waitTask.then((udata) => {
  124. return this.findByIdAndUpdate(userId, udata, { runValidators: true });
  125. });
  126. };
  127. /**
  128. * MODEL - Delete a user
  129. *
  130. * @param {String} userId The user ID
  131. * @return {Promise} Promise of the delete operation
  132. */
  133. userSchema.statics.erase = function(userId) {
  134. return this.findByIdAndRemove(userId);
  135. };
  136. module.exports = modb.model('User', userSchema);