internal static void AddAdministrator(string aStoreName) { try { AzAuthorizationStore store = new AzAuthorizationStore(); string storeLocation = AzManReader.GetAuthStoreLocation(aStoreName); //4 = AZ_AZSTORE_FLAG_BATCH_UPDATE store.Initialize(4, storeLocation, null); foreach (IAzApplication3 application in store.Applications) { //Create a new role assignment IAzRoleAssignments roleAssignments = application.RoleAssignments; bool hasAdministrator = false; foreach (IAzRoleAssignment roleassignment in roleAssignments) { if (roleassignment.Name.Equals("Administrator")) { hasAdministrator = true; } } if (!hasAdministrator) { IAzRoleAssignment newRoleAssignment = application.CreateRoleAssignment("Administrator"); newRoleAssignment.AddRoleDefinition("Administrator"); newRoleAssignment.Submit(); application.Submit(); } } } catch (COMException ce) { MessageBox.Show(null, ce.Message + "\n" + ce.ErrorCode.ToString(), "COMException occurred"); } }
public static bool CreateRole(string role, string aStoreName) { bool success = false; try { AzAuthorizationStore store = new AzAuthorizationStore(); string storeLocation = AzManReader.GetAuthStoreLocation(aStoreName); string roleName = "_" + role; //4 = AZ_AZSTORE_FLAG_BATCH_UPDATE store.Initialize(4, storeLocation, null); foreach (IAzApplication3 application in store.Applications) { //Create a new role definition IAzRoleDefinition newRole = application.CreateRoleDefinition(roleName); //Create a new role assignment IAzRoleAssignment newRoleAssignment = application.CreateRoleAssignment(roleName); newRole.Submit(); newRoleAssignment.AddRoleDefinition(roleName); newRoleAssignment.Submit(); application.Submit(); } success = true; } catch (COMException ce) { if (ce.ErrorCode.Equals(-2147024713)) { MessageBox.Show(null, "Role already exist in this application.", "Role already exist"); } else { MessageBox.Show(null, ce.Message + "\n" + ce.ErrorCode.ToString(), "COMException occurred"); } } catch (Exception ex) { if (ex is UnauthorizedAccessException) { MessageBox.Show("Access denied to " + aStoreName + "AuthStore.xml. Maybe it is read-only?", "", MessageBoxButtons.OK); } else { MessageBox.Show("Could not create role. Maybe it already exists?", "", MessageBoxButtons.OK); } success = false; } return(success); }