2014-01-10 07:25:14 +08:00
|
|
|
#mixin for all guardian methods dealing with topic permisions
|
|
|
|
module TopicGuardian
|
|
|
|
|
|
|
|
def can_remove_allowed_users?(topic)
|
|
|
|
is_staff?
|
|
|
|
end
|
|
|
|
|
|
|
|
# Creating Methods
|
|
|
|
def can_create_topic?(parent)
|
2014-06-10 03:21:01 +08:00
|
|
|
is_staff? ||
|
2014-06-09 23:03:10 +08:00
|
|
|
(user &&
|
|
|
|
user.trust_level >= SiteSetting.min_trust_to_create_topic.to_i &&
|
|
|
|
can_create_post?(parent))
|
2014-01-10 07:25:14 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def can_create_topic_on_category?(category)
|
2014-01-21 22:21:38 +08:00
|
|
|
can_create_topic?(nil) &&
|
2015-09-08 00:52:53 +08:00
|
|
|
(!category || Category.topic_create_allowed(self).where(id: category.id).count == 1)
|
2014-01-10 07:25:14 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def can_create_post_on_topic?(topic)
|
|
|
|
# No users can create posts on deleted topics
|
|
|
|
return false if topic.trashed?
|
2016-04-13 13:59:38 +08:00
|
|
|
return true if is_admin?
|
2014-01-10 07:25:14 +08:00
|
|
|
|
2016-04-13 13:59:38 +08:00
|
|
|
trusted = (authenticated? && user.has_trust_level?(TrustLevel[4])) || is_moderator?
|
|
|
|
|
|
|
|
(!(topic.closed? || topic.archived?) || trusted) && can_create_post?(topic)
|
2014-01-10 07:25:14 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# Editing Method
|
|
|
|
def can_edit_topic?(topic)
|
2014-07-29 22:40:02 +08:00
|
|
|
return false if Discourse.static_doc_topic_ids.include?(topic.id) && !is_admin?
|
2015-02-26 13:08:52 +08:00
|
|
|
return false unless can_see?(topic)
|
2016-04-13 13:59:38 +08:00
|
|
|
|
|
|
|
return true if is_admin?
|
|
|
|
return true if is_moderator? && can_create_post?(topic)
|
|
|
|
|
2016-01-29 03:05:56 +08:00
|
|
|
# TL4 users can edit archived topics, but can not edit private messages
|
|
|
|
return true if (topic.archived && !topic.private_message? && user.has_trust_level?(TrustLevel[4]) && can_create_post?(topic))
|
2016-04-13 13:59:38 +08:00
|
|
|
|
2016-01-29 03:05:56 +08:00
|
|
|
# TL3 users can not edit archived topics and private messages
|
|
|
|
return true if (!topic.archived && !topic.private_message? && user.has_trust_level?(TrustLevel[3]) && can_create_post?(topic))
|
2015-05-01 05:03:51 +08:00
|
|
|
|
2014-08-16 00:44:58 +08:00
|
|
|
return false if topic.archived
|
2015-02-26 03:53:21 +08:00
|
|
|
is_my_own?(topic) && !topic.edit_time_limit_expired?
|
2014-01-10 07:25:14 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# Recovery Method
|
|
|
|
def can_recover_topic?(topic)
|
|
|
|
is_staff?
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_delete_topic?(topic)
|
|
|
|
!topic.trashed? &&
|
|
|
|
is_staff? &&
|
2014-08-14 05:02:44 +08:00
|
|
|
!(Category.exists?(topic_id: topic.id)) &&
|
|
|
|
!Discourse.static_doc_topic_ids.include?(topic.id)
|
2014-01-10 07:25:14 +08:00
|
|
|
end
|
|
|
|
|
2016-05-01 19:48:43 +08:00
|
|
|
def can_convert_topic?(topic)
|
|
|
|
topic && !topic.trashed? && is_admin?
|
|
|
|
end
|
|
|
|
|
2014-01-10 07:25:14 +08:00
|
|
|
def can_reply_as_new_topic?(topic)
|
2014-09-05 13:20:39 +08:00
|
|
|
authenticated? && topic && not(topic.private_message?) && @user.has_trust_level?(TrustLevel[1])
|
2014-01-10 07:25:14 +08:00
|
|
|
end
|
|
|
|
|
2014-07-16 05:02:43 +08:00
|
|
|
def can_see_deleted_topics?
|
|
|
|
is_staff?
|
|
|
|
end
|
|
|
|
|
2014-01-10 07:25:14 +08:00
|
|
|
def can_see_topic?(topic)
|
2014-05-12 22:30:10 +08:00
|
|
|
return false unless topic
|
2014-07-16 05:02:43 +08:00
|
|
|
# Admins can see everything
|
2014-05-13 03:26:36 +08:00
|
|
|
return true if is_admin?
|
2014-07-16 05:02:43 +08:00
|
|
|
# Deleted topics
|
|
|
|
return false if topic.deleted_at && !can_see_deleted_topics?
|
2014-01-10 07:25:14 +08:00
|
|
|
|
2014-08-05 12:37:28 +08:00
|
|
|
if topic.private_message?
|
|
|
|
return authenticated? &&
|
|
|
|
topic.all_allowed_users.where(id: @user.id).exists?
|
|
|
|
end
|
|
|
|
|
2014-05-12 22:30:10 +08:00
|
|
|
# not secure, or I can see it
|
2014-08-05 12:37:28 +08:00
|
|
|
!topic.read_restricted_category? || can_see_category?(topic.category)
|
2015-02-13 00:52:59 +08:00
|
|
|
end
|
2014-01-10 07:25:14 +08:00
|
|
|
|
2015-09-18 15:14:10 +08:00
|
|
|
def can_see_topic_if_not_deleted?(topic)
|
|
|
|
return false unless topic
|
|
|
|
# Admins can see everything
|
|
|
|
return true if is_admin?
|
|
|
|
# Deleted topics
|
|
|
|
# return false if topic.deleted_at && !can_see_deleted_topics?
|
|
|
|
|
|
|
|
if topic.private_message?
|
|
|
|
return authenticated? &&
|
|
|
|
topic.all_allowed_users.where(id: @user.id).exists?
|
|
|
|
end
|
|
|
|
|
|
|
|
# not secure, or I can see it
|
|
|
|
!topic.read_restricted_category? || can_see_category?(topic.category)
|
|
|
|
end
|
|
|
|
|
2015-02-13 00:52:59 +08: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-10 07:25:14 +08:00
|
|
|
end
|
2015-02-13 00:52:59 +08:00
|
|
|
|
2014-01-21 22:21:38 +08:00
|
|
|
end
|