标签:des style blog http color io os 使用 ar
原文:C#控制管理VisualSVN ServerVisualSVN 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
下面一些代码供参考:
- using System.Management;
- using System.Management.Instrumentation;
并添加 System.Management,System.Management.Instrumentation这两个组件的引用。
1)创建Subversion 用户 ‘user1‘,密码为‘secret‘
- ManagementClass userClass = newManagementClass("root\\VisualSVN","VisualSVN_User",null);
- ManagementBaseObject inParams =
- userClass.GetMethodParameters("Create");
- inParams["Name"] = "user1";
- inParams["Password"] = "secret";
- ManagementBaseObject outParams =
- userClass.InvokeMethod("Create", inParams, null);
2)创建新的repository ‘Repo1‘
- ManagementClass repoClass = newManagementClass("root\\VisualSVN","VisualSVN_Repository",null);
- ManagementBaseObject inParams =
- repoClass.GetMethodParameters("Create");
- inParams["Name"] = "Repo1";
- ManagementBaseObject outParams =
- repoClass.InvokeMethod("Create", inParams, null);
|
3)设置SID S-1-5-32-545 (‘BUILTIN\Users‘) Windows用户组对‘Test‘ repository的读/写权限. FYI: AccessLevel 值在MOF有描述: "0 - 无权限, 1 - 只读, 2 - 读/写". |
- ManagementClass userClass = newManagementClass("root\\VisualSVN","VisualSVN_WindowsAccount",null);
- ManagementClass authzClass = newManagementClass("root\\VisualSVN","VisualSVN_SecurityDescriptor",null);
- ManagementClass permClass = newManagementClass("root\\VisualSVN","VisualSVN_PermissionEntry",null);
- ManagementObject userObject = userClass.CreateInstance();
- userObject.SetPropertyValue("SID","S-1-5-32-545");
- ManagementObject permObject = permClass.CreateInstance();
- permObject.SetPropertyValue("Account", userObject);
- permObject.SetPropertyValue("AccessLevel", 2);
- ManagementObject repo = newManagementObject("VisualSVN_Repository.Name=‘Test‘");
- ManagementBaseObject inParams =
- authzClass.GetMethodParameters("SetSecurity");
- inParams["Object"] = repo;
- inParams["Permissions"] = newobject[] { permObject };
- ManagementBaseObject outParams =
- authzClass.InvokeMethod("SetSecurity", inParams, null);
封装后的Helper类:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Management;
-
- namespace Whir.Software.InteralServerManage.SVNManager
- {
-
-
-
- public class SVNHelper
- {
- #region 设置
-
- #region 设置仓库权限
-
- public enum AccessLevel : uint
- {
- NoAccess = 0,
- ReadOnly,
- ReadWrite
- }
-
-
-
-
-
-
- public static bool SetRepositoryPermission(string userName, string repository)
- {
- return SetRepositoryPermission(userName, repository, 2);
- }
-
-
-
-
-
-
- public static bool SetRepositoryPermission(List<string> users, string repository)
- {
- string userNames = "";
- foreach (string user in users)
- {
- userNames += user + ",";
- }
- return SetRepositoryPermission(userNames, repository, 2);
- }
-
-
-
-
-
-
-
- public static bool SetRepositoryPermission(string userName, string repository, int permission)
- {
- try
- {
- string[] users = userName.Split(new[] {","}, StringSplitOptions.RemoveEmptyEntries);
- IDictionary<string, AccessLevel> permissions = GetPermissions(repository, "/");
- foreach (string s in users)
- {
- if (!permissions.ContainsKey(s))
- {
- permissions.Add(s, AccessLevel.ReadWrite);
- }
- }
- SetPermissions(repository, "/", permissions);
- return true;
- }
- catch (Exception)
- {
- return false;
- }
- }
-
-
-
-
-
-
- private static ManagementObject GetRepositoryObject(string name)
- {
- return new ManagementObject("root\\VisualSVN", string.Format("VisualSVN_Repository.Name=‘{0}‘", name), null);
- }
-
-
-
-
-
-
-
- private static ManagementObject GetPermissionObject(string sid, AccessLevel accessLevel)
- {
- var accountClass = new ManagementClass("root\\VisualSVN",
- "VisualSVN_WindowsAccount", null);
- var entryClass = new ManagementClass("root\\VisualSVN",
- "VisualSVN_PermissionEntry", null);
- ManagementObject account = accountClass.CreateInstance();
- if (account != null) account["SID"] = sid;
- ManagementObject entry = entryClass.CreateInstance();
- if (entry != null)
- {
- entry["AccessLevel"] = accessLevel;
- entry["Account"] = account;
- return entry;
- }
- return null;
- }
-
-
-
-
-
-
-
- private static void SetPermissions(string repositoryName, string path,
- IEnumerable<KeyValuePair<string, AccessLevel>> permissions)
- {
- ManagementObject repository = GetRepositoryObject(repositoryName);
- ManagementBaseObject inParameters = repository.GetMethodParameters("SetSecurity");
- inParameters["Path"] = path;
- IEnumerable<ManagementObject> permissionObjects =
- permissions.Select(p => GetPermissionObject(p.Key, p.Value));
- inParameters["Permissions"] = permissionObjects.ToArray();
- repository.InvokeMethod("SetSecurity", inParameters, null);
- }
-
-
-
-
-
-
-
- private static IDictionary<string, AccessLevel> GetPermissions(string repositoryName, string path)
- {
- ManagementObject repository = GetRepositoryObject(repositoryName);
- ManagementBaseObject inParameters = repository.GetMethodParameters("GetSecurity");
- inParameters["Path"] = path;
- ManagementBaseObject outParameters = repository.InvokeMethod("GetSecurity", inParameters, null);
-
- var permissions = new Dictionary<string, AccessLevel>();
-
- if (outParameters != null)
- foreach (ManagementBaseObject p in (ManagementBaseObject[]) outParameters["Permissions"])
- {
-
-
-
-
- var account = (ManagementBaseObject) p["Account"];
- var sid = (string) account["SID"];
- var accessLevel = (AccessLevel) p["AccessLevel"];
-
- permissions[sid] = accessLevel;
- }
-
- return permissions;
- }
-
- #endregion
-
- #region 创建用户组
-
-
-
-
- public static bool CreatGroup(string groupName)
- {
- try
- {
- var svn = new ManagementClass("root\\VisualSVN", "VisualSVN_Group", null);
- ManagementBaseObject @params = svn.GetMethodParameters("Create");
- @params["Name"] = groupName.Trim();
- @params["Members"] = new object[] {};
- svn.InvokeMethod("Create", @params, null);
- return true;
- }
- catch (Exception)
- {
- return false;
- }
- }
-
- #endregion
-
- #region 创建用户
-
-
-
-
-
-
-
- public static bool CreateUser(string userName, string password)
- {
- try
- {
- var svn = new ManagementClass("root\\VisualSVN", "VisualSVN_User", null);
- ManagementBaseObject @params = svn.GetMethodParameters("Create");
- @params["Name"] = userName.Trim();
- @params["Password"] = password.Trim();
- svn.InvokeMethod("Create", @params, null);
- return true;
- }
- catch (Exception)
- {
- return false;
- }
- }
-
- #endregion
-
- #region 创建svn仓库
-
-
-
-
-
-
- public static bool CreateRepository(string name)
- {
- try
- {
- var svn = new ManagementClass("root\\VisualSVN", "VisualSVN_Repository", null);
- ManagementBaseObject @params = svn.GetMethodParameters("Create");
- @params["Name"] = name.Trim();
- svn.InvokeMethod("Create", @params, null);
- return true;
- }
- catch (Exception)
- {
- return false;
- }
- }
-
- #endregion
-
- #region 创建svn仓库目录
-
-
-
-
-
-
-
- public static bool CreateRepositoryFolders(string repositories, string[] name)
- {
- try
- {
- var repository = new ManagementClass("root\\VisualSVN", "VisualSVN_Repository", null);
- ManagementObject repoObject = repository.CreateInstance();
- if (repoObject != null)
- {
- repoObject.SetPropertyValue("Name", repositories);
- ManagementBaseObject inParams = repository.GetMethodParameters("CreateFolders");
- inParams["Folders"] = name;
- inParams["Message"] = "";
- repoObject.InvokeMethod("CreateFolders", inParams, null);
- }
- return true;
- }
- catch (Exception)
- {
- return false;
- }
- }
-
- #endregion
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- #endregion
-
- #region 读取
-
-
-
-
-
-
- public static List<string> GetGroupUsers(string groupName)
- {
- var listUsers = new List<string>();
- var group = new ManagementClass("root\\VisualSVN", "VisualSVN_Group", null);
- ManagementObject instance = group.CreateInstance();
- if (instance != null)
- {
- instance.SetPropertyValue("Name", groupName.Trim());
- ManagementBaseObject outParams = instance.InvokeMethod("GetMembers", null, null);
- if (outParams != null)
- {
- var members = outParams["Members"] as ManagementBaseObject[];
- if (members != null)
- {
- foreach (ManagementBaseObject member in members)
- {
- object name = member["Name"];
- listUsers.Add(name.ToString());
- }
- }
- }
- }
- return listUsers;
- }
-
- public static List<ManagementBaseObject> GetGroupUsersArr(string groupName)
- {
- var list = new List<ManagementBaseObject>();
- var group = new ManagementClass("root\\VisualSVN", "VisualSVN_Group", null);
- ManagementObject instance = group.CreateInstance();
- if (instance != null)
- {
- instance.SetPropertyValue("Name", groupName.Trim());
- ManagementBaseObject outParams = instance.InvokeMethod("GetMembers", null, null);
- if (outParams != null)
- {
- var members = outParams["Members"] as ManagementBaseObject[];
- if (members != null)
- {
- foreach (ManagementBaseObject member in members)
- {
- list.Add(member);
- }
- }
- }
- }
- return list;
- }
-
- #endregion
- }
- }
参考文章:
1.http://msdn.microsoft.com/en-us/library/bb404655
2.http://stackoverflow.com/questions/10017282/control-visualsvn-server-from-c-sharp
C#控制管理VisualSVN Server
标签:des style blog http color io os 使用 ar
原文地址:http://www.cnblogs.com/lonelyxmas/p/4004015.html