From 8db947bcfe2f3e988c995696f0fd24d9edfdc3b0 Mon Sep 17 00:00:00 2001 From: Saito Date: Fri, 29 Jun 2012 18:11:37 +0800 Subject: [PATCH] implements protected branches to smart http protocal --- config/initializers/grack_auth.rb | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/config/initializers/grack_auth.rb b/config/initializers/grack_auth.rb index 27a0a1d..bb34ce6 100644 --- a/config/initializers/grack_auth.rb +++ b/config/initializers/grack_auth.rb @@ -18,14 +18,29 @@ module Grack elsif @env['REQUEST_METHOD'] == 'POST' if @env['REQUEST_URI'].end_with?('git-upload-pack') return project.dev_access_for?(user) - elsif @env['REQUEST_URI'].end_with?('git-upload-pack') - #TODO master branch protection - return project.dev_access_for?(user) + elsif @env['REQUEST_URI'].end_with?('git-receive-pack') + if project.protected_branches.map(&:name).include?(current_ref) + project.master_access_for?(user) + else + project.dev_access_for?(user) + end else false end + else + false end - end# valid? + + def current_ref + if @env["HTTP_CONTENT_ENCODING"] =~ /gzip/ + input = Zlib::GzipReader.new(@request.body).string + else + input = @request.body.string + end + + oldrev, newrev, ref = input.split(' ') + /refs\/heads\/([\w-]+)/.match(ref).to_a.last + end end# Auth end# Grack -- libgit2 0.21.2