debian-mirror-gitlab/lib/gitlab/git_access.rb

141 lines
3.6 KiB
Ruby
Raw Normal View History

2016-08-24 12:49:21 +05:30
# Check a user's access to perform a git action. All public methods in this
# class return an instance of `GitlabAccessStatus`
2014-09-02 18:07:02 +05:30
module Gitlab
class GitAccess
DOWNLOAD_COMMANDS = %w{ git-upload-pack git-upload-archive }
PUSH_COMMANDS = %w{ git-receive-pack }
2016-08-24 12:49:21 +05:30
attr_reader :actor, :project, :protocol, :user_access
2014-09-02 18:07:02 +05:30
2016-08-24 12:49:21 +05:30
def initialize(actor, project, protocol)
2015-04-26 12:48:37 +05:30
@actor = actor
@project = project
2016-08-24 12:49:21 +05:30
@protocol = protocol
@user_access = UserAccess.new(user, project: project)
2015-04-26 12:48:37 +05:30
end
def check(cmd, changes = nil)
2016-08-24 12:49:21 +05:30
return build_status_object(false, "Git access over #{protocol.upcase} is not allowed") unless protocol_allowed?
2015-09-11 14:41:01 +05:30
unless actor
return build_status_object(false, "No user or key was provided.")
end
2016-08-24 12:49:21 +05:30
if user && !user_access.allowed?
2015-09-11 14:41:01 +05:30
return build_status_object(false, "Your account has been blocked.")
end
2016-08-24 12:49:21 +05:30
unless project && (user_access.can_read_project? || deploy_key_can_read_project?)
2015-09-11 14:41:01 +05:30
return build_status_object(false, 'The project you were looking for could not be found.')
end
2014-09-02 18:07:02 +05:30
case cmd
when *DOWNLOAD_COMMANDS
2015-04-26 12:48:37 +05:30
download_access_check
2014-09-02 18:07:02 +05:30
when *PUSH_COMMANDS
2015-04-26 12:48:37 +05:30
push_access_check(changes)
2014-09-02 18:07:02 +05:30
else
2015-09-11 14:41:01 +05:30
build_status_object(false, "The command you're trying to execute is not allowed.")
2014-09-02 18:07:02 +05:30
end
end
2015-04-26 12:48:37 +05:30
def download_access_check
if user
user_download_access_check
elsif deploy_key
2015-09-11 14:41:01 +05:30
build_status_object(true)
2014-09-02 18:07:02 +05:30
else
2015-04-26 12:48:37 +05:30
raise 'Wrong actor'
2014-09-02 18:07:02 +05:30
end
end
2015-04-26 12:48:37 +05:30
def push_access_check(changes)
if user
user_push_access_check(changes)
elsif deploy_key
2015-09-11 14:41:01 +05:30
build_status_object(false, "Deploy keys are not allowed to push code.")
2014-09-02 18:07:02 +05:30
else
2015-04-26 12:48:37 +05:30
raise 'Wrong actor'
end
end
def user_download_access_check
2016-08-24 12:49:21 +05:30
unless user_access.can_do_action?(:download_code)
2015-09-11 14:41:01 +05:30
return build_status_object(false, "You are not allowed to download code from this project.")
2015-04-26 12:48:37 +05:30
end
2015-09-11 14:41:01 +05:30
build_status_object(true)
2015-04-26 12:48:37 +05:30
end
def user_push_access_check(changes)
if changes.blank?
return build_status_object(true)
end
unless project.repository.exists?
2015-09-11 14:41:01 +05:30
return build_status_object(false, "A repository for this project does not exist yet.")
2015-04-26 12:48:37 +05:30
end
changes = changes.lines if changes.kind_of?(String)
# Iterate over all changes to find if user allowed all of them to be applied
changes.map(&:strip).reject(&:blank?).each do |change|
status = change_access_check(change)
unless status.allowed?
# If user does not have access to make at least one change - cancel all push
return status
end
end
build_status_object(true)
end
def change_access_check(change)
2016-08-24 12:49:21 +05:30
Checks::ChangeAccess.new(change, user_access: user_access, project: project).exec
2015-04-26 12:48:37 +05:30
end
2016-08-24 12:49:21 +05:30
def protocol_allowed?
Gitlab::ProtocolAccess.allowed?(protocol)
2014-09-02 18:07:02 +05:30
end
private
2016-08-24 12:49:21 +05:30
def matching_merge_request?(newrev, branch_name)
Checks::MatchingMergeRequest.new(newrev, branch_name, project).match?
2014-09-02 18:07:02 +05:30
end
2015-04-26 12:48:37 +05:30
2016-08-24 12:49:21 +05:30
def deploy_key
actor if actor.is_a?(DeployKey)
2015-04-26 12:48:37 +05:30
end
2016-08-24 12:49:21 +05:30
def deploy_key_can_read_project?
if deploy_key
return true if project.public?
deploy_key.projects.include?(project)
2015-04-26 12:48:37 +05:30
else
2016-08-24 12:49:21 +05:30
false
2015-04-26 12:48:37 +05:30
end
end
protected
2016-08-24 12:49:21 +05:30
def user
return @user if defined?(@user)
@user =
case actor
when User
actor
when DeployKey
nil
when Key
actor.user
end
end
2015-04-26 12:48:37 +05:30
def build_status_object(status, message = '')
GitAccessStatus.new(status, message)
end
2014-09-02 18:07:02 +05:30
end
end