refactor: how admins change emails (#11973)

* refactor: how admins change emails

ability for admins to change emails from acp
ability for admins to change passwords from acp
only users themselves can use /user/<slug>/edit/email
group actions in manage users dropdown
admins can use the same modal from profile page instead of interstitial to update email
add missing checks to addEmail, if email take throw error
add targetUid to email change event

* test: bunch of baloney

* test: remove old test
This commit is contained in:
Barış Soner Uşaklı
2023-08-30 19:29:46 -04:00
committed by GitHub
parent 6ae0d207a7
commit 8db13d8e86
14 changed files with 217 additions and 85 deletions

View File

@@ -1,8 +1,8 @@
'use strict';
define('admin/manage/users', [
'translator', 'benchpress', 'autocomplete', 'api', 'slugify', 'bootbox', 'alerts', 'accounts/invite', 'helpers',
], function (translator, Benchpress, autocomplete, api, slugify, bootbox, alerts, AccountInvite, helpers) {
'translator', 'benchpress', 'autocomplete', 'api', 'slugify', 'bootbox', 'alerts', 'accounts/invite', 'helpers', 'admin/modules/change-email',
], function (translator, Benchpress, autocomplete, api, slugify, bootbox, alerts, AccountInvite, helpers, changeEmail) {
const Users = {};
Users.init = function () {
@@ -273,6 +273,26 @@ define('admin/manage/users', [
socket.emit('admin.user.resetLockouts', uids, done('[[admin/manage/users:alerts.lockout-reset-success]]'));
});
$('.change-email').on('click', function () {
const uids = getSelectedUids();
if (uids.length !== 1) {
return alerts.error('[[admin/manage/users:alerts.select-a-single-user-to-change-email]]');
}
changeEmail.init({
uid: uids[0],
onSuccess: function (newEmail) {
update('.notvalidated', false);
update('.pending', false);
update('.expired', false);
update('.validated', false);
update('.validated-by-admin', !!newEmail);
update('.no-email', !newEmail);
$('.users-table [component="user/select/single"]:checked').parents('.user-row').find('.validated-by-admin .email').text(newEmail);
// $('.users-table [component="user/select/single"]:checked').parents('.user-row').find('.no-email').
},
});
});
$('.validate-email').on('click', function () {
const uids = getSelectedUids();
if (!uids.length) {
@@ -311,6 +331,51 @@ define('admin/manage/users', [
});
});
$('.change-password').on('click', async function () {
const uids = getSelectedUids();
if (!uids.length) {
return;
}
async function changePassword(modal) {
const newPassword = modal.find('#newPassword').val();
const confirmPassword = modal.find('#confirmPassword').val();
if (newPassword !== confirmPassword) {
throw new Error('[[[user:change_password_error_match]]');
}
await Promise.all(uids.map(uid => api.put('/users/' + uid + '/password', {
currentPassword: '',
newPassword: newPassword,
})));
}
const modal = bootbox.dialog({
message: `<div class="d-flex flex-column gap-2">
<label class="form-label">[[user:new_password]]</label>
<input id="newPassword" class="form-control" type="text">
<label class="form-label">[[user:confirm_password]]</label>
<input id="confirmPassword" class="form-control" type="text">
</div>`,
title: '[[admin/manage/users:change-password]]',
onEscape: true,
buttons: {
cancel: {
label: '[[admin/manage/users:alerts.button-cancel]]',
className: 'btn-link',
},
change: {
label: '[[admin/manage/users:alerts.button-change]]',
className: 'btn-primary',
callback: function () {
changePassword(modal).then(() => {
modal.modal('hide');
}).catch(alerts.error);
return false;
},
},
},
});
});
$('.password-reset-email').on('click', function () {
const uids = getSelectedUids();
if (!uids.length) {

View File

@@ -0,0 +1,41 @@
'use strict';
define('admin/modules/change-email', [
'api', 'bootbox', 'alerts',
], function (api, bootbox, alerts) {
const ChangeEmail = {};
ChangeEmail.init = function (params) {
const modal = bootbox.dialog({
message: `
<label class="form-label">[[admin/manage/users:new-email]]</label>
<input id="newEmail" class="form-control" type="text" value="${utils.escapeHTML(params.email || '')}">
`,
title: '[[admin/manage/users:change-email]]',
onEscape: true,
buttons: {
cancel: {
label: '[[admin/manage/users:alerts.button-cancel]]',
className: 'btn-link',
},
change: {
label: '[[admin/manage/users:alerts.button-change]]',
className: 'btn-primary',
callback: function () {
const newEmail = modal.find('#newEmail').val();
api.post('/users/' + params.uid + '/emails', {
skipConfirmation: true,
email: newEmail,
}).then(() => {
modal.modal('hide');
params.onSuccess(newEmail);
}).catch(alerts.error);
return false;
},
},
},
});
};
return ChangeEmail;
});