public void UserAuthHandler_RegisterAdmin_Valid()
        {
            var repository    = new FakeUserAuthRepository();
            var logRepository = new FakeAccessLogRepository();
            var handler       = new UserAuthHandler(repository, logRepository);
            var command       = new RegisterAdminUserAuthCommand();

            command.Username = "******";
            command.Password = "******";
            command.Role     = "user";
            command.Active   = true;
            var result = handler.RegisterAdmin(command, "userIdentity");

            Assert.IsTrue(result.Success);
        }
        public CommandResult RegisterAdmin(RegisterAdminUserAuthCommand command, string userIdentity)
        {
            var exist = _repository.Exists(command.Username);

            if (exist)
            {
                AddNotification("Já existe um usuario cadastrado com esse nome. ");
            }

            var user = new UserAuth(command.Username, command.Password, command.Role, command.Active);

            AddNotifications(user);

            if (Invalid)
            {
                return(new CommandResult(false, GroupNotifications.Group(Notifications), command));
            }

            // Add Hash e Salt
            var salt = Salt.Create();
            var hash = Hash.Create(user.Password, salt);

            if (!Hash.Validate(user.Password, salt, hash))
            {
                AddNotification("Erro na geração do Hash. ");
            }

            user.AddHash(hash, Convert.ToBase64String(salt));

            _repository.Register(user);

            var log = new AccessLog(
                "Register",
                DateTime.Now,
                userIdentity,
                "UserAuth",
                $"Nome usuario registrado: {command.Username}");

            _log.Register(log);

            user.HidePassword();

            return(new CommandResult(true, "Cadastro realizado. ", user));
        }
Ejemplo n.º 3
0
        public CommandResult RegisterAdmin(RegisterAdminUserAuthCommand command)
        {
            CommandResult result = _handler.RegisterAdmin(command, User.Identity.Name);

            return(result);
        }