VisualSVN Server可以用WMI接口管理(Windows Management Instrumentation)。 VisualSVN Server安装的计算机中,位于%VISUALSVN_SERVER%\WMI下的MOF文件描述了该接口。可以用C#代码控制管理VisualSVN Server。

我机器VisualSVN安装在D盘,MOF文件存在位置:D:\Program Files\VisualSVN Server\WMI\VisualSVNServer.mof

下面一些代码供参考:

 

  1. using System.Management;  
  2. using System.Management.Instrumentation;  


并添加 System.Management,System.Management.Instrumentation这两个组件的引用。

 

1)创建Subversion 用户 'user1',密码为'secret'

 

  1. ManagementClass userClass = newManagementClass("root\\VisualSVN","VisualSVN_User",null);  
  2. // Obtain in-parameters for the method  
  3. ManagementBaseObject inParams =  
  4. userClass.GetMethodParameters("Create");  
  5. // Add the input parameters.  
  6. inParams["Name"] = "user1";  
  7. inParams["Password"] = "secret";  
  8. // Execute the method and obtain the return values.  
  9. ManagementBaseObject outParams =  
  10. userClass.InvokeMethod("Create", inParams, null);  


2)创建新的repository 'Repo1'

 

 

  1. ManagementClass repoClass = newManagementClass("root\\VisualSVN","VisualSVN_Repository",null);  
  2. // Obtain in-parameters for the method  
  3. ManagementBaseObject inParams =  
  4. repoClass.GetMethodParameters("Create");  
  5. // Add the input parameters.  
  6. inParams["Name"] = "Repo1";  
  7. // Execute the method and obtain the return values.  
  8. ManagementBaseObject outParams =  
  9. repoClass.InvokeMethod("Create", inParams, null);  

 

  3)设置SID S-1-5-32-545 ('BUILTIN\Users') Windows用户组对'Test' repository的读/写权限. FYI: AccessLevel 值在MOF有描述: "0 - 无权限, 1 - 只读, 2 - 读/写".
  1. ManagementClass userClass = newManagementClass("root\\VisualSVN","VisualSVN_WindowsAccount",null);  
  2. ManagementClass authzClass = newManagementClass("root\\VisualSVN","VisualSVN_SecurityDescriptor",null);  
  3. ManagementClass permClass = newManagementClass("root\\VisualSVN","VisualSVN_PermissionEntry",null);  
  4. ManagementObject userObject = userClass.CreateInstance();  
  5. userObject.SetPropertyValue("SID","S-1-5-32-545");  
  6. ManagementObject permObject = permClass.CreateInstance();  
  7. permObject.SetPropertyValue("Account", userObject);  
  8. permObject.SetPropertyValue("AccessLevel", 2);  
  9. ManagementObject repo = newManagementObject("VisualSVN_Repository.Name='Test'");  
  10. ManagementBaseObject inParams =  
  11. authzClass.GetMethodParameters("SetSecurity");  
  12. inParams["Object"] = repo;  
  13. inParams["Permissions"] = newobject[] { permObject };  
  14. ManagementBaseObject outParams =  
  15. authzClass.InvokeMethod("SetSecurity", inParams, null);  
封装后的Helper类:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Management;//调用windows WMI需引入此类库  
  5.   
  6. namespace Whir.Software.InteralServerManage.SVNManager  
  7. {  
  8.     /// <summary>  
  9.     ///     Visual SVN 操作辅助类:使用WMI控制VisualSVN,MOF文件存在位置:C:\Program Files\VisualSVN Server\WMI\VisualSVNServer.mof  
  10.     /// </summary>  
  11.     public class SVNHelper  
  12.     {  
  13.         #region 设置  
  14.  
  15.         #region 设置仓库权限  
  16.   
  17.         public enum AccessLevel : uint  
  18.         {  
  19.             NoAccess = 0,  
  20.             ReadOnly,  
  21.             ReadWrite  
  22.         }  
  23.   
  24.         /// <summary>  
  25.         ///     设置仓库权限(给用户授权)  
  26.         /// </summary>  
  27.         /// <param name="userName">用户名</param>  
  28.         /// <param name="repository">SVN仓库</param>  
  29.         public static bool SetRepositoryPermission(string userName, string repository)  
  30.         {  
  31.             return SetRepositoryPermission(userName, repository, 2);  
  32.         }  
  33.   
  34.         /// <summary>  
  35.         ///     设置仓库权限(给用户授权)  
  36.         /// </summary>  
  37.         /// <param name="users">用户名</param>  
  38.         /// <param name="repository">SVN仓库</param>  
  39.         public static bool SetRepositoryPermission(List<string> users, string repository)  
  40.         {  
  41.             string userNames = "";  
  42.             foreach (string user in users)  
  43.             {  
  44.                 userNames += user + ",";  
  45.             }  
  46.             return SetRepositoryPermission(userNames, repository, 2);  
  47.         }  
  48.   
  49.         /// <summary>  
  50.         ///     设置仓库权限(给用户授权)  
  51.         /// </summary>  
  52.         /// <param name="userName">用户名</param>  
  53.         /// <param name="repository">SVN仓库</param>  
  54.         /// <param name="permission"> 权限码:0拒绝,1只读,2读写</param>  
  55.         public static bool SetRepositoryPermission(string userName, string repository, int permission)  
  56.         {  
  57.             try  
  58.             {  
  59.                 string[] users = userName.Split(new[] {","}, StringSplitOptions.RemoveEmptyEntries);  
  60.                 IDictionary<string, AccessLevel> permissions = GetPermissions(repository, "/");  
  61.                 foreach (string s in users)  
  62.                 {  
  63.                     if (!permissions.ContainsKey(s))  
  64.                     {  
  65.                         permissions.Add(s, AccessLevel.ReadWrite);  
  66.                     }  
  67.                 }  
  68.                 SetPermissions(repository, "/", permissions);  
  69.                 return true;  
  70.             }  
  71.             catch (Exception)  
  72.             {  
  73.                 return false;  
  74.             }  
  75.         }  
  76.   
  77.         /// <summary>  
  78.         ///     根据仓库名取得仓库实体  
  79.         /// </summary>  
  80.         /// <param name="name"></param>  
  81.         /// <returns></returns>  
  82.         private static ManagementObject GetRepositoryObject(string name)  
  83.         {  
  84.             return new ManagementObject("root\\VisualSVN", string.Format("VisualSVN_Repository.Name='{0}'", name), null);  
  85.         }  
  86.   
  87.         /// <summary>  
  88.         ///     读取权限实体  
  89.         /// </summary>  
  90.         /// <param name="sid"></param>  
  91.         /// <param name="accessLevel"></param>  
  92.         /// <returns></returns>  
  93.         private static ManagementObject GetPermissionObject(string sid, AccessLevel accessLevel)  
  94.         {  
  95.             var accountClass = new ManagementClass("root\\VisualSVN",  
  96.                                                    "VisualSVN_WindowsAccount", null);  
  97.             var entryClass = new ManagementClass("root\\VisualSVN",  
  98.                                                  "VisualSVN_PermissionEntry", null);  
  99.             ManagementObject account = accountClass.CreateInstance();  
  100.             if (account != null) account["SID"] = sid;  
  101.             ManagementObject entry = entryClass.CreateInstance();  
  102.             if (entry != null)  
  103.             {  
  104.                 entry["AccessLevel"] = accessLevel;  
  105.                 entry["Account"] = account;  
  106.                 return entry;  
  107.             }  
  108.             return null;  
  109.         }  
  110.   
  111.         /// <summary>  
  112.         ///     设置仓库权限  
  113.         /// </summary>  
  114.         /// <param name="repositoryName"></param>  
  115.         /// <param name="path"></param>  
  116.         /// <param name="permissions"></param>  
  117.         private static void SetPermissions(string repositoryName, string path,  
  118.                                            IEnumerable<KeyValuePair<string, AccessLevel>> permissions)  
  119.         {  
  120.             ManagementObject repository = GetRepositoryObject(repositoryName);  
  121.             ManagementBaseObject inParameters = repository.GetMethodParameters("SetSecurity");  
  122.             inParameters["Path"] = path;  
  123.             IEnumerable<ManagementObject> permissionObjects =  
  124.                 permissions.Select(p => GetPermissionObject(p.Key, p.Value));  
  125.             inParameters["Permissions"] = permissionObjects.ToArray();  
  126.             repository.InvokeMethod("SetSecurity", inParameters, null);  
  127.         }  
  128.   
  129.         /// <summary>  
  130.         ///     读取仓库权限  
  131.         /// </summary>  
  132.         /// <param name="repositoryName"></param>  
  133.         /// <param name="path"></param>  
  134.         /// <returns></returns>  
  135.         private static IDictionary<string, AccessLevel> GetPermissions(string repositoryName, string path)  
  136.         {  
  137.             ManagementObject repository = GetRepositoryObject(repositoryName);  
  138.             ManagementBaseObject inParameters = repository.GetMethodParameters("GetSecurity");  
  139.             inParameters["Path"] = path;  
  140.             ManagementBaseObject outParameters = repository.InvokeMethod("GetSecurity", inParameters, null);  
  141.   
  142.             var permissions = new Dictionary<string, AccessLevel>();  
  143.   
  144.             if (outParameters != null)  
  145.                 foreach (ManagementBaseObject p in (ManagementBaseObject[]) outParameters["Permissions"])  
  146.                 {  
  147.                     // NOTE: This will fail if VisualSVN Server is configured to use Subversion  
  148.                     // authentication.  In that case you'd probably want to check if the account  
  149.                     // is a VisualSVN_WindowsAccount or a VisualSVN_SubversionAccount instance  
  150.                     // and tweak the property name accordingly.   
  151.                     var account = (ManagementBaseObject) p["Account"];  
  152.                     var sid = (string) account["SID"];  
  153.                     var accessLevel = (AccessLevel) p["AccessLevel"];  
  154.   
  155.                     permissions[sid] = accessLevel;  
  156.                 }  
  157.   
  158.             return permissions;  
  159.         }  
  160.  
  161.         #endregion  
  162.  
  163.         #region 创建用户组  
  164.   
  165.         /// <summary>  
  166.         ///     创建用户组  
  167.         /// </summary>  
  168.         public static bool CreatGroup(string groupName)  
  169.         {  
  170.             try  
  171.             {  
  172.                 var svn = new ManagementClass("root\\VisualSVN", "VisualSVN_Group", null);  
  173.                 ManagementBaseObject @params = svn.GetMethodParameters("Create");  
  174.                 @params["Name"] = groupName.Trim();  
  175.                 @params["Members"] = new object[] {};  
  176.                 svn.InvokeMethod("Create", @params, null);  
  177.                 return true;  
  178.             }  
  179.             catch (Exception)  
  180.             {  
  181.                 return false;  
  182.             }  
  183.         }  
  184.  
  185.         #endregion  
  186.  
  187.         #region 创建用户  
  188.   
  189.         /// <summary>  
  190.         ///     创建用户  
  191.         /// </summary>  
  192.         /// <param name="userName"></param>  
  193.         /// <param name="password"></param>  
  194.         /// <returns></returns>  
  195.         public static bool CreateUser(string userName, string password)  
  196.         {  
  197.             try  
  198.             {  
  199.                 var svn = new ManagementClass("root\\VisualSVN", "VisualSVN_User", null);  
  200.                 ManagementBaseObject @params = svn.GetMethodParameters("Create");  
  201.                 @params["Name"] = userName.Trim();  
  202.                 @params["Password"] = password.Trim();  
  203.                 svn.InvokeMethod("Create", @params, null);  
  204.                 return true;  
  205.             }  
  206.             catch (Exception)  
  207.             {  
  208.                 return false;  
  209.             }  
  210.         }  
  211.  
  212.         #endregion  
  213.  
  214.         #region  创建svn仓库  
  215.   
  216.         /// <summary>  
  217.         ///     创建svn仓库  
  218.         /// </summary>  
  219.         /// <param name="name"></param>  
  220.         /// <returns></returns>  
  221.         public static bool CreateRepository(string name)  
  222.         {  
  223.             try  
  224.             {  
  225.                 var svn = new ManagementClass("root\\VisualSVN", "VisualSVN_Repository", null);  
  226.                 ManagementBaseObject @params = svn.GetMethodParameters("Create"); //创建方法参数引用  
  227.                 @params["Name"] = name.Trim(); //传入参数  
  228.                 svn.InvokeMethod("Create", @params, null); //执行  
  229.                 return true;  
  230.             }  
  231.             catch (Exception)  
  232.             {  
  233.                 return false;  
  234.             }  
  235.         }  
  236.  
  237.         #endregion  
  238.  
  239.         #region  创建svn仓库目录  
  240.   
  241.         /// <summary>  
  242.         ///     创建svn仓库目录  
  243.         /// </summary>  
  244.         /// <param name="repositories"> </param>  
  245.         /// <param name="name"></param>  
  246.         /// <returns></returns>  
  247.         public static bool CreateRepositoryFolders(string repositories, string[] name)  
  248.         {  
  249.             try  
  250.             {  
  251.                 var repository = new ManagementClass("root\\VisualSVN", "VisualSVN_Repository", null);  
  252.                 ManagementObject repoObject = repository.CreateInstance();  
  253.                 if (repoObject != null)  
  254.                 {  
  255.                     repoObject.SetPropertyValue("Name", repositories);  
  256.                     ManagementBaseObject inParams = repository.GetMethodParameters("CreateFolders");  
  257.                     inParams["Folders"] = name;  
  258.                     inParams["Message"] = "";  
  259.                     repoObject.InvokeMethod("CreateFolders", inParams, null);  
  260.                 }  
  261.                 return true;  
  262.             }  
  263.             catch (Exception)  
  264.             {  
  265.                 return false;  
  266.             }  
  267.         }  
  268.  
  269.         #endregion  
  270.   
  271.         //public static bool SetMemberGroup(string userName, string groupName)  
  272.         //{  
  273.         //    try  
  274.         //    {  
  275.         //        var userObj = GetGroupUsersArr(groupName);  
  276.         //        foreach (ManagementBaseObject baseObject in userObj)  
  277.         //        {  
  278.         //            if (baseObject["Name"].ToString().ToLower() == userName)  
  279.         //            {  
  280.         //                return false;  
  281.         //            }  
  282.         //        }  
  283.         //        var addUser = new ManagementClass("root\\VisualSVN", "VisualSVN_SubversionAccount", null).CreateInstance();  
  284.         //        if (addUser != null)  
  285.         //        {  
  286.         //            addUser.SetPropertyValue("Name", userName);  
  287.         //            userObj.Add(addUser);  
  288.         //        }  
  289.   
  290.         //        var svnUser = new ManagementClass("root\\VisualSVN", "VisualSVN_Group", null);  
  291.         //        ManagementBaseObject inParams = svnUser.GetMethodParameters("SetMembers");  
  292.         //        inParams["Members"] = new object[] { userObj };  
  293.         //        svnUser.InvokeMethod("SetMembers", inParams, null);  
  294.         //        return true;  
  295.         //    }  
  296.         //    catch (Exception)  
  297.         //    {  
  298.         //        return false;  
  299.         //    }  
  300.         //}  
  301.  
  302.         #endregion  
  303.  
  304.         #region 读取  
  305.   
  306.         /// <summary>  
  307.         ///     读取指定组里的用户  
  308.         /// </summary>  
  309.         /// <param name="groupName"></param>  
  310.         /// <returns></returns>  
  311.         public static List<string> GetGroupUsers(string groupName)  
  312.         {  
  313.             var listUsers = new List<string>();  
  314.             var group = new ManagementClass("root\\VisualSVN", "VisualSVN_Group", null);  
  315.             ManagementObject instance = group.CreateInstance();  
  316.             if (instance != null)  
  317.             {  
  318.                 instance.SetPropertyValue("Name", groupName.Trim());  
  319.                 ManagementBaseObject outParams = instance.InvokeMethod("GetMembers", null, null); //通过实例来调用方法  
  320.                 if (outParams != null)  
  321.                 {  
  322.                     var members = outParams["Members"] as ManagementBaseObject[];  
  323.                     if (members != null)  
  324.                     {  
  325.                         foreach (ManagementBaseObject member in members)  
  326.                         {  
  327.                             object name = member["Name"];  
  328.                             listUsers.Add(name.ToString());  
  329.                         }  
  330.                     }  
  331.                 }  
  332.             }  
  333.             return listUsers;  
  334.         }  
  335.   
  336.         public static List<ManagementBaseObject> GetGroupUsersArr(string groupName)  
  337.         {  
  338.             var list = new List<ManagementBaseObject>();  
  339.             var group = new ManagementClass("root\\VisualSVN", "VisualSVN_Group", null);  
  340.             ManagementObject instance = group.CreateInstance();  
  341.             if (instance != null)  
  342.             {  
  343.                 instance.SetPropertyValue("Name", groupName.Trim());  
  344.                 ManagementBaseObject outParams = instance.InvokeMethod("GetMembers", null, null); //通过实例来调用方法  
  345.                 if (outParams != null)  
  346.                 {  
  347.                     var members = outParams["Members"] as ManagementBaseObject[];  
  348.                     if (members != null)  
  349.                     {  
  350.                         foreach (ManagementBaseObject member in members)  
  351.                         {  
  352.                             list.Add(member);  
  353.                         }  
  354.                     }  
  355.                 }  
  356.             }  
  357.             return list;  
  358.         }  
  359.  
  360.         #endregion  
  361.     }  
  362. }  
  参考文章:

1.http://msdn.microsoft.com/en-us/library/bb404655

2.http://stackoverflow.com/questions/10017282/control-visualsvn-server-from-c-sharp

posted on 2014-10-07 21:34  GC2013  阅读(782)  评论(2编辑  收藏  举报