1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 2× 2× 1× 1× 3× 3× 1× 1× 1× 1× 1× | 'use strict' let _ = require('lodash') let mongoose = require('mongoose') let Bluebird = require('bluebird') let crypto = require('crypto') let Jwt = require('app/services/jwt') let Errors = require('app/errors') let Schema = mongoose.Schema let ObjectId = Schema.ObjectId const RESTRICTED = new Set(['email', 'password', 'salt']) /** * Jedi Master Schema */ var MasterSchema = new Schema({ firstName: { type: String, required: true }, lastName: String, origin: String, masters: [{ type: ObjectId, ref: 'Master' }], apprentices: [{ type: ObjectId, ref: 'Master' }], skills: [String], level: Number, email: { type: String, select: false, set: val => val.toLowerCase() }, phoneNumber: String, role: { type: String, default: 'user' }, password: { type: String, select: false }, salt: { type: String, default: function () { var now = new Date().getTime() return crypto .createHash('md5') .update('hello' + now) .digest('hex') }, select: false }, createdOn: { type: Date, default: Date.now }, updatedOn: { type: Date, default: Date.now } }) MasterSchema.virtual('token').get(function () { let payload = { _id: this._id, role: this.role } return Jwt.signToken(payload) }) MasterSchema.pre('save', function (next) { // Update the timestamp this.updatedOn = Date.now() return next() }) /** * Check if password is valid * * @param {String} password * * @return {Boolean} */ MasterSchema.methods.validatePassword = function validatePassword (password) { password = password || '' let pwHash = crypto .createHash('sha1') .update(password + this.salt) .digest('hex') return this.password === pwHash } /** * Set the password for this master * * @param {String} password */ MasterSchema.methods.setPassword = function setPassword (password) { this.password = crypto .createHash('sha1') .update(password + this.salt) .digest('hex') return this } // Convert from 'firstName,lastName' to // { firstName: 1, lastName: 1} MasterSchema.statics.reduceFields = function (fields) { return fields.split(',').reduce((memo, field) => { if (RESTRICTED.has(field)) return memo memo[field] = 1 return memo }, {}) } /** * Find a master by their email * * @param {String} email * * @return {Promise} */ MasterSchema.statics.findByEmail = function findByEmail (email) { // We need to have an email to search upon if (_.isEmpty(email)) { return Bluebird.reject(new Errors.BadData('Missing email')) } return this .findOne({ 'email': email.toLowerCase() }) .select('+email +password +salt') .then(function (master) { // If null, we didn't find a match return _.isNull(master) ? Bluebird.reject(new Errors.NotFound(`No master found with ${email}`)) : master }) } module.exports = mongoose.model('Master', MasterSchema, 'masters') |