Merge branch 'passport' of github.com:coralproject/talk into passport

This commit is contained in:
Belen Curcio
2016-11-11 19:42:20 -03:00
8 changed files with 221 additions and 81 deletions
+36 -58
View File
@@ -12,6 +12,7 @@ const UserSchema = new mongoose.Schema({
required: true
},
displayName: String,
photo: String,
disabled: Boolean,
password: String,
profiles: [{
@@ -25,11 +26,6 @@ const UserSchema = new mongoose.Schema({
}
}],
roles: [String]
}, {
timestamps: {
createdAt: 'created_at',
updatedAt: 'updated_at'
}
});
// Add the indixies on the user profile data.
@@ -57,22 +53,6 @@ UserSchema.options.toJSON.transform = (doc, ret, options) => {
return ret;
};
/**
* toObject overrides to remove the password field from the toObject
* output.
*/
UserSchema.options.toObject = {};
UserSchema.options.toObject.hide = 'password';
UserSchema.options.toObject.transform = (doc, ret, options) => {
if (options.hide) {
options.hide.split(' ').forEach((prop) => {
delete ret[prop];
});
}
return ret;
};
/**
* Finds a user given their email address that we have for them in the system
* and ensures that the retuned user matches the password passed in as well.
@@ -121,21 +101,19 @@ UserSchema.statics.findLocalUser = function(email, password) {
UserSchema.statics.mergeUsers = function(dstUserID, srcUserID) {
let srcUser, dstUser;
return Promise
.all([
User.findOne({id: dstUserID}).exec(),
User.findOne({id: srcUserID}).exec()
])
.then((users) => {
dstUser = users[0];
srcUser = users[1];
return Promise.all([
User.findOne({id: dstUserID}).exec(),
User.findOne({id: srcUserID}).exec()
]).then((users) => {
dstUser = users[0];
srcUser = users[1];
srcUser.profiles.forEach((profile) => {
dstUser.profiles.push(profile);
});
srcUser.profiles.forEach((profile) => {
dstUser.profiles.push(profile);
});
return srcUser.remove();
})
return srcUser.remove();
})
.then(() => dstUser.save());
};
@@ -146,34 +124,34 @@ UserSchema.statics.mergeUsers = function(dstUserID, srcUserID) {
* @param {Function} done [description]
*/
UserSchema.statics.findOrCreateExternalUser = function(profile) {
return User
.findOne({
profiles: {
$elemMatch: {
return User.findOne({
profiles: {
$elemMatch: {
id: profile.id,
provider: profile.provider
}
}
})
.then((user) => {
if (user) {
return user;
}
// The user was not found, lets create them!
user = new User({
displayName: profile.displayName,
roles: [],
photo: Array.isArray(profile.photos) && profile.photos.length > 0 ? profile.photos[0].value : null,
profiles: [
{
id: profile.id,
provider: profile.provider
}
}
})
.then((user) => {
if (user) {
return user;
}
// The user was not found, lets create them!
user = new User({
displayName: profile.displayName,
roles: [],
profiles: [
{
id: profile.id,
provider: profile.provider
}
]
});
return user.save();
]
});
return user.save();
});
};
UserSchema.statics.changePassword = function(id, password) {