public async Task ChangeAccountStatusAsync(ChangeAccountStatusRequest changeAccountStatus, int userId) { var user = await _uow.UserRepository.FindBy(u => u.Identity == changeAccountStatus.Identity).FirstOrDefaultAsync(); if (user == null) { throw new UserNotFoundException(_localizer); } if (user.Id == userId && changeAccountStatus.Active == false) { throw new AccountDeactivatedForbiddenException(_localizer); } if (changeAccountStatus.Active == false) { user.Status = StatusEnum.INACTIVE; } else { user.Status = StatusEnum.ACTIVE; } user.ModifiedAt = DateTime.UtcNow; _uow.UserRepository.Update(user); await _uow.CommitAsync(); }
public async Task <IActionResult> ChangeAccountStatus([FromBody] ChangeAccountStatusRequest changeAccountStatus) { var loggedUser = User.GetUserIdFromToken(); await _accountService.ChangeAccountStatusAsync(changeAccountStatus, loggedUser); return(Ok()); }
public async Task <User> ChangeAccountStatusAsync(ChangeAccountStatusRequest changeAccountStatus, int userId) { var user = await _uow.UserRepository.FindBy(u => u.Id == userId).FirstOrDefaultAsync(); if (user == null) { throw new NotFoundException(ExceptionConstants.NOT_FOUND, "User"); } if (user.Id == userId && changeAccountStatus.Active == false) { throw new MismeAPI.Common.Exceptions.InvalidDataException(ExceptionConstants.INVALID_DATA, "Account"); } if (changeAccountStatus.Active == false) { user.Status = StatusEnum.INACTIVE; } else { user.Status = StatusEnum.ACTIVE; } user.ModifiedAt = DateTime.UtcNow; _uow.UserRepository.Update(user); await _uow.CommitAsync(); return(user); }
public async Task <IActionResult> ChangeAccountStatus([FromBody] ChangeAccountStatusRequest changeAccountStatus) { var claimsIdentity = this.User.Identity as ClaimsIdentity; var userId = claimsIdentity.FindFirst(ClaimTypes.UserData)?.Value; await _accountService.ChangeAccountStatusAsync(changeAccountStatus, int.Parse(userId)); return(Ok()); }
public async Task <IActionResult> ChangeAccountStatus([FromBody] ChangeAccountStatusRequest changeAccountStatus) { var loggedUser = User.GetUserIdFromToken(); var user = await _accountService.ChangeAccountStatusAsync(changeAccountStatus, loggedUser); var mapped = _mapper.Map <UserResponse>(user); if (user.Status == StatusEnum.ACTIVE) { await _hub.Clients.All.SendAsync(HubConstants.USER_ACTIVATED, mapped); } else if (user.Status == StatusEnum.INACTIVE) { await _hub.Clients.All.SendAsync(HubConstants.USER_DISABLED, mapped); } return(Ok()); }
public void InactiveUserChangeAccountStatusHimselfShouldReturnForbiddenException() { // ARRANGE var fakeClaims = new List <Claim>() { new(ClaimTypes.UserData, "5") }; var identity = new ClaimsIdentity(fakeClaims, "Test"); var claimsPrincipal = new ClaimsPrincipal(identity); var httpContext = new DefaultHttpContext { User = new ClaimsPrincipal(claimsPrincipal) }; httpContext.Request.Headers.Add("Authorization", @"Bearer s0m34cc$3$$T0k3n"); using var context = new CoreDbContext(ContextOptions); var accountService = new AccountService(Config.Object, new UnitOfWork(context), new Mock <IStringLocalizer <IAccountService> >().Object, DetectionService.Object, storageService); var accountController = new AccountController(accountService, new Mock <AutoMapper.IMapper>().Object, new Mock <IEmailService>().Object, new Mock <IWebHostEnvironment>().Object) { ControllerContext = new ControllerContext() { HttpContext = httpContext } }; var fakeChangeAccountStatus = new ChangeAccountStatusRequest { Identity = "someRandomIdentityString", Active = true }; // ACT var aggregateException = accountController.ChangeAccountStatus(fakeChangeAccountStatus).Exception; var taskResult = (BaseForbiddenException)aggregateException?.InnerException; // ASSERT if (taskResult != null) { Assert.Equal(403, taskResult.HttpCode); } }
public void SuccessfullyChangeAccountStatusShouldReturnOk() { // ARRANGE var fakeClaims = new List <Claim>() { new(ClaimTypes.UserData, "4") }; var identity = new ClaimsIdentity(fakeClaims, "Test"); var claimsPrincipal = new ClaimsPrincipal(identity); var httpContext = new DefaultHttpContext { User = new ClaimsPrincipal(claimsPrincipal) }; httpContext.Request.Headers.Add("Authorization", @"Bearer s0m34cc$3$$T0k3n"); using var context = new CoreDbContext(ContextOptions); var accountService = new AccountService(Config.Object, new UnitOfWork(context), new Mock <IStringLocalizer <IAccountService> >().Object, DetectionService.Object, storageService); var accountController = new AccountController(accountService, new Mock <AutoMapper.IMapper>().Object, new Mock <IEmailService>().Object, new Mock <IWebHostEnvironment>().Object) { ControllerContext = new ControllerContext() { HttpContext = httpContext } }; var fakeChangeAccountStatus = new ChangeAccountStatusRequest { Identity = "someRandomIdentityString", Active = true }; // ACT var taskResult = (OkResult)accountController.ChangeAccountStatus(fakeChangeAccountStatus).Result; // ASSERT Assert.Equal(200, taskResult.StatusCode); }
public async Task ChangeAccountStatusAsync(ChangeAccountStatusRequest changeAccountStatus, int userId) { // Check if the Master exist var master = await _uow.UserRepository.FirstOrDefaultAsync(u => u.Id == userId); if (master == null) { throw new UserNotFoundException(_localizer); } // Find the Inactive User var user = await _uow.UserRepository.FirstOrDefaultAsync(u => u.Identity == changeAccountStatus.Identity); if (user == null) { throw new UserNotFoundException(_localizer); } // You can't reActivate yourself if (user.Id == userId && user.Status == StatusEnum.INACTIVE) { throw new AccountDeactivatedForbiddenException(_localizer); } if (changeAccountStatus.Active == false) { user.Status = StatusEnum.INACTIVE; } else { user.Status = StatusEnum.ACTIVE; } user.ModifiedAt = DateTime.UtcNow; _uow.UserRepository.Update(user); await _uow.CommitAsync(); }
public ActionResult ChangeAccountStatus(ChangeAccountStatusRequest request) { using (var db = new LibraryEntities()) { // select account associated with username var targetAccount = FindAccount(db, request.User); if (targetAccount == null) { // if user not found return(notification.ResetPasswordUserNotFound()); } var newStatus = new AccountStatusLog { AccountId = targetAccount.AccountId, AccountStatusTypeId = request.StatusId, LogDateTime = DateTime.Now }; using (var transaction = db.Database.BeginTransaction()) { try { db.AccountStatusLogs.Add(newStatus); db.SaveChanges(); transaction.Commit(); return(notification.UpdateAccountStatusSuccess()); } catch (Exception) { transaction.Rollback(); return(notification.UnknownError()); } } } }