This repository was archived by the owner on Aug 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2k
Refactor user #343
Closed
Closed
Refactor user #343
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,7 +26,18 @@ exports.update = function(req, res) { | |
| user.updated = Date.now(); | ||
| user.displayName = user.firstName + ' ' + user.lastName; | ||
|
|
||
| user.save(function(err) { | ||
| user.identities.forEach(function(auth){ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. spacing after ). |
||
| auth.accessToken = auth.accessToken || null; | ||
| auth.refreshToken = auth.refreshToken || null; | ||
| auth.providerData = auth.providerData || null; | ||
| }); | ||
|
|
||
| if(req.body.username) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. spacing. |
||
| user.removeOtherIdentity('username', req.body.username); | ||
| user.setIdentity('username', req.body.username); | ||
| } | ||
|
|
||
| user.save(function(err) { | ||
| if (err) { | ||
| return res.status(400).send({ | ||
| message: errorHandler.getErrorMessage(err) | ||
|
|
||
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,9 @@ | |
| */ | ||
| var mongoose = require('mongoose'), | ||
| Schema = mongoose.Schema, | ||
| crypto = require('crypto'); | ||
| crypto = require('crypto'), | ||
| UserModel, | ||
| eachAsync = require( 'each-async' ); | ||
|
|
||
| /** | ||
| * A Validation function for local strategy properties | ||
|
|
@@ -41,19 +43,13 @@ var UserSchema = new Schema({ | |
| type: String, | ||
| trim: true | ||
| }, | ||
| email: { | ||
| type: String, | ||
| trim: true, | ||
| default: '', | ||
| validate: [validateLocalStrategyProperty, 'Please fill in your email'], | ||
| match: [/.+\@.+\..+/, 'Please fill a valid email address'] | ||
| }, | ||
| username: { | ||
| type: String, | ||
| unique: 'Username already exists', | ||
| required: 'Please fill in a username', | ||
| trim: true | ||
| }, | ||
| identities : [ { | ||
| provider : { type : String }, | ||
| id : { type : String }, | ||
| accessToken : { type : String }, | ||
| refreshToken : { type : String }, | ||
| providerData : { type : Object } | ||
| } ], | ||
| password: { | ||
| type: String, | ||
| default: '', | ||
|
|
@@ -62,12 +58,6 @@ var UserSchema = new Schema({ | |
| salt: { | ||
| type: String | ||
| }, | ||
| provider: { | ||
| type: String, | ||
| required: 'Provider is required' | ||
| }, | ||
| providerData: {}, | ||
| additionalProvidersData: {}, | ||
| roles: { | ||
| type: [{ | ||
| type: String, | ||
|
|
@@ -91,17 +81,51 @@ var UserSchema = new Schema({ | |
| } | ||
| }); | ||
|
|
||
| UserSchema.virtual('email').get(function () { | ||
| return this.getIdentity(UserModel.EMAIL); | ||
| } ).set(function(email) { | ||
| this.setIdentity(UserModel.EMAIL, email); | ||
| }); | ||
|
|
||
| UserSchema.virtual('username').get(function () { | ||
| return this.getIdentity(UserModel.USERNAME); | ||
| } ).set(function(username) { | ||
| this.setIdentity(UserModel.USERNAME, username); | ||
| }); | ||
|
|
||
| /** | ||
| * Hook a pre save method to hash the password | ||
| */ | ||
| UserSchema.pre('save', function(next) { | ||
| if (this.password && this.password.length > 6) { | ||
| this.salt = crypto.randomBytes(16).toString('base64'); | ||
| this.password = this.hashPassword(this.password); | ||
| } | ||
|
|
||
| next(); | ||
| }); | ||
| UserSchema.pre( 'save', function ( next ) { | ||
| if ( this.password && this.password.length > 6 ) { | ||
| this.salt = new Buffer( crypto.randomBytes( 16 ).toString( 'base64' ), 'base64' ); | ||
| this.password = this.hashPassword( this.password ); | ||
| } | ||
| if ( !this.identities || this.identities.length === 0 ) { | ||
| return next(); | ||
| } | ||
| var _this = this; | ||
| eachAsync( _this.identities, function ( identity, index, done ) { | ||
| UserModel.findByProvider( identity.provider, identity.id, function ( err, user ) { | ||
| if ( err ) { | ||
| return next( err ); | ||
| } | ||
| if ( user === null ) { | ||
| return done(); | ||
| } | ||
| if ( !_this._id || _this._id.toString() !== user._id.toString() ) { | ||
| return next( new Error( identity.provider + ' : ' + identity.id + ' is assinged to other user' ) ); | ||
| } | ||
| return done(); | ||
| } ); | ||
| }, function ( error ) { | ||
| if ( error ) { | ||
| return next( error ); | ||
| } | ||
| next(); | ||
| } ); | ||
| } ); | ||
|
|
||
| /** | ||
| * Create instance method for hashing a password | ||
|
|
@@ -121,26 +145,150 @@ UserSchema.methods.authenticate = function(password) { | |
| return this.password === this.hashPassword(password); | ||
| }; | ||
|
|
||
| /** | ||
| * Create instance method for authenticating user | ||
| */ | ||
| UserSchema.methods.setIdentity = function(provider, id, accessToken, refreshToken, providerData) { | ||
| var found = false; | ||
| var auth = { | ||
| provider: provider, | ||
| id : id | ||
| }; | ||
| if(!this.identities) { | ||
| this.identities = []; | ||
| } | ||
| this.identities.forEach(function(identity) { | ||
| if(identity.provider === provider && identity.id === id) { | ||
| auth = identity; | ||
| found = true; | ||
| } | ||
| }); | ||
| auth.accessToken = accessToken || null; | ||
| auth.refreshToken = refreshToken || null; | ||
| auth.providerData = providerData || null; | ||
| if(!found) { | ||
| this.identities.push(auth); | ||
| } | ||
| }; | ||
|
|
||
| UserSchema.methods.getIdentity = function(provider, id) { | ||
| var auth = null; | ||
| if ( this.identities ) { | ||
| this.identities.forEach( function ( identity ) { | ||
| if ( identity.provider === provider && (identity.id === id || id === undefined || id === null )) { | ||
| auth = identity; | ||
| } | ||
| } ); | ||
| } | ||
| return auth; | ||
| }; | ||
|
|
||
| UserSchema.methods.removeIdentity = function ( provider, id ) { | ||
| if ( this.identities ) { | ||
| var c = this.identities.length - 1; | ||
| while ( c >= 0 ) { | ||
| if ( this.identities[ c ] && this.identities[ c ].provider === provider && this.identities[ c ] === id ) { | ||
| this.identities = this.identities.splice( c, 1 ); | ||
| } | ||
| c--; | ||
| } | ||
| } | ||
| }; | ||
|
|
||
|
|
||
| UserSchema.methods.removeOtherIdentity = function ( provider, id ) { | ||
| if ( this.identities ) { | ||
| this.identities.forEach(function(auth,idx) { | ||
| if ( auth.provider === provider && auth.id !== id ) { | ||
| this.identities.splice( idx, 1 ); | ||
| } | ||
| }); | ||
| } | ||
| }; | ||
|
|
||
|
|
||
|
|
||
| /** | ||
| * Find possible not used username | ||
| */ | ||
| UserSchema.statics.findByProvider = function ( provider, id, callback ) { | ||
| var _this = this; | ||
|
|
||
| var providerQuery = provider; | ||
|
|
||
| if(Array.isArray(provider)) { | ||
| providerQuery = { | ||
| '$in' : provider | ||
| }; | ||
| } | ||
|
|
||
| _this.findOne( { | ||
| identities : { | ||
| $elemMatch : { | ||
| provider : providerQuery, | ||
| id : id | ||
| } | ||
| } | ||
| }, function ( err, user ) { | ||
| if ( !err ) { | ||
| return callback( null, user ); | ||
| } else { | ||
| callback( null ); | ||
| } | ||
| } ); | ||
| }; | ||
|
|
||
| /** | ||
| * Find possible not used username | ||
| */ | ||
| UserSchema.statics.findUniqueUsername = function(username, suffix, callback) { | ||
| var _this = this; | ||
| var possibleUsername = username + (suffix || ''); | ||
|
|
||
| _this.findOne({ | ||
| username: possibleUsername | ||
| }, function(err, user) { | ||
| if (!err) { | ||
| if (!user) { | ||
| callback(possibleUsername); | ||
| _this.findByProvider( UserModel.USERNAME, possibleUsername, function ( err, user ) { | ||
| if ( !err ) { | ||
| if ( !user ) { | ||
| callback( possibleUsername ); | ||
| } else { | ||
| return _this.findUniqueUsername(username, (suffix || 0) + 1, callback); | ||
| return _this.findUniqueUsername( username, (suffix || 0) + 1, callback ); | ||
| } | ||
| } else { | ||
| callback(null); | ||
| callback( null ); | ||
| } | ||
| }); | ||
| } ); | ||
| }; | ||
|
|
||
| mongoose.model('User', UserSchema); | ||
| UserSchema.statics.oAuthHandle = function ( currentUser, provider, id, accessToken, refreshToken, providerData, userData, callback ) { | ||
| if ( !currentUser ) { | ||
| UserModel.findByProvider( provider, id, function ( err, user ) { | ||
| if ( err ) { | ||
| return callback( err ); | ||
| } | ||
| if ( !user ) { | ||
| var possibleUsername = userData.username || ((userData.email) ? userData.email.split( '@' )[ 0 ] : ''); | ||
| return UserModel.findUniqueUsername( possibleUsername, null, function ( availableUsername ) { | ||
| user = new UserModel( userData ); | ||
| user.setIdentity( provider, id, accessToken, refreshToken, providerData ); | ||
| user.save( function ( err ) { | ||
| return callback( err, user, true ); | ||
| } ); | ||
| } ); | ||
| } | ||
| return callback( new Error( 'You shoun\'t go to that line, ever' ), user, false ); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does it means if execution reaches this line? |
||
| } ); | ||
|
|
||
| } else { | ||
| currentUser.setIdentity( provider, id, accessToken, refreshToken, providerData ); | ||
| return currentUser.save( function ( err ) { | ||
| return callback( err, currentUser, '/#!/settings/accounts' ); | ||
| } ); | ||
| } | ||
| }; | ||
|
|
||
|
|
||
|
|
||
|
|
||
| UserSchema.statics.USERNAME = 'username'; | ||
| UserSchema.statics.EMAIL = 'email'; | ||
|
|
||
| UserModel = mongoose.model('User', UserSchema); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please keep it consistent as |
||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
spacing.