2014-03-19 15:01:00 -04:00
|
|
|
module Gitlab
|
|
|
|
class GitAccess
|
|
|
|
DOWNLOAD_COMMANDS = %w{ git-upload-pack git-upload-archive }
|
|
|
|
PUSH_COMMANDS = %w{ git-receive-pack }
|
|
|
|
|
|
|
|
attr_reader :params, :project, :git_cmd, :user
|
|
|
|
|
2014-11-14 11:23:55 -05:00
|
|
|
def check(actor, cmd, project, changes = nil)
|
2014-03-19 15:01:00 -04:00
|
|
|
case cmd
|
|
|
|
when *DOWNLOAD_COMMANDS
|
2014-12-01 09:25:10 -05:00
|
|
|
download_access_check(actor, project)
|
2014-03-19 15:01:00 -04:00
|
|
|
when *PUSH_COMMANDS
|
|
|
|
if actor.is_a? User
|
2014-11-14 11:23:55 -05:00
|
|
|
push_access_check(actor, project, changes)
|
2014-03-19 15:01:00 -04:00
|
|
|
elsif actor.is_a? DeployKey
|
2014-11-14 11:23:55 -05:00
|
|
|
return build_status_object(false, "Deploy key not allowed to push")
|
2014-03-19 15:01:00 -04:00
|
|
|
elsif actor.is_a? Key
|
2014-11-14 11:23:55 -05:00
|
|
|
push_access_check(actor.user, project, changes)
|
2014-03-19 15:01:00 -04:00
|
|
|
else
|
|
|
|
raise 'Wrong actor'
|
|
|
|
end
|
|
|
|
else
|
2014-11-14 11:23:55 -05:00
|
|
|
return build_status_object(false, "Wrong command")
|
2014-03-19 15:01:00 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-12-01 09:25:10 -05:00
|
|
|
def download_access_check(actor, project)
|
|
|
|
if actor.is_a?(User)
|
|
|
|
user_download_access_check(actor, project)
|
|
|
|
elsif actor.is_a?(DeployKey)
|
|
|
|
if actor.projects.include?(project)
|
|
|
|
build_status_object(true)
|
|
|
|
else
|
|
|
|
build_status_object(false, "Deploy key not allowed to access this project")
|
|
|
|
end
|
|
|
|
elsif actor.is_a? Key
|
|
|
|
user_download_access_check(actor.user, project)
|
|
|
|
else
|
|
|
|
raise 'Wrong actor'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def user_download_access_check(user, project)
|
2014-11-14 11:23:55 -05:00
|
|
|
if user && user_allowed?(user) && user.can?(:download_code, project)
|
|
|
|
build_status_object(true)
|
2014-03-19 15:01:00 -04:00
|
|
|
else
|
2014-11-14 11:23:55 -05:00
|
|
|
build_status_object(false, "You don't have access")
|
2014-03-19 15:01:00 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-11-14 11:23:55 -05:00
|
|
|
def push_access_check(user, project, changes)
|
|
|
|
return build_status_object(false, "You don't have access") unless user && user_allowed?(user)
|
|
|
|
return build_status_object(true) if changes.blank?
|
2014-09-01 12:57:25 -04:00
|
|
|
|
|
|
|
changes = changes.lines if changes.kind_of?(String)
|
|
|
|
|
|
|
|
# Iterate over all changes to find if user allowed all of them to be applied
|
|
|
|
changes.each do |change|
|
2014-11-14 11:23:55 -05:00
|
|
|
status = change_access_check(user, project, change)
|
|
|
|
unless status.allowed?
|
2014-09-01 12:57:25 -04:00
|
|
|
# If user does not have access to make at least one change - cancel all push
|
2014-11-14 11:23:55 -05:00
|
|
|
return status
|
2014-09-01 12:57:25 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-11-14 11:23:55 -05:00
|
|
|
return build_status_object(true)
|
2014-09-01 12:57:25 -04:00
|
|
|
end
|
|
|
|
|
2014-11-14 11:23:55 -05:00
|
|
|
def change_access_check(user, project, change)
|
2014-10-07 09:05:24 -04:00
|
|
|
oldrev, newrev, ref = change.split(' ')
|
|
|
|
|
|
|
|
action = if project.protected_branch?(branch_name(ref))
|
|
|
|
# we dont allow force push to protected branch
|
|
|
|
if forced_push?(project, oldrev, newrev)
|
|
|
|
:force_push_code_to_protected_branches
|
|
|
|
# and we dont allow remove of protected branch
|
2014-11-03 14:02:12 -05:00
|
|
|
elsif newrev == Gitlab::Git::BLANK_SHA
|
2014-10-07 09:05:24 -04:00
|
|
|
:remove_protected_branches
|
|
|
|
else
|
|
|
|
:push_code_to_protected_branches
|
|
|
|
end
|
|
|
|
elsif project.repository && project.repository.tag_names.include?(tag_name(ref))
|
|
|
|
# Prevent any changes to existing git tag unless user has permissions
|
|
|
|
:admin_project
|
|
|
|
else
|
|
|
|
:push_code
|
|
|
|
end
|
|
|
|
|
2014-11-14 11:23:55 -05:00
|
|
|
if user.can?(action, project)
|
|
|
|
build_status_object(true)
|
|
|
|
else
|
|
|
|
build_status_object(false, "You don't have permission")
|
|
|
|
end
|
2014-10-07 09:05:24 -04:00
|
|
|
end
|
|
|
|
|
2014-09-23 07:18:36 -04:00
|
|
|
def forced_push?(project, oldrev, newrev)
|
2014-12-02 10:42:56 -05:00
|
|
|
Gitlab::ForcePushCheck.force_push?(project, oldrev, newrev)
|
2014-03-19 15:01:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def user_allowed?(user)
|
2014-05-15 04:17:13 -04:00
|
|
|
Gitlab::UserAccess.allowed?(user)
|
2014-03-19 15:01:00 -04:00
|
|
|
end
|
2014-09-23 07:18:36 -04:00
|
|
|
|
|
|
|
def branch_name(ref)
|
|
|
|
ref = ref.to_s
|
|
|
|
if ref.start_with?('refs/heads')
|
|
|
|
ref.sub(%r{\Arefs/heads/}, '')
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def tag_name(ref)
|
|
|
|
ref = ref.to_s
|
|
|
|
if ref.start_with?('refs/tags')
|
|
|
|
ref.sub(%r{\Arefs/tags/}, '')
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
2014-11-14 11:23:55 -05:00
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def build_status_object(status, message = '')
|
|
|
|
GitAccessStatus.new(status, message)
|
|
|
|
end
|
2014-03-19 15:01:00 -04:00
|
|
|
end
|
|
|
|
end
|