Advertisement
Guest User

Untitled

a guest
Apr 6th, 2016
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.39 KB | None | 0 0
  1. 'use strict';
  2.  
  3. var Thing = require('../api/thing/thing.model');
  4. var User = require('../api/user/user.model');
  5. var Item = require('../api/item/item.model');
  6. var Calendar = require('../api/calendar/calendar.model');
  7.  
  8. Thing.find({}).remove(function() {});
  9.  
  10. User.find({}).remove(function() {
  11. User.create({
  12. provider: 'local',
  13. role: 'student',
  14. name: 'Student',
  15. email: 'student@test.com',
  16. password: 'test',
  17. pin: '0807'
  18. }, {
  19. provider: 'local',
  20. role: 'teacher',
  21. name: 'Teacher',
  22. email: 'teacher@test.com',
  23. password: 'test',
  24. pin: '0807'
  25. }, {
  26. provider: 'local',
  27. role: 'admin',
  28. name: 'Admin',
  29. email: 'admin@admin.com',
  30. password: 'admin',
  31. pin: '0807'
  32. }, function() {
  33. console.log('finished populating users');
  34. }, function(err) {
  35. console.log(err);
  36. });
  37. });
  38.  
  39. Calendar.find({}).remove(function() {});
  40. console.log("Removed Calendars");
  41.  
  42. Item.find({}).remove(function () {
  43. Item.create({
  44. calendarId: "dd7sfasd8f8sd",
  45. title: "title",
  46. description: "description",
  47. date: new Date(),
  48. checklists: [],
  49. attachments: [],
  50. status: "Not Completed",
  51. edit: false,
  52. verification: "test", //This will not be here in the long run
  53. verify: false
  54. }, {
  55. calendarId: "323k3k2l23lk4j4",
  56. title: "other",
  57. description: "description",
  58. date: new Date(),
  59. checklists: [],
  60. attachments: [],
  61. status: "Completed",
  62. edit: false,
  63. verification: "test", //This will not be here in the long run
  64. verify: false
  65. }, {
  66. calendarId: "323k3k2l23lk4j4",
  67. title: "title",
  68. description: "description",
  69. date: new Date(),
  70. checklists: [],
  71. attachments: [],
  72. status: "Verified",
  73. edit: false,
  74. verification: "test", //This will not be here in the long run
  75. verify: false
  76. }, {
  77. calendarId: "323k3k2l23lk4j4",
  78. title: "test",
  79. description: "description",
  80. date: new Date(),
  81. checklists: [],
  82. attachments: [],
  83. status: "Not Completed",
  84. edit: false,
  85. verification: "test", //This will not be here in the long run
  86. verify: false
  87.  
  88. }, function() {
  89. console.log('finished populating items');
  90. }
  91. );
  92.  
  93. });
  94.  
  95. Express server listening on 9000, in development mode
  96. Done waiting!
  97.  
  98. Running "open:server" (open) task
  99.  
  100. Running "watch" task
  101. Waiting...
  102. { _id: 5704a4d8b414a48822cd30a6, students: [], role: 'teacher' }
  103. [Error: Invalid or no password]
  104. finished populating items
  105.  
  106. UserSchema
  107. .pre('save', function(next) {
  108. if (!this.isNew) return next();
  109. console.log(this);
  110. if (!validatePresenceOf(this.hashedPassword) && authTypes.indexOf(this.provider) === -1) {
  111. next(new Error('Invalid or no password'));
  112. if (!validatePresenceOf(this.hashedPin))
  113. next(new Error('Invalid pin'));
  114. } else {
  115. next();
  116. }
  117. });
  118.  
  119. 'use strict';
  120.  
  121. var mongoose = require('mongoose');
  122. var Schema = mongoose.Schema;
  123. var crypto = require('crypto');
  124. var authTypes = ['github', 'twitter', 'facebook', 'google'];
  125.  
  126. var Student = new Schema({
  127. firstName: String,
  128. lastName: String,
  129. age: Number
  130. });
  131.  
  132. var UserSchema = new Schema({
  133. name: String,
  134. username: String,
  135. email: { type: String, lowercase: true },
  136. role: {
  137. type: String,
  138. default: 'teacher'
  139. },
  140. teachersEmail: { type: String, lowercase: true },
  141. students: [Student],
  142. status: String,
  143. hashedPassword: String,
  144. hashedPin: String,
  145. hasPassword: Boolean,
  146. provider: String,
  147. salt: String,
  148. pinSalt: String,
  149. facebook: {},
  150. twitter: {},
  151. google: {},
  152. github: {}
  153. });
  154.  
  155. /**
  156. * Virtuals
  157. */
  158. UserSchema
  159. .virtual('password')
  160. .set(function(password) {
  161. this._password = password;
  162. this.salt = this.makeSalt();
  163. this.hashedPassword = this.encryptPassword(password);
  164. })
  165. .get(function() {
  166. return this._password;
  167. });
  168.  
  169. UserSchema
  170. .virtual('pin')
  171. .set(function(pin) {
  172. this._pin = pin;
  173. this.pinSalt = this.makeSalt();
  174. this.hashedPin = this.encryptPin(pin);
  175. })
  176. .get(function() {
  177. return this._pin;
  178. });
  179.  
  180. // Public profile information
  181. UserSchema
  182. .virtual('profile')
  183. .get(function() {
  184. return {
  185. 'name': this.name,
  186. 'role': this.role
  187. };
  188. });
  189.  
  190. // Non-sensitive info we'll be putting in the token
  191. UserSchema
  192. .virtual('token')
  193. .get(function() {
  194. return {
  195. '_id': this._id,
  196. 'role': this.role
  197. };
  198. });
  199.  
  200. /**
  201. * Validations
  202. */
  203.  
  204. // Validate empty email
  205. UserSchema
  206. .path('email')
  207. .validate(function(email) {
  208. if (authTypes.indexOf(this.provider) !== -1) return true;
  209. return email.length;
  210. }, 'Email cannot be blank');
  211.  
  212. // Validate empty password
  213. UserSchema
  214. .path('hashedPassword')
  215. .validate(function(hashedPassword) {
  216. if (authTypes.indexOf(this.provider) !== -1) return true;
  217. return hashedPassword.length;
  218. }, 'Password cannot be blank');
  219.  
  220. // Validate empty pin
  221. UserSchema
  222. .path('hashedPin')
  223. .validate(function(hashedPin) {
  224. return hashedPin.length;
  225. }, 'PIN cannot be blank');
  226.  
  227. // Validate empty pin
  228. UserSchema
  229. .path('hashedPin')
  230. .validate(function(hashedPin) {
  231. return hashedPin.length == 4;
  232. }, 'PIN must be 4 characters in length');
  233.  
  234. // Validate email is not taken
  235. UserSchema
  236. .path('email')
  237. .validate(function(value, respond) {
  238. var self = this;
  239. this.constructor.findOne({email: value}, function(err, user) {
  240. if(err) throw err;
  241. if(user) {
  242. if(self.id === user.id) return respond(true);
  243. return respond(false);
  244. }
  245. respond(true);
  246. });
  247. }, 'The specified email address is already in use.');
  248.  
  249. var validatePresenceOf = function(value) {
  250. return value && value.length;
  251. };
  252.  
  253. /**
  254. * Pre-save hook
  255. */
  256. UserSchema
  257. .pre('save', function(next) {
  258. if (!this.isNew) return next();
  259. console.log(this);
  260. if (!validatePresenceOf(this.hashedPassword) && authTypes.indexOf(this.provider) === -1) {
  261. next(new Error('Invalid or no password'));
  262. if (!validatePresenceOf(this.hashedPin))
  263. next(new Error('Invalid pin'));
  264. } else {
  265. next();
  266. }
  267. });
  268. /**
  269. * Methods
  270. */
  271. UserSchema.methods = {
  272. /**
  273. * Authenticate - check if the passwords are the same
  274. *
  275. * @param {String} plainText
  276. * @return {Boolean}
  277. * @api public
  278. */
  279. authenticate: function(plainText) {
  280. if (this.hashedPassword) {
  281. return this.encryptPassword(plainText) === this.hashedPassword;
  282. } else {
  283. return !!(this.google || this.facebook);
  284. }
  285. },
  286.  
  287. verify: function(plainText) {
  288. return this.encryptPin(plainText) === this.hashedPin;
  289. },
  290.  
  291. /**
  292. * Make salt
  293. *
  294. * @return {String}
  295. * @api public
  296. */
  297. makeSalt: function() {
  298. return crypto.randomBytes(16).toString('base64');
  299. },
  300.  
  301. /**
  302. * Encrypt password
  303. *
  304. * @param {String} password
  305. * @return {String}
  306. * @api public
  307. */
  308. encryptPassword: function(password) {
  309. if (!password || !this.salt) return '';
  310. var salt = new Buffer(this.salt, 'base64');
  311. return crypto.pbkdf2Sync(password, salt, 10000, 64).toString('base64');
  312. },
  313.  
  314. encryptPin: function(pin) {
  315. if (!pin || !this.pinSalt) return '';
  316. var pinSalt = new Buffer(this.pinSalt, 'base64');
  317. return crypto.pbkdf2Sync(pin, pinSalt, 10000, 64).toString('base64');
  318. }
  319. };
  320.  
  321. module.exports = mongoose.model('User', UserSchema);
  322.  
  323. User.find({}).remove(function() {
  324. User.create({
  325. provider: 'local',
  326. role: 'student',
  327. name: 'Student',
  328. email: 'student@test.com',
  329. password: 'test',
  330. pin: '0807'
  331. }, function() {
  332. console.log('Added user');
  333. }, function(err) {
  334. console.log(err);
  335. });
  336.  
  337. User.create({
  338. provider: 'local',
  339. role: 'teacher',
  340. name: 'Teacher',
  341. email: 'teacher@test.com',
  342. password: 'test',
  343. pin: '0807'
  344. }, function() {
  345. console.log('Added user');
  346. }, function(err) {
  347. console.log(err);
  348. });
  349. User.create({
  350. provider: 'local',
  351. role: 'admin',
  352. name: 'Admin',
  353. email: 'admin@admin.com',
  354. password: 'admin',
  355. pin: '0807'
  356. }, function() {
  357. console.log('Added user');
  358. }, function(err) {
  359. console.log(err);
  360. });
  361. });
  362.  
  363. Running "watch" task
  364. Completed in 2.258s at Wed Apr 06 2016 00:08:34 GMT-0600 (Mountain Daylight Time) - Waiting...
  365. { _id: 5704a7e2d85623902a78e1fc, students: [], role: 'teacher' }
  366. [Error: Invalid or no password]
  367. { _id: 5704a7e2d85623902a78e1fe, students: [], role: 'teacher' }
  368. [Error: Invalid or no password]
  369. { _id: 5704a7e2d85623902a78e200, students: [], role: 'teacher' }
  370. [Error: Invalid or no password]
  371. finished populating items
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement