有好长时间没有写过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文件看看说明:
500)this.width=500'># The pre-commit hook is invoked before a Subversion txn is500)this.width=500'># committed. Subversion runs this hook by invoking a program500)this.width=500'># (script, executable, binary, etc.) named 'pre-commit' (for which500)this.width=500'># this file is a template), with the following ordered arguments:500)this.width=500'>#500)this.width=500'># [1] REPOS-PATH (the path to this repository)500)this.width=500'># [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的帮助.
500)this.width=500'>Name500)this.width=500'>svnlook author — Print the author.500)this.width=500'>500)this.width=500'>Synopsis500)this.width=500'>svnlook author REPOS_PATH500)this.width=500'>Description500)this.width=500'>Print the author of a revision or transaction in the repository.500)this.width=500'>500)this.width=500'>Switches500)this.width=500'>--revision (-r)500)this.width=500'>--transaction (-t)500)this.width=500'>500)this.width=500'>Examples500)this.width=500'>svnlook author is handy, but not very exciting:500)this.width=500'>500)this.width=500'>$ svnlook author -r 40 /usr/local/svn/repos 500)this.width=500'>sally500)this.width=500'>500)this.width=500'>通过Repository的路径和TXN可以得到当前提交者的UserName.
500)this.width=500'>Name500)this.width=500'>svnlook changed — Print the paths that were changed.500)this.width=500'>500)this.width=500'>Synopsis500)this.width=500'>svnlook changed REPOS_PATH500)this.width=500'>Description500)this.width=500'>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).500)this.width=500'>500)this.width=500'>Switches500)this.width=500'>--revision (-r)500)this.width=500'>--transaction (-t)500)this.width=500'>500)this.width=500'>Examples500)this.width=500'>This shows a list of all the changed files in revision 39 of a test repository:500)this.width=500'>500)this.width=500'>$ svnlook changed -r 39 /usr/local/svn/repos500)this.width=500'>A trunk/vendors/deli/500)this.width=500'>A trunk/vendors/deli/chips.txt500)this.width=500'>A trunk/vendors/deli/sandwich.txt500)this.width=500'>A trunk/vendors/deli/pickle.txt500)this.width=500'>500)this.width=500'>同上,可以得到当前提交者提交的内容以及提交类型.呵呵,到了这里,实现权限控制的思路应该完全清楚了:)1,通过Pre-commit.exe得到 Repository和TXN.2,通过Svnlook.exe分别得到UserName和CommitContents3,对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文件做为配置文件.Wish have a good day~ |