Refactor user command handlers and update JWT token logic
This commit is contained in:
@@ -1,16 +1,24 @@
|
|||||||
|
using CMSMicroservice.Protobuf.Protos.User;
|
||||||
|
|
||||||
namespace FrontOffice.BFF.Application.UserCQ.Commands.SetPasswordForUser;
|
namespace FrontOffice.BFF.Application.UserCQ.Commands.SetPasswordForUser;
|
||||||
public class SetPasswordForUserCommandHandler : IRequestHandler<SetPasswordForUserCommand, Unit>
|
public class SetPasswordForUserCommandHandler : IRequestHandler<SetPasswordForUserCommand, Unit>
|
||||||
{
|
{
|
||||||
private readonly IApplicationContractContext _context;
|
private readonly IApplicationContractContext _context;
|
||||||
|
private readonly ICurrentUserService _currentUserService;
|
||||||
|
|
||||||
public SetPasswordForUserCommandHandler(IApplicationContractContext context)
|
public SetPasswordForUserCommandHandler(IApplicationContractContext context, ICurrentUserService currentUserService)
|
||||||
{
|
{
|
||||||
_context = context;
|
_context = context;
|
||||||
|
_currentUserService = currentUserService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<Unit> Handle(SetPasswordForUserCommand request, CancellationToken cancellationToken)
|
public async Task<Unit> Handle(SetPasswordForUserCommand request, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
//TODO: Implement your business logic
|
var setPasswordRequest = request.Adapt<SetPasswordForUserRequest>();
|
||||||
|
setPasswordRequest.UserId = long.Parse(_currentUserService.UserId ?? throw new InvalidOperationException());
|
||||||
|
await _context.User.SetPasswordForUserAsync(setPasswordRequest, cancellationToken: cancellationToken);
|
||||||
return new Unit();
|
return new Unit();
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
using CMSMicroservice.Protobuf.Protos.User;
|
using CMSMicroservice.Protobuf.Protos.User;
|
||||||
|
|
||||||
namespace FrontOffice.BFF.Application.UserCQ.Commands.UpdateUser;
|
namespace FrontOffice.BFF.Application.UserCQ.Commands.UpdateUser;
|
||||||
|
|
||||||
public class UpdateUserCommandHandler : IRequestHandler<UpdateUserCommand, Unit>
|
public class UpdateUserCommandHandler : IRequestHandler<UpdateUserCommand, Unit>
|
||||||
{
|
{
|
||||||
private readonly IApplicationContractContext _context;
|
private readonly IApplicationContractContext _context;
|
||||||
@@ -12,7 +13,56 @@ public class UpdateUserCommandHandler : IRequestHandler<UpdateUserCommand, Unit>
|
|||||||
|
|
||||||
public async Task<Unit> Handle(UpdateUserCommand request, CancellationToken cancellationToken)
|
public async Task<Unit> Handle(UpdateUserCommand request, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
await _context.User.UpdateUserAsync(request: request.Adapt<UpdateUserRequest>(), cancellationToken: cancellationToken);
|
var updatingUserRequest = new UpdateUserRequest();
|
||||||
|
var existUser = await _context.User.GetUserAsync(new GetUserRequest()
|
||||||
|
{
|
||||||
|
Id = request.Id
|
||||||
|
}, cancellationToken: cancellationToken);
|
||||||
|
if (existUser == null)
|
||||||
|
throw new NotFoundException("User not found");
|
||||||
|
|
||||||
|
if (!string.IsNullOrEmpty(request.FirstName))
|
||||||
|
{
|
||||||
|
updatingUserRequest.FirstName = request.FirstName;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!string.IsNullOrEmpty(request.LastName))
|
||||||
|
{
|
||||||
|
updatingUserRequest.LastName = request.LastName;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!string.IsNullOrEmpty(request.NationalCode))
|
||||||
|
{
|
||||||
|
updatingUserRequest.NationalCode = request.NationalCode;
|
||||||
|
}
|
||||||
|
if (request.BirthDate.HasValue)
|
||||||
|
{
|
||||||
|
updatingUserRequest.BirthDate = Timestamp.FromDateTime(DateTime.SpecifyKind(request.BirthDate.Value, DateTimeKind.Utc));;
|
||||||
|
}
|
||||||
|
if (!string.IsNullOrEmpty(request.AvatarPath))
|
||||||
|
{
|
||||||
|
updatingUserRequest.AvatarPath = request.AvatarPath;
|
||||||
|
}
|
||||||
|
// if (request.AvatarFile!= null)
|
||||||
|
// {
|
||||||
|
// }
|
||||||
|
if (request.PushNotifications!=existUser.PushNotifications)
|
||||||
|
{
|
||||||
|
updatingUserRequest.PushNotifications = request.PushNotifications;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (request.EmailNotifications!=existUser.EmailNotifications)
|
||||||
|
{
|
||||||
|
updatingUserRequest.EmailNotifications = request.EmailNotifications;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (request.SmsNotifications!=existUser.SmsNotifications)
|
||||||
|
{
|
||||||
|
updatingUserRequest.SmsNotifications = request.SmsNotifications;
|
||||||
|
}
|
||||||
|
|
||||||
|
await _context.User.UpdateUserAsync(request: request.Adapt<UpdateUserRequest>(),
|
||||||
|
cancellationToken: cancellationToken);
|
||||||
return Unit.Value;
|
return Unit.Value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,3 +1,5 @@
|
|||||||
|
using CMSMicroservice.Protobuf.Protos.User;
|
||||||
|
|
||||||
namespace FrontOffice.BFF.Application.UserCQ.Queries.AdminGetJwtToken;
|
namespace FrontOffice.BFF.Application.UserCQ.Queries.AdminGetJwtToken;
|
||||||
public class AdminGetJwtTokenQueryHandler : IRequestHandler<AdminGetJwtTokenQuery, AdminGetJwtTokenResponseDto>
|
public class AdminGetJwtTokenQueryHandler : IRequestHandler<AdminGetJwtTokenQuery, AdminGetJwtTokenResponseDto>
|
||||||
{
|
{
|
||||||
@@ -10,7 +12,8 @@ public class AdminGetJwtTokenQueryHandler : IRequestHandler<AdminGetJwtTokenQuer
|
|||||||
|
|
||||||
public async Task<AdminGetJwtTokenResponseDto> Handle(AdminGetJwtTokenQuery request, CancellationToken cancellationToken)
|
public async Task<AdminGetJwtTokenResponseDto> Handle(AdminGetJwtTokenQuery request, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
//TODO: Implement your business logic
|
var response = await _context.User.AdminGetJwtTokenAsync(request.Adapt<AdminGetJwtTokenRequest>(),
|
||||||
return new AdminGetJwtTokenResponseDto();
|
cancellationToken: cancellationToken);
|
||||||
|
return response.Adapt<AdminGetJwtTokenResponseDto>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Afrino.PYMSMicroservice.Protobuf" Version="0.0.11" />
|
<PackageReference Include="Afrino.PYMSMicroservice.Protobuf" Version="0.0.11" />
|
||||||
<PackageReference Include="Foursat.CMSMicroservice.Protobuf" Version="0.0.117" />
|
<PackageReference Include="Foursat.CMSMicroservice.Protobuf" Version="0.0.118" />
|
||||||
<PackageReference Include="Google.Protobuf" Version="3.33.0" />
|
<PackageReference Include="Google.Protobuf" Version="3.33.0" />
|
||||||
<PackageReference Include="Grpc.Net.ClientFactory" Version="2.54.0" />
|
<PackageReference Include="Grpc.Net.ClientFactory" Version="2.54.0" />
|
||||||
<PackageReference Include="Grpc.Tools" Version="2.76.0">
|
<PackageReference Include="Grpc.Tools" Version="2.76.0">
|
||||||
|
|||||||
@@ -31,8 +31,8 @@ public static class ConfigureServices
|
|||||||
jwtBearerOptions.RequireHttpsMetadata = false;
|
jwtBearerOptions.RequireHttpsMetadata = false;
|
||||||
jwtBearerOptions.TokenValidationParameters = new TokenValidationParameters
|
jwtBearerOptions.TokenValidationParameters = new TokenValidationParameters
|
||||||
{
|
{
|
||||||
ValidateIssuer = true,
|
ValidateIssuer = false,//todo change to true in production
|
||||||
ValidateAudience = true,
|
ValidateAudience = false,//todo change to true in production
|
||||||
ValidateLifetime = true,
|
ValidateLifetime = true,
|
||||||
ValidateIssuerSigningKey = true,
|
ValidateIssuerSigningKey = true,
|
||||||
ValidIssuer = configuration["JwtIssuer"],
|
ValidIssuer = configuration["JwtIssuer"],
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
using FrontOffice.BFF.Protobuf.Protos.User;
|
|
||||||
using FrontOffice.BFF.WebApi.Common.Services;
|
using FrontOffice.BFF.WebApi.Common.Services;
|
||||||
using FrontOffice.BFF.Application.UserCQ.Commands.UpdateUser;
|
using FrontOffice.BFF.Application.UserCQ.Commands.UpdateUser;
|
||||||
using FrontOffice.BFF.Application.UserCQ.Commands.DeleteUser;
|
using FrontOffice.BFF.Application.UserCQ.Commands.DeleteUser;
|
||||||
@@ -8,6 +7,8 @@ using FrontOffice.BFF.Application.UserCQ.Commands.CreateNewOtpToken;
|
|||||||
using FrontOffice.BFF.Application.UserCQ.Commands.VerifyOtpToken;
|
using FrontOffice.BFF.Application.UserCQ.Commands.VerifyOtpToken;
|
||||||
using FrontOffice.BFF.Application.UserCQ.Queries.AdminGetJwtToken;
|
using FrontOffice.BFF.Application.UserCQ.Queries.AdminGetJwtToken;
|
||||||
using FrontOffice.BFF.Application.UserCQ.Commands.SetPasswordForUser;
|
using FrontOffice.BFF.Application.UserCQ.Commands.SetPasswordForUser;
|
||||||
|
using FrontOffice.BFF.User.Protobuf.Protos.User;
|
||||||
|
|
||||||
namespace FrontOffice.BFF.WebApi.Services;
|
namespace FrontOffice.BFF.WebApi.Services;
|
||||||
public class UserService : UserContract.UserContractBase
|
public class UserService : UserContract.UserContractBase
|
||||||
{
|
{
|
||||||
@@ -17,6 +18,7 @@ public class UserService : UserContract.UserContractBase
|
|||||||
{
|
{
|
||||||
_dispatchRequestToCQRS = dispatchRequestToCQRS;
|
_dispatchRequestToCQRS = dispatchRequestToCQRS;
|
||||||
}
|
}
|
||||||
|
[Authorize(Roles = "user")]
|
||||||
public override async Task<Empty> UpdateUser(UpdateUserRequest request, ServerCallContext context)
|
public override async Task<Empty> UpdateUser(UpdateUserRequest request, ServerCallContext context)
|
||||||
{
|
{
|
||||||
return await _dispatchRequestToCQRS.Handle<UpdateUserRequest, UpdateUserCommand>(request, context);
|
return await _dispatchRequestToCQRS.Handle<UpdateUserRequest, UpdateUserCommand>(request, context);
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
using FluentValidation;
|
using FluentValidation;
|
||||||
using FrontOfficeMicroservice.Protobuf.Protos.User;
|
using FrontOffice.BFF.User.Protobuf.Protos.User;
|
||||||
namespace FrontOfficeMicroservice.Protobuf.Validator.User;
|
namespace FrontOfficeMicroservice.Protobuf.Validator.User;
|
||||||
|
|
||||||
public class AdminGetJwtTokenRequestValidator : AbstractValidator<AdminGetJwtTokenRequest>
|
public class AdminGetJwtTokenRequestValidator : AbstractValidator<AdminGetJwtTokenRequest>
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
using FluentValidation;
|
using FluentValidation;
|
||||||
using FrontOfficeMicroservice.Protobuf.Protos.User;
|
using FrontOffice.BFF.User.Protobuf.Protos.User;
|
||||||
namespace FrontOfficeMicroservice.Protobuf.Validator.User;
|
namespace FrontOfficeMicroservice.Protobuf.Validator.User;
|
||||||
|
|
||||||
public class SetPasswordForUserRequestValidator : AbstractValidator<SetPasswordForUserRequest>
|
public class SetPasswordForUserRequestValidator : AbstractValidator<SetPasswordForUserRequest>
|
||||||
|
|||||||
Reference in New Issue
Block a user