有好長(zhǎng)時(shí)間沒(méi)有寫過(guò)Blog了...具體的原因真是一大堆啊,不說(shuō)也罷,反正,偶現(xiàn)在是回來(lái)了:)
嘿嘿.
The Return of the hBifTS.
這幾天一直在玩SubVersion,據(jù)說(shuō)是比CVS更加好用的,更加強(qiáng)大的版本控制系統(tǒng)...
它的優(yōu)點(diǎn)有很多,具體就不在這里介紹了.隨便Google一把,都會(huì)有一大堆的:)
SubVersion的Server的實(shí)現(xiàn)有兩種形式,一種就是和Apache的集成,可以實(shí)現(xiàn)http/https的訪問(wèn).而且還可以很方便的通過(guò)Apache的集成來(lái)實(shí)現(xiàn)Per-Directory的認(rèn)證.
另一種方式就是使用svnserve.exe.這是SubVersion內(nèi)置的服務(wù)器.使用起來(lái)要比與Apache要方便,快捷很多.
其功能也相差不了多少(厄,還是相差很多的.)
現(xiàn)在的一個(gè)問(wèn)題就是使用svnserve的方式,不支持Per-Directory的權(quán)限認(rèn)證.
讓我們來(lái)考慮一下怎么實(shí)現(xiàn)Per-Directory的權(quán)限認(rèn)證.
實(shí)現(xiàn)這樣的方式,我們得得到當(dāng)前提交者的UserName,然后就是提交者的提交的路徑.有了這兩個(gè),就可以通過(guò)相應(yīng)的權(quán)限規(guī)則來(lái)判斷是否有權(quán)限提交到些目錄下面.最后一個(gè)就是能確認(rèn)提交或是取消提交,否則權(quán)限控制完全沒(méi)有意義了:)
由于Svn是用C/C++寫的,而且提供了相應(yīng)的.h文件,便于二次開發(fā)/增加插件功能.但是對(duì)于C#而言,就是一個(gè)惡夢(mèng)(想想成堆的結(jié)構(gòu)的聲明,函數(shù)的導(dǎo)入),幸好SubVersion提供了Hooks Script,可以讓我們?cè)赟tart-commit/pre-commit/post-commit的時(shí)候做我們想做的事.既然這樣,實(shí)現(xiàn)一個(gè)類似Per-Directory的認(rèn)證就不是很困難了:)
打開一個(gè)生成的SubVersion的代碼倉(cāng)庫(kù),在目錄下面有一個(gè)Hooks的目錄,進(jìn)去,我們可以看到5個(gè)文件.
打開Pre-commit.tmpl文件看看說(shuō)明:
# 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) 由上面的說(shuō)明可得知,當(dāng)Svn收到了用戶提交上來(lái)的文件,在最終存入Repository前,會(huì)調(diào)用Pre-commit.exe/bat程序.同時(shí)傳兩個(gè)參數(shù)進(jìn)去,一個(gè)就是提交的Repository的路徑(服務(wù)器端的實(shí)際路徑),第二個(gè)就是TXN-Name. Transaction Name,這個(gè)是用于標(biāo)識(shí)此提交進(jìn)程的事務(wù)ID.
有了上述的兩個(gè)參數(shù).再加上Svnlook.exe.我們就可以完成我們想做的事了~
先來(lái)看看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
通過(guò)Repository的路徑和TXN可以得到當(dāng)前提交者的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
同上,可以得到當(dāng)前提交者提交的內(nèi)容以及提交類型.
呵呵,到了這里,實(shí)現(xiàn)權(quán)限控制的思路應(yīng)該完全清楚了:)
1,通過(guò)Pre-commit.exe得到 Repository和TXN.
2,通過(guò)Svnlook.exe分別得到UserName和CommitContents
3,對(duì)UserName和CommitContents中的提交路徑以及權(quán)限規(guī)則分析.
4,如果有權(quán)限提交,返回0.否則返回非0的值.
TestSvnAuth.zip這里是我實(shí)現(xiàn)的一個(gè)權(quán)限控制,當(dāng)然了,沒(méi)有完全實(shí)現(xiàn)Per-Directory的權(quán)限認(rèn)證.不過(guò)我想做為我自己來(lái)使用已夠了:)
config.ini是配置文件,要和pre-commit.exe放置于同一目錄下面.
[Config]
BaseDirectory = Cnblogs/Helper/
指明所有的用戶只能在Cnblogs/Helper/路徑下面提交/修改/刪除文件.(注:此路徑應(yīng)該在svn repository中存在)
[Allow]
hbifts = dudu,steven
表明dudu和steven可以在hbifts用戶的目錄下面(包含子目錄)即: Cnblogs/Helper/hbifts/ 進(jìn)行修改.
[Deny]
Accounts = hbifts,dudu
表明hbifts和dudu不允許在此Repository中進(jìn)行任何修改,提交任何文件.
注: Deny比Allow的優(yōu)先級(jí)要高.由于測(cè)試的關(guān)系,我將所有的函數(shù)調(diào)用記錄寫入了一個(gè).txt文件中,各位測(cè)試的時(shí)候請(qǐng)自行去掉些函數(shù)或修改log文件路徑.由于每提交一次此程序會(huì)運(yùn)行一次,從性能方式考慮,未使用xml文件做為配置文件.