mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
DEV: Support inverted guardian predicate methods
This commit is contained in:
@@ -96,6 +96,21 @@ class Guardian
|
||||
end
|
||||
end
|
||||
|
||||
# Support `cannot_do?` as an alias of `if !can_do?` and `unless can_do?`.
|
||||
def method_missing(name, *arguments)
|
||||
prefix, check = name.to_s.split("_", 2)
|
||||
|
||||
if prefix == "cannot"
|
||||
!send("can_#{check}", *arguments)
|
||||
else
|
||||
super
|
||||
end
|
||||
end
|
||||
|
||||
def respond_to_missing?(name)
|
||||
name.to_s.start_with?("cannot_") || super
|
||||
end
|
||||
|
||||
attr_reader :request
|
||||
|
||||
def initialize(user = nil, request = nil)
|
||||
@@ -188,7 +203,7 @@ class Guardian
|
||||
# can_create_klass_on_parent?
|
||||
target = klass.name.underscore
|
||||
if parent.present?
|
||||
return false unless can_see?(parent)
|
||||
return false if cannot_see?(parent)
|
||||
target << "_on_#{parent.class.name.underscore}"
|
||||
end
|
||||
create_method = :"can_create_#{target}?"
|
||||
@@ -605,7 +620,7 @@ class Guardian
|
||||
return false if SiteSetting.secure_uploads?
|
||||
return false if topic.blank?
|
||||
return false if topic.private_message?
|
||||
return false unless can_see_topic?(topic)
|
||||
return false if cannot_see_topic?(topic)
|
||||
is_staff?
|
||||
end
|
||||
|
||||
|
@@ -19,6 +19,6 @@ module EnsureMagic
|
||||
|
||||
# Make sure we can see the object. Will raise a NotFound if it's nil
|
||||
def ensure_can_see!(obj)
|
||||
raise Discourse::InvalidAccess.new("Can't see #{obj}") unless can_see?(obj)
|
||||
raise Discourse::InvalidAccess.new("Can't see #{obj}") if cannot_see?(obj)
|
||||
end
|
||||
end
|
||||
|
@@ -217,7 +217,7 @@ module PostGuardian
|
||||
end
|
||||
|
||||
def can_delete_post?(post)
|
||||
return false if !can_see_post?(post)
|
||||
return false if cannot_see_post?(post)
|
||||
|
||||
# Can't delete the first post
|
||||
return false if post.is_first_post?
|
||||
@@ -247,7 +247,7 @@ module PostGuardian
|
||||
return false if !SiteSetting.can_permanently_delete
|
||||
return false if !post
|
||||
return false if post.is_first_post?
|
||||
return false if !is_admin? || !can_edit_post?(post)
|
||||
return false if !is_admin? || cannot_edit_post?(post)
|
||||
return false if !post.deleted_at
|
||||
if post.deleted_by_id == @user.id && post.deleted_at >= Post::PERMANENT_DELETE_TIMER.ago
|
||||
return false
|
||||
@@ -310,7 +310,7 @@ module PostGuardian
|
||||
def can_see_post?(post)
|
||||
return false if post.blank?
|
||||
return true if is_admin?
|
||||
return false unless can_see_post_topic?(post)
|
||||
return false if cannot_see_post_topic?(post)
|
||||
unless post.user == @user || Topic.visible_post_types(@user).include?(post.post_type)
|
||||
return false
|
||||
end
|
||||
|
@@ -4,7 +4,7 @@
|
||||
module PostRevisionGuardian
|
||||
def can_see_post_revision?(post_revision)
|
||||
return false unless post_revision
|
||||
return false if post_revision.hidden && !can_view_hidden_post_revisions?
|
||||
return false if post_revision.hidden && cannot_view_hidden_post_revisions?
|
||||
|
||||
can_view_edit_history?(post_revision.post)
|
||||
end
|
||||
|
@@ -90,7 +90,7 @@ module TopicGuardian
|
||||
# Editing Method
|
||||
def can_edit_topic?(topic)
|
||||
return false if Discourse.static_doc_topic_ids.include?(topic.id) && !is_admin?
|
||||
return false unless can_see?(topic)
|
||||
return false if cannot_see?(topic)
|
||||
|
||||
first_post = topic.first_post
|
||||
|
||||
@@ -107,7 +107,7 @@ module TopicGuardian
|
||||
SiteSetting.allow_uncategorized_topics ||
|
||||
topic.category_id != SiteSetting.uncategorized_category_id
|
||||
)
|
||||
return false if !can_create_topic_on_category?(topic.category)
|
||||
return false if cannot_create_topic_on_category?(topic.category)
|
||||
end
|
||||
|
||||
# Editing a shared draft.
|
||||
@@ -186,7 +186,7 @@ module TopicGuardian
|
||||
.count
|
||||
return false if all_posts_count > 1
|
||||
|
||||
return false if !is_admin? || !can_see_topic?(topic)
|
||||
return false if !is_admin? || cannot_see_topic?(topic)
|
||||
return false if !topic.deleted_at
|
||||
if topic.deleted_by_id == @user.id && topic.deleted_at >= Post::PERMANENT_DELETE_TIMER.ago
|
||||
return false
|
||||
@@ -247,7 +247,7 @@ module TopicGuardian
|
||||
end
|
||||
|
||||
# Filter out topics with shared drafts if user cannot see shared drafts
|
||||
if !can_see_shared_draft?
|
||||
if cannot_see_shared_draft?
|
||||
default_scope =
|
||||
default_scope.left_outer_joins(:shared_draft).where("shared_drafts.id IS NULL")
|
||||
end
|
||||
@@ -269,13 +269,13 @@ module TopicGuardian
|
||||
def can_see_topic?(topic, hide_deleted = true)
|
||||
return false unless topic
|
||||
return true if is_admin? && !SiteSetting.suppress_secured_categories_from_admin
|
||||
return false if hide_deleted && topic.deleted_at && !can_see_deleted_topics?(topic.category)
|
||||
return false if hide_deleted && topic.deleted_at && cannot_see_deleted_topics?(topic.category)
|
||||
|
||||
if topic.private_message?
|
||||
return authenticated? && topic.all_allowed_users.where(id: @user.id).exists?
|
||||
end
|
||||
|
||||
return false if topic.shared_draft && !can_see_shared_draft?
|
||||
return false if topic.shared_draft && cannot_see_shared_draft?
|
||||
|
||||
category = topic.category
|
||||
can_see_category?(category) &&
|
||||
@@ -327,8 +327,8 @@ module TopicGuardian
|
||||
end
|
||||
|
||||
def can_edit_tags?(topic)
|
||||
return false unless can_tag_topics?
|
||||
return false if topic.private_message? && !can_tag_pms?
|
||||
return false if cannot_tag_topics?
|
||||
return false if topic.private_message? && cannot_tag_pms?
|
||||
return true if can_edit_topic?(topic)
|
||||
|
||||
if topic&.first_post&.wiki &&
|
||||
@@ -355,7 +355,7 @@ module TopicGuardian
|
||||
|
||||
def can_move_posts?(topic)
|
||||
return false if is_silenced?
|
||||
return false unless can_perform_action_available_to_group_moderators?(topic)
|
||||
return false if cannot_perform_action_available_to_group_moderators?(topic)
|
||||
return false if topic.archetype == "private_message" && !is_staff?
|
||||
true
|
||||
end
|
||||
|
@@ -102,7 +102,7 @@ module UserGuardian
|
||||
end
|
||||
|
||||
def restrict_user_fields?(user)
|
||||
(user.trust_level == TrustLevel[0] && anonymous?) || !can_see_profile?(user)
|
||||
(user.trust_level == TrustLevel[0] && anonymous?) || cannot_see_profile?(user)
|
||||
end
|
||||
|
||||
def can_see_staff_info?(user)
|
||||
|
Reference in New Issue
Block a user