diff options
author | Lurifax <stianalexanderolsen@gmail.com> | 2013-05-01 22:20:18 +0200 |
---|---|---|
committer | Lurifax <stianalexanderolsen@gmail.com> | 2013-05-01 22:20:18 +0200 |
commit | 464d4717ace36646db7636f2ebc7701a921c75dd (patch) | |
tree | 670d7fa67e7633ca818cc5b48e8886fab2d6ab3f /models | |
parent | Changes in text (diff) | |
parent | added generateRandomAccessToken method (diff) | |
download | Divid-464d4717ace36646db7636f2ebc7701a921c75dd.tar.gz |
Changes in text in home.ejs
Diffstat (limited to 'models')
-rw-r--r-- | models/Access.js | 42 | ||||
-rw-r--r-- | models/Project.js | 67 | ||||
-rw-r--r-- | models/User.js (renamed from models/user.js) | 23 | ||||
-rw-r--r-- | models/project.js | 39 |
4 files changed, 131 insertions, 40 deletions
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 index b61f7c9..4d32941 100644 --- a/models/user.js +++ b/models/User.js @@ -5,7 +5,7 @@ var mongoose = require('mongoose') , Schema = mongoose.Schema - , crypto =require('crypto') + , crypto = require('crypto') , authTypes = ['facebook', 'twitter']; @@ -20,6 +20,7 @@ var UserSchema = new Schema({ provider: String, hashed_password: String, salt: String, + accessToken: String, facebook: {}, twitter: {} }); @@ -98,6 +99,7 @@ UserSchema.methods = { return this.encryptPassword(plainText) === this.hashed_password; }, + /** * Make salt * @@ -109,6 +111,7 @@ UserSchema.methods = { return Math.round((new Date().valueOf() * Math.random())) + ''; }, + /** * Encrypt password * @@ -120,6 +123,24 @@ UserSchema.methods = { encryptPassword: function(password) { if (!password) return ''; return crypto.createHmac('sha1', this.salt).update(password).digest('hex'); + }, + + + /** + * Generate random access token for Remember Me function + * + * @return {String} + * @api public + */ + + generateRandomToken: function() { + var chars = "_!abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890" + , token = new Date().getTime() + '_'; + for (var i = 0; i < 16; i++) { + var x = Math.floor(Math.random() * 62); + token += chars.charAt(x); + } + return token; } } diff --git a/models/project.js b/models/project.js deleted file mode 100644 index 2424694..0000000 --- a/models/project.js +++ /dev/null @@ -1,39 +0,0 @@ - -/** - * Module dependencies - */ - -var mongoose = require('mongoose') - , Schema = mongoose.Schema; - - - - - -var ProjectSchema = new Schema({ - user: { type: String, 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 } -}); - - -// 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.methods = { - - log: function() { - console.log('wat. wat logged this'); - } - -} - -mongoose.model('Project', ProjectSchema); |