Gitbilt hooks 简单的账户操作权限控制

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值