Vault API: Users and Roles, part 2

By Marat Mirgaleev

Issue

How do I get the list of Roles from the Vault server?

Solution

We will need to use the Autodesk.Connectivity.WebServices.AdminService class for this.

In my previous post I explained how to get the list of the Vault server users. Let’s continue working with the same code sample and add a ListRoles() method to our AdminSample class:

    // List all roles in the database
    //===============================================================
    public static void ListRoles()
    {
      using (MyVaultServiceManager mgr = new MyVaultServiceManager(
                                MyVaultServiceManager.Mode.ReadOnly))
      {
        try
        {
          // GetAllRoles() - this is the way to get all the Roles
          //------------------------------------------------------
          Role[] roles = mgr.Services.AdminService.GetAllRoles();
 
          // Prepare a string to show the Roles in a message box
          string msg = "Id |   Namen";
                msg += "-------------n";
          foreach (Role role in roles)
            msg += role.Id.ToString() + ": " + role.Name + "n";
          MessageBox.Show(msg, "Roles found");
 
          // This is how we do find some arbitrary role
          Role admin = FindRole(roles, "Administrator");
          if (admin != null)
            MessageBox.Show("Administrator role Id is "
                          + admin.Id.ToString());
        }
        catch (System.Exception err)
        {
          MessageBox.Show(err.ToString());
        } // try
      } // using
    } // ListRoles()

Also, in this piece of code we look for a role called “Administrator”, for this purpose we’ve created a very simple function which iterates through the roles array:

    // Find a role by its name.
    // Parameters: - An array of roles;
    //             - Role name to find.
    // Returns the found Role or null, if didn't find.
    //===============================================================
    private static Role FindRole(Role[] i_roles, string i_roleName)
    {
      Role found = null;
      foreach(Role role in i_roles)
        if (role.Name == i_roleName)
        {
          found = role;
          break;
        }
      return found;
    } 

This is the program output showing the list of Roles that exist in Vault by default:

                       image


Comments

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading