c# - 在 C# : How to Create a List of Tuples containing a Control and a list of users which are authorized to access this Control? 中

标签 c# list generics tuples

我正在为一家律师事务所构建一个 C# 应用程序。在每个 Form 中都有一些 Control,只有部分用户可以访问(对于其他用户 - 这些 Control 将被禁用)。

我是这样构建的:有一个名为 AuthorizedForm 的类继承自 Form。此类有一个名为 SetAuthorization 的方法,它启用用户可以访问的所有 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 class 的代码(在 this 答案的帮助下构建):

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仅,或仅 ControlUserList,因此我可以仅为用户组或单个用户设置权限。

如果不显着更改我编写的代码,我想不出一种方法来做到这一点。有没有办法以相对简单和优雅的方式做到这一点?如果没有,您建议如何做?

最佳答案

如何使用一个不是元组的对象来保存授权,它比元组<>更优雅:

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

关于c# - 在 C# : How to Create a List of Tuples containing a Control and a list of users which are authorized to access this Control? 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35995281/

相关文章:

c# - 通过服务删除 Windows 登录屏幕

c - 链表基本内存(C语言)

c# - 我无法在 C# 中将 list<object[]> 转换为 list <T[]>

generics - 了解 syb 样板文件消除

c# - .NET 告诉我 TypeLoadException : violates the constraint of type parameter 'T' while my code clearly doesn't violate it

c# - 从 C# 调用 powershell cmdlet

c# - asp.net mvc 在数据库中插入数据并刷新部分

c# - 在 C# 中将年份从 4 位数字转换为 2 位数字并再次返回

Python。基于两个键和一个字典对二维列表进行排序

java - 如何在哈希集中进行比较,如果第二个元素相等则返回第二个值?