Skip to content

Commit

Permalink
refactor: change pwd change logic
Browse files Browse the repository at this point in the history
add one more test
  • Loading branch information
barisusakli committed Aug 13, 2020
1 parent bb8f7c7 commit 846b7d2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
18 changes: 6 additions & 12 deletions src/user/profile.js
Expand Up @@ -278,24 +278,18 @@ module.exports = function (User) {
if (meta.config['password:disableEdit'] && !isAdmin) {
throw new Error('[[error:no-privileges]]');
}
let isAdminOrPasswordMatch = false;

const isSelf = parseInt(uid, 10) === parseInt(data.uid, 10);

if (!isAdmin && !isSelf) {
throw new Error('[[user:change_password_error_privileges]]');
}

if (
(isAdmin && !isSelf) || // Admins ok
(!hasPassword && isSelf) // Initial password set ok
) {
isAdminOrPasswordMatch = true;
} else {
isAdminOrPasswordMatch = await User.isPasswordCorrect(data.uid, data.currentPassword, data.ip);
}

if (!isAdminOrPasswordMatch) {
throw new Error('[[user:change_password_error_wrong_current]]');
if (isSelf && hasPassword) {
const correct = await User.isPasswordCorrect(data.uid, data.currentPassword, data.ip);
if (!correct) {
throw new Error('[[user:change_password_error_wrong_current]]');
}
}

const hashedPassword = await User.hashPassword(data.newPassword);
Expand Down
13 changes: 13 additions & 0 deletions test/user.js
Expand Up @@ -860,6 +860,19 @@ describe('User', function () {
assert(correct);
});

it('should not let admin change their password if current password is incorrect', async function () {
const adminUid = await User.create({ username: 'adminforgotpwd', password: 'admin1234' });
await groups.join('administrators', adminUid);

let err;
try {
await socketUser.changePassword({ uid: adminUid }, { uid: adminUid, newPassword: '654321', currentPassword: 'wrongpwd' });
} catch (_err) {
err = _err;
}
assert.equal(err.message, '[[user:change_password_error_wrong_current]]');
});

it('should change username', function (done) {
socketUser.changeUsernameEmail({ uid: uid }, { uid: uid, username: 'updatedAgain', password: '123456' }, function (err) {
assert.ifError(err);
Expand Down

0 comments on commit 846b7d2

Please sign in to comment.