From 5db3b7353fd3760736ed095a9f14e4a74f31e20f Mon Sep 17 00:00:00 2001 From: Lan Nguyen Thuy Date: Wed, 28 Aug 2024 17:41:46 +0700 Subject: [PATCH] move change to current user repository --- .../change-password-current-user.action.ts | 6 ++-- .../repository/current-user.repository.ts | 27 +++++++++++++++ .../current-user.server.data-source.ts | 20 +++++++++++ .../change-user-password.action.ts | 12 +++++-- .../change-user-password.repository.ts | 5 ++- ...change-user-password.server.data-source.ts | 33 ++++++------------- 6 files changed, 72 insertions(+), 31 deletions(-) diff --git a/src/Umbraco.Web.UI.Client/src/packages/user/current-user/profile/change-password-current-user.action.ts b/src/Umbraco.Web.UI.Client/src/packages/user/current-user/profile/change-password-current-user.action.ts index c167e62841..038b97a215 100644 --- a/src/Umbraco.Web.UI.Client/src/packages/user/current-user/profile/change-password-current-user.action.ts +++ b/src/Umbraco.Web.UI.Client/src/packages/user/current-user/profile/change-password-current-user.action.ts @@ -3,7 +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 {UmbChangeUserPasswordRepository} from '../../../user/user/repository/change-password/change-user-password.repository.js' +import UmbCurrentUserRepository from '../repository/current-user.repository.js'; export class UmbChangePasswordCurrentUserAction extends UmbActionBase> implements UmbCurrentUserAction @@ -41,8 +41,8 @@ export class UmbChangePasswordCurrentUserAction }); const data = await modalContext.onSubmit(); - const repository = new UmbChangeUserPasswordRepository(this); - await repository.changePassword(this.#unique, data.newPassword, data.oldPassword, data.isCurrentUser); + const repository = new UmbCurrentUserRepository(this); + await repository.changePassword(data.newPassword, data.oldPassword); } } diff --git a/src/Umbraco.Web.UI.Client/src/packages/user/current-user/repository/current-user.repository.ts b/src/Umbraco.Web.UI.Client/src/packages/user/current-user/repository/current-user.repository.ts index a97f2257c9..4b736e9459 100644 --- a/src/Umbraco.Web.UI.Client/src/packages/user/current-user/repository/current-user.repository.ts +++ b/src/Umbraco.Web.UI.Client/src/packages/user/current-user/repository/current-user.repository.ts @@ -2,6 +2,7 @@ import { UmbCurrentUserServerDataSource } from './current-user.server.data-sourc import { UMB_CURRENT_USER_STORE_CONTEXT } from './current-user.store.token.js'; import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api'; import { UmbRepositoryBase } from '@umbraco-cms/backoffice/repository'; +import { UMB_NOTIFICATION_CONTEXT, UmbNotificationContext } from '@umbraco-cms/backoffice/notification'; /** * A repository for the current user @@ -12,6 +13,7 @@ export class UmbCurrentUserRepository extends UmbRepositoryBase { #currentUserSource = new UmbCurrentUserServerDataSource(this._host); #currentUserStore?: typeof UMB_CURRENT_USER_STORE_CONTEXT.TYPE; #init: Promise; + protected notificationContext?: UmbNotificationContext; constructor(host: UmbControllerHost) { super(host); @@ -20,6 +22,10 @@ export class UmbCurrentUserRepository extends UmbRepositoryBase { this.consumeContext(UMB_CURRENT_USER_STORE_CONTEXT, (instance) => { this.#currentUserStore = instance; }).asPromise(), + + this.consumeContext(UMB_NOTIFICATION_CONTEXT, (instance) => { + this.notificationContext = instance; + }).asPromise(), ]); } @@ -108,6 +114,27 @@ export class UmbCurrentUserRepository extends UmbRepositoryBase { return {}; } + /** + * Change password for current user + * @param userId + * @param newPassword + * @param oldPassword + * @param isCurrentUser + * @returns + */ + async changePassword(newPassword: string, oldPassword: string) { + if (!newPassword) throw new Error('New password is missing'); + if (!oldPassword) throw new Error('Old password is missing'); + + const { data, error } = await this.#currentUserSource.changePassword(newPassword, oldPassword); + + if (!error) { + const notification = { data: { message: `Password changed` } }; + this.notificationContext?.peek('positive', notification); + } + + return { data, error }; + } } export default UmbCurrentUserRepository; diff --git a/src/Umbraco.Web.UI.Client/src/packages/user/current-user/repository/current-user.server.data-source.ts b/src/Umbraco.Web.UI.Client/src/packages/user/current-user/repository/current-user.server.data-source.ts index 5e46118f53..31cc1a5694 100644 --- a/src/Umbraco.Web.UI.Client/src/packages/user/current-user/repository/current-user.server.data-source.ts +++ b/src/Umbraco.Web.UI.Client/src/packages/user/current-user/repository/current-user.server.data-source.ts @@ -115,4 +115,24 @@ export class UmbCurrentUserServerDataSource { return {}; } + + /** + * Change the password for current user + * @param id + * @param newPassword + * @param oldPassword + * @param isCurrentUser + * @returns + */ + async changePassword(newPassword: string, oldPassword: string) { + return tryExecuteAndNotify( + this.#host, + UserService.postCurrentUserByIdChangePassword({ + requestBody: { + newPassword, + oldPassword + }, + }), + ); + } } diff --git a/src/Umbraco.Web.UI.Client/src/packages/user/user/entity-actions/change-password/change-user-password.action.ts b/src/Umbraco.Web.UI.Client/src/packages/user/user/entity-actions/change-password/change-user-password.action.ts index 2ed0dad977..d9ef88b767 100644 --- a/src/Umbraco.Web.UI.Client/src/packages/user/user/entity-actions/change-password/change-user-password.action.ts +++ b/src/Umbraco.Web.UI.Client/src/packages/user/user/entity-actions/change-password/change-user-password.action.ts @@ -3,6 +3,7 @@ import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api'; import type { UmbEntityActionArgs } from '@umbraco-cms/backoffice/entity-action'; import { UmbEntityActionBase } from '@umbraco-cms/backoffice/entity-action'; import { UMB_MODAL_MANAGER_CONTEXT, UMB_CHANGE_PASSWORD_MODAL } from '@umbraco-cms/backoffice/modal'; +import UmbCurrentUserRepository from 'src/packages/user/current-user/repository/current-user.repository.js'; export class UmbChangeUserPasswordEntityAction extends UmbEntityActionBase { constructor(host: UmbControllerHost, args: UmbEntityActionArgs) { @@ -23,8 +24,15 @@ export class UmbChangeUserPasswordEntityAction extends UmbEntityActionBase