宅基地侵权起诉状:增强SVN的Commit权限控制

来源:百度文库 编辑:偶看新闻 时间:2024/03/28 23:41:04

增强SVN的Commit权限控制

Posted on 2005-04-03 19:37 hbifts 阅读(2548) 评论(4)  编辑 收藏 引用 收藏至365Key 所属分类: .NET

有好长时间没有写过Blog了...具体的原因真是一大堆啊,不说也罢,反正,偶现在是回来了:)
嘿嘿.

The Return of the hBifTS.

这几天一直在玩SubVersion,据说是比CVS更加好用的,更加强大的版本控制系统...
它的优点有很多,具体就不在这里介绍了.随便Google一把,都会有一大堆的:)

SubVersion的Server的实现有两种形式,一种就是和Apache的集成,可以实现http/https的访问.而且还可以很方便的通过Apache的集成来实现Per-Directory的认证.
另一种方式就是使用svnserve.exe.这是SubVersion内置的服务器.使用起来要比与Apache要方便,快捷很多.
其功能也相差不了多少(厄,还是相差很多的.)

现在的一个问题就是使用svnserve的方式,不支持Per-Directory的权限认证.
让我们来考虑一下怎么实现Per-Directory的权限认证.
实现这样的方式,我们得得到当前提交者的UserName,然后就是提交者的提交的路径.有了这两个,就可以通过相应的权限规则来判断是否有权限提交到些目录下面.最后一个就是能确认提交或是取消提交,否则权限控制完全没有意义了:)

由于Svn是用C/C++写的,而且提供了相应的.h文件,便于二次开发/增加插件功能.但是对于C#而言,就是一个恶梦(想想成堆的结构的声明,函数的导入),幸好SubVersion提供了Hooks Script,可以让我们在Start-commit/pre-commit/post-commit的时候做我们想做的事.既然这样,实现一个类似Per-Directory的认证就不是很困难了:)

打开一个生成的SubVersion的代码仓库,在目录下面有一个Hooks的目录,进去,我们可以看到5个文件.
打开Pre-commit.tmpl文件看看说明:

# The pre-commit hook is invoked before a Subversion txn is
# committed.  Subversion runs this hook by invoking a program
# (script, executable, binary, etc.) named ‘pre-commit‘ (for which
# this file is a template), with the following ordered arguments:
#
#   [1] REPOS-PATH   (the path to this repository)
#   [2] TXN-NAME     (the name of the txn about to be committed)
由上面的说明可得知,当Svn收到了用户提交上来的文件,在最终存入Repository前,会调用Pre-commit.exe/bat程序.同时传两个参数进去,一个就是提交的Repository的路径(服务器端的实际路径),第二个就是TXN-Name. Transaction Name,这个是用于标识此提交进程的事务ID.

有了上述的两个参数.再加上Svnlook.exe.我们就可以完成我们想做的事了~
先来看看Svnlook.exe的帮助.
Name
svnlook author — Print the author.

Synopsis
svnlook author REPOS_PATH
Description
Print the author of a revision or transaction in the repository.

Switches
--revision (-r)
--transaction (-t)

Examples
svnlook author is handy, but not very exciting:

$ svnlook author -r 40 /usr/local/svn/repos 
sally


通过Repository的路径和TXN可以得到当前提交者的UserName.
Name
svnlook changed — Print the paths that were changed.

Synopsis
svnlook changed REPOS_PATH
Description
Print the paths that were changed in a particular revision or transaction, as well as an “svn update-style” status letter in the first column: A for added, D for deleted, and U for updated (modified).

Switches
--revision (-r)
--transaction (-t)

Examples
This shows a list of all the changed files in revision 39 of a test repository:

$ svnlook changed -r 39 /usr/local/svn/repos
A   trunk/vendors/deli/
A   trunk/vendors/deli/chips.txt
A   trunk/vendors/deli/sandwich.txt
A   trunk/vendors/deli/pickle.txt

同上,可以得到当前提交者提交的内容以及提交类型.

呵呵,到了这里,实现权限控制的思路应该完全清楚了:)
1,通过Pre-commit.exe得到 Repository和TXN.
2,通过Svnlook.exe分别得到UserName和CommitContents
3,对UserName和CommitContents中的提交路径以及权限规则分析.
4,如果有权限提交,返回0.否则返回非0的值.

TestSvnAuth.zip

这里是我实现的一个权限控制,当然了,没有完全实现Per-Directory的权限认证.不过我想做为我自己来使用已够了:)
config.ini是配置文件,要和pre-commit.exe放置于同一目录下面.

[Config]
BaseDirectory = Cnblogs/Helper/
指明所有的用户只能在Cnblogs/Helper/路径下面提交/修改/删除文件.(注:此路径应该在svn repository中存在)

[Allow]
hbifts = dudu,steven
表明dudu和steven可以在hbifts用户的目录下面(包含子目录)即: Cnblogs/Helper/hbifts/ 进行修改.

[Deny]
Accounts = hbifts,dudu
表明hbifts和dudu不允许在此Repository中进行任何修改,提交任何文件.

注: Deny比Allow的优先级要高.由于测试的关系,我将所有的函数调用记录写入了一个.txt文件中,各位测试的时候请自行去掉些函数或修改log文件路径.由于每提交一次此程序会运行一次,从性能方式考虑,未使用xml文件做为配置文件.