Exemplo n.º 1
0
        public List <UsuarioDTO> FindUsuarios <KProperty>(string texto, Expression <Func <Usuario, KProperty> > orderByExpression, bool ascending, int pageIndex, int pageCount)
        {
            try
            {
                if (pageIndex <= 0 || pageCount <= 0)
                {
                    throw new Exception("Argumentos da paginação inválidos.");
                }

                var            spec     = UsuarioSpecifications.ConsultaTexto(texto);
                List <Usuario> usuarios = _usuarioRepository.GetPaged <KProperty>(pageIndex, pageCount, spec, orderByExpression, ascending).ToList();

                var adapter = TypeAdapterFactory.CreateAdapter();
                return(adapter.Adapt <List <Usuario>, List <UsuarioDTO> >(usuarios));
            }
            catch (ApplicationValidationErrorsException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                LoggerFactory.CreateLog().LogError(ex);
                throw new Exception("O servidor não respondeu.");
            }
        }
 public long CountUsuarios(string texto)
 {
     try
     {
         var spec = UsuarioSpecifications.ConsultaTexto(texto);
         return(_usuarioRepository.Count(spec));
     }
     catch (Exception ex)
     {
         throw ManipuladorDeExcecao.TrateExcecao(ex);
     }
 }
        private void ValideToken(string token)
        {
            var spec       = UsuarioSpecifications.ConsulteTokenSenha(token);
            var tokenSenha = _tokenSenhaRepository.AllMatching(spec).SingleOrDefault();

            if (tokenSenha == null)
            {
                throw new AppException("Link inválido ou já utilizado.");
            }

            if (tokenSenha.Data > DateTime.Now.AddDays(2))
            {
                throw new AppException("Este link expirou.");
            }
        }
        public List <UsuarioListDTO> FindUsuarios <KProperty>(string texto, Expression <Func <Usuario, KProperty> > orderByExpression, bool ascending, int pageIndex, int pageCount)
        {
            try
            {
                var            spec     = UsuarioSpecifications.ConsultaTexto(texto);
                List <Usuario> usuarios = _usuarioRepository.GetPaged <KProperty>(pageIndex, pageCount, spec, orderByExpression, ascending).ToList();

                var adapter = TypeAdapterFactory.CreateAdapter();
                return(adapter.Adapt <List <Usuario>, List <UsuarioListDTO> >(usuarios));
            }
            catch (Exception ex)
            {
                throw ManipuladorDeExcecao.TrateExcecao(ex);
            }
        }
Exemplo n.º 5
0
 public long CountUsuarios(string texto)
 {
     try
     {
         var spec = UsuarioSpecifications.ConsultaTexto(texto);
         return(_usuarioRepository.Count(spec));
     }
     catch (ApplicationValidationErrorsException ex)
     {
         throw ex;
     }
     catch (Exception ex)
     {
         LoggerFactory.CreateLog().LogError(ex);
         throw new Exception("O servidor não respondeu.");
     }
 }
        void AlterarUsuario(Usuario persistido, Usuario corrente)
        {
            var validator = EntityValidatorFactory.CreateValidator();

            if (!validator.IsValid(corrente))
            {
                throw new AppException(validator.GetInvalidMessages <Usuario>(corrente));
            }

            var specExisteUsuario = UsuarioSpecifications.ConsultaNomeUsuario(corrente.NomeUsuario);

            if (_usuarioRepository.AllMatching(specExisteUsuario).Where(c => c.Id != persistido.Id).Any())
            {
                throw new AppException("Já existe um usuário cadastrado com este nome de usuário.");
            }

            _usuarioRepository.Merge(persistido, corrente);
            _usuarioRepository.Commit();
        }
        void SalvarUsuario(Usuario usuario)
        {
            var validator = EntityValidatorFactory.CreateValidator();

            if (!validator.IsValid(usuario))
            {
                throw new AppException(validator.GetInvalidMessages <Usuario>(usuario));
            }

            var specExisteUsuario = UsuarioSpecifications.ConsultaNomeUsuario(usuario.NomeUsuario);

            if (_usuarioRepository.AllMatching(specExisteUsuario).Any())
            {
                throw new AppException("Já existe um usuário cadastrado com este nome de usuário.");
            }

            _usuarioRepository.Add(usuario);
            _usuarioRepository.Commit();
        }
Exemplo n.º 8
0
        public List <UsuarioDTO> FindUsuarios <KProperty>(string texto, Expression <Func <Usuario, KProperty> > orderByExpression, bool ascending = true)
        {
            try
            {
                var            spec     = UsuarioSpecifications.ConsultaTexto(texto);
                List <Usuario> usuarios = _usuarioRepository.AllMatching <KProperty>(spec, orderByExpression, ascending).ToList();

                var adapter = TypeAdapterFactory.CreateAdapter();
                return(adapter.Adapt <List <Usuario>, List <UsuarioDTO> >(usuarios));
            }
            catch (ApplicationValidationErrorsException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                LoggerFactory.CreateLog().LogError(ex);
                throw new Exception("O servidor não respondeu.");
            }
        }
Exemplo n.º 9
0
        public void AutenticarUsuario(string email, string senha)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(email))
                {
                    throw new ApplicationValidationErrorsException("Digite o email.");
                }

                if (string.IsNullOrWhiteSpace(senha))
                {
                    throw new ApplicationValidationErrorsException("Digite a senha.");
                }

                var spec    = UsuarioSpecifications.ConsultaEmail(email);
                var usuario = _usuarioRepository.AllMatching(spec).SingleOrDefault();
                if (usuario == null)
                {
                    throw new ApplicationValidationErrorsException("Usuário não encontrado.");
                }

                if (usuario.Senha != senha)
                {
                    throw new ApplicationValidationErrorsException("Senha inválida.");
                }

                var adapter    = TypeAdapterFactory.CreateAdapter();
                var usuarioDTO = adapter.Adapt <Usuario, UsuarioDTO>(usuario);

                ControladorDeSessao.Autenticar(usuarioDTO);
            }
            catch (ApplicationValidationErrorsException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                LoggerFactory.CreateLog().LogError(ex);
                throw new Exception("O servidor não respondeu.");
            }
        }
        public void RedefinirSenhaComToken(string token, string senha, string confirmaSenha)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(token))
                {
                    throw new ArgumentException("Valor não pode ser nulo ou vazio.", "token");
                }

                ValideToken(token);

                if (string.IsNullOrWhiteSpace(senha))
                {
                    throw new AppException("Nova senha não informada.");
                }

                if (string.IsNullOrWhiteSpace(confirmaSenha))
                {
                    throw new AppException("Confirmação da nova senha não informada.");
                }

                if (senha != confirmaSenha)
                {
                    throw new AppException("Nova senha não confere com a confirmação da nova senha.");
                }

                var spec       = UsuarioSpecifications.ConsulteTokenSenha(token);
                var tokenSenha = _tokenSenhaRepository.AllMatching(spec).Single();

                var usuario = _usuarioRepository.Get(tokenSenha.UsuarioId);

                usuario.Senha = Encryption.Encrypt(senha);
                _tokenSenhaRepository.Remove(tokenSenha);

                _tokenSenhaRepository.Commit();
            }
            catch (Exception ex)
            {
                throw ManipuladorDeExcecao.TrateExcecao(ex);
            }
        }
        public string GerarTokenSenha(int usuarioId)
        {
            try
            {
                if (usuarioId <= 0)
                {
                    throw new ArgumentException("Valor inválido.", "usuarioId");
                }

                var usuario = _usuarioRepository.Get(usuarioId);
                if (usuario == null)
                {
                    throw new Exception("Usuario não encontrada.");
                }

                var spec         = UsuarioSpecifications.ConsulteTokenSenhaUsuario(usuarioId);
                var tokensSenhas = _tokenSenhaRepository.AllMatching(spec).ToList();

                foreach (var item in tokensSenhas.ToList())
                {
                    _tokenSenhaRepository.Remove(item);
                }

                var tokenSenha = UsuarioFactory.CreateTokenSenha(usuarioId);
                var validator  = EntityValidatorFactory.CreateValidator();
                if (!validator.IsValid(tokenSenha))
                {
                    throw new AppException(validator.GetInvalidMessages(tokenSenha));
                }

                _tokenSenhaRepository.Add(tokenSenha);
                _tokenSenhaRepository.Commit();

                return(tokenSenha.Token);
            }
            catch (Exception ex)
            {
                throw ManipuladorDeExcecao.TrateExcecao(ex);
            }
        }
        public void AutenticarUsuario(string nomeUsuario, string senha, bool continuarConectado)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(nomeUsuario))
                {
                    throw new AppException("Digite o nome de usuário.");
                }

                if (string.IsNullOrWhiteSpace(senha))
                {
                    throw new AppException("Digite a senha.");
                }

                var spec    = UsuarioSpecifications.ConsultaNomeUsuario(nomeUsuario);
                var usuario = _usuarioRepository.AllMatching(spec).SingleOrDefault();
                if (usuario == null)
                {
                    throw new AppException("Usuário não encontrado.");
                }

                if (usuario.Senha != Encryption.Encrypt(senha))
                {
                    throw new AppException("Senha inválida.");
                }

                if (!usuario.Ativo)
                {
                    throw new AppException("Não foi possível autenticar o usuário. Por favor contate o suporte.");
                }

                //TODO: verificar usuario inadimplente

                ControladorDeSessao.Autenticar(usuario, continuarConectado);
            }
            catch (Exception ex)
            {
                throw ManipuladorDeExcecao.TrateExcecao(ex);
            }
        }
        public UsuarioDTO FindUsuario(string nomeUsuario)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(nomeUsuario))
                {
                    throw new AppException("Informe o nome de usuário.");
                }

                var spec    = UsuarioSpecifications.ConsultaNomeUsuario(nomeUsuario);
                var usuario = _usuarioRepository.AllMatching(spec).SingleOrDefault();
                if (usuario == null)
                {
                    throw new AppException("Usuário não encontrado.");
                }

                var adapter = TypeAdapterFactory.CreateAdapter();
                return(adapter.Adapt <Usuario, UsuarioDTO>(usuario));
            }
            catch (Exception ex)
            {
                throw ManipuladorDeExcecao.TrateExcecao(ex);
            }
        }