move change to current user repository
This commit is contained in:
committed by
Jacob Overgaard
parent
a054da0203
commit
5db3b7353f
@@ -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<ArgsMetaType = never>
|
||||
extends UmbActionBase<UmbCurrentUserActionArgs<ArgsMetaType>>
|
||||
implements UmbCurrentUserAction<ArgsMetaType>
|
||||
@@ -41,8 +41,8 @@ export class UmbChangePasswordCurrentUserAction<ArgsMetaType = never>
|
||||
});
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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<unknown>;
|
||||
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;
|
||||
|
||||
@@ -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
|
||||
},
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<never> {
|
||||
constructor(host: UmbControllerHost, args: UmbEntityActionArgs<never>) {
|
||||
@@ -23,8 +24,15 @@ export class UmbChangeUserPasswordEntityAction extends UmbEntityActionBase<never
|
||||
|
||||
const data = await modalContext.onSubmit();
|
||||
|
||||
const repository = new UmbChangeUserPasswordRepository(this);
|
||||
await repository.changePassword(this.args.unique, data.newPassword, data.oldPassword, data.isCurrentUser);
|
||||
if(data.isCurrentUser){
|
||||
const repository = new UmbCurrentUserRepository(this);
|
||||
await repository.changePassword(data.newPassword, data.oldPassword);
|
||||
}
|
||||
else{
|
||||
const repository = new UmbChangeUserPasswordRepository(this);
|
||||
await repository.changePassword(this.args.unique, data.newPassword);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -10,13 +10,12 @@ export class UmbChangeUserPasswordRepository extends UmbUserRepositoryBase {
|
||||
this.#changePasswordSource = new UmbChangeUserPasswordServerDataSource(host);
|
||||
}
|
||||
|
||||
async changePassword(userId: string, newPassword: string, oldPassword: string, isCurrentUser: boolean) {
|
||||
async changePassword(userId: string, newPassword: string) {
|
||||
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, oldPassword, isCurrentUser);
|
||||
const { data, error } = await this.#changePasswordSource.changePassword(userId, newPassword);
|
||||
|
||||
if (!error) {
|
||||
const notification = { data: { message: `Password changed` } };
|
||||
|
||||
@@ -25,30 +25,17 @@ export class UmbChangeUserPasswordServerDataSource {
|
||||
* @returns {*}
|
||||
* @memberof UmbChangeUserPasswordServerDataSource
|
||||
*/
|
||||
async changePassword(id: string, newPassword: string, oldPassword: string, isCurrentUser: boolean) {
|
||||
async changePassword(id: string, newPassword: string) {
|
||||
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
|
||||
},
|
||||
}),
|
||||
);
|
||||
}
|
||||
return tryExecuteAndNotify(
|
||||
this.#host,
|
||||
UserService.postUserByIdChangePassword({
|
||||
id,
|
||||
requestBody: {
|
||||
newPassword
|
||||
},
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user