如何解决处理每个访问级别允许的命令?
| 我有一组命令,例如: 。踢 .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,并在每次发布主要命令时更新数据。
这只是一个例子,我可能会过分考虑这个想法,但是我确实想听听一些有关如何处理此问题的建议和想法?
解决方法
您可以尝试这样。尽管您可能希望通过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);
}
}
, 看起来像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.
所有特权都获得默认权限,默认权限初始化为\“ denied \”(存储在上下文中)
当在运行时更改权限时,将重建有效权限。
当系统必须检查权限时,它将查找用户并最终进行身份验证。如果用户通过了身份验证,则通过上下文查找有效权限。如果找到该用户的有效许可,则查询请求的特权,并检查相应的许可。如果我们被授予或拒绝,则检查完成。如果未找到权限,则使用该特权的默认权限。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。