C#控制管理VisualSVN Server 分类: C# 2014-05-29 15:51 796人阅读 评论(0) 收藏
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
下面一些代码供参考:
using System.Management;
using System.Management.Instrumentation;
并添加 System.Management,System.Management.Instrumentation这两个组件的引用。
1)创建Subversion 用户 'user1',密码为'secret'
ManagementClass userClass = newManagementClass("root\\VisualSVN","VisualSVN_User",null);
// Obtain in-parameters for the method
ManagementBaseObject inParams =
userClass.GetMethodParameters("Create");
// Add the input parameters.
inParams["Name"] = "user1";
inParams["Password"] = "secret";
// Execute the method and obtain the return values.
ManagementBaseObject outParams =
userClass.InvokeMethod("Create", inParams, null);
2)创建新的repository 'Repo1'
ManagementClass repoClass = newManagementClass("root\\VisualSVN","VisualSVN_Repository",null);
// Obtain in-parameters for the method
ManagementBaseObject inParams =
repoClass.GetMethodParameters("Create");
// Add the input parameters.
inParams["Name"] = "Repo1";
// Execute the method and obtain the return values.
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);
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;//调用windows WMI需引入此类库 namespace Whir.Software.InteralServerManage.SVNManager
{
/// <summary>
/// Visual SVN 操作辅助类:使用WMI控制VisualSVN,MOF文件存在位置:C:\Program Files\VisualSVN Server\WMI\VisualSVNServer.mof
/// </summary>
public class SVNHelper
{
#region 设置 #region 设置仓库权限 public enum AccessLevel : uint
{
NoAccess = 0,
ReadOnly,
ReadWrite
} /// <summary>
/// 设置仓库权限(给用户授权)
/// </summary>
/// <param name="userName">用户名</param>
/// <param name="repository">SVN仓库</param>
public static bool SetRepositoryPermission(string userName, string repository)
{
return SetRepositoryPermission(userName, repository, 2);
} /// <summary>
/// 设置仓库权限(给用户授权)
/// </summary>
/// <param name="users">用户名</param>
/// <param name="repository">SVN仓库</param>
public static bool SetRepositoryPermission(List<string> users, string repository)
{
string userNames = "";
foreach (string user in users)
{
userNames += user + ",";
}
return SetRepositoryPermission(userNames, repository, 2);
} /// <summary>
/// 设置仓库权限(给用户授权)
/// </summary>
/// <param name="userName">用户名</param>
/// <param name="repository">SVN仓库</param>
/// <param name="permission"> 权限码:0拒绝,1只读,2读写</param>
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;
}
} /// <summary>
/// 根据仓库名取得仓库实体
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
private static ManagementObject GetRepositoryObject(string name)
{
return new ManagementObject("root\\VisualSVN", string.Format("VisualSVN_Repository.Name='{0}'", name), null);
} /// <summary>
/// 读取权限实体
/// </summary>
/// <param name="sid"></param>
/// <param name="accessLevel"></param>
/// <returns></returns>
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;
} /// <summary>
/// 设置仓库权限
/// </summary>
/// <param name="repositoryName"></param>
/// <param name="path"></param>
/// <param name="permissions"></param>
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);
} /// <summary>
/// 读取仓库权限
/// </summary>
/// <param name="repositoryName"></param>
/// <param name="path"></param>
/// <returns></returns>
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"])
{
// NOTE: This will fail if VisualSVN Server is configured to use Subversion
// authentication. In that case you'd probably want to check if the account
// is a VisualSVN_WindowsAccount or a VisualSVN_SubversionAccount instance
// and tweak the property name accordingly.
var account = (ManagementBaseObject) p["Account"];
var sid = (string) account["SID"];
var accessLevel = (AccessLevel) p["AccessLevel"]; permissions[sid] = accessLevel;
} return permissions;
} #endregion #region 创建用户组 /// <summary>
/// 创建用户组
/// </summary>
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 创建用户 /// <summary>
/// 创建用户
/// </summary>
/// <param name="userName"></param>
/// <param name="password"></param>
/// <returns></returns>
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仓库 /// <summary>
/// 创建svn仓库
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
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仓库目录 /// <summary>
/// 创建svn仓库目录
/// </summary>
/// <param name="repositories"> </param>
/// <param name="name"></param>
/// <returns></returns>
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 //public static bool SetMemberGroup(string userName, string groupName)
//{
// try
// {
// var userObj = GetGroupUsersArr(groupName);
// foreach (ManagementBaseObject baseObject in userObj)
// {
// if (baseObject["Name"].ToString().ToLower() == userName)
// {
// return false;
// }
// }
// var addUser = new ManagementClass("root\\VisualSVN", "VisualSVN_SubversionAccount", null).CreateInstance();
// if (addUser != null)
// {
// addUser.SetPropertyValue("Name", userName);
// userObj.Add(addUser);
// } // var svnUser = new ManagementClass("root\\VisualSVN", "VisualSVN_Group", null);
// ManagementBaseObject inParams = svnUser.GetMethodParameters("SetMembers");
// inParams["Members"] = new object[] { userObj };
// svnUser.InvokeMethod("SetMembers", inParams, null);
// return true;
// }
// catch (Exception)
// {
// return false;
// }
//} #endregion #region 读取 /// <summary>
/// 读取指定组里的用户
/// </summary>
/// <param name="groupName"></param>
/// <returns></returns>
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 分类: C# 2014-05-29 15:51 796人阅读 评论(0) 收藏的更多相关文章
- hadoop调优之一:概述 分类: A1_HADOOP B3_LINUX 2015-03-13 20:51 395人阅读 评论(0) 收藏
hadoop集群性能低下的常见原因 (一)硬件环境 1.CPU/内存不足,或未充分利用 2.网络原因 3.磁盘原因 (二)map任务原因 1.输入文件中小文件过多,导致多次启动和停止JVM进程.可以设 ...
- Http,Https(SSL)的Url绝对路径,相对路径解决方案Security Switch 4.2的配置和使用 分类: ASP.NET 2014-11-05 12:51 97人阅读 评论(0) 收藏
下载地址1:https://securityswitch.googlecode.com/files/SecuritySwitch%20v4.2.0.0%20-%20Binary.zip 下载地址2:h ...
- IOS之富文本编辑 分类: ios技术 2015-03-06 22:51 89人阅读 评论(0) 收藏
之前做项目时遇到一个问题: 使用UITextView显示一段电影的简介,由于字数比较多,所以字体设置的很小,行间距和段间距也很小,一大段文字挤在一起看起来很别扭,想要把行间距调大,结 ...
- Find The Multiple 分类: 搜索 POJ 2015-08-09 15:19 3人阅读 评论(0) 收藏
Find The Multiple Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 21851 Accepted: 8984 Sp ...
- DZY Loves Chemistry 分类: CF 比赛 图论 2015-08-08 15:51 3人阅读 评论(0) 收藏
DZY Loves Chemistry time limit per test 1 second memory limit per test 256 megabytes input standard ...
- 周赛-DZY Loves Chessboard 分类: 比赛 搜索 2015-08-08 15:48 4人阅读 评论(0) 收藏
DZY Loves Chessboard time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Ultra-QuickSort 分类: POJ 排序 2015-08-03 15:39 2人阅读 评论(0) 收藏
Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 48111 Accepted: 17549 ...
- Drainage Ditches 分类: POJ 图论 2015-07-29 15:01 7人阅读 评论(0) 收藏
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 62016 Accepted: 23808 De ...
- cf 61E. Enemy is weak 树状数组求逆序数(WA) 分类: Brush Mode 2014-10-19 15:16 104人阅读 评论(0) 收藏
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> ...
随机推荐
- java 小记
1.获取web项目根目录的绝对路径 request.getContextPath() 获取项目名称,如 /BiYeSheJi getServletContext().getRealPath(& ...
- 锋利的jQuery-4--动画方法总结简表
- jquery设置和获得checkbox选中问题
1.设置checkbox选中: //选中多选框 checkbox=$("#agentinfo input[name='veri[]']"); //循环多选框中的值 checkbox ...
- Metasploit自动攻击和选择模块攻击详解
Author:魔术@Freebuf.com 0×1自动攻击 终端启动Metasploit,因为我现在Source Code,所以这样启动! 连接数据库 安装方法,执行以下命令即可(请用ROOT执行). ...
- http https 区别
HTTPS和HTTP的区别 一.https协议需要到ca申请证书,一般免费证书很少,需要交费. 二.http是超文本传输协议,信息是明文传输,https 则是具有安全性的ssl加密传输协议. 三. ...
- C# ManualResetEvent 的方法介绍
名称 说明 1. Close 在派生类中被重写时,释放由当前 WaitHandle 持有的所有资源. (继承自 WaitHandle.)在XNA Framework中,此成员由 Close() ...
- 如何用sqlyog实现远程连接mysql
1,sqlyog客户端,用root用户远程链接mysql时,提示“访问被拒绝”,在网上搜索了一下原因. 原来是mysql没有授权其远程链接,所以你只能在客户端里面链接. 怎么解决呢? 原表数据 mys ...
- Spark-1.0.0 standalone分布式安装教程
Spark目前支持多种分布式部署方式:一.Standalone Deploy Mode:二Amazon EC2.:三.Apache Mesos:四.Hadoop YARN.第一种方式是单独部署,不需要 ...
- java笔记--枚举总结与详解
由于工作原因,已经有两礼拜没有更新博客了,好不容易完成了工作项目,终于又可以在博客园上愉快的玩耍了. 嗯,今天下午梳理了一下关于java枚举的笔记,比较长,不过还是觉得挺厚实的,哈哈,有出入的地方,欢 ...
- C# params关键字
params数组的要点 C#开发语言中 params 是关键字,可以指定在参数数目可变处采用参数的方法参数.在函数的参数数目可变而执行的代码差异很小的时候很有用! class Program { st ...