Exemple #1
0
        public async Task AlterarSenha(AlterarSenhaUsuarioViewModel alterarSenha)
        {
            ContextUserDTO _loggedUser   = _facadeAuth.GetLoggedUser();
            Usuario        _autenticacao = _repository.GetByUserName(_loggedUser.UserName);

            if (!_autenticacao.IsExpiredPassword())
            {
                SignInResult _senhaAntigaResult = await _signInManager.CheckPasswordSignInAsync(_autenticacao, alterarSenha.SenhaAntiga, false);

                if (!_senhaAntigaResult.Succeeded)
                {
                    throw new ApiException(ApiErrorCodes.SENANTINV, nameof(alterarSenha.SenhaAntiga));
                }
            }

            await ValidatePassword(alterarSenha.NovaSenha, alterarSenha.ConfirmacaoNovaSenha, _autenticacao);

            IdentityResult changePasswordResult = await _userManager.ChangePasswordAsync(_autenticacao,
                                                                                         alterarSenha.SenhaAntiga,
                                                                                         alterarSenha.NovaSenha);

            _autenticacao.SetUnexpirablePassword();

            if (!changePasswordResult.Succeeded || !_uow.Commit())
            {
                throw new ApiException(ApiErrorCodes.ERALTSEIDNT);
            }
        }
Exemple #2
0
        protected override Task HandleRequirementAsync(
            AuthorizationHandlerContext context,
            HasPermissionRequirement requirement)
        {
            string _profile = requirement.Profile;

            ContextUserDTO _loggedUser = _facadeAuth.GetLoggedUser();

            if (string.IsNullOrEmpty(_profile) || _loggedUser.HasPermisson(_profile))
            {
                // Pode haver o mesmo attribute na Controller e nos métodos.
                // Então definimos o usuário logado Para que caso passe nesse mesmo
                // handler novamente não retorne Forbidden em um recurso já permitido.
                if (!_loggedUser.IsAllowedInHasPermissionPolicyChallenge)
                {
                    _facadeAuth.SetLoggedUserAllowedInHasPermissionPolicyChallenge();
                }

                context.Succeed(requirement);

                // Segue a pipeline de middlewares.
                return(Task.CompletedTask);
            }

            // Como o middleware de ExceptionHandler foi registrado primeiro,
            // ao lançarmos a Exception, o response da request sairá no formato esperado.
            if (!_loggedUser.IsAuthenticated)
            {
                throw new ApiException(ApiErrorCodes.NOTALLW);
            }

            if (!context.PendingRequirements.Any(pending => pending.GetType() == typeof(HasPermissionRequirement) &&
                                                 _loggedUser.HasPermisson(((HasPermissionRequirement)pending).Profile)))
            {
                context.Fail();
                throw new ApiException(ApiErrorCodes.NOTALLW);
            }

            context.Succeed(requirement);
            return(Task.CompletedTask);
        }