Created
May 19, 2011 21:05
-
-
Save KATT/981741 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mongoose = require('mongoose'), | |
Schema = mongoose.Schema, | |
ObjectId = Schema.ObjectId, | |
hashlib = require('hashlib'); | |
var SAFE_COLS_TO_RETURN = function() { | |
return { | |
_id:1, | |
user:1, | |
email:1, | |
'name.first':1, | |
'name.last':1, | |
'name.full':1 | |
}; | |
}; | |
var salt = 'xyz'; | |
function hash() { | |
var str = salt; | |
for (var i in hash.arguments) { | |
str += ";" + hash.arguments[i]; | |
} | |
//console.log("hash base: ", str); | |
return hashlib.sha1(str); | |
} | |
function setPassword(pwd) { | |
pwd = hash(pwd); | |
this.apiKey = hash(this.user,pwd,Math.random()); | |
return pwd; | |
} | |
function toLower(str) { | |
return str.toLowerCase(); | |
} | |
var UserSchema = new Schema({ | |
user : { type: String, unique: true, validate: /^[A-Z0-9._%+-]+$/i }, | |
password : { type: String, index: true, set: setPassword }, | |
email : { type: String, unique: false, set: toLower, validate: /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i }, | |
confirmed : { type: Boolean, default: false, index: true }, | |
apiKey : { type: String, unique: true }, | |
created : {type: Date, default: Date.now}, | |
name: { | |
first: String, | |
last: String | |
} | |
}); | |
UserSchema.virtual('name.full').get( function () { | |
return this.name.first + " " + this.name.last; | |
}); | |
UserSchema.static('auth', function(userOrEmail,password,_calllback) { | |
var regex = new RegExp('^'+userOrEmail+'$', 'i'); | |
var obj = { | |
$or: [ | |
{user:regex}, | |
{email:regex} | |
], | |
password: hash(password) | |
}; | |
// Here it's okay to return the apiKey as well. | |
var safe_cols = SAFE_COLS_TO_RETURN(); | |
safe_cols.apiKey = 1; | |
this.findOne(obj,safe_cols, _calllback); | |
}); | |
// | |
var UserFriendsSchema = new Schema({ | |
users : [ UserSchema.ObjectId ], | |
// As this is going to be an embedded collection User, this indicates if that was this users' request | |
requester : { type: ObjectId, index: true }, | |
confirmed : { type: Boolean, default: false, index: true }, | |
created : {type: Date, default: Date.now}, | |
}); | |
UserFriendsSchema.index({users: 1}, {unique:true}); // Fine, ensures we only have one of these relations | |
// CUSTOM METHODS | |
UserSchema.method('findFriends', function (_callback) { | |
var thisId = this._id; | |
mongoose.model('UserFriends').find({users: thisId},function(err, friendships) { | |
var friendsIds = []; | |
// Let's look through our friends and gather an array with our friends' user ids | |
for (var i in friendships) { | |
var friendship = friendships[i]; | |
var userId1 = friendship.users[0]; | |
var userId2 = friendship.users[1]; | |
var friendId = (thisId != userId1 | |
? userId1 | |
: userId2); | |
friendsIds.push(friendId); | |
} | |
mongoose.model('User').find({_id: {$in: friendsIds}}, SAFE_COLS_TO_RETURN(), function(err, users) { | |
// Finally, here should we have all our friends in var users | |
_callback(err, users); | |
}) | |
}); | |
}); | |
var User = mongoose.model('User', UserSchema), | |
UserFriends = mongoose.model('UserFriends', UserFriendsSchema); | |
module.exports = { | |
User : User, | |
UserFriends : UserFriends, | |
SAFE_COLS_TO_RETURN : SAFE_COLS_TO_RETURN | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment