Commit 7d035c58 authored by Rubén Dávila's avatar Rubén Dávila

Small refactor to reduce number of methods in Gitlab::Checks::ChangeAccess

Plus small update for LfsLocksApiController
parent 1b00af9b
......@@ -61,7 +61,7 @@ class Projects::LfsLocksApiController < Projects::GitHttpClientController
end
def download_request?
%w(index).include?(params[:action])
params[:action] == 'index'
end
def upload_request?
......
......@@ -120,48 +120,21 @@ module Gitlab
end
end
def commit_check
@commit_check ||= Gitlab::Checks::CommitCheck.new(project, user_access.user, newrev, oldrev)
end
def commits_check
return if deletion? || newrev.nil?
commits.each do |commit|
check_commit_diff(commit)
commit_check.validate(commit, validations_for_commit(commit))
end
end
def check_commit_diff(commit)
validations = validations_for_commit(commit)
return if validations.empty?
commit.raw_deltas.each do |diff|
validations.each do |validation|
if error = validation.call(diff)
raise ::Gitlab::GitAccess::UnauthorizedError, error
end
end
end
nil
end
def validations_for_commit(commit)
validate_lfs_file_locks? ? [lfs_file_locks_validation] : []
end
def validate_lfs_file_locks?
project.lfs_enabled? && project.lfs_file_locks.any? && newrev && oldrev
end
def lfs_file_locks_validation
lambda do |diff|
path = diff.new_path || diff.old_path
lfs_lock = project.lfs_file_locks.find_by(path: path)
if lfs_lock && lfs_lock.user != user_access.user
return "The path '#{lfs_lock.path}' is locked in Git LFS by #{lfs_lock.user.name}"
end
end
# Method overwritten in EE to inject custom validations
def validations_for_commit(_)
commit_check.validations
end
private
......
module Gitlab
module Checks
class CommitCheck
attr_reader :project, :user, :newrev, :oldrev
def initialize(project, user, newrev, oldrev)
@project = project
@user = user
@newrev = user
@oldrev = user
end
def validate(commit, validations)
return if validations.empty?
commit.raw_deltas.each do |diff|
validations.each do |validation|
if error = validation.call(diff)
raise ::Gitlab::GitAccess::UnauthorizedError, error
end
end
end
nil
end
def validations
validate_lfs_file_locks? ? [lfs_file_locks_validation] : []
end
private
def validate_lfs_file_locks?
project.lfs_enabled? && project.lfs_file_locks.any? && newrev && oldrev
end
def lfs_file_locks_validation
lambda do |diff|
path = diff.new_path || diff.old_path
lfs_lock = project.lfs_file_locks.find_by(path: path)
if lfs_lock && lfs_lock.user != user
return "The path '#{lfs_lock.path}' is locked in Git LFS by #{lfs_lock.user.name}"
end
end
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment