diff options
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/Access.js | 26 | ||||
-rw-r--r-- | app/models/Project.js | 18 | ||||
-rw-r--r-- | app/models/User.js | 40 | ||||
-rw-r--r-- | app/models/pPost.js | 33 |
4 files changed, 56 insertions, 61 deletions
diff --git a/app/models/Access.js b/app/models/Access.js index f4bee92..aa48340 100644 --- a/app/models/Access.js +++ b/app/models/Access.js @@ -19,13 +19,13 @@ var mongoose = require('mongoose') */ var AccessSchema = new Schema({ - user: { type: Schema.ObjectId, ref: 'User' }, - creator: { type: Schema.ObjectId, ref: 'User' }, - project: { type: Schema.ObjectId, ref: 'Project' }, - permissions: { type: Number, default: '3' }, - randomToken: { type: String }, - created: { type: Date, default: Date.now }, - updated: { type: Date, default: Date.now } + user: { type: Schema.ObjectId, ref: 'User' } + , creator: { type: Schema.ObjectId, ref: 'User' } + , project: { type: Schema.ObjectId, ref: 'Project' } + , permissions: { type: Number, default: '3' } + , randomToken: String + , created: { type: Date, default: Date.now } + , updated: { type: Date, default: Date.now } }); @@ -38,7 +38,6 @@ AccessSchema.methods = { * * @param {Number} length * @return {String} - * @api public */ generateRandomToken: function(length) { @@ -58,15 +57,15 @@ AccessSchema.statics = { /** * Load ALL accesses for a single user + * This will load all the persmissions a user has, and include info about the projects * * @param {ObjectId} id * @param {Function} callback - * @api private */ loadUser: function(id, callback) { this.find({ user: id }) - .populate('project') + .populate('project') // will joins the data from the project .sort({ 'created': -1 }) // sort by date .exec(callback); }, @@ -77,7 +76,6 @@ AccessSchema.statics = { * * @param {ObjectId} project * @param {Function} callback - * @api private */ loadProject: function(project, callback) { @@ -90,10 +88,11 @@ AccessSchema.statics = { /** * Load all users associated with several projects + * This is useful for the dashboard, where all the projects of a user is loaded. This way + * we can print the name of the participants in each project the user has access to. * - * @param {Arrau[ObjectId]} projects + * @param {Array[ObjectId]} projects * @param {Function} callback - * @api private */ loadProjects: function(projects, callback) { @@ -111,7 +110,6 @@ AccessSchema.statics = { * @param {ObjectId} project * @param {Number} permissisons * @param {Function} callback - * @api private */ checkAccess: function(user, project, permissions, callback) { diff --git a/app/models/Project.js b/app/models/Project.js index ba0e8e7..5f8e196 100644 --- a/app/models/Project.js +++ b/app/models/Project.js @@ -7,14 +7,14 @@ var mongoose = require('mongoose') , Schema = mongoose.Schema; var ProjectSchema = new Schema({ - user: { type: Schema.ObjectId, ref: 'User' }, - name: { type: String, default: '', trim: true }, - description: {type: String, default: '', trim: true }, - currency: { type: String, default: 'kr', trim: true }, - public: { type: String, default: 'invite-only' }, - created: { type: Date, default: Date.now }, - updated: { type: Date, default: Date.now }, - shortURL: { type: String, unique: true } + user: { type: Schema.ObjectId, ref: 'User' } + , name: { type: String, default: '', trim: true } + , description: { type: String, default: '', trim: true } + , currency: { type: String, default: 'kr', trim: true } + , public: { type: String, default: 'invite-only' } + , created: { type: Date, default: Date.now } + , updated: { type: Date, default: Date.now } + , shortURL: { type: String, unique: true } }); // the four validations below only apply if you are signing up traditionally @@ -25,6 +25,8 @@ ProjectSchema.path('name').validate(function(name) { }, 'Project name cannot be blank'); + +// This is a pre-save-hook. It runs every time an object is saved. ProjectSchema.pre('save', function(next) { if (this.shortURL !== undefined) return next(); var chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; diff --git a/app/models/User.js b/app/models/User.js index 668b36f..c07a298 100644 --- a/app/models/User.js +++ b/app/models/User.js @@ -20,19 +20,19 @@ var mongoose = require('mongoose') */ var UserSchema = new Schema({ - name: String, - email: { type: String, unique: true }, - username: String, - provider: String, - hashed_password: String, - salt: String, - accessToken: String, - facebook: {}, - twitter: {}, - status: { type: Number, default: 2 }, - randomToken: String, - created: { type: Date, default: Date.now }, - updated: { type: Date, default: Date.now } + name: String + , email: { type: String, unique: true } + , username: String + , provider: String + , hashed_password: String + , salt: String + , accessToken: String + , facebook: {} + , twitter: {} + , status: { type: Number, default: 2 } + , randomToken: String + , created: { type: Date, default: Date.now } + , updated: { type: Date, default: Date.now } }); @@ -108,19 +108,19 @@ UserSchema.methods = { */ authenticate: function(plainText) { - return this.encryptPassword(plainText) === this.hashed_password; + return this.encryptPassword(plainText) === this.hashed_password; // will return true or false }, /** * Make salt + * This is used to make the password hash cryptographically stronger * * @return {String} - * @api public */ makeSalt: function() { - return Math.round((new Date().valueOf() * Math.random())) + ''; + return Math.round((new Date().valueOf() * Math.random())) + ''; // valueOf date and random number = random stuff! }, @@ -129,12 +129,12 @@ UserSchema.methods = { * * @param {String} password * @return {String} - * @api public */ encryptPassword: function(password) { if (!password) return ''; - return crypto.createHmac('sha1', this.salt).update(password).digest('hex'); + // if the user isn't registered, he has no salt. Therefore he can not log in. He has to use his email. + return crypto.createHmac('sha1', this.salt || Math.random() + Math.random()).update(password).digest('hex'); }, @@ -144,13 +144,12 @@ UserSchema.methods = { * @param {Number} length * @param {Boolean} noDate * @return {String} - * @api public */ generateRandomToken: function(length, noDate) { if (typeof(length) === undefined) length = 16; // default length of token var chars = '_-abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890' - , token = noDate ? '' : new Date().getTime() + '_'; + , token = noDate ? '' : new Date().getTime() + '_'; // if noDate is provided as true, the random token will not start with a timestamp. for (var i = 0; i < length; i++) { var x = Math.floor(Math.random() * chars.length); token += chars.charAt(x); @@ -166,7 +165,6 @@ UserSchema.statics = { * * @param {String} email * @param {Function} callback - * @api private */ loadUser: function(email, callback) { diff --git a/app/models/pPost.js b/app/models/pPost.js index 1f53984..d138c8f 100644 --- a/app/models/pPost.js +++ b/app/models/pPost.js @@ -7,18 +7,18 @@ var mongoose = require('mongoose') , Schema = mongoose.Schema; var pPostSchema = new Schema({ - user: { type: Schema.ObjectId, ref: 'User' }, - for: { type: Schema.ObjectId, ref: 'User' }, - project: { type: Schema.ObjectId, ref: 'Project' }, - what: { type: String, default: '', trim: true }, - comment: { type: String, default: '', trim: true }, - participants: [], - value: { type: Number, defailt: 0 }, - file: { type: String, default: '', trim: true }, - currency: { type: String, default: 'kr', trim: true }, - created: { type: Date, default: Date.now }, - updated: { type: Date, default: Date.now }, - when: { type: Date, default: Date.now } + user: { type: Schema.ObjectId, ref: 'User' } + , for: { type: Schema.ObjectId, ref: 'User' } + , project: { type: Schema.ObjectId, ref: 'Project' } + , what: { type: String, default: '', trim: true } + , comment: { type: String, default: '', trim: true } + , participants: [] + , value: { type: Number, default: 0 } + , file: { type: String, default: '', trim: true } + , currency: { type: String, default: 'kr', trim: true } + , created: { type: Date, default: Date.now } + , updated: { type: Date, default: Date.now } + , when: { type: Date, default: Date.now } }); @@ -31,12 +31,11 @@ pPostSchema.statics = { * * @param {ObjectId} id * @param {Function} callback - * @api private */ load: function(id, callback) { this.findOne({ _id: id }) - .populate({ path: 'user', select: '_id, name'}) + .populate({ path: 'user', select: '_id, name'}) // include name of the user who posted it .exec(callback); }, @@ -46,23 +45,21 @@ pPostSchema.statics = { * * @param {ObjectId} project * @param {Function} callback - * @api private */ loadProject: function(project, callback) { this.find({ project: project }) - .populate('user') + .populate({ path: 'user', select: '_id, name'}) .sort({ 'when': -1, 'created': -1 }) .exec(callback); }, /** - * Find last ten posts belonging projects a user is part of, by project ids + * Find all posts that belongs to projects provided in an array * * @param {Array[ObjectId]} projects * @param {Function} callback - * @api private */ loadByProjects: function(projects, callback) { |