【问题标题】:Getting a list of Tasks that belong to a Role from Azman从 Azman 获取属于某个角色的任务列表
【发布时间】:2010-12-10 07:25:02
【问题描述】:

我正在使用来自 COM 引用“azroles 1.0 类型库”的 AZROLESLib,并且我正在尝试为我当前在授权管理器中设置的每个角色创建指定任务的列表,但是当我循环时角色的任务,我得到角色名称。

我环顾四周,但找不到任何有用的东西。

这是我目前得到的(它不是超级漂亮,但我现在只是想让它工作)。

AzAuthorizationStoreClass AzManStore = new AzAuthorizationStoreClass();
AzManStore.Initialize(0, ConfigurationManager.ConnectionStrings["AzManStore"].ConnectionString, null);
IAzApplication azApp = AzManStore.OpenApplication("StoreName", null);
StringBuilder output = new StringBuilder();
Array tasks = null;
foreach (IAzRole currentRole in azApp.Roles)
{
    output.Append(currentRole.Name + "<br />");

    tasks = (Array)currentRole.Tasks;
    foreach (object ob in tasks)
    {
        output.Append("&nbsp;&nbsp; -" + ob.ToString() + "<br />");
    }
}             
return output.ToString();

结果是

  • 管理员 -管理员

  • 客户经理 -客户经理

  • 企业营销专员 -企业营销专员

  • 普通员工 -普通员工

  • 营销经理 -营销经理

  • 区域营销专员 -区域营销专员

  • 销售经理 -销售经理

  • 网站管理员 -网站管理员

但结果应该是这样的:

  • 网站管理员
    • 网站维护
    • 新闻维护
    • 事件维护
    • 报告阅读

提前致谢。

【问题讨论】:

    标签: c# roles task azman


    【解决方案1】:

    嗯,欢迎了解 AzMan 的困惑 :-) 目前有 3 个不同的版本/界面,有 2 种不同的方法来满足您的要求。

    从标准 COM 接口 (IAzApplication),app.Roles 指的是角色 Assignments(分配给角色的成员),而我认为您想要的是角色定义,直到版本 3 后期才作为自己的类型引入。

    对于 IAzApplication:要访问角色定义,您需要遍历所有 app.Tasks 并检查 task.IsRoleDefinition 标志以获取角色定义。

    | IAzApplication3     | IAzApplication            |
    |---------------------|---------------------------|
    | app.RoleAssignments | app.Roles                 |
    
    | app.RoleDefinitions | app.Tasks                 |
    |                     | and only consider tasks:  |
    |                     | task.IsRoleDefinition = 1 |
    

    注意:您还应该记住,在 AzMan 中您尝试做的事情并不像您的原始代码解决方案那么简单。角色可以由子角色、任务和操作组成,任务可以由子任务和操作组成......所以你真的需要递归角色来建立每个给定角色中操作、任务和角色的完整列表。

    以下代码将为所有版本的 AzMan 输出应用角色定义的完整层次结构(只是为了幻想它有一个 .NET 3.5 回调 lambda 和一个泛型,但这可以为 .NET 重写1.0 很简单)。回调返回类型(角色、任务、操作)、名称和层次深度(用于缩进):

        private static void ProcessAzManRoleDefinitions(IAzApplication app, IAzTask task, int level, Action<string, string, int> callbackAction)
        {
            bool isRole = (task.IsRoleDefinition == 1);
    
            callbackAction((isRole ? "Role" : "Task"), task.Name, level);
            level++;
    
            // Iterate over any subtasks defined for this task (or role)
            Array tasks = (Array)task.Tasks;
            foreach (string taskName in tasks)
            {
                IAzTask currentTask = app.OpenTask(taskName, null);
    
                // Need to recursively process child roles and tasks
                ProcessAzManRoleDefinitions(app, currentTask, level, callbackAction);
            }
    
            // Iterate over any opeations defined for this task (or role)
            Array taskOperations = (Array)task.Operations;
            foreach (string operationName in taskOperations)
                callbackAction("Operation", operationName, level);
        }
    
        private static string GetRoleDefinitionHierarchy()
        {
            AzAuthorizationStore azManStore = new AzAuthorizationStoreClass();
            azManStore.Initialize(0, "connectionstring", null);
            IAzApplication azApp = azManStore.OpenApplication("TestApp", null);
    
            StringBuilder output = new StringBuilder();
    
            foreach (IAzTask task in azApp.Tasks)
            {
                if (task.IsRoleDefinition == 1)
                    ProcessAzManRoleDefinitions(azApp, task, 0, (type, name, level) => output.Append("".PadLeft(level * 2) + type + ": " + name + "\n"));
            }
    
            return output.ToString();
        }
    

    如果您知道您的目标平台将是 Windows 7、Vista 或 Windows Server 2008,那么您应该使用 IAzManApplication3 接口,并且这个接口的定义要好得多(RoleDefinition 有它自己的集合/类型)。如果您在 Windows XP 上进行开发,则需要安装 Windows Server 2008 管理包,它会随更新的 AzMan DLL 一起提供。

    对于 AzMan v3,以下代码将遍历角色定义、任务和操作的层次结构(它与您最初询问的 v3 等效):

    private string GetAllRoleDefinitionHierarchies()
    {
        AzAuthorizationStore azManStore = new AzAuthorizationStoreClass();
        azManStore.Initialize(0, "connectionstring", null);
        IAzApplication3 azApp = azManStore.OpenApplication("TestApp", null) as IAzApplication3;
    
        if (azApp == null)
            throw new NotSupportedException("Getting Role Definitions is not supported by older versions of AzMan COM interface");
    
        StringBuilder output = new StringBuilder();
        foreach (IAzRoleDefinition currentRoleDefinition in azApp.RoleDefinitions)
        {
            output.Append(currentRoleDefinition.Name + "<br />");
            Array roleTasks = (Array) currentRoleDefinition.Tasks;
            foreach (string taskId in roleTasks)
            {
                IAzTask currentTask = azApp.OpenTask(taskId, null);
                output.Append("&nbsp;&nbsp; - Task: " + currentTask.Name + "<br />");
    
                Array taskOperations = (Array)currentTask.Operations;
                foreach (string operationId in taskOperations)
                {
                    IAzOperation currentOperation = azApp.OpenOperation(operationId, null);
                    output.Append("&nbsp;&nbsp;&nbsp;&nbsp; - Operation: " + currentOperation.Name + "<br />");
                }
            }
        }
    
        return output.ToString();
    }
    

    任务或操作上没有枚举器,只有一个名称数组,因此如果您想要名称以外的任何内容,则需要调用 App.OpenXXX() 以获取更多信息。

    希望这会有所帮助...

    【讨论】:

    • 我相信我为 Windows 8.1 安装了正确的 SDK,但我找不到 IAzApplication3 的参考。你能告诉我如何将它添加到我的项目中吗?谢谢!
    • 在 Visual Studio 中,您可以使用对 AzRolesLib 的 COM 引用(我在 Windows 10 上检查过,它仍然可用)或通过添加引用来添加对 GAC 中的互操作库的引用,单击浏览并选择“C:\Windows\assembly\GAC_64\Microsoft.Interop.Security.AzRoles\2.0.0.0__31bf3856ad364e35\Microsoft.Interop.Security.AzRoles.dll”或 GAC_32 等效项。
    猜你喜欢
    • 1970-01-01
    • 2013-08-07
    • 2019-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-04
    • 2018-04-15
    • 2021-03-03
    相关资源
    最近更新 更多