[英]In C#: How to Create a List of Tuples containing a Control and a list of users which are authorized to access this Control?
我正在为一家律师事务所构建C#应用程序。 在每个Form
中都有一些Control
,只有某些用户可以访问(对于其他用户-这些Control
将被禁用)。
我是这样构建的:有一个称为AuthorizedForm
的类,它从Form
继承。 此类具有一种称为SetAuthorization
的方法,该方法启用用户可能访问的所有Control
,并禁用其余Control
。 应用程序中的每个表单都继承自AuthorizedForm
。
当用户登录时,将SetAuthorization
方法,并将接收到的用户ID和规则列表作为参数接收。 每个规则都包含一个Control
,以及可以访问该Control
的用户列表。
该列表实际上分为两个列表:用户组(例如律师,律师助理等)和个人用户。 因此,例如,如果我将“管理员”组的ID和“律师”组的ID放在第一个列表中,并将其中一个秘书的ID放在第二个列表中,则Control
将被定义为为所有管理员和律师以及该秘书Enabled
,而对于其他办公室雇员, Control
将定义为Disabled
。
该列表称为AuthorizationList
,实际上是从Tuple
类型的List
类继承的类,该类接收Control
, GroupList
和UserList
(最后两个不过是类型为byte
的List
)。
GroupList
和 UserList
的定义 :
public class GroupList : List<byte> { }
public class UserList : List<byte> { }
AuthorizedList
类 的代码 (在此答案的帮助下构建) :
public class AuthorizationList : List<Tuple<Control, GroupList, UserList>>
{
public void Add(Control control, GroupList groups, UserList users)
{
Add(new Tuple<Control, GroupList, UserList>(control, groups, users));
}
}
该 AuthorizedForm
类:
public class AuthorizedForm : Form
{
public void SetAuthorization(User loggedInUser, AuthorizationList authorizationList)
{
foreach (var rule in authorizationList)
{
bool isAuthorized = false;
foreach (byte group in rule.Item2)
{
if (group == loggedInUser.Group)
{
isAuthorized = true;
break;
}
}
if (!isAuthorized)
{
foreach (byte user in rule.Item3)
{
if (user == loggedInUser.Id)
{
isAuthorized = true;
break;
}
}
}
if(!isAuthorized)
{
rule.Item1.Enabled = false;
}
}
}
}
从登录功能 调用 SetAuthorization
:
if (user.isLoggedIn)
{
SetAuthorization(user, new AuthorizationList()
{
/*
CONTROL | GROUPS IDS LIST | USER IDS LIST
*/
{ textBox1, new GroupList() { 1, 2, 3 }, new UserList() { 4 } },
{ textBox2, new GroupList() { 1, 2 }, new UserList() { 4 } },
{ button1, new GroupList() { 1 }, new UserList() { 4, 3 } },
});
}
现在,我想让AuthorizationList
是还的类型Tuple
,接收Control
和GroupList
,或仅一个Control
和UserList
而已,所以我可以设置用户组只,或只有个别用户的权限。
我想不出一种方法,而无需显着更改我编写的代码。 有没有办法以一种相对简单而优雅的方式做到这一点? 如果没有,您会如何建议呢?
如何使用不是Tuple的对象来保存授权,它比Tuple <>更优雅:
public class GroupList : List<byte> { }
public class UserList : List<byte> { }
public class Authorization
{
public Control Control { get; set; }
public GroupList Groups { get; set; }
public UserList Users { get; set; }
}
public class AuthorizationList : List<Authorization>
{
public void Add(Control control, GroupList groupList, UserList userList)
{
Add(new Authorization
{
Control = control,
Groups = groupList,
Users = userList
});
}
public void Add(Control control, GroupList groupList)
{
Add(control, groupList, null);
}
public void Add(Control control, UserList userList)
{
Add(control, null, userList);
}
}
您可以利用以下事实:传递空列表不会影响您的逻辑。
为了使其更加“优雅”,请在AuthorizationList
类中再添加两个Add
重载:
public class AuthorizationList : List<Tuple<Control, GroupList, UserList>>
{
public void Add(Control control, GroupList groups, UserList users)
{
Add(new Tuple<Control, GroupList, UserList>(control, groups, users));
}
public void Add(Control control, GroupList groups)
{
Add(new Tuple<Control, GroupList, UserList>(control, groups, new UserList()));
}
public void Add(Control control, UserList users)
{
Add(new Tuple<Control, GroupList, UserList>(control, new GroupList(), users));
}
}
现在,您可以传递GroupList
, UserList
或两者:
SetAuthorization(user, new AuthorizationList()
{
{ textBox1, new GroupList() { 1, 2, 3 }, new UserList() { 4 } },
{ textBox2, new GroupList() { 1, 2 } },
{ button1, new UserList() { 4, 3 } },
});
我正在尝试理解您的问题... AuthorizationList是元组列表
而且您希望它仅接收两个参数,即COntrol和grouplist或Control和User列表
在我看来,AuthorizationList应该是一个KeyValuePair>
或类似的东西。 然后检查类型以查看列表是用户列表还是组列表
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.