From d556ef525f593ccb8c688e340f70659849ac74a3 Mon Sep 17 00:00:00 2001 From: Dennis Eriksen Date: Tue, 30 Apr 2013 14:41:30 +0200 Subject: renamed files to match model names --- models/Access.js | 42 ++++++++++++++++++ models/Project.js | 67 ++++++++++++++++++++++++++++ models/User.js | 128 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ models/access.js | 42 ------------------ models/project.js | 67 ---------------------------- models/user.js | 128 ------------------------------------------------------ 6 files changed, 237 insertions(+), 237 deletions(-) create mode 100644 models/Access.js create mode 100644 models/Project.js create mode 100644 models/User.js delete mode 100644 models/access.js delete mode 100644 models/project.js delete mode 100644 models/user.js (limited to 'models') diff --git a/models/Access.js b/models/Access.js new file mode 100644 index 0000000..ce75f8c --- /dev/null +++ b/models/Access.js @@ -0,0 +1,42 @@ + +/** + * Module dependencies + */ + +var mongoose = require('mongoose') + , Schema = mongoose.Schema; + +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' }, + created: { type: Date, default: Date.now }, + updated: { type: Date, default: Date.now } +}); + +// the four validations below only apply if you are signing up traditionally + +AccessSchema.statics = { + + log: function() { + console.log('wat. wat logged this'); + }, + + /** + * Load ALL accesses for a single user + * + * @param {ObjectId} id + * @param {Function} callback + * @api private + */ + + loadUser: function(id, callback) { + this.find({ user: id }) + .populate('project') + .sort({ 'created': -1 }) // sort by date + .exec(callback); + } +} + +mongoose.model('Access', AccessSchema); diff --git a/models/Project.js b/models/Project.js new file mode 100644 index 0000000..f29c7f8 --- /dev/null +++ b/models/Project.js @@ -0,0 +1,67 @@ + +/** + * Module dependencies + */ + +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 } +}); + +// the four validations below only apply if you are signing up traditionally + +ProjectSchema.path('name').validate(function(name) { + // if you're authenticated by any of the oauth strategies (facebook, twitter), don't validate + return name.length; +}, 'Project name cannot be blank'); + + +ProjectSchema.pre('save', function(next) { + if (this.shortURL !== undefined) return next(); + var chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; + this.shortURL = ''; + for (var i = 0; i < 6; i++) { + var x = Math.floor(Math.random() * chars.length); + this.shortURL += chars.charAt(x); + } + console.log('SHORT: ' + this.shortURL); + next(); +}); + + + +ProjectSchema.statics = { + + log: function() { + console.log('wat. wat logged this'); + }, + + + + /** + * Find project by id + * + * @param {ObjectId} id + * @param {Function} callback + * @api private + */ + + load: function(id, callback) { + this.findOne({ _id: id }) + .populate('user') + .exec(callback); + } + + +} + +mongoose.model('Project', ProjectSchema); diff --git a/models/User.js b/models/User.js new file mode 100644 index 0000000..b61f7c9 --- /dev/null +++ b/models/User.js @@ -0,0 +1,128 @@ + +/** + * Module dependencies + */ + +var mongoose = require('mongoose') + , Schema = mongoose.Schema + , crypto =require('crypto') + , authTypes = ['facebook', 'twitter']; + + +/** + * User schema + */ + +var UserSchema = new Schema({ + name: String, + email: String, + username: String, + provider: String, + hashed_password: String, + salt: String, + facebook: {}, + twitter: {} +}); + +/** + * Virtuals + */ + +UserSchema + .virtual('password') + .set(function(password) { + this._password = password + this.salt = this.makeSalt() + this.hashed_password = this.encryptPassword(password) + }).get(function() { return this._password }); + +/** + * Validations + */ +var validatePrecenceOf = function (value) { + return value && value.length; +} + +// the four validations below only apply if you are signing up traditionally + +UserSchema.path('name').validate(function(name) { + // if you're authenticated by any of the oauth strategies (facebook, twitter), don't validate + if(authTypes.indexOf(this.provider) !== -1) return true; + return name.length; +}, 'Name cannot be blank'); + +UserSchema.path('email').validate(function(email) { + if(authTypes.indexOf(this.provider) !== -1) return true; + return email.length; +}, 'Email cannot be blank'); + +UserSchema.path('username').validate(function(username) { + if(authTypes.indexOf(this.provider) !== -1) return true; + return username.length; +}, 'Username cannot be blank'); + +UserSchema.path('hashed_password').validate(function(hashed_password) { + if(authTypes.indexOf(this.provider) !== -1) return true; + return hashed_password.length; +}, 'Password cannot be blank'); + +/** + * Pre-save hook + */ + +UserSchema.pre('save', function(next) { + if (!this.isNew) return next(); + + if(!validatePrecenceOf(this.password) + && authTypes.indexOf(this.provider) === -1) + next(new Error('Invalid password')); + else next(); +}); + + +/** + * Methods + */ + +UserSchema.methods = { + + /** + * Authenticate - check if passwords are the same + * + * @param {String} plainText + * @return {Bolean} + * @api public + */ + + authenticate: function(plainText) { + return this.encryptPassword(plainText) === this.hashed_password; + }, + + /** + * Make salt + * + * @return {String} + * @api public + */ + + makeSalt: function() { + return Math.round((new Date().valueOf() * Math.random())) + ''; + }, + + /** + * Encrypt password + * + * @param {String} password + * @return {String} + * @api public + */ + + encryptPassword: function(password) { + if (!password) return ''; + return crypto.createHmac('sha1', this.salt).update(password).digest('hex'); + } +} + +mongoose.model('User', UserSchema); + + diff --git a/models/access.js b/models/access.js deleted file mode 100644 index ce75f8c..0000000 --- a/models/access.js +++ /dev/null @@ -1,42 +0,0 @@ - -/** - * Module dependencies - */ - -var mongoose = require('mongoose') - , Schema = mongoose.Schema; - -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' }, - created: { type: Date, default: Date.now }, - updated: { type: Date, default: Date.now } -}); - -// the four validations below only apply if you are signing up traditionally - -AccessSchema.statics = { - - log: function() { - console.log('wat. wat logged this'); - }, - - /** - * Load ALL accesses for a single user - * - * @param {ObjectId} id - * @param {Function} callback - * @api private - */ - - loadUser: function(id, callback) { - this.find({ user: id }) - .populate('project') - .sort({ 'created': -1 }) // sort by date - .exec(callback); - } -} - -mongoose.model('Access', AccessSchema); diff --git a/models/project.js b/models/project.js deleted file mode 100644 index f29c7f8..0000000 --- a/models/project.js +++ /dev/null @@ -1,67 +0,0 @@ - -/** - * Module dependencies - */ - -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 } -}); - -// the four validations below only apply if you are signing up traditionally - -ProjectSchema.path('name').validate(function(name) { - // if you're authenticated by any of the oauth strategies (facebook, twitter), don't validate - return name.length; -}, 'Project name cannot be blank'); - - -ProjectSchema.pre('save', function(next) { - if (this.shortURL !== undefined) return next(); - var chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; - this.shortURL = ''; - for (var i = 0; i < 6; i++) { - var x = Math.floor(Math.random() * chars.length); - this.shortURL += chars.charAt(x); - } - console.log('SHORT: ' + this.shortURL); - next(); -}); - - - -ProjectSchema.statics = { - - log: function() { - console.log('wat. wat logged this'); - }, - - - - /** - * Find project by id - * - * @param {ObjectId} id - * @param {Function} callback - * @api private - */ - - load: function(id, callback) { - this.findOne({ _id: id }) - .populate('user') - .exec(callback); - } - - -} - -mongoose.model('Project', ProjectSchema); diff --git a/models/user.js b/models/user.js deleted file mode 100644 index b61f7c9..0000000 --- a/models/user.js +++ /dev/null @@ -1,128 +0,0 @@ - -/** - * Module dependencies - */ - -var mongoose = require('mongoose') - , Schema = mongoose.Schema - , crypto =require('crypto') - , authTypes = ['facebook', 'twitter']; - - -/** - * User schema - */ - -var UserSchema = new Schema({ - name: String, - email: String, - username: String, - provider: String, - hashed_password: String, - salt: String, - facebook: {}, - twitter: {} -}); - -/** - * Virtuals - */ - -UserSchema - .virtual('password') - .set(function(password) { - this._password = password - this.salt = this.makeSalt() - this.hashed_password = this.encryptPassword(password) - }).get(function() { return this._password }); - -/** - * Validations - */ -var validatePrecenceOf = function (value) { - return value && value.length; -} - -// the four validations below only apply if you are signing up traditionally - -UserSchema.path('name').validate(function(name) { - // if you're authenticated by any of the oauth strategies (facebook, twitter), don't validate - if(authTypes.indexOf(this.provider) !== -1) return true; - return name.length; -}, 'Name cannot be blank'); - -UserSchema.path('email').validate(function(email) { - if(authTypes.indexOf(this.provider) !== -1) return true; - return email.length; -}, 'Email cannot be blank'); - -UserSchema.path('username').validate(function(username) { - if(authTypes.indexOf(this.provider) !== -1) return true; - return username.length; -}, 'Username cannot be blank'); - -UserSchema.path('hashed_password').validate(function(hashed_password) { - if(authTypes.indexOf(this.provider) !== -1) return true; - return hashed_password.length; -}, 'Password cannot be blank'); - -/** - * Pre-save hook - */ - -UserSchema.pre('save', function(next) { - if (!this.isNew) return next(); - - if(!validatePrecenceOf(this.password) - && authTypes.indexOf(this.provider) === -1) - next(new Error('Invalid password')); - else next(); -}); - - -/** - * Methods - */ - -UserSchema.methods = { - - /** - * Authenticate - check if passwords are the same - * - * @param {String} plainText - * @return {Bolean} - * @api public - */ - - authenticate: function(plainText) { - return this.encryptPassword(plainText) === this.hashed_password; - }, - - /** - * Make salt - * - * @return {String} - * @api public - */ - - makeSalt: function() { - return Math.round((new Date().valueOf() * Math.random())) + ''; - }, - - /** - * Encrypt password - * - * @param {String} password - * @return {String} - * @api public - */ - - encryptPassword: function(password) { - if (!password) return ''; - return crypto.createHmac('sha1', this.salt).update(password).digest('hex'); - } -} - -mongoose.model('User', UserSchema); - - -- cgit v1.2.3