1+ // Copyright (c) Dmytro Kyshchenko. All rights reserved.
2+ // Licensed under the GPL-3.0 license. See LICENSE file in the project root for full license information.
3+
4+ using FluentValidation ;
5+ using MediatR ;
6+ using Pyro . Domain . Shared . CurrentUserProvider ;
7+ using Pyro . Domain . Shared . Exceptions ;
8+
9+ namespace Pyro . Domain . Identity . Commands ;
10+
11+ public record ChangePassword ( string OldPassword , string NewPassword ) : IRequest ;
12+
13+ public class ChangePasswordValidator : AbstractValidator < ChangePassword >
14+ {
15+ public ChangePasswordValidator ( )
16+ {
17+ RuleFor ( x => x . OldPassword )
18+ . NotEmpty ( ) ;
19+
20+ RuleFor ( x => x . NewPassword )
21+ . NotEmpty ( )
22+ . MinimumLength ( 8 ) ;
23+ }
24+ }
25+
26+ public class ChangePasswordHandler : IRequestHandler < ChangePassword >
27+ {
28+ private readonly ICurrentUserProvider currentUserProvider ;
29+ private readonly IUserRepository userRepository ;
30+ private readonly IPasswordService passwordService ;
31+
32+ public ChangePasswordHandler (
33+ ICurrentUserProvider currentUserProvider ,
34+ IUserRepository userRepository ,
35+ IPasswordService passwordService )
36+ {
37+ this . currentUserProvider = currentUserProvider ;
38+ this . userRepository = userRepository ;
39+ this . passwordService = passwordService ;
40+ }
41+
42+ public async Task Handle ( ChangePassword request , CancellationToken cancellationToken )
43+ {
44+ var currentUser = currentUserProvider . GetCurrentUser ( ) ;
45+ var user = await userRepository . GetUserById ( currentUser . Id , cancellationToken ) ??
46+ throw new NotFoundException ( $ "The user (Id: { currentUser . Id } ) not found") ;
47+
48+ user . ChangePassword ( passwordService , request . OldPassword , request . NewPassword ) ;
49+ }
50+ }
0 commit comments