Commit c9461083 authored by Alex Kalderimis's avatar Alex Kalderimis

Cleaner implementation of visible_to_user?

This splits the ability check into two parts: the capability name and
the object of the check, simplifying matters considerably and reducing
repetition.
parent 960eff07
......@@ -135,29 +135,11 @@ class Event < ApplicationRecord
super(presenter_class: ::EventPresenter)
end
# rubocop:disable Metrics/CyclomaticComplexity
# rubocop:disable Metrics/PerceivedComplexity
def visible_to_user?(user = nil)
if push_action? || commit_note?
Ability.allowed?(user, :download_code, project)
elsif membership_changed?
Ability.allowed?(user, :read_project, project)
elsif created_project_action?
Ability.allowed?(user, :read_project, project)
elsif issue? || issue_note?
Ability.allowed?(user, :read_issue, note? ? note_target : target)
elsif merge_request? || merge_request_note?
Ability.allowed?(user, :read_merge_request, note? ? note_target : target)
elsif personal_snippet_note? || project_snippet_note?
Ability.allowed?(user, :read_snippet, note_target)
elsif milestone?
Ability.allowed?(user, :read_milestone, project)
else
false # No other event types are visible
end
return false unless capability.present?
Ability.allowed?(user, capability, permission_object)
end
# rubocop:enable Metrics/PerceivedComplexity
# rubocop:enable Metrics/CyclomaticComplexity
def resource_parent
project || group
......@@ -364,8 +346,38 @@ class Event < ApplicationRecord
Event._to_partial_path
end
protected
def capability
@capability ||= begin
if push_action? || commit_note?
:download_code
elsif membership_changed? || created_project_action?
:read_project
elsif issue? || issue_note?
:read_issue
elsif merge_request? || merge_request_note?
:read_merge_request
elsif personal_snippet_note? || project_snippet_note?
:read_snippet
elsif milestone?
:read_milestone
end
end
end
private
def permission_object
if note?
note_target
elsif target_id.present?
target
else
project
end
end
def push_action_name
if new_ref?
"pushed new"
......
......@@ -18,15 +18,21 @@ module EE
scope :epics, -> { where(target_type: 'Epic') }
end
override :visible_to_user?
def visible_to_user?(user = nil)
if epic?
Ability.allowed?(user, :read_epic, target)
elsif epic_note?
Ability.allowed?(user, :read_epic, note_target)
else
super
end
override :capability
def capability
@capability ||= begin
if epic? || epic_note?
:read_epic
elsif design_note?
:read_design
else
super
end
end
end
def design_note?
note? && note.for_design?
end
def epic_note?
......
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