Commit b84eb343 authored by Lin Jen-Shin's avatar Lin Jen-Shin

Try to merge permission checks into one

parent 7bd5e571
...@@ -19,18 +19,20 @@ module Ci ...@@ -19,18 +19,20 @@ module Ci
return error('Pipeline is disabled') return error('Pipeline is disabled')
end end
unless trigger_request || can?(current_user, :create_pipeline, project) triggering_user = current_user || trigger_request.trigger.owner
return error('Insufficient permissions to create a new pipeline')
unless allowed_to_trigger_pipeline?(triggering_user)
if can?(triggering_user, :create_pipeline, project)
return error("Insufficient permissions for protected ref '#{ref}'")
else
return error('Insufficient permissions to create a new pipeline')
end
end end
unless branch? || tag? unless branch? || tag?
return error('Reference not found') return error('Reference not found')
end end
unless triggering_user_allowed_for_ref?(trigger_request)
return error("Insufficient permissions for protected ref '#{ref}'")
end
unless commit unless commit
return error('Commit not found') return error('Commit not found')
end end
...@@ -74,9 +76,7 @@ module Ci ...@@ -74,9 +76,7 @@ module Ci
pipeline.tap(&:process!) pipeline.tap(&:process!)
end end
def triggering_user_allowed_for_ref?(trigger_request) def allowed_to_trigger_pipeline?(triggering_user)
triggering_user = current_user || trigger_request.trigger.owner
if triggering_user if triggering_user
allowed_to_create?(triggering_user) allowed_to_create?(triggering_user)
else # legacy triggers don't have a corresponding user else # legacy triggers don't have a corresponding user
...@@ -87,7 +87,7 @@ module Ci ...@@ -87,7 +87,7 @@ module Ci
def allowed_to_create?(triggering_user) def allowed_to_create?(triggering_user)
access = Gitlab::UserAccess.new(triggering_user, project: project) access = Gitlab::UserAccess.new(triggering_user, project: project)
Ability.allowed?(triggering_user, :create_pipeline, project) && can?(triggering_user, :create_pipeline, project) &&
if branch? if branch?
access.can_update_branch?(ref) access.can_update_branch?(ref)
elsif tag? elsif tag?
......
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