如何在C#中向可视化svn服务器添加权限条目

led*_*per 5 c# svn wmi visualsvn-server

我查看了这个问题,并使用WMI界面创建了一个repo并为用户添加权限.我现在遇到的问题是:如果我正在尝试更新我创建的repo并将另一个用户添加到repo,它会清除当前使用(完全从repo中删除它)并只添加一个人.

所以,我需要弄清楚如何做两件事:

  1. 将用户添加到现有仓库
  2. 从现有仓库中删除用户

我想,一旦有了,我就可以弄清楚其余的互动.我提到了wof文件并发现了我认为需要实现的这些条目:

class VisualSVN_Repository

[provider("VisualSVNWMIProvider"), dynamic]
class VisualSVN_Repository
    {
    [Description ("Repository name"), key]
    string Name;

    ...
    [implemented] void GetSecurity([in] string Path,
                               [out] VisualSVN_PermissionEntry Permissions[]);
    [implemented] void SetSecurity([in] string Path,
                               [in] VisualSVN_PermissionEntry Permissions[],
                               [in] boolean ResetChildren = false);
}
Run Code Online (Sandbox Code Playgroud)

我正在实现set security,如下所示:

static public void UpdatePermissions(string sid, string repository, AccessLevel level, bool isAdmin = false)
{
    ManagementClass repoClass = new ManagementClass("root\\VisualSVN", "VisualSVN_Repository", null);
     ManagementObject repoObject = repoClass.CreateInstance();
    repoObject.SetPropertyValue("Name", repository);

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

    inParams["Path"] = "/";
    inParams["Permissions"] = new object[] { permObject };

    ManagementBaseObject outParams =
        repoObject.InvokeMethod("SetSecurity", inParams, null);
}
Run Code Online (Sandbox Code Playgroud)

这有效,但就像我说的,只适用于一个用户.它似乎清除了那里的任何东西,只是添加了一个用户对象.

我认为我需要与之交互的另一种方法是"GetSecurity",看起来它返回一个VisualSVN_PermissionEntry数组

VisualSVN_PermissionEntry

class VisualSVN_PermissionEntry
{
    VisualSVN_Account Account;
    uint32 AccessLevel;
};
Run Code Online (Sandbox Code Playgroud)

所以这个具有AccessLevel属性和VisualSVN_Account对象

VisualSVN_Account(我正在使用Windows身份验证,所以我需要使用那个)

[provider("VisualSVNWMIProvider"), dynamic, abstract]
class VisualSVN_Account
{
};

class VisualSVN_WindowsAccount : VisualSVN_Account
{
    [key] string SID;
};
Run Code Online (Sandbox Code Playgroud)

所以这里是我迷失的地方,并且可以真正使用我可以运行的C#片段形式的帮助.我假设我需要调用"GetSecurity"然后迭代这些结果并将它们添加到"SetSecurity"的输入参数的对象数组中.我似乎无法让它工作.我遇到的一些伪代码我遇到了各种错误(主要是对象引用错误)

ManagementBaseObject inSecParams=
        repoClass.GetMethodParameters("GetSecurity");

    inSecParams["Path"] = "/";

ManagementBaseObject existingPerms =
        repoObject.InvokeMethod("GetSecurity");

//now I need to loop through the array existingPerms and add them to an array of VisualSVN_PermissionEntry object.

--or--
//can I take this result and just add the users I need to add to it somehow.
Run Code Online (Sandbox Code Playgroud)

任何提示或输入都表示赞赏.在这件事结束之前我不能回家,而我的妻子正在做我最喜欢的晚餐.

led*_*per 5

我前一段时间从visualSVN的某个人那里得到了这个,认为我应该粘贴工作解决方案!

以下是处理创建和存储库权限分配的整个类文件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using System.IO;
using System.Management;

public static class SVNManager
{
    public enum AccessLevel : uint
    {
        NoAccess = 0, ReadOnly, ReadWrite
    }

    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);
        var account = accountClass.CreateInstance();
        account["SID"] = sid;
        var entry = entryClass.CreateInstance();
        entry["AccessLevel"] = accessLevel;
        entry["Account"] = account;
        return entry;
    }

    private static IDictionary<string, AccessLevel> GetPermissions(string repositoryName, string path)
    {
        var repository = GetRepositoryObject(repositoryName);
        var inParameters = repository.GetMethodParameters("GetSecurity");
        inParameters["Path"] = path;
        var outParameters = repository.InvokeMethod("GetSecurity", inParameters, null);

        var permissions = new Dictionary<string, AccessLevel>();

        foreach (var 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;
    }

    private static void SetPermissions(string repositoryName, string path, IDictionary<string, AccessLevel> permissions)
    {
        var repository = GetRepositoryObject(repositoryName);

        var inParameters = repository.GetMethodParameters("SetSecurity");
    inParameters["Path"] = path;

        var permissionObjects = permissions.Select(p => GetPermissionObject(p.Key, p.Value));
        inParameters["Permissions"] = permissionObjects.ToArray();

        repository.InvokeMethod("SetSecurity", inParameters, null);
    }

    /// <summary>
    /// Will execute the commands needed to create a repository on the SVN server
    /// </summary>
    /// <param name="r">Object with the repository name.</param>
    /// <returns>True if creation was successful, False if there was a failure.</returns>
    static public bool CreateRepository(repository r)
    {
        ManagementClass repoClass = new ManagementClass("root\\VisualSVN", "VisualSVN_Repository", null);
        // Obtain in-parameters for the method
        ManagementBaseObject inParams = repoClass.GetMethodParameters("Create");


        // Add the input parameters.
        inParams["Name"] = r.name;

        // Execute the method and obtain the return values.
        ManagementBaseObject outParams =
            repoClass.InvokeMethod("Create", inParams, null);

        return true;
    }
}
Run Code Online (Sandbox Code Playgroud)

我认为那是所有相关的代码.如果你看到任何遗漏,请告诉我,我可以仔细检查项目(这是一个月,我忘记了大部分内容)

  • 以下是PowerShell代码的变体:https://gist.github.com/sean-m/9522905非常感谢! (3认同)