From 180c24f498f4d77fb6b04efedf093573ae9def68 Mon Sep 17 00:00:00 2001 From: OldHawk Date: Mon, 22 May 2017 14:09:32 +0800 Subject: [PATCH] feat(user): set first signup user role to 'admin' --- .../users/server/models/user.server.model.js | 26 +++++++++---------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/modules/users/server/models/user.server.model.js b/modules/users/server/models/user.server.model.js index 0373b0da..09c3b0dc 100644 --- a/modules/users/server/models/user.server.model.js +++ b/modules/users/server/models/user.server.model.js @@ -199,32 +199,30 @@ UserSchema.methods.toJSON = function (options) { * Hook a pre save method to hash the password */ UserSchema.pre('save', function (next) { + var user = this; + if (this.password && this.isModified('password')) { this.salt = crypto.randomBytes(16).toString('base64'); this.password = this.hashPassword(this.password); } + if (this.uploaded === 0 || this.downloaded === 0) { + this.ratio = 0; + } else { + this.ratio = Math.round((this.uploaded / this.downloaded) * 100) / 100; + } + this.constructor.count(function (err, count) { if (err) { return next(err); } + if (count === 0) { - this.roles = ['admmin']; + user.roles = ['admin']; } + + next(); }); - - next(); -}); - -/** - * Hook a post save method to set the ratio - */ -UserSchema.post('save', function (doc) { - if (doc.uploaded === 0 || doc.downloaded === 0) { - doc.ratio = 0; - } else { - doc.ratio = Math.round((doc.uploaded / doc.downloaded) * 100) / 100; - } }); /**