Issue
Is there any programmatic way to add a user to the Vault server?
Solution
Yes, in Vault API we can easily create a new user with usage of the Autodesk.Connectivity.WebServices.AdminService class.
I will add a new method to the AdminSample class which we were working on in these posts: Vault API: Users and Roles, part 1 and Vault API: Users and Roles, part 2.
Sorry that I hardcoded the user’s data, I hope it is Ok for the purpose of a sample showing the process. Also, we will need to assign some role and some vault for the new user, this will require to find the IDs of the respective objects.
Here is our new method called AddUser():
// Add a new user.
// Let it be me with the 'Adminstrator' role and access to
// the vault called 'MaratVault'.
//===============================================================
public static void AddUser()
{
using (MyVaultServiceManager mgr = new MyVaultServiceManager(
MyVaultServiceManager.Mode.ReadWrite))
{
try
{
// Let's find the 'Administrator' role
//------------------------------------------------------
Role[] roles = mgr.Services.AdminService.GetAllRoles();
Role admin = FindRole(roles, "Administrator");
if( admin == null )
{
MessageBox.Show("FindRole() failed.");
return;
}
long[] roleIdArray = new long[] { admin.Id };
// I want to provide the user access to the vault called
// "MaratVault". So, let's find that vault Id:
//---------------------------------------------------------
KnowledgeVault vault = mgr.Services.KnowledgeVaultService.
GetKnowledgeVaultByName("MaratVault");
long[] vaultIdArray = new long[] { vault.Id };
// Now create the user
//---------------------------------------------------------
User newUser = mgr.Services.AdminService.AddUser(
"MaratM", // user name
"123456", // password
AuthTyp.Vault, // authentication - not Active Directory
"Marat", // first name
"Mirgaleev", // last name
"email", // email address
true, // is active
roleIdArray, // which roles
vaultIdArray); // which vaults
}
catch (System.Exception err)
{
MessageBox.Show("Probably, a user name conflict.\n"
+ "The user name may already exist.\n\n"
+ err.ToString(), "Error");
} // try
} // using
} // AddUser()
You may have noticed that we connect to Vault in the ‘Read-and-Write’ mode only if we need to change or add some information.
Let’s check whether is really works:
Nice, we did it! And it wasn't very difficult.