public async Task <ActionResult <AuthResult> > AccessTokenAsync(UserCredentials credentials)
        {
            var user = await _userService.GetUserAsync(credentials.UserName);

            if (user == null)
            {
                return(NotFound(OperationDetails.Error($"User '{credentials.UserName}' not found.")));
            }
            var identity = user.Identity as UserIdentity;

            if (await _userManager.CheckPasswordAsync(identity, credentials.Password))
            {
                var result = new AuthResult
                {
                    AccessToken = await JwtHelper.GetEncodedJwtAsync(identity, _userManager),
                    AppState    = new InitialAppState
                    {
                        IsAuthenticated = true,
                        UserName        = user.Identity.UserName,
                        Photo           = Url.Content(user.Profile.PhotoFilePath),
                        IsAdmin         = await _userManager.IsInRoleAsync(user.Identity as UserIdentity, "Administrator"),
                    }
                };
                if (result.AppState.IsAdmin)
                {
                    result.AppState.NewMessagesCount = (await _messageService.GetMessagesAsync()).Where(x => !x.IsRead).Count();
                    result.AppState.Roles            = result.AppState.IsAdmin ? _roleManager.Roles.Select(role => role.Name) : null;
                }
                return(result);
            }
            return(BadRequest(OperationDetails.Error("Invalid password.")));
        }
        public override void OnException(ExceptionContext context)
        {
            var ex = context.Exception;

            if (ex is EntityNotFoundException)
            {
                context.Result = new NotFoundObjectResult(OperationDetails.Error(ex.Message));
                return;
            }

            if (ex is DuplicateEntityException)
            {
                context.Result = new BadRequestObjectResult(OperationDetails.Error(ex.Message));
                return;
            }

            if (ex is DomainServiceException)
            {
                string message;
                if (ex.InnerException != null && ex.InnerException is InvalidOperationException)
                {
                    message = ex.InnerException.Message;
                }
                else
                {
                    message = ex.Message;
                }
                context.Result = new BadRequestObjectResult(OperationDetails.Error(message));
                return;
            }

            context.Result = new StatusCodeResult(500);
        }
예제 #3
0
        public IActionResult Withdraw([FromBody] double amount)
        {
            if (amount < _bankingService.GetBankOperationMinAmoun())
            {
                return(BadRequest(OperationDetails.Error("Withdraw error")));
            }

            var result = _bankingService.Withdraw(new BankOperation(_userIdentityService.GetUserId(User.Claims), amount));

            return(result.Succeeded == true ? (IActionResult)Ok(result) : BadRequest(result));
        }
        public async Task <ActionResult <DomainUser> > GetUserAsync([FromRoute] string userName)
        {
            var user = await _userService.GetUserAsync(userName);

            if (user == null)
            {
                return(NotFound(OperationDetails.Error($"The entity of type '{typeof(DomainUser)}' with value '{userName}' " +
                                                       $"for '{nameof(IUserIdentity.UserName)}' not found.")));
            }
            return(PrepareForReturn(user));
        }
예제 #5
0
        public IActionResult RegisterUser([FromBody] Identity identity)
        {
            if (identity == null)
            {
                return(BadRequest(OperationDetails.Error("Registration error")));
            }

            var result = _userIdentityService.RegisterUser(identity.Name, identity.Password);

            return(result.Succeeded == true ? (IActionResult)Ok(result) : BadRequest(result));
        }
예제 #6
0
        public IActionResult Transfer([FromBody] BankOperation bankOperation)
        {
            if (bankOperation == null || bankOperation.Amount < _bankingService.GetBankOperationMinAmoun())
            {
                return(BadRequest(OperationDetails.Error("Transfer error")));
            }

            bankOperation.SenderId = _userIdentityService.GetUserId(User.Claims);
            var result = _bankingService.Transfer(bankOperation);

            return(result.Succeeded == true ? (IActionResult)Ok(result) : BadRequest(result));
        }
        public async Task <IActionResult> RegisterAsync([FromForm] RegisterUserBindingModel model)
        {
            var user = await _userService.GetUserAsync(model.UserName);

            if (user == null)
            {
                var creator = await _userService.GetUserAsync(User?.Identity?.Name);

                await _userService.CreateUserAsync(model.UserName, model.Password, model.Email, model.Role, creator);

                return(Ok(OperationDetails.Success($"User '@{model.UserName}' has been registered successfully.")));
            }
            else
            {
                return(BadRequest(OperationDetails.Error($"User '@{model.UserName}' already exists.")));
            }
        }
        private UserIdentityController GetUserIdentityController()
        {
            var mockIdentityService = new Mock <IUserIdentityService>();

            mockIdentityService.Setup(identit => identit.IdentityUser(It.Is <string>(s => s.Contains(invalidUser.Name)),
                                                                      It.IsAny <string>())).Returns(invalidUser);

            mockIdentityService.Setup(identit => identit.IdentityUser(It.Is <string>(s => s.Contains(validUser.Name)),
                                                                      It.Is <string>(s => s.Contains(validUser.Password)))).Returns(validUser);

            mockIdentityService.Setup(identit => identit.RegisterUser(It.Is <string>(s => s.Contains(validUser.Name)),
                                                                      It.IsAny <string>())).Returns(OperationDetails.Error(""));

            mockIdentityService.Setup(identit => identit.RegisterUser(It.Is <string>(s => s.Contains(validUser.Name)),
                                                                      It.Is <string>(s => s.Contains(validUser.Password)))).Returns(validOperationDetails);

            mockIdentityService.Setup(identit => identit.GetIdentity(It.Is <User>(u => u.Name == validUser.Name && u.Password == validUser.Password)))
            .Returns("token");

            var controller = new UserIdentityController(mockIdentityService.Object);

            return(controller);
        }