Getting a list of Tasks that belong to a Role from Azman

2.7k views Asked by At

I'm using the AZROLESLib which is from the COM references "azroles 1.0 Type Library" and I am trying to create a list of the designated tasks for each role that I have currently set in my authorization manager but when I loop through the tasks for the role, I get the role name.

I've looked all around but couldn't find anything that would help.

Here's what I got currently (It's not super pretty but i'm just trying to get it working at the moment).

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

What comes out is:

  • Administrator -Administrator

  • Account Manager -Account Manager

  • Corporate Marketing Specialist -Corporate Marketing Specialist

  • General Employee -General Employee

  • Marketing Manager -Marketing Manager

  • Regional Marketing Specialist -Regional Marketing Specialist

  • Sales Manager -Sales Manager

  • Webmaster -Webmaster

but what should come out is something like:

  • Webmaster
    • Websites Maintain
    • News Maintain
    • Events Maintain
    • Reports Read

Thanks in advance.

1

There are 1 answers

2
Logic Labs On

Umm, welcome to the confusion that is AzMan :-) There are currently 3 different versions/interfaces, with 2 different methods of doing what you ask.

From the stanard COM interface (IAzApplication), app.Roles refers to Role Assignments (Member assigned to roles), whereas I think what you want are Role Definitions, which weren't introduced as their own type until later in version 3.

For IAzApplication: to access Role Definitions you need to iterate over all the app.Tasks and check the task.IsRoleDefinition flag to get the role definitions.

| IAzApplication3     | IAzApplication            |
|---------------------|---------------------------|
| app.RoleAssignments | app.Roles                 |

| app.RoleDefinitions | app.Tasks                 |
|                     | and only consider tasks:  |
|                     | task.IsRoleDefinition = 1 |

Note: You should also bear in mind that what you're attempting to do is not quite as simple in AzMan as your original code solution. Roles can consist of sub-Roles, Tasks and Operations, Tasks can consist of sub-Tasks and Operations .. so you really need to recurse over the roles to build up a complete list of operations, tasks and roles in each given role.

The following code will output a full hierarchy of an apps Role Definitions for all versions of AzMan (just to be fancy it's got a .NET 3.5 callback lambda and a generic in it, but this could be re-written for .NET 1.0 easy enough). The callback returns the type (Role, Task, Operation), name and the hierarchy depth (for indenting):

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

If you know your target platform is going to be Windows 7, Vista or Windows Server 2008 then you should be using the IAzManApplication3 interface, and this is a lot better defined (RoleDefinition has it's own collection/type). If you're developing on Windows XP, you will need to install the Windows Server 2008 Administration Pack and this will come with the updated AzMan DLL.

For AzMan v3, the following code will iterate over the hierarchy of Role Definitions, Tasks and Operations (it's the v3 equivalent of what you were asking originally):

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

There are no enumerators on the tasks or operations, just an array of names, so if you want anything other than the name you need to call App.OpenXXX() to get more information.

Hope this helps ...