Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. module.exports = {
  2.  
  3. attributes: {
  4. firstName: {
  5. type: 'string',
  6. required: true
  7. },
  8. lastName: {
  9. type: 'string',
  10. required: true
  11. },
  12. fullName: function() {
  13. return this.firstName + " " + this.lastName;
  14. },
  15. email: {
  16. type: 'email',
  17. required: true,
  18. unique: true
  19. },
  20. encryptedPassword: {
  21. type: 'string'
  22. },
  23. role: {
  24. model: 'role'
  25. },
  26. groups: {
  27. collection: 'group',
  28. via: 'users'
  29. }
  30. },
  31.  
  32. toJSON: function() {
  33. var obj = this.toObject();
  34. delete obj.password;
  35. delete obj.confirmation;
  36. delete obj._csrf;
  37. return obj;
  38. },
  39.  
  40. beforeCreate: function (values, next) {
  41. // Makes sure the password and password confirmation match
  42. if (!values.password || values.password != values.confirmation) {
  43. return next({err: ['Password does not match password confirmation.']});
  44. }
  45.  
  46. // Encrypts the password/confirmation to be stored in the db
  47. require('bcrypt').hash(values.password, 10, function passwordEncrypted(err, encryptedPassword) {
  48. values.encryptedPassword = encryptedPassword;
  49.  
  50. next();
  51. });
  52. }
  53. };
  54.  
  55. module.exports = {
  56.  
  57. attributes: {
  58. name: {
  59. type: 'string',
  60. required: true,
  61. unique: true
  62. },
  63. users: {
  64. collection: 'user',
  65. via: 'role'
  66. },
  67. permissions: {
  68. collection: 'permission',
  69. via: 'roles',
  70. dominant: true
  71. }
  72. }
  73. };
  74.  
  75. User.findOne(1).populate('role').exec(function(err, user) {
  76.  
  77. if (err) {throw new Error(err);}
  78. console.log(user.role.name);
  79.  
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement