【问题标题】:How to add permission entries to visual svn server in C# [duplicate]如何在 C# 中向可视 svn 服务器添加权限条目 [重复]
【发布时间】:2013-11-20 21:55:54
【问题描述】:

我查看了this question 并使用 WMI 界面创建了一个存储库并为用户添加了权限。我现在遇到的问题是:如果我尝试更新我创建的存储库并将 another 用户添加到存储库,它会清除当前使用(将其完全从存储库中删除)和只添加一个人。

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

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

我想一旦我有了它,我就能弄清楚我的其余互动。我参考了 wof 文件并找到了我认为我需要实现的这些条目:

类 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);
}

我正在像这样实现集合安全性:

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);
}

这可行,但就像我说的那样,仅适用于一个用户。似乎它清除了那里的任何东西,只添加了一个用户对象。

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

VisualSVN_PermissionEntry

class VisualSVN_PermissionEntry
{
    VisualSVN_Account Account;
    uint32 AccessLevel;
};

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

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

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

class VisualSVN_WindowsAccount : VisualSVN_Account
{
    [key] string SID;
};

所以这就是我迷路的地方。我假设我需要调用“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.

【问题讨论】:

标签: c# svn wmi visualsvn-server


【解决方案1】:

我不久前从 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;
    }
}

我认为这就是所有相关代码。如果您发现缺少任何内容,请告诉我,我可以仔细检查项目(已经一个月了,我忘记了大部分内容)

【讨论】:

猜你喜欢
  • 2018-09-23
  • 1970-01-01
  • 1970-01-01
  • 2023-03-23
  • 2017-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多