Ejemplo n.º 1
0
        public void Dado_um_email_e_senha_validos_o_usuario_deve_ser_autenticado()
        {
            var _email        = "*****@*****.**";
            var _senha        = "P@ssw0rd";
            var command       = new LoginCommand(_email, _senha);
            var commandResult = (GenericCommandResult)_handler.Handle(command);

            Assert.IsTrue(commandResult.Sucess);
        }
Ejemplo n.º 2
0
 public ActionResult Read(string nome)
 {
     try
     {
         var handler = new UsuarioHandler();
         var command = new UsuarioReadCommand()
         {
             Nome = nome
         };
         var result = (CommandResult <UsuarioReadOut>)handler.Handle(command, Service);
         return(Ok(result));
     }
     catch (ArgumentNullException e)
     {
         return(NotFound(new CommandResult <bool>()
         {
             Message = e.Message
         }));
     }
     catch (Exception e)
     {
         return(NotFound(new CommandResult <bool>()
         {
             Message = e.Message
         }));
     }
 }
Ejemplo n.º 3
0
 public IActionResult Update([FromBody] UsuarioUpdateIn value)
 {
     try
     {
         var handler = new UsuarioHandler();
         var command = new UsuarioUpdateCommand()
         {
             Id = value.Id, Nome = value.Nome, Email = value.Email, Senha = value.Senha
         };
         var result = (CommandResult <bool>)handler.Handle(command, Service);
         return(Ok(result));
     }
     catch (ArgumentNullException e)
     {
         return(NotFound(new CommandResult <bool>()
         {
             Message = e.Message
         }));
     }
     catch (Exception e)
     {
         return(NotFound(new CommandResult <bool>()
         {
             Message = e.Message
         }));
     }
 }
Ejemplo n.º 4
0
        public IActionResult Authenticate([FromBody] AuthenticateIn value)
        {
            if (value.Nome == "Wilham Ezequiel de Sousa" && value.Senha == "123")
            {
                var securityKey = Configuration["SecurityKey"];

                var handler = new UsuarioHandler();
                var command = new UsuarioAuthenticateCommand()
                {
                    Nome = value.Nome, Senha = value.Senha, SecurityKey = securityKey
                };
                var result = (CommandResult <string>)handler.Handle(command, Service);
                return(Ok(result));
            }
            else
            {
                bool sucess = false;
                var  result = new CommandResult <bool>()
                {
                    Success = false, Message = "Usuário ou senha inválidos"
                };
                result.Data = sucess;
                return(Unauthorized(result));
            }
        }
Ejemplo n.º 5
0
 public IActionResult TrocaPassword([FromBody] UsuarioTrocaPasswordIn value)
 {
     try
     {
         var handler = new UsuarioHandler();
         var command = new UsuarioTrocaPasswordCommand()
         {
             Nome = value.Nome, SenhaAtual = value.SenhaAtual, SenhaNova = value.SenhaNova
         };
         var result = (CommandResult <bool>)handler.Handle(command, Service);
         return(Ok(result));
     }
     catch (ArgumentNullException e)
     {
         return(NotFound(new CommandResult <bool>()
         {
             Message = e.Message
         }));
     }
     catch (Exception e)
     {
         return(NotFound(new CommandResult <bool>()
         {
             Message = e.Message
         }));
     }
 }
Ejemplo n.º 6
0
 public ActionResult Delete(int id)
 {
     try
     {
         var handler = new UsuarioHandler();
         var command = new UsuarioDeleteCommand()
         {
             Id = id
         };
         var result = (CommandResult <bool>)handler.Handle(command, Service);
         return(Ok(result));
     }
     catch (ArgumentNullException e)
     {
         return(NotFound(new CommandResult <bool>()
         {
             Message = e.Message
         }));
     }
     catch (Exception e)
     {
         return(NotFound(new CommandResult <bool>()
         {
             Message = e.Message
         }));
     }
 }
Ejemplo n.º 7
0
        public GenericCommandResult Create([FromBody] RegistrarUsuarioCommand command, [FromServices] UsuarioHandler handler)
        {
            command.IdFirebase = HttpContext.User.Identities.FirstOrDefault().Claims.FirstOrDefault(c => c.Type.Contains("user_id")).Value;
            command.Email      = HttpContext.User.Identities.FirstOrDefault().Claims.FirstOrDefault(c => c.Type.Contains("email")).Value;

            return((GenericCommandResult)handler.Handle(command));
        }
Ejemplo n.º 8
0
 public GenericCommandResult AlterarUsuario([FromServices] UsuarioHandler handler, [FromBody] AlterarUsuarioCommand usuario)
 {
     return((GenericCommandResult)handler.Handle(new AlterarUsuarioCommand
     {
         IdUsuario = usuario.IdUsuario,
         Perfil = usuario.Perfil,
         Ativo = usuario.Ativo
     }));
 }
Ejemplo n.º 9
0
        public void AutheticateSuccess()
        {
            var handler = new UsuarioHandler();
            var command = new UsuarioAuthenticateCommand()
            {
                SecurityKey = key, Nome = "UsuarioExistente", Senha = "123"
            };
            var result = (CommandResult <string>)handler.Handle(command, Service);

            Assert.AreEqual(true, result.Success);
        }
Ejemplo n.º 10
0
        public void TrocaPasswordSuccess()
        {
            var handler = new UsuarioHandler();
            var command = new UsuarioTrocaPasswordCommand()
            {
                Nome = "UsuarioExistente", SenhaAtual = "123", SenhaNova = "456"
            };
            var result = (CommandResult <bool>)handler.Handle(command, Service);

            Assert.AreEqual(true, result.Success);
        }
Ejemplo n.º 11
0
        public void UpdateEmailErro()
        {
            var handler = new UsuarioHandler();
            var command = new UsuarioUpdateCommand()
            {
                Id = 1, Email = "wil.com", Nome = "wilham", Senha = "123"
            };

            handler.Handle(command, Service);
            Assert.AreEqual(false, handler.Valid);
        }
Ejemplo n.º 12
0
        public void UpdateTamanhoNomeMinimoErro()
        {
            var handler = new UsuarioHandler();
            var command = new UsuarioUpdateCommand()
            {
                Id = 1, Email = "*****@*****.**", Nome = "W", Senha = "123"
            };

            handler.Handle(command, Service);
            Assert.AreEqual(false, handler.Valid); //nao precisa acoplar o result do command
        }
Ejemplo n.º 13
0
        public GenericCommandResult Login([FromServices] UsuarioHandler handler)
        {
            var idUserFirebase = HttpContext.User.Identities.FirstOrDefault().Claims.FirstOrDefault(c => c.Type.Contains("user_id")).Value;
            var email          = HttpContext.User.Identities.FirstOrDefault().Claims.FirstOrDefault(c => c.Type.Contains("email")).Value;

            return((GenericCommandResult)handler.Handle(new LogarUsuarioCommand
            {
                IdFirebase = idUserFirebase,
                Email = email
            }));
        }
Ejemplo n.º 14
0
        public void CreateJaExisteErro()
        {
            var handler = new UsuarioHandler();
            var command = new UsuarioCreateCommand()
            {
                Email = "wil.com", Nome = "Fake", Senha = "123"
            };

            handler.Handle(command, Service);
            Assert.AreEqual(false, handler.Valid);
        }
Ejemplo n.º 15
0
        public void CreateSucesso()
        {
            var handler = new UsuarioHandler();
            var command = new UsuarioCreateCommand()
            {
                Email = "*****@*****.**", Nome = "UsuarioExistente", Senha = "123"
            };

            handler.Handle(command, Service);
            Assert.AreEqual(true, handler.Valid); //nao precisa acoplar o result do command
        }
Ejemplo n.º 16
0
        public void DeleteRegistroInexistenteErro()
        {
            var handler = new UsuarioHandler();
            var command = new UsuarioDeleteCommand()
            {
                Id = 0
            };
            var result = (CommandResult <bool>)handler.Handle(command, Service);

            Assert.AreEqual(false, result.Success);
        }
Ejemplo n.º 17
0
        public void DeleteSucesso()
        {
            var handler = new UsuarioHandler();
            var command = new UsuarioDeleteCommand()
            {
                Id = 1
            };
            var result = (CommandResult <bool>)handler.Handle(command, Service);

            Assert.AreEqual(true, result.Success);
        }
Ejemplo n.º 18
0
        public void ReadRegistroInexistenteErro()
        {
            var handler = new UsuarioHandler();
            var command = new UsuarioReadCommand()
            {
                Nome = "UsuarioInexistente"
            };
            var result = (CommandResult <UsuarioReadOut>)handler.Handle(command, Service);

            Assert.AreEqual(false, result.Success);
        }
Ejemplo n.º 19
0
        public void ReadSucesso()
        {
            var handler = new UsuarioHandler();
            var command = new UsuarioReadCommand()
            {
                Nome = "UsuarioExistente"
            };
            var result = (CommandResult <UsuarioReadOut>)handler.Handle(command, Service);

            Assert.AreEqual(true, result.Success);
        }
Ejemplo n.º 20
0
        public IActionResult Post([FromBody] AdicionarUsuarioCommand command)
        {
            var result = _usuarioHandler.Handle(command);


            return(Response(result, _usuarioHandler.Notifications));
        }
Ejemplo n.º 21
0
        public IComandResult Post([FromBody] RegistroUsuarioComand command)
        {
            var result = (ComandResult)_usuarioHandler.Handle(command);

            this.Commit(result.Success);
            return(result);
        }
 public ICommandResult UsuarioNovo([FromBody] AdicionarUsuarioCommand command)
 {
     return((UsuarioCommandResult)_handler.Handle(command));
 }
Ejemplo n.º 23
0
 public GenericoUsuarioCommandResult Deletar([FromBody] DeletarUsuarioCommand command, [FromServices] UsuarioHandler handler)
 {
     return((GenericoUsuarioCommandResult)handler.Handle(command));
 }
Ejemplo n.º 24
0
        public object Post(

            [FromBody] AutenticarUsuarioComand usuario,
            [FromServices] SigningConfigurations signingConfigurations,
            [FromServices] TokenConfigurations tokenConfigurations)

        {
            bool credenciaisValidas = false;

            var resposta = (ComandResult)_usuarioHandler.Handle(usuario);


            if (resposta.Success == false)
            {
                return(resposta);
            }
            else
            {
                usuarioComand = DeserializerObjeto <UsuarioComand> .RetornoObjetoTipado(resposta.Data);

                credenciaisValidas = true;
                Commit(resposta.Success);
            }

            //var usuarioBase = usersDAO.Find(usuario.UserID);

            //credenciaisValidas = (usuarioBase != null usuario.UserID == usuarioBase.UserID && usuario.AccessKey == usuarioBase.AccessKey);


            if (credenciaisValidas)
            {
                ClaimsIdentity identity = new ClaimsIdentity(

                    new GenericIdentity(usuarioComand.Id, "UsuarioId"),

                    new[] {
                    new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString("N")),
                    new Claim(JwtRegisteredClaimNames.UniqueName, usuarioComand.Id),
                    new Claim(JwtRegisteredClaimNames.Email, usuario.Login),
                    new Claim(JwtRegisteredClaimNames.Sid, usuarioComand.Id),
                    new Claim("Administrador", usuarioComand.TipoUsuario)
                    //new Claim(JwtRegisteredClaimNames.NameId, usuario.Nome),
                    //new Claim(JwtRegisteredClaimNames.UniqueName, usuario.Id.ToString())
                }

                    );

                var usuarioHomeCommandResult = new UsuarioHomeCommandResult(usuario.Login, usuarioComand.Nome, usuarioComand.SobreNome, usuarioComand.Foto, usuarioComand.TipoUsuario);

                DateTime dataCriacao   = DateTime.Now;
                DateTime dataExpiracao = dataCriacao +

                                         TimeSpan.FromSeconds(tokenConfigurations.Seconds);


                var handler = new JwtSecurityTokenHandler();

                var securityToken = handler.CreateToken(new SecurityTokenDescriptor

                {
                    Issuer = tokenConfigurations.Issuer,

                    Audience = tokenConfigurations.Audience,

                    SigningCredentials = signingConfigurations.SigningCredentials,

                    Subject = identity,

                    NotBefore = dataCriacao,

                    Expires = dataExpiracao
                });

                var token = handler.WriteToken(securityToken);


                return(new
                {
                    authenticated = true,

                    created = dataCriacao.ToString("yyyy-MM-dd HH:mm:ss"),

                    expiration = dataExpiracao.ToString("yyyy-MM-dd HH:mm:ss"),

                    accessToken = token,

                    message = "OK",

                    Usuario = usuarioComand
                });
            }

            else

            {
                return(new

                {
                    authenticated = false,

                    message = "Falha ao autenticar"
                });
            }
        }