Example #1
0
 public bool Respected(
     IHttpContextAccessor httpContextAccessor,
     AuthorizationHandlerContext context,
     IPerfilDeAcessoService perfilDeAcessoService)
 {
     try
     {
         return(perfilDeAcessoService.Obter(context.User.UserIdSession())?.Perfil == TipoUsuario.Admin);
     }
     catch
     {
         return(false);
     }
 }
Example #2
0
        public async Task <CredentialModel> ValidateCredentials(UserModel user)
        {
            bool           credenciaisValidas = false;
            Usuario        userIdentity       = null;
            PerfilDeAcesso perfilDeAcesso     = null;

            user.Validate();

            if (user != null)
            {
                userIdentity = await _userManager.FindByEmailAsync(user.UserID);

                if (userIdentity != null)
                {
                    SignInResult resultadoLogin = await _signInManager.CheckPasswordSignInAsync(
                        userIdentity,
                        user.Password,
                        false
                        );

                    if (resultadoLogin.Succeeded)
                    {
                        credenciaisValidas = await _userManager.IsInRoleAsync(userIdentity, RolesModel.Principal);

                        perfilDeAcesso = _perfilDeAcessoService.Obter(userIdentity.Id);

                        if (perfilDeAcesso.EstaInativo())
                        {
                            throw new UnauthorizedAccessException("Usuário inativado, entre em contato com um administrador");
                        }
                    }
                    else
                    {
                        throw new UnauthorizedAccessException("E-mail ou senha inválidos");
                    }
                }
                else
                {
                    throw new UnauthorizedAccessException("E-mail ou senha inválidos");
                }
            }
            else
            {
                throw new InvalidOperationException("Ocorreu um erro desconhecido, se persistir reporte");
            }

            return(new CredentialModel(credenciaisValidas, new UserModel(userIdentity.Id, userIdentity.Email, userIdentity.UserName, perfilDeAcesso.Perfil.ToString())));
        }