fix bug change password for current user

This commit is contained in:
Lan Nguyen Thuy
2024-08-28 11:14:20 +07:00
committed by Jacob Overgaard
parent e10b817017
commit a9e53412c1
12 changed files with 158 additions and 16 deletions

File diff suppressed because one or more lines are too long

View File

@@ -5068,6 +5068,12 @@ export type PostUserByIdChangePasswordData = {
export type PostUserByIdChangePasswordResponse = string;
export type PostCurrentUserChangePasswordData = {
requestBody?: ChangePasswordCurrentUserRequestModel;
};
export type PostCurrentUserChangePasswordResponse = string;
export type PostUserByIdResetPasswordData = {
id: string;
};

View File

@@ -9,6 +9,7 @@ export interface UmbChangePasswordModalData {
export interface UmbChangePasswordModalValue {
oldPassword: string;
newPassword: string;
isCurrentUser: boolean;
}
export const UMB_CHANGE_PASSWORD_MODAL = new UmbModalToken<UmbChangePasswordModalData, UmbChangePasswordModalValue>(

View File

@@ -3,6 +3,7 @@ import { UmbActionBase } from '@umbraco-cms/backoffice/action';
import type { UmbCurrentUserAction, UmbCurrentUserActionArgs } from '@umbraco-cms/backoffice/extension-registry';
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
import { UMB_CHANGE_PASSWORD_MODAL, UMB_MODAL_MANAGER_CONTEXT } from '@umbraco-cms/backoffice/modal';
import { UmbChangeCurrentUserPasswordRepository } from '../repository/index.js';
export class UmbChangePasswordCurrentUserAction<ArgsMetaType = never>
extends UmbActionBase<UmbCurrentUserActionArgs<ArgsMetaType>>
@@ -32,13 +33,17 @@ export class UmbChangePasswordCurrentUserAction<ArgsMetaType = never>
if (!this.#unique) return;
const modalManager = await this.getContext(UMB_MODAL_MANAGER_CONTEXT);
modalManager.open(this, UMB_CHANGE_PASSWORD_MODAL, {
const modalContext = modalManager.open(this, UMB_CHANGE_PASSWORD_MODAL, {
data: {
user: {
unique: this.#unique,
},
},
});
const data = await modalContext.onSubmit();
const repository = new UmbChangeCurrentUserPasswordRepository(this);
await repository.changePassword(this.#unique, data.newPassword, data.oldPassword, data.isCurrentUser);
}
}

View File

@@ -0,0 +1,35 @@
import { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
import { UmbCurrentUserRepository } from '../current-user.repository.js';
import type { UmbNotificationContext } from '@umbraco-cms/backoffice/notification';
import { UMB_NOTIFICATION_CONTEXT } from '@umbraco-cms/backoffice/notification';
import {UmbChangeCurrentUserPasswordServerDataSource} from './change-current-user-password.server.data-source.js'
export class UmbChangeCurrentUserPasswordRepository extends UmbCurrentUserRepository{
#changePasswordSource: UmbChangeCurrentUserPasswordServerDataSource;
protected notificationContext?: UmbNotificationContext;
constructor(host: UmbControllerHost){
super(host);
this.#changePasswordSource = new UmbChangeCurrentUserPasswordServerDataSource(host);
this.consumeContext(UMB_NOTIFICATION_CONTEXT, (instance) => {
this.notificationContext = instance;
}).asPromise();
}
async changePassword(userId: string, newPassword: string, oldPassword: string, isCurrentUser: boolean) {
if (!userId) throw new Error('User id is missing');
if (!newPassword) throw new Error('New password is missing');
if (isCurrentUser && !oldPassword) throw new Error('Old password is missing');
const { data, error } = await this.#changePasswordSource.changePassword(userId, newPassword, oldPassword, isCurrentUser);
if (!error) {
const notification = { data: { message: `Password changed` } };
this.notificationContext?.peek('positive', notification);
}
return { data, error };
}
}
export default UmbChangeCurrentUserPasswordRepository;

View File

@@ -0,0 +1,55 @@
import { UserService } from '@umbraco-cms/backoffice/external/backend-api';
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
import { tryExecuteAndNotify } from '@umbraco-cms/backoffice/resources';
/**
* A server data source for changing the password of a user
* @export
* @class UmbChangeCurrentUserPasswordServerDataSource
*/
export class UmbChangeCurrentUserPasswordServerDataSource {
#host: UmbControllerHost;
/**
* Creates an instance of UmbChangeCurrentUserPasswordServerDataSource.
* @param {UmbControllerHost} host
* @memberof UmbChangeCurrentUserPasswordServerDataSource
*/
constructor(host: UmbControllerHost) {
this.#host = host;
}
/**
* Change the password of a user
* @param {string} id
* @param {string} newPassword
* @returns {*}
* @memberof UmbChangeCurrentUserPasswordServerDataSource
*/
async changePassword(id: string, newPassword: string, oldPassword: string, isCurrentUser: boolean) {
if (!id) throw new Error('User Id is missing');
if(isCurrentUser){
return tryExecuteAndNotify(
this.#host,
UserService.postCurrentUserByIdChangePassword({
requestBody: {
newPassword,
oldPassword
},
}),
);
}
else{
return tryExecuteAndNotify(
this.#host,
UserService.postUserByIdChangePassword({
id,
requestBody: {
newPassword
},
}),
);
}
}
}

View File

@@ -0,0 +1 @@
export { UmbChangeCurrentUserPasswordRepository } from './change-current-user-password.repository.js'

View File

@@ -2,3 +2,4 @@ export { UMB_CURRENT_USER_REPOSITORY_ALIAS } from './constants.js';
export { UMB_CURRENT_USER_STORE_CONTEXT } from './current-user.store.token.js';
export { UmbCurrentUserRepository } from './current-user.repository.js';
export { UmbCurrentUserStore } from './current-user.store.js';
export * from './change-password/index.js';

View File

@@ -38,9 +38,10 @@ export class UmbChangePasswordModalElement extends UmbModalBaseElement<
// TODO: validate that the new password and confirm password match
const oldPassword = formData.get('oldPassword') as string;
const newPassword = formData.get('newPassword') as string;
const isCurrentUser = this._isCurrentUser;
//const confirmPassword = formData.get('confirmPassword') as string;
this.value = { oldPassword, newPassword };
this.value = { oldPassword, newPassword, isCurrentUser };
this.modalContext?.submit();
}

View File

@@ -24,7 +24,7 @@ export class UmbChangeUserPasswordEntityAction extends UmbEntityActionBase<never
const data = await modalContext.onSubmit();
const repository = new UmbChangeUserPasswordRepository(this);
await repository.changePassword(this.args.unique, data.newPassword);
await repository.changePassword(this.args.unique, data.newPassword, data.oldPassword, data.isCurrentUser);
}
}

View File

@@ -10,12 +10,13 @@ export class UmbChangeUserPasswordRepository extends UmbUserRepositoryBase {
this.#changePasswordSource = new UmbChangeUserPasswordServerDataSource(host);
}
async changePassword(userId: string, newPassword: string) {
async changePassword(userId: string, newPassword: string, oldPassword: string, isCurrentUser: boolean) {
if (!userId) throw new Error('User id is missing');
if (!newPassword) throw new Error('New password is missing');
if (isCurrentUser && !oldPassword) throw new Error('Old password is missing');
await this.init;
const { data, error } = await this.#changePasswordSource.changePassword(userId, newPassword);
const { data, error } = await this.#changePasswordSource.changePassword(userId, newPassword, oldPassword, isCurrentUser);
if (!error) {
const notification = { data: { message: `Password changed` } };

View File

@@ -25,17 +25,30 @@ export class UmbChangeUserPasswordServerDataSource {
* @returns {*}
* @memberof UmbChangeUserPasswordServerDataSource
*/
async changePassword(id: string, newPassword: string) {
async changePassword(id: string, newPassword: string, oldPassword: string, isCurrentUser: boolean) {
if (!id) throw new Error('User Id is missing');
return tryExecuteAndNotify(
this.#host,
UserService.postUserByIdChangePassword({
id,
requestBody: {
newPassword,
},
}),
);
if(isCurrentUser){
return tryExecuteAndNotify(
this.#host,
UserService.postCurrentUserByIdChangePassword({
requestBody: {
newPassword,
oldPassword
},
}),
);
}
else{
return tryExecuteAndNotify(
this.#host,
UserService.postUserByIdChangePassword({
id,
requestBody: {
newPassword
},
}),
);
}
}
}