2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-01-09 18:25:14 -05:00
|
|
|
#mixin for all guardian methods dealing with topic permisions
|
|
|
|
module TopicGuardian
|
|
|
|
|
2017-10-10 04:26:56 -04:00
|
|
|
def can_remove_allowed_users?(topic, target_user = nil)
|
|
|
|
is_staff? ||
|
|
|
|
(
|
|
|
|
topic.allowed_users.count > 1 &&
|
|
|
|
topic.user != target_user &&
|
|
|
|
!!(target_user && user == target_user)
|
|
|
|
)
|
2014-01-09 18:25:14 -05:00
|
|
|
end
|
|
|
|
|
2019-05-03 14:26:37 -04:00
|
|
|
def can_review_topic?(topic)
|
|
|
|
return false if anonymous? || topic.nil?
|
|
|
|
return true if is_staff?
|
|
|
|
|
|
|
|
SiteSetting.enable_category_group_review? &&
|
|
|
|
topic.category.present? &&
|
|
|
|
topic.category.reviewable_by_group_id.present? &&
|
|
|
|
GroupUser.where(group_id: topic.category.reviewable_by_group_id, user_id: user.id).exists?
|
|
|
|
end
|
|
|
|
|
2018-03-13 15:59:12 -04:00
|
|
|
def can_create_shared_draft?
|
|
|
|
is_staff? && SiteSetting.shared_drafts_enabled?
|
|
|
|
end
|
|
|
|
|
2019-05-07 13:34:15 -04:00
|
|
|
def can_create_whisper?
|
|
|
|
is_staff? && SiteSetting.enable_whispers?
|
|
|
|
end
|
|
|
|
|
2018-03-13 15:59:12 -04:00
|
|
|
def can_publish_topic?(topic, category)
|
|
|
|
is_staff? && can_see?(topic) && can_create_topic?(category)
|
|
|
|
end
|
|
|
|
|
2014-01-09 18:25:14 -05:00
|
|
|
# Creating Methods
|
|
|
|
def can_create_topic?(parent)
|
2014-06-09 15:21:01 -04:00
|
|
|
is_staff? ||
|
2014-06-09 11:03:10 -04:00
|
|
|
(user &&
|
|
|
|
user.trust_level >= SiteSetting.min_trust_to_create_topic.to_i &&
|
2019-06-26 07:02:53 -04:00
|
|
|
can_create_post?(parent) &&
|
|
|
|
Category.topic_create_allowed(self).limit(1).count == 1)
|
2014-01-09 18:25:14 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def can_create_topic_on_category?(category)
|
2018-03-01 20:13:04 -05:00
|
|
|
# allow for category to be a number as well
|
2018-03-12 22:20:47 -04:00
|
|
|
category_id = Category === category ? category.id : category
|
2018-03-01 20:13:04 -05:00
|
|
|
|
2014-01-21 09:21:38 -05:00
|
|
|
can_create_topic?(nil) &&
|
2018-03-01 20:13:04 -05:00
|
|
|
(!category || Category.topic_create_allowed(self).where(id: category_id).count == 1)
|
2014-01-09 18:25:14 -05:00
|
|
|
end
|
|
|
|
|
2018-07-12 22:51:08 -04:00
|
|
|
def can_move_topic_to_category?(category)
|
|
|
|
category = Category === category ? category : Category.find(category || SiteSetting.uncategorized_category_id)
|
|
|
|
|
|
|
|
is_staff? || (can_create_topic_on_category?(category) && !category.require_topic_approval?)
|
|
|
|
end
|
|
|
|
|
2014-01-09 18:25:14 -05:00
|
|
|
def can_create_post_on_topic?(topic)
|
|
|
|
# No users can create posts on deleted topics
|
2016-09-09 12:15:56 -04:00
|
|
|
return false if topic.blank?
|
2014-01-09 18:25:14 -05:00
|
|
|
return false if topic.trashed?
|
2016-04-13 01:59:38 -04:00
|
|
|
return true if is_admin?
|
2014-01-09 18:25:14 -05:00
|
|
|
|
2016-04-13 01:59:38 -04:00
|
|
|
trusted = (authenticated? && user.has_trust_level?(TrustLevel[4])) || is_moderator?
|
|
|
|
|
|
|
|
(!(topic.closed? || topic.archived?) || trusted) && can_create_post?(topic)
|
2014-01-09 18:25:14 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Editing Method
|
|
|
|
def can_edit_topic?(topic)
|
2014-07-29 10:40:02 -04:00
|
|
|
return false if Discourse.static_doc_topic_ids.include?(topic.id) && !is_admin?
|
2015-02-26 00:08:52 -05:00
|
|
|
return false unless can_see?(topic)
|
2016-04-13 01:59:38 -04:00
|
|
|
|
|
|
|
return true if is_admin?
|
|
|
|
return true if is_moderator? && can_create_post?(topic)
|
|
|
|
|
2016-06-01 15:41:56 -04:00
|
|
|
# can't edit topics in secured categories where you don't have permission to create topics
|
2019-06-26 03:53:29 -04:00
|
|
|
# except for a tiny edge case where the topic is uncategorized and you are trying
|
|
|
|
# to fix it but uncategorized is disabled
|
|
|
|
if (
|
|
|
|
SiteSetting.allow_uncategorized_topics ||
|
|
|
|
topic.category_id != SiteSetting.uncategorized_category_id
|
|
|
|
)
|
|
|
|
return false if !can_create_topic_on_category?(topic.category)
|
|
|
|
end
|
2016-06-01 15:41:56 -04:00
|
|
|
|
2016-01-28 14:05:56 -05:00
|
|
|
# TL4 users can edit archived topics, but can not edit private messages
|
2018-02-22 20:39:24 -05:00
|
|
|
return true if (
|
|
|
|
SiteSetting.trusted_users_can_edit_others? &&
|
|
|
|
topic.archived &&
|
|
|
|
!topic.private_message? &&
|
|
|
|
user.has_trust_level?(TrustLevel[4]) &&
|
|
|
|
can_create_post?(topic)
|
|
|
|
)
|
2016-04-13 01:59:38 -04:00
|
|
|
|
2016-01-28 14:05:56 -05:00
|
|
|
# TL3 users can not edit archived topics and private messages
|
2018-02-22 20:39:24 -05:00
|
|
|
return true if (
|
|
|
|
SiteSetting.trusted_users_can_edit_others? &&
|
|
|
|
!topic.archived &&
|
|
|
|
!topic.private_message? &&
|
|
|
|
user.has_trust_level?(TrustLevel[3]) &&
|
|
|
|
can_create_post?(topic)
|
|
|
|
)
|
2015-04-30 17:03:51 -04:00
|
|
|
|
2014-08-15 12:44:58 -04:00
|
|
|
return false if topic.archived
|
2019-06-18 14:22:38 -04:00
|
|
|
is_my_own?(topic) &&
|
2019-09-06 07:44:12 -04:00
|
|
|
!topic.edit_time_limit_expired?(user) &&
|
2019-06-18 14:22:38 -04:00
|
|
|
!Post.where(topic_id: topic.id, post_number: 1).where.not(locked_by_id: nil).exists?
|
2014-01-09 18:25:14 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Recovery Method
|
|
|
|
def can_recover_topic?(topic)
|
2019-03-29 12:10:05 -04:00
|
|
|
if is_staff?
|
|
|
|
!!(topic && topic.deleted_at && topic.user)
|
|
|
|
else
|
|
|
|
topic && can_recover_post?(topic.ordered_posts.first)
|
|
|
|
end
|
2014-01-09 18:25:14 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def can_delete_topic?(topic)
|
|
|
|
!topic.trashed? &&
|
2019-03-29 15:59:19 -04:00
|
|
|
(is_staff? || (is_my_own?(topic) && topic.posts_count <= 1 && topic.created_at && topic.created_at > 24.hours.ago)) &&
|
2019-03-29 12:10:05 -04:00
|
|
|
!topic.is_category_topic? &&
|
2014-08-13 17:02:44 -04:00
|
|
|
!Discourse.static_doc_topic_ids.include?(topic.id)
|
2014-01-09 18:25:14 -05:00
|
|
|
end
|
|
|
|
|
2016-05-01 07:48:43 -04:00
|
|
|
def can_convert_topic?(topic)
|
2018-03-02 20:28:39 -05:00
|
|
|
return false unless SiteSetting.enable_personal_messages?
|
2017-10-02 04:04:58 -04:00
|
|
|
return false if topic.blank?
|
2018-05-24 04:41:51 -04:00
|
|
|
return false if topic.trashed?
|
|
|
|
return false if topic.is_category_topic?
|
2016-05-04 12:29:56 -04:00
|
|
|
return true if is_admin?
|
|
|
|
is_moderator? && can_create_post?(topic)
|
2016-05-01 07:48:43 -04:00
|
|
|
end
|
|
|
|
|
2014-01-09 18:25:14 -05:00
|
|
|
def can_reply_as_new_topic?(topic)
|
2016-11-29 12:59:42 -05:00
|
|
|
authenticated? && topic && @user.has_trust_level?(TrustLevel[1])
|
2014-01-09 18:25:14 -05:00
|
|
|
end
|
|
|
|
|
2014-07-15 17:02:43 -04:00
|
|
|
def can_see_deleted_topics?
|
|
|
|
is_staff?
|
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def can_see_topic?(topic, hide_deleted = true)
|
2014-05-12 10:30:10 -04:00
|
|
|
return false unless topic
|
2014-05-12 15:26:36 -04:00
|
|
|
return true if is_admin?
|
2016-06-27 08:36:57 -04:00
|
|
|
return false if hide_deleted && topic.deleted_at && !can_see_deleted_topics?
|
2014-01-09 18:25:14 -05:00
|
|
|
|
2014-08-05 00:37:28 -04:00
|
|
|
if topic.private_message?
|
2016-06-27 08:36:57 -04:00
|
|
|
return authenticated? && topic.all_allowed_users.where(id: @user.id).exists?
|
2014-08-05 00:37:28 -04:00
|
|
|
end
|
|
|
|
|
2016-06-27 08:36:57 -04:00
|
|
|
can_see_category?(topic.category)
|
2015-02-12 11:52:59 -05:00
|
|
|
end
|
2014-01-09 18:25:14 -05:00
|
|
|
|
2015-09-18 03:14:10 -04:00
|
|
|
def can_see_topic_if_not_deleted?(topic)
|
2016-06-27 08:36:57 -04:00
|
|
|
can_see_topic?(topic, false)
|
2015-09-18 03:14:10 -04:00
|
|
|
end
|
|
|
|
|
2019-07-04 04:12:39 -04:00
|
|
|
def can_get_access_to_topic?(topic)
|
|
|
|
topic&.access_topic_via_group.present? && authenticated?
|
|
|
|
end
|
|
|
|
|
2015-02-12 11:52:59 -05:00
|
|
|
def filter_allowed_categories(records)
|
|
|
|
unless is_admin?
|
|
|
|
allowed_ids = allowed_category_ids
|
|
|
|
if allowed_ids.length > 0
|
|
|
|
records = records.where('topics.category_id IS NULL or topics.category_id IN (?)', allowed_ids)
|
|
|
|
else
|
|
|
|
records = records.where('topics.category_id IS NULL')
|
|
|
|
end
|
|
|
|
records = records.references(:categories)
|
|
|
|
end
|
|
|
|
records
|
2014-01-09 18:25:14 -05:00
|
|
|
end
|
2015-02-12 11:52:59 -05:00
|
|
|
|
2016-12-05 07:31:43 -05:00
|
|
|
def can_edit_featured_link?(category_id)
|
2016-12-15 17:46:43 -05:00
|
|
|
return false unless SiteSetting.topic_featured_link_enabled
|
2017-07-27 21:20:09 -04:00
|
|
|
Category.where(id: category_id || SiteSetting.uncategorized_category_id, topic_featured_link_allowed: true).exists?
|
2016-12-05 07:31:43 -05:00
|
|
|
end
|
2018-08-09 20:51:03 -04:00
|
|
|
|
|
|
|
def can_update_bumped_at?
|
2019-01-02 10:57:05 -05:00
|
|
|
is_staff? || @user.has_trust_level?(TrustLevel[4])
|
2018-08-09 20:51:03 -04:00
|
|
|
end
|
2019-04-02 03:08:15 -04:00
|
|
|
|
|
|
|
def can_banner_topic?(topic)
|
2019-06-25 06:49:29 -04:00
|
|
|
topic && authenticated? && !topic.private_message? && is_staff?
|
2019-04-02 03:08:15 -04:00
|
|
|
end
|
2014-01-21 09:21:38 -05:00
|
|
|
end
|