import com.gitblit.GitBlit
import com.gitblit.models.RepositoryModel
import com.gitblit.models.UserModel
import org.eclipse.jgit.transport.ReceiveCommand
import org.eclipse.jgit.transport.ReceiveCommand.Result
import org.slf4j.Logger
// 受保护的命令类型映射到返回的结果类型
// 不包括的命令将跳过authz检查
def protectedCmds = [
UPDATE_NONFASTFORWARD: Result.REJECTED_NONFASTFORWARD,
DELETE: Result.REJECTED_NODELETE
]
// 保护文献的正则表达式列表
def protectedRefs = [
"refs/heads/master",
"refs/tags/.+"
]
// 被授权在受保护的参考上执行受保护命令的团队
def authorizedTeams = [ "admins" ]
for (ReceiveCommand command : commands) {
def updateType = command.type
def updatedRef = command.refName
// 找到匹配更新的ref的第一个正则表达式,如果有的话
def refPattern = protectedRefs.find { updatedRef.matches ~it }
// 找到更新类型的拒绝结果,如果有的话
def result = protectedCmds[updateType.name()]
// 如果ref受保护并且具有映射的拒绝结果,则命令需要authz
if (refPattern && result) {
// 验证用户是否是任何授权团队的成员
def team = authorizedTeams.find { user.isTeamMember it }
if (team) {
// 不要调整命令结果
logger.info "${user.username} authorized for ${updateType} of protected ref ${repository.name}:${updatedRef} (${command.oldId.name} -> ${command.newId.name})"
} else {
// 将命令结果标记为拒绝
command.setResult(result, "${user.username} cannot ${updateType} protected ref ${repository.name}:${updatedRef} matching pattern ${refPattern}")
}
}
}
转载于:https://my.oschina.net/u/3756690/blog/1610062