0

我有一组命令,例如:

  • 。踢
  • .unban
  • 。禁止
  • .unvouch
  • 。担保
  • 。添加
  • .del
  • 。说

这些命令用于聊天室,我有几个具有不同访问权限的用户,例如:

  • 允许管理员使用所有命令。
  • 版主可以使用 .kick、.vouch .unvouch .say
  • 允许vip使用.say
  • 基本不能使用任何命令

当使用命令时,它会转到房间中的机器人,该机器人将在执行命令之前验证用户、访问权限和所有内容。

最初我有一个分配给列表的用户类:

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>();

现在我想实现一种简单的方法来查询/检查/验证给定用户是否有权使用给定命令,但我不确定如何处理它。

我正在考虑将第二个班级分配给这样的列表:

public class UserAccess
{
    public string AccessLevel { get; set; }
    public List<string> Commands = new List<string>();
}

public List<UserAccess> accessList = new List<UserAccess>();

并使用以下内容进行查询:

var user = userList.Find(x => x.Name == currentUser);
if (user != null && accessList.Exists(x => x.AccessLevel == user.Access && x.Commands.Contains(str_cmd))
{
    // use the command
}
else
{
    // cannot use the command
}

正如我上面提到的,我有一个后台工作人员,它不断读取聊天消息以捕获用户何时键入命令,然后验证和处理队列中的所有内容。

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

这只是一个例子,我可能会过度思考这个想法,但我确实想听听一些关于如何处理这个问题的建议和想法?

4

2 回答 2

1

你可以尝试这样的事情。尽管您可能希望通过 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);
    }
}
于 2011-05-27T21:26:47.370 回答
0

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

优点:相当容易实现
缺点:限制命令/权限级别有点分层

允许任意复杂权限的替代方案:

使用类型:

User - 代表一个用户
Group - 代表一组 User-Objects
Context - 代表规则集的上下文(就 IRC 而言,这可能是一个通道)
Privilege - 代表可以使用的权限或命令
Permission - 表示权限被授予或拒绝

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

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

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.

所有权限都获得一个默认权限,初始化为“拒绝”(存储在上下文中)

在运行时更改权限时,将重建有效权限。

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

于 2011-05-27T22:18:29.327 回答