Refactor to remove a special case

This commit is contained in:
Michael Kozono 2017-05-22 12:44:59 -07:00
parent 7d469cf1c1
commit 0a0f66c816
3 changed files with 29 additions and 7 deletions

9
lib/gitlab/ci_access.rb Normal file
View file

@ -0,0 +1,9 @@
module Gitlab
# For backwards compatibility, generic CI (which is a build without a user) is
# allowed to :build_download_code without any other checks.
class CiAccess
def can_do_action?(action)
action == :build_download_code
end
end
end

View file

@ -29,7 +29,11 @@ module Gitlab
@project = project @project = project
@protocol = protocol @protocol = protocol
@authentication_abilities = authentication_abilities @authentication_abilities = authentication_abilities
@user_access = UserAccess.new(user, project: project) @user_access = if ci?
CiAccess.new
else
UserAccess.new(user, project: project)
end
end end
def check(cmd, changes) def check(cmd, changes)
@ -62,11 +66,6 @@ module Gitlab
authentication_abilities.include?(:build_download_code) && user_access.can_do_action?(:build_download_code) authentication_abilities.include?(:build_download_code) && user_access.can_do_action?(:build_download_code)
end end
# Allow generic CI (build without a user) for backwards compatibility
def ci_can_download_code?
authentication_abilities.include?(:build_download_code) && ci?
end
def protocol_allowed? def protocol_allowed?
Gitlab::ProtocolAccess.allowed?(protocol) Gitlab::ProtocolAccess.allowed?(protocol)
end end
@ -129,7 +128,6 @@ module Gitlab
return if deploy_key? return if deploy_key?
passed = user_can_download_code? || passed = user_can_download_code? ||
ci_can_download_code? ||
build_can_download_code? || build_can_download_code? ||
guest_can_download_code? guest_can_download_code?

View file

@ -0,0 +1,15 @@
require 'spec_helper'
describe Gitlab::CiAccess, lib: true do
let(:access) { Gitlab::CiAccess.new }
describe '#can_do_action?' do
context 'when action is :build_download_code' do
it { expect(access.can_do_action?(:build_download_code)).to be_truthy }
end
context 'when action is not :build_download_code' do
it { expect(access.can_do_action?(:download_code)).to be_falsey }
end
end
end