Example #1
0
        /// <summary>
        /// authorizes command.
        /// checks if team has rights to execute this command.
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        private BusinessResponse AuthorizeCommand(ref Dto.Command command)
        {
            var resp = new BusinessResponse
            {
                ResponseCode = ResponseCode.Unauthorized
            };

            var scopeId = command.ScopeId;

            var teamScope = _team.TeamScopes.FirstOrDefault(p => p.ScopeId == scopeId);

            if (teamScope == null)
            {
                command = _commands.First(p => p.Id == DatabaseKey.Command.Authorize);
                return(resp);
            }

            resp.ResponseCode = ResponseCode.Success;
            return(resp);
        }
Example #2
0
        /// <summary>
        /// checks and sets command
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        private BusinessResponse CheckCommand(ref Dto.Command command)
        {
            var resp = new BusinessResponse
            {
                ResponseCode = ResponseCode.NoContent
            };

            string commandText;

            var array = command.Text
                        .ToLower()
                        .Replace("  ", " ") // more than one space char is unaccaptable
                        .Split(' ');        //split by space char

            if (array.Length <= 2)
            {
                if (array[0] == "set" && array[1] == "channel")
                {
                    commandText = "set channel";  //special case for set channel cmd
                }
                else
                {
                    commandText = array[0]; //one word cmd (like 'help') or 2 words cmd with 1 parameter
                }
            }
            else
            {
                commandText = $"{array[0]} {array[1]}"; //cmd has only two words max
            }

            command = _commands.FirstOrDefault(p => p.Text == commandText);

            if (command == null)
            {
                command = _commands.First(p => p.Id == DatabaseKey.Command.CheckCommandExistance);
                return(resp);
            }

            resp.ResponseCode = ResponseCode.Success;
            return(resp);
        }
Example #3
0
        /// <summary>
        /// checks existance and authorizes command.
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        protected virtual BusinessResponse <SlackMessage> IsCommandValid(ref Dto.Command command)
        {
            var resp = new BusinessResponse <SlackMessage>()
            {
                ResponseCode = ResponseCode.Fail
            };

            //check is cmd exist. If true set all cmd properties from db.
            var cmdExistanceResp = CheckCommand(ref command);

            if (cmdExistanceResp.ResponseCode != ResponseCode.Success)
            {
                resp.ResponseCode = cmdExistanceResp.ResponseCode;

                var errorMessage = GetSlackMessage(command.Responses.First(p => p.Language == _team.Language).ErrorText);
                resp.ResponseData = errorMessage;

                return(resp);
            }

            //check if team has rights to execute this command.
            var authResp = AuthorizeCommand(ref command);

            if (authResp.ResponseCode != ResponseCode.Success)
            {
                resp.ResponseCode = authResp.ResponseCode;

                var errorMessage = GetSlackMessage(command.Responses.First(p => p.Language == _team.Language).ErrorText);
                resp.ResponseData = errorMessage;

                return(resp);
            }

            resp.ResponseCode = ResponseCode.Success;

            return(resp);
        }