如何为多个用户实现一个命令列表?

How to implement a command list for multiple users?

我有一个命令列表,例如:

我还有用户:

目标是使用户能够调用他们可用的命令。 例如,

如果我在输入端有两个变量 - commandNameuserName.[=10,那么实现这个的最佳方法是什么=]

这个目前是在switch上做的,但是对于项目的伸缩非常不方便。

您的解决方案是权限。

  1. 创建角色并为其分配一组权限;
  2. 为您的用户分配角色并创建为特定用户添加额外权限的能力;
  3. 将您的命令处理程序拆分为不同的方法(没有开关!)并在开始执行命令时检查权限。

这是我认为命令处理程序应该看起来像的伪代码:

public ExecutionResult ExecuteCommand1(User user, string[] arguments) {
   if(!user.HasPermission("admin.test.permission")) {
       return new ExecutionResult() {
           Error = true,
           Message = "You don't have permissions to execute that command!"
       };
   }

   user.GiveKarma(1337);

   return new ExecutionResult {
       Message = "Karma points were added!"
   };
}

权限标识符不应像我的示例代码中那样是字符串。

您可以使用任何东西:字符串、枚举等

您可以创建包含命令和用户对命令的访问权限的字典列表(或数据库中的类似命令):

 var commandList = new Dictionary<string, Action>();
            commandList.Add("command1", () => { Console.WriteLine("command1 is executed"); });
            commandList.Add("command2", () => { Console.WriteLine("command2 is executed"); });
            commandList.Add("command3", () => { Console.WriteLine("command3 is executed"); });
            var listOfUserCommands = new Dictionary<string, List<string>>();
            listOfUserCommands.Add("user1", new List<string>() { "command1" });
            listOfUserCommands.Add("user2", new List<string>() { "command2", "command3" });

            var userName = "user1";
            var commandName = "command2";
            if (listOfUserCommands.ContainsKey(userName) && listOfUserCommands[userName].Contains(commandName))
            {
                commandList[commandName]();
            }

可能会修改为使用命令别名:

public interface IExecutionCommand
    {
        Action Action { set; get;}
        List<string> Aliases { set; get; }

    }
    public class ExecutionCommand1: IExecutionCommand
    {
        public Action Action { set; get; }
        public List<string> Aliases { set; get; }
        public ExecutionCommand1()
        {
            Action = () => { Console.WriteLine("command1 is executed"); };
            Aliases = new List<string>() { "cmd1" };
        }
    }
    public class ExecutionCommand2: IExecutionCommand
    {
        public Action Action { set; get; }
        public List<string> Aliases { set; get; }
        public ExecutionCommand2()
        {
            Action = () => { Console.WriteLine("command2 is executed"); };
            Aliases = new List<string>() { "cmd2" };
        }
    }
    public class ExecutionCommand3: IExecutionCommand
    {
        public Action Action { set; get; }
        public List<string> Aliases { set; get; }
        public ExecutionCommand3()
        {
            Action = () => { Console.WriteLine("command3 is executed"); };
            Aliases = new List<string>() { "cmd3" };
        }
    }
    public static void Main()
    {
        //_startScanning();
        var commandList = new List<IExecutionCommand>();
        commandList.Add(new ExecutionCommand1());
        commandList.Add(new ExecutionCommand2());
        commandList.Add(new ExecutionCommand3());
        var listOfUserCommands = new Dictionary<string, List<string>>();
        listOfUserCommands.Add("user1", new List<string>() { "cmd2" });
        listOfUserCommands.Add("user2", new List<string>() { "command1", "command3" });

        var userName = "user1";
        var commandName = "cmd2";

        if (listOfUserCommands.ContainsKey(userName) && listOfUserCommands[userName].Contains(commandName))
        {
            IExecutionCommand command = commandList.FirstOrDefault(x => x.Aliases.Contains(commandName));
            command?.Action();
        }
    }

这就是我效仿 Oleg Bondarenko 的例子。解决方案并不完美,但突然有人需要它。

Dictionary<string, List<IExecutionCommand>> listOfUserCommands = new Dictionary<string, List<IExecutionCommand>>();

public interface IExecutionCommand
{
    Action Action { set; get; }
    List<string> Aliases { set; get; }
}

public class commandTest : IExecutionCommand
{
    public Action Action { set; get; }
    public List<string> Aliases { set; get; }
    public commandTest()
    {
        Action = () => { Console.WriteLine("command1 is executed"); };
        Aliases = new List<string>() { "test", "check", "tests" };
    }
}
public class commandEx2 : IExecutionCommand
{
    public Action Action { set; get; }
    public List<string> Aliases { set; get; }
    public commandEx2()
    {
        Action = () => { Console.WriteLine("command2 is executed"); };
        Aliases = new List<string>() { "cmd2" };
    }
}


private IExecutionCommand FindingCommandFromUser(string commandName, List<IExecutionCommand> commandList)
{
    foreach (IExecutionCommand command in commandList)
        if (command.Aliases.Contains(commandName))
            return command;
    return null;
}

public static void Main()
{
    listOfUserCommands.Add("user1",
                            new List<IExecutionCommand>()
                            {
                                    new commandTest(),
                                    new commandEx2()
                            });

    string userName = "user1";
    string commandName = "check";

    IExecutionCommand curCommand = null;

    if (listOfUserCommands.ContainsKey(userName) && ((curCommand = FindingCommandFromUser(commandName, listOfUserCommands[userName])) != null))
        curCommand.Action();
}