WMI (Wind. Management Instrumentation) + VisualSVN - permissions for multiple users

mishelka

New member
Joined
Oct 18, 2012
Messages
2
Programming Experience
5-10
Hi all,

I'm trying to automate some actions on VisualSVN in C# using WMI (Windows Management Instrumentation). I found a way of how to create a repository automatically, user and add permissions to a user on a particular repository - it's working fine. Here're the link sources I use:
java - how to create a new user for visual svn server using svnkit javaapi? - Server Fault
https://groups.google.com/forum/?fromgroups=#!topic/visualsvn/0NTgU8XzbUo


The problem is exactly as described in the second link in the end. I would like to add permissions to multiple users on a single repository.
When I execute this code to add permissions multiple times with different users, it will rewrite the previously added permissions. Here's the code:


public void AddPermissions(string userName, String repoName) {
ManagementObject repo = new ManagementObject("VisualSVN_Repository.Name='" + repoName + "'");

ManagementClass userClass = new ManagementClass("root\\VisualSVN", "VisualSVN_User", null);
ManagementClass authzClass = new ManagementClass("root\\VisualSVN", "VisualSVN_SecurityDescriptor", null);
ManagementClass permClass = new ManagementClass("root\\VisualSVN", "VisualSVN_PermissionEntry", null);

ManagementObject userObject = userClass.CreateInstance();
userObject.SetPropertyValue("Name", userName);

ManagementObject permObject = permClass.CreateInstance();
permObject.SetPropertyValue("Account", userObject);
permObject.SetPropertyValue("AccessLevel", 2);

ManagementBaseObject inParams =
authzClass.GetMethodParameters("SetSecurity");

inParams["Object"] = repo;
inParams["Permissions"] = new object[] { permObject };

ManagementBaseObject outParams =
authzClass.InvokeMethod("SetSecurity", inParams, null);

}


I call this method twice, once with the user "user1" and reponame "newRepo" - it adds the permissions correctly.
The second time I call it with the user "user2" and the same reponame "newRepo". This adds the permissions to the user2, but REMOVES the previously added permissions for user1.
Could somebody help me with this please? :panda: I'm sorry, I'm not very good with C# but I need this, it's not possible to code in Java as far as I know.
 
I SOLVED IT ! :)
Here's the working code:


public void AddPermissions(string[] userNames, String repoName) {
ManagementObject repo = new ManagementObject("VisualSVN_Repository.Name='" + repoName + "'");

ManagementClass userClass = new ManagementClass("root\\VisualSVN", "VisualSVN_User", null);
ManagementClass authzClass = new ManagementClass("root\\VisualSVN", "VisualSVN_SecurityDescriptor", null);
ManagementClass permClass = new ManagementClass("root\\VisualSVN", "VisualSVN_PermissionEntry", null);

//I create multiple users and multiple permObjects
ManagementObject[] users = new ManagementObject[userNames.Length];
ManagementObject[] permObjects = new ManagementObject[userNames.Length];

for(int i = 0; i < userNames.Length; i++) {
String userName = userNames;
ManagementObject user = userClass.CreateInstance();
user.SetPropertyValue("Name", userNames);
users = user;

ManagementObject permObject = permClass.CreateInstance();
permObject.SetPropertyValue("Account", user);
permObject.SetPropertyValue("AccessLevel", 2);
permObjects = permObject;
}

ManagementBaseObject inParams =
authzClass.GetMethodParameters("SetSecurity");

//Then I input the multiple permObjects into the permissions param value
inParams["Object"] = repo;
inParams["Permissions"] = permObjects;

//permissions for multiple users are added on one repository
ManagementBaseObject outParams =
authzClass.InvokeMethod("SetSecurity", inParams, null);
}

:)
 
Back
Top Bottom