Guest User

Untitled

a guest
Feb 19th, 2018
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. var mongoose = require('mongoose')
  2. , Schema = mongoose.Schema
  3. , crypto = require('crypto')
  4. , sys = require('sys')
  5. , User
  6. ;
  7.  
  8. function defineModels(mongoose, fn) {
  9. var Schema = mongoose.Schema
  10. , ObjectId = Schema.ObjectId
  11. ;
  12.  
  13. function validatePresenceOf(value) {
  14. return value && value.length;
  15. }
  16.  
  17. function trim (str) {
  18. var str = str.replace(/^\s\s*/, '')
  19. , ws = /\s/
  20. , i = str.length;
  21. while (ws.test(str.charAt(--i)));
  22. return str.slice(0, i + 1);
  23. }
  24.  
  25. var User = new Schema ({
  26. 'email' : { type: String, set: trim, validate: [validatePresenceOf, 'an email is required'], index: { unique: true } }
  27. , 'hashed_password': String
  28. , 'salt': String
  29. , 'username': { type: String, set: trim, validate: [validatePresenceOf, 'a username is required'], index: { unique: true } }
  30. , 'fullname': { type: String, set: trim }
  31. , 'homepage': { type: String, set: trim }
  32. , 'prefs' : {
  33. 'sharing' : { type: Boolean, default: true }
  34. , 'https' : { type: Boolean, default: false }
  35. }
  36. , 'role' : { type: String, enum: [ 'admin', 'user' ], default: 'user' }
  37. , 'date': Date
  38. , 'deleted': { type: Boolean, default: false }
  39. , 'count' : { type: Number, min: 0, default: 1 }
  40. }
  41. , {use$SetOnSave: false}
  42. );
  43.  
  44. User.path('date')
  45. .default(function() {
  46. return new Date()
  47. })
  48. .set(function(v){
  49. return v == 'now' ? new Date() : v;
  50. });
  51.  
  52. User.virtual('id')
  53. .get(function() {
  54. return this._id.toHexString();
  55. });
  56.  
  57. User.virtual('password')
  58. .set(function(password) {
  59. this._password = password;
  60. this.salt = this.makeSalt();
  61. this.hashed_password = this.encryptPassword(password);
  62. })
  63. .get(function() { return this._password; });
  64.  
  65. User.method('authenticate', function(plainText) {
  66. return this.encryptPassword(plainText) === this.hashed_password;
  67. });
  68.  
  69. User.method('makeSalt', function() {
  70. return Math.round((new Date().valueOf() * Math.random())) + '';
  71. });
  72.  
  73. User.method('encryptPassword', function(password) {
  74. return crypto.createHmac('sha1', this.salt).update(password).digest('hex');
  75. });
  76.  
  77. User.pre('save', function(next) {
  78. if (!this.isNew) next();
  79. if (!validatePresenceOf(this.password)) {
  80. next(new Error('Invalid password'));
  81. } else {
  82. next();
  83. }
  84. });
  85.  
  86. mongoose.model('User', User);
  87. fn();
  88. };
  89.  
  90. defineModels(mongoose, function() {
  91. User = mongoose.model('User');
  92. db = mongoose.connect('mongodb://localhost/use-set-test');
  93. });
  94.  
  95. User.findById( process.argv[2], function(err, doc) {
  96. if (err) sys.puts('Error: '+ JSON.stringify(err));
  97. else if (doc) {
  98. sys.puts('Original user: '+ JSON.stringify(doc));
  99. var newStuff = {};
  100. newStuff.email = 'dan@bar.com';
  101. newStuff.prefs = { sharing: false, https: true };
  102. Object.keys(newStuff).forEach (function(key) {
  103. if (doc[key] != newStuff[key]) doc[key] = newStuff[key];
  104. });
  105. doc.save(function(err){
  106. if(!err) {
  107. sys.puts('Updated user per doc.save(): '+ JSON.stringify(doc));
  108. User.find( {}, function(err2, doc2) {
  109. sys.puts('Actual doc...: '+ JSON.stringify(doc2));
  110. process.exit(0);
  111. });
  112. }
  113. else sys.puts('Error: '+ JSON.stringify(err));
  114. });
  115. } else sys.puts('User '+process.argv[2]+' not found');
  116. });
Add Comment
Please, Sign In to add comment