Created
May 4, 2014 20:20
-
-
Save Vadorequest/11523289 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
///<reference path='./../../lib/def/defLoader.d.ts'/> | |
import model = require('./Model'); | |
export module Models { | |
/** | |
* Model used to manage users. | |
* The model is primary static, but, to make it easy to use, some things are also stored for each instance. | |
* That allows the code to use both Model or instance of Model such as: | |
* Model.schema | |
* model.Schema | |
*/ | |
export class UserModel extends model.Models.Model implements model.Models.IModel{ | |
/** | |
************************************************************************************************* | |
****************************** Public methods & attributes ************************************** | |
************************************************************************************************* | |
*/ | |
/** | |
* Name of the model. | |
* MUST start by uppercase letter! | |
*/ | |
public static modelName: string = 'User'; | |
/** | |
* Readable schema as object. | |
*/ | |
public static schema: any = require('../../../shared/schemas/userSchema.js'); | |
/** | |
* Object that contains all the indexes configuration to apply when a schema is created. | |
* The first object contains the fields. | |
* The second object contains the field options. | |
* @see http://mongoosejs.com/docs/api.html#schema_Schema-index | |
*/ | |
public static indexes: any = []; | |
/** | |
* Schema as mongoose Schema type. | |
*/ | |
public static Schema: mongoose.Schema = UserModel.getNewSchemaInstance(UserModel); | |
/** | |
* The mongoose Model that uses the mongoose schema. | |
*/ | |
public static model: any = UserModel.getNewModelInstance(UserModel); | |
/** | |
* Helpers to always get the property, from instance or static. | |
*/ | |
public modelName: string = UserModel.modelName; | |
public schema: mongoose.Schema = UserModel.schema; | |
public model: mongoose.Model<any> = UserModel.model; | |
/** | |
************************************************************************************************* | |
***************************** Extended methods & attributes ************************************** | |
************************************************************************************************* | |
*/ | |
/** | |
* These fields are protected, the user password is required to access to them. | |
* These fields are basically shared between applications. | |
* @private | |
*/ | |
private static _protectedFields: string[] = [ | |
'login', | |
'nickname', | |
'email', | |
'role', | |
'enabled', | |
'nativeLanguage', | |
'usedLanguages' | |
]; | |
/** | |
* Method to use to hash the user password. | |
*/ | |
private static _passwordHashMethod: string = 'sha256'; | |
/** | |
* Digest to use to hash the user password. | |
*/ | |
private static _passwordDigest: string = 'hex'; | |
/** | |
* Returns the protected fields. | |
* @returns {string[]} | |
*/ | |
public static getProtectedFields(): string[]{ | |
return this._protectedFields; | |
} | |
/** | |
* Hash a user password depending on the password hash configuration. Currently SHA256 in hexadecimal. | |
* Assuming crypto is global. | |
* | |
* @param password User password. | |
* @returns {string} Hashed password. | |
*/ | |
public static hashPassword(password: string): string{ | |
return crypto | |
.createHash(UserModel._passwordHashMethod) | |
.update(password) | |
.digest(UserModel._passwordDigest) | |
} | |
} | |
/** | |
* Don't forget that some methods such as exists() are written in the Model class and available for all Models. | |
* The following methods belong ONLY to the mongoose model instance, not to the Model class itself! | |
* | |
************************************************************************************************* | |
******************************** Extended Model methods ***************************************** | |
************************************************************************************************* | |
*/ | |
/** | |
* Connect a user to the game. | |
* | |
* @param user User to check. {} | |
* @param callback Callback to execute. | |
*/ | |
UserModel.model.checkAuthentication = (user, callback) => { | |
// Force to provide login and password. | |
UserModel.model.exists({login: user.login, password: UserModel.hashPassword(user.password)}, function(err, userFound){ | |
// Load public profile. | |
UserModel.model._getProtectedInformation(userFound, function(userPublic){ | |
// Provides only public fields. | |
callback(new __message("__17", {err: err, user: userPublic}, !err && userFound ? true: false)); | |
}); | |
}); | |
}; | |
/** | |
* Get the protected fields for the found user. | |
* | |
* @param user User to find. | |
* @param callback Callback to execute. | |
*/ | |
UserModel.model.getProtectedInformation = (user, callback) => { | |
// We are looking for an unique user. | |
UserModel.model.exists(user, function(err, userFound){ | |
if(err){ | |
UserModel.model.errorHandler(err, UserModel, callback); | |
}else{ | |
// Load public profile. | |
UserModel.model._getProtectedInformation(userFound, function(userPublic){ | |
// Provides only public fields. | |
callback(new __message('', {err: err, user: userPublic}, err ? false: true)); | |
}); | |
} | |
}); | |
}; | |
/** | |
* Get the protected fields of a user. | |
* | |
* @param user Instance of model. | |
* @param callback Callback to execute. | |
* @private | |
*/ | |
UserModel.model.hashPassword = (user, callback): any => { | |
var err = false; | |
if(user && user.password){ | |
user.password = UserModel.hashPassword(user.password); | |
}else{ | |
err = true; | |
} | |
callback(new __message(err ? '__18': '', {user: user}, err ? false: true)); | |
}; | |
/** | |
************************************************************************************************* | |
*************************** Methods to use only locally (private) ******************************* | |
************************************************************************************************* | |
*/ | |
/** | |
* Get the protected fields of a user. | |
* | |
* @param user Instance of model. | |
* @param callback Callback to execute. | |
* @private | |
*/ | |
UserModel.model._getProtectedInformation = (user, callback): any => { | |
var userPublic = {}; | |
// Get fields to share. | |
var publicFields = UserModel.getProtectedFields(); | |
// Fill the userPublic var with public fields only. | |
for(var field in publicFields){ | |
userPublic[publicFields[field]] = user[publicFields[field]]; | |
} | |
callback(userPublic); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment