Esempio n. 1
0
        public async Task <TblDocumentos> Delete(int idDocumento)
        {
            TblDocumentos documento = await _repository.GetById(idDocumento);

            documento.DFim = DateTime.Now;

            return(await _repository.Update(documento));
        }
Esempio n. 2
0
        public async Task <TblDocumentos> Update(TblDocumentos documento)
        {
            _context.Entry(documento).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();

                return(documento);
            }
            catch (Exception exception)
            {
                throw exception;
            }
        }
Esempio n. 3
0
        public async Task <TblDocumentos> Create(TblDocumentos documento)
        {
            _context.TblDocumentos.Add(documento);

            try
            {
                await _context.SaveChangesAsync();

                return(documento);
            }
            catch (Exception exception)
            {
                throw exception;
            }
        }
        public async Task <ActionResult <TblDocumentos> > Update([FromBody] TblDocumentos documento)
        {
            if (documento == null)
            {
                return(BadRequest(ModelState));
            }

            try
            {
                return(Ok(await _service.Update(documento)));
            }
            catch (Exception exception)
            {
                return(BadRequest(exception));
            }
        }
Esempio n. 5
0
        public async Task <object> NovaSenha(QpNovaSenha parametros)
        {
            TblDocumentos documento = await _documentosService.GetByDocumento(parametros.Documento);

            if (documento != null)
            {
                TblPessoas pessoa = await _pessoasService.GetById(documento.NIdPessoa);

                if (pessoa.DNascimento == parametros.DataNascimento)
                {
                    var    plainTextBytes     = Encoding.UTF8.GetBytes(parametros.Senha);
                    string encodedText        = Convert.ToBase64String(plainTextBytes);
                    string senhaCriptografada = encodedText;

                    TblSenhas senha = new TblSenhas {
                        NIdPessoa = pessoa.NIdPessoa, SSenha = senhaCriptografada
                    };

                    try
                    {
                        bool senhaCriada = (await _senhasService.Create(senha) != null);

                        if (senhaCriada)
                        {
                            return(_tokenService.CreateToken(pessoa.NIdPessoa));
                        }
                        else
                        {
                            throw new ArgumentException("Falha ao criar senha.", "mensagem");
                        }
                    }
                    catch (Exception exception)
                    {
                        throw new ArgumentException(exception.Message, "mensagem");
                    }
                }
                else
                {
                    throw new ArgumentException("Data de nascimento inválida.", "mensagem");
                }
            }
            else
            {
                throw new ArgumentException("Documento não localizado.", "mensagem");
            }
        }
Esempio n. 6
0
        public async Task <object> Login(CredentialsModel credentials)
        {
            // Variável auxiliar
            TblPessoas baseUser = new TblPessoas();

            // Altera o fluxo dependendo a validação solicitada
            if (credentials.GrantType == "password")
            {
                // Verifica se o usuário ou senha são nulos
                if (!string.IsNullOrWhiteSpace(credentials.User) && !string.IsNullOrWhiteSpace(credentials.Password))
                {
                    // Variáveis auxiliares
                    bool userExiste = false;
                    int  idPessoa   = 0;

                    // Determina se é email ou cpf e busca a existência do dado
                    if (credentials.User.All(char.IsDigit))
                    {
                        TblDocumentos documento = await _documentosService.GetByDocumento(credentials.User);

                        if (documento != null)
                        {
                            userExiste = true;
                            idPessoa   = documento.NIdPessoa;
                        }
                    }
                    else
                    {
                        TblEmails email = await _emailsService.GetByEmail(credentials.User);

                        if (email != null)
                        {
                            userExiste = true;
                            idPessoa   = email.NIdPessoa;
                        }
                    }

                    if (userExiste)
                    {
                        // Converte a senha em base64
                        var    plainTextBytes = Encoding.UTF8.GetBytes(credentials.Password);
                        string encodedText    = Convert.ToBase64String(plainTextBytes);
                        credentials.Password = encodedText;

                        // Verifica a existência
                        bool senhaExiste = (await _senhasService.GetBySenha(idPessoa, credentials.Password) != null);

                        if (senhaExiste && idPessoa > 0)
                        {
                            baseUser = await _pessoasService.GetById(idPessoa);
                        }
                        else
                        {
                            throw new ArgumentException("Usuário e/ou senha incorreto(s).", "mensagem");
                        }
                    }
                    else
                    {
                        throw new ArgumentException("Usuário não existe.", "mensagem");
                    }
                }
                else
                {
                    throw new ArgumentException("Usuário e/ou senha incorreto(s).", "mensagem");
                }
            }
            else if (credentials.GrantType == "refresh_token")
            {
                // Verifica se o usuário ou refresh token são nulos
                if (!string.IsNullOrWhiteSpace(credentials.User) && !string.IsNullOrWhiteSpace(credentials.RefreshToken))
                {
                    bool tokenValido = false;
                    // Busca a pessoa relacionada ao token (User = ID)
                    baseUser = await _pessoasService.GetById(Convert.ToInt32(credentials.User));

                    tokenValido = _tokenService.DeleteToken(credentials.RefreshToken, baseUser.NIdPessoa);

                    if (!tokenValido)
                    {
                        throw new ArgumentException("Usuário e/ou token inválido(s).", "mensagem");
                    }
                }
                else
                {
                    throw new ArgumentException("Usuário e/ou token inválido(s).", "mensagem");
                }
            }

            return(_tokenService.CreateToken(baseUser.NIdPessoa));
        }
Esempio n. 7
0
        public async Task <TblDocumentos> Update(TblDocumentos documento)
        {
            documento.DAlteracao = DateTime.Now;

            return(await _repository.Update(documento));
        }
Esempio n. 8
0
        public async Task <TblDocumentos> Create(TblDocumentos documento)
        {
            documento.DInicio = DateTime.Now;

            return(await _repository.Create(documento));
        }