C# 处理每个访问级别允许的命令?

C# 处理每个访问级别允许的命令?,c#,.net-3.5,access-levels,C#,.net 3.5,Access Levels,我有一组命令,如: .踢 .unban 潘基文 unvouch先生 .担保 .添加 德尔先生 .说 这些命令在聊天室中使用,在聊天室中,我有几个用户具有不同的访问权限,例如: 允许管理员使用所有命令 主持人可以使用.kick、.vouch.unvouch.say Vip可以使用。比如说 Basic不能使用任何命令 当一个命令被使用时,它会传到房间里的一个机器人,这个机器人会在执行命令之前验证用户、访问权限和所有内容 最初,我将一个用户类分配给一个列表: public class User

我有一组命令,如:

  • .踢
  • .unban
  • 潘基文
  • unvouch先生
  • .担保
  • .添加
  • 德尔先生
  • .说
这些命令在聊天室中使用,在聊天室中,我有几个用户具有不同的访问权限,例如:

  • 允许管理员使用所有命令
  • 主持人可以使用.kick、.vouch.unvouch.say
  • Vip可以使用。比如说
  • Basic不能使用任何命令
当一个命令被使用时,它会传到房间里的一个机器人,这个机器人会在执行命令之前验证用户、访问权限和所有内容

最初,我将一个用户类分配给一个列表:

public class Users
{
    public string Name { get; set; }
    public string Comments { get; set; }
    public string Access { get; set; }
}

public List<Users> userList = new List<Users>();
正如我前面提到的,我有一个后台工作人员,当用户键入一个命令,然后验证和处理队列中的所有内容时,他会不断地读取聊天消息来捕获

注册用户和访问级别由我的网站API填写,该API会在应用程序启动时将JSON返回给我的应用程序,并在发出主要命令时不时更新数据


这只是一个例子,我可能想得太多了,但我确实希望听到一些建议和想法,告诉我如何处理这个问题?你可以试试这样的方法。尽管您可能希望通过Db或通过定义实际命令的属性/属性来定义访问列表

public class User
{
    public static readonly UserAccess[] AccessList = {
            new UserAccess() { AccessLevel = "Admin",
                               Commands = {".kick",".ban",".unban"}
            },
            new UserAccess() { AccessLevel = "User",
                               Commands = {".add",".del"}
            },
            new UserAccess() { AccessLevel = "Vip",
                               Commands = {".say"}
            }};

    public string Name { get; set; }
    public string Comments { get; set; }
    public string Access { get; private set; } //Assuming you can't modify this so we add the next property
    public UserAccess AccessLevel { get; private set; }

    public User(string access)
    {
        this.Access = access;
        this.AccessLevel = AccessList.FirstOrDefault(x => x.AccessLevel == access);
    }
}

public class UserAccess
{
    public string AccessLevel { get; set; }
    public List<string> Commands = new List<string>();
    public bool HasCommand(string command)
    {
        return this.Commands.Any(x => x == command);
    }
}
公共类用户
{
公共静态只读用户访问[]访问列表={
新建UserAccess(){AccessLevel=“Admin”,
命令={.kick',.ban',.unban}
},
新建UserAccess(){AccessLevel=“User”,
命令={.add“,.del”}
},
新建UserAccess(){AccessLevel=“Vip”,
命令={.say“}
}};
公共字符串名称{get;set;}
公共字符串注释{get;set;}
公共字符串访问{get;private set;}//假设您不能修改它,那么我们添加下一个属性
公共用户访问访问级别{get;private set;}
公共用户(字符串访问)
{
这个.访问=访问;
this.AccessLevel=AccessList.FirstOrDefault(x=>x.AccessLevel==access);
}
}
公共类用户访问
{
公共字符串访问级别{get;set;}
public List命令=new List();
公共bool命令(字符串命令)
{
返回this.Commands.Any(x=>x==command);
}
}

看起来像IRC机器人或服务应用程序。“通常”的方法是拥有一个命令/权限列表,其中int表示所需的访问级别,以及一个具有相应访问级别的用户列表。。。每当使用命令/权限时,请尝试从列表中获取用户访问级别,如果用户不在列表中,请获取默认值(0)。然后将该值与所用命令/权限的值进行比较

优点:易于实施
缺点:将命令/权限级别限制为某种层次结构

允许任意复杂权限的备选方案:

使用类型:

用户-表示用户
组-表示一组用户对象
上下文-表示规则集的上下文(就IRC而言,这可能是一个通道)
特权-表示可以使用的特权或命令
权限-表示授予或拒绝特权

用户和组对象可以通过上下文与权限列表相关联

上下文存储在此上下文中拥有权限的用户或在此上下文中拥有权限的组成员的用户的有效权限列表。通过以下方式确定这些有效权限:

for each user itterate all group memberships  
   for each group itterate all permissions  
      if the permission is denying a Privilege, add this privilege as denied to the effective permissions of this user, overwriting any permission for this privilege that may already be present in that list
      else, if the permission is granting a privilege add it to the effective permissions only if no permission for this privilege is present in the list

finally itterate over the users permissions
add the permission to the effective permissions list, overwriting any permission for this privilege that may already be present in the list.
所有权限都获得默认权限,初始化为“拒绝”(存储在上下文中)

在运行时更改权限时,将重新生成有效权限


当系统必须检查权限时,它会查找用户并最终进行身份验证。如果用户经过身份验证,则上下文将查找有效权限。如果找到该用户的有效权限,将查找请求的权限,并检查相应的权限。如果我们被批准或拒绝,检查就完成了。如果找不到权限,则使用该权限的默认权限。

我没有为每个用户加载命令列表,我有一个访问级别列表,其中包含可以使用的命令,如上面的问题所示,即访问列表和user类上用户的访问属性。哦,好的,然后,我要做的唯一更改是将
UserAccess
的一个实例作为
User
类的一个属性。为了方便起见,可以在
用户访问
上为
.hascondm(string)
添加一个方法,您介意给我举个例子说明它是什么样子吗?
public class User
{
    public static readonly UserAccess[] AccessList = {
            new UserAccess() { AccessLevel = "Admin",
                               Commands = {".kick",".ban",".unban"}
            },
            new UserAccess() { AccessLevel = "User",
                               Commands = {".add",".del"}
            },
            new UserAccess() { AccessLevel = "Vip",
                               Commands = {".say"}
            }};

    public string Name { get; set; }
    public string Comments { get; set; }
    public string Access { get; private set; } //Assuming you can't modify this so we add the next property
    public UserAccess AccessLevel { get; private set; }

    public User(string access)
    {
        this.Access = access;
        this.AccessLevel = AccessList.FirstOrDefault(x => x.AccessLevel == access);
    }
}

public class UserAccess
{
    public string AccessLevel { get; set; }
    public List<string> Commands = new List<string>();
    public bool HasCommand(string command)
    {
        return this.Commands.Any(x => x == command);
    }
}
for each user itterate all group memberships  
   for each group itterate all permissions  
      if the permission is denying a Privilege, add this privilege as denied to the effective permissions of this user, overwriting any permission for this privilege that may already be present in that list
      else, if the permission is granting a privilege add it to the effective permissions only if no permission for this privilege is present in the list

finally itterate over the users permissions
add the permission to the effective permissions list, overwriting any permission for this privilege that may already be present in the list.