public ActionResult LogOff()
        {
            var command = new LogOffCommand();

            HandleCommand(command, Json("LogOff"));

            return RedirectToAction("Index", "Home");
        }
Example #2
0
        public ActionResult LogOff()
        {
            var command = new LogOffCommand();

            HandleCommand(command, Json("LogOff"));

            return(RedirectToAction("Index", "Home"));
        }
        public async void ReadCommandSettings(MqttPublisher publisher)
        {
            while (this.CommandSettingsFileLocked)
            {
                await Task.Delay(500);
            }
            this.CommandSettingsFileLocked = true;
            List <ConfiguredCommand> commands = new List <ConfiguredCommand>();

            using (var stream = new FileStream(Path.Combine(path, "configured-commands.json"), FileMode.Open))
            {
                Log.Logger.Information($"reading configured commands from: {stream.Name}");
                if (stream.Length > 0)
                {
                    commands = await JsonSerializer.DeserializeAsync <List <ConfiguredCommand> >(stream);
                }
                stream.Close();
                this.CommandSettingsFileLocked = false;
            }

            foreach (ConfiguredCommand configuredCommand in commands)
            {
                AbstractCommand command = null;
                switch (configuredCommand.Type)
                {
                case "ShutdownCommand":
                    command = new ShutdownCommand(publisher, configuredCommand.Name, configuredCommand.Id);
                    break;

                case "RestartCommand":
                    command = new RestartCommand(publisher, configuredCommand.Name, configuredCommand.Id);
                    break;

                case "LogOffCommand":
                    command = new LogOffCommand(publisher, configuredCommand.Name, configuredCommand.Id);
                    break;

                case "CustomCommand":
                    command = new CustomCommand(publisher, configuredCommand.Command, configuredCommand.Name, configuredCommand.Id);
                    break;

                default:
                    Log.Logger.Error("unsupported command type in config");
                    break;
                }
                if (command != null)
                {
                    this.ConfiguredCommands.Add(command);
                }
            }
        }
Example #4
0
        public async Task Handle_ErrorGettingCurrentUser_ReturnGetCurrentUserError()
        {
            // Arrange

            LogOffCommand request      = new LogOffCommand();
            Result        failedResult = Results.Fail <int>(new Error());

            _currentUserService.Setup(x => x.GetIdCurrentUser()).Returns(failedResult);

            // Act

            Result result = await _handler.Handle(request, CancellationToken.None);

            // Assert
            result.Should().BeEquivalentTo(failedResult);
        }
Example #5
0
        public async Task Handle_CurrentUserDoesntExist_SpecificErrorAnd401()
        {
            // Arrange

            LogOffCommand request   = new LogOffCommand();
            int           accountId = ConstantsAccountsCQTest.Id;

            _currentUserService.Setup(x => x.GetIdCurrentUser()).Returns(Results.Ok <int>(accountId));
            _accountRepository.Setup(x => x.ExistsAccount(accountId)).Returns(false);

            // Act

            Result result = await _handler.Handle(request, CancellationToken.None);

            // Assert
            result.IsSuccess.Should().BeFalse();
            result.Errors.Count.Should().Be(1);
            result.Errors[0].Message.Should().Be(ErrorsMessagesConstants.USER_ID_NOT_FOUND);
            result.Errors[0].Metadata[ErrorKeyPropsConstants.ERROR_CODE].Should().Be(ErrorsCodesContants.USER_ID_NOT_FOUND);
            result.Errors[0].Metadata[ErrorKeyPropsConstants.ERROR_HTTP_CODE].Should().Be(401);
        }
Example #6
0
        public async Task Handle_ValidUser_HappyFlow()
        {
            // Arrange

            LogOffCommand request   = new LogOffCommand();
            int           accountId = ConstantsAccountsCQTest.Id;

            _currentUserService.Setup(x => x.GetIdCurrentUser()).Returns(Results.Ok <int>(accountId));
            _accountRepository.Setup(x => x.ExistsAccount(accountId)).Returns(true);
            _accountRepository.Setup(x => x.GetAccount(accountId)).Returns(ConstantsAccountsCQTest.AccountTest);
            Result successResult = Results.Ok();

            _accountRepository.Setup(x => x.SaveModifications()).Returns(successResult);

            // Act

            Result result = await _handler.Handle(request, CancellationToken.None);

            // Assert
            result.IsSuccess.Should().BeTrue();
            result.Should().Be(successResult);
            ConstantsAccountsCQTest.AccountTest.Token.Should().Be(string.Empty);
        }
        /// <summary>
        /// Adds a command to the configured commands. This properly initializes the class, subscribes to the command topic and writes it to the config file.
        /// </summary>
        /// <param name="commandType"></param>
        /// <param name="json"></param>
        public void AddCommand(AvailableCommands commandType, string json)
        {
            var serializerOptions = new JsonSerializerOptions
            {
                Converters = { new DynamicJsonConverter() }
            };
            dynamic model = JsonSerializer.Deserialize <dynamic>(json, serializerOptions);

            AbstractCommand commandToCreate = null;

            switch (commandType)
            {
            case AvailableCommands.ShutdownCommand:
                commandToCreate = new ShutdownCommand(this._publisher, model.Name);
                break;

            case AvailableCommands.RestartCommand:
                commandToCreate = new RestartCommand(this._publisher, model.Name);
                break;

            case AvailableCommands.LogOffCommand:
                commandToCreate = new LogOffCommand(this._publisher, model.Name);
                break;

            case AvailableCommands.CustomCommand:
                commandToCreate = new CustomCommand(this._publisher, model.Command, model.Name);
                break;

            default:
                Log.Logger.Error("Unknown sensortype");
                break;
            }
            if (commandToCreate != null)
            {
                this._configurationService.AddConfiguredCommand(commandToCreate);
            }
        }
Example #8
0
 public void Handle(LogOffCommand command)
 {
     CookieHandler.Remove();
 }
Example #9
0
        public async Task <ActionResult <Guid> > LogOff([FromBody] LogOffCommand command)
        {
            var userId = await Mediator.Send(command);

            return(Ok(userId));
        }