2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-12-30 08:08:02 -05:00
|
|
|
# mixin for all guardian methods dealing with post permissions
|
2014-05-12 10:30:10 -04:00
|
|
|
module PostGuardian
|
2018-06-13 14:57:32 -04:00
|
|
|
def unrestricted_link_posting?
|
|
|
|
authenticated? && @user.has_trust_level?(TrustLevel[SiteSetting.min_trust_to_post_links])
|
|
|
|
end
|
|
|
|
|
|
|
|
def link_posting_access
|
|
|
|
if unrestricted_link_posting?
|
|
|
|
"full"
|
2020-07-26 20:23:54 -04:00
|
|
|
elsif SiteSetting.allowed_link_domains.present?
|
2018-06-13 14:57:32 -04:00
|
|
|
"limited"
|
|
|
|
else
|
|
|
|
"none"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_post_link?(host: nil)
|
|
|
|
return false if host.blank?
|
|
|
|
|
2020-07-26 20:23:54 -04:00
|
|
|
unrestricted_link_posting? || SiteSetting.allowed_link_domains.split("|").include?(host)
|
2018-02-08 12:56:10 -05:00
|
|
|
end
|
|
|
|
|
2014-01-09 18:25:14 -05:00
|
|
|
# Can the user act on the post in a particular way.
|
|
|
|
# taken_actions = the list of actions the user has already taken
|
2017-09-08 01:07:22 -04:00
|
|
|
def post_can_act?(post, action_key, opts: {}, can_see_post: nil)
|
|
|
|
return false unless (can_see_post.nil? && can_see_post?(post)) || can_see_post
|
2016-12-20 23:01:26 -05:00
|
|
|
|
|
|
|
# no warnings except for staff
|
2020-03-11 08:03:20 -04:00
|
|
|
if action_key == :notify_user &&
|
|
|
|
(
|
|
|
|
post.user.blank? ||
|
|
|
|
(!is_staff? && opts[:is_warning].present? && opts[:is_warning] == "true")
|
2023-01-09 07:10:19 -05:00
|
|
|
)
|
2020-03-11 08:03:20 -04:00
|
|
|
return false
|
2023-01-09 07:10:19 -05:00
|
|
|
end
|
2016-12-20 23:01:26 -05:00
|
|
|
|
2014-01-09 18:25:14 -05:00
|
|
|
taken = opts[:taken_actions].try(:keys).to_a
|
2020-08-19 19:31:40 -04:00
|
|
|
is_flag =
|
|
|
|
PostActionType.notify_flag_types[action_key] || PostActionType.custom_types[action_key]
|
2014-01-09 18:25:14 -05:00
|
|
|
already_taken_this_action = taken.any? && taken.include?(PostActionType.types[action_key])
|
2018-02-27 22:22:51 -05:00
|
|
|
already_did_flagging = taken.any? && (taken & PostActionType.notify_flag_types.values).any?
|
2023-01-09 07:10:19 -05:00
|
|
|
|
2015-04-07 22:29:43 -04:00
|
|
|
result =
|
|
|
|
if authenticated? && post && !@user.anonymous?
|
2018-08-17 11:06:01 -04:00
|
|
|
# Silenced users can't flag
|
|
|
|
return false if is_flag && @user.silenced?
|
2023-01-09 07:10:19 -05:00
|
|
|
|
2018-11-05 10:00:59 -05:00
|
|
|
# Hidden posts can't be flagged
|
|
|
|
return false if is_flag && post.hidden?
|
2023-01-09 07:10:19 -05:00
|
|
|
|
2018-02-12 14:56:21 -05:00
|
|
|
# post made by staff, but we don't allow staff flags
|
2018-02-14 15:46:04 -05:00
|
|
|
return false if is_flag && (!SiteSetting.allow_flagging_staff?) && post&.user&.staff?
|
2023-01-09 07:10:19 -05:00
|
|
|
|
2022-10-04 20:50:20 -04:00
|
|
|
if action_key == :notify_user &&
|
|
|
|
!@user.in_any_groups?(SiteSetting.personal_message_enabled_groups_map)
|
2018-02-27 22:22:51 -05:00
|
|
|
return false
|
2023-01-09 07:10:19 -05:00
|
|
|
end
|
|
|
|
|
2014-03-10 11:48:27 -04:00
|
|
|
# we allow flagging for trust level 1 and higher
|
2015-01-08 10:06:43 -05:00
|
|
|
# always allowed for private messages
|
2023-01-09 07:10:19 -05:00
|
|
|
(
|
2018-02-06 17:12:27 -05:00
|
|
|
is_flag && not(already_did_flagging) &&
|
2023-01-09 07:10:19 -05:00
|
|
|
(
|
2018-02-06 17:12:27 -05:00
|
|
|
@user.has_trust_level?(TrustLevel[SiteSetting.min_trust_to_flag_posts]) ||
|
|
|
|
post.topic.private_message?
|
2023-01-09 07:10:19 -05:00
|
|
|
)
|
|
|
|
) ||
|
2014-01-09 18:25:14 -05:00
|
|
|
# not a flagging action, and haven't done it already
|
|
|
|
not(is_flag || already_taken_this_action) &&
|
2014-08-07 13:12:35 -04:00
|
|
|
# nothing except flagging on archived topics
|
2018-02-27 22:22:51 -05:00
|
|
|
not(post.topic&.archived?) &&
|
2014-08-07 13:12:35 -04:00
|
|
|
# nothing except flagging on deleted posts
|
|
|
|
not(post.trashed?) &&
|
2014-01-09 18:25:14 -05:00
|
|
|
# don't like your own stuff
|
2020-03-11 08:03:20 -04:00
|
|
|
not(action_key == :like && (post.user.blank? || is_my_own?(post)))
|
2018-02-27 22:22:51 -05:00
|
|
|
end
|
2014-12-19 16:47:39 -05:00
|
|
|
|
2015-04-07 22:29:43 -04:00
|
|
|
!!result
|
2014-01-09 18:25:14 -05:00
|
|
|
end
|
|
|
|
|
2018-01-25 15:38:40 -05:00
|
|
|
def can_lock_post?(post)
|
|
|
|
can_see_post?(post) && is_staff?
|
|
|
|
end
|
|
|
|
|
2014-01-09 18:25:14 -05:00
|
|
|
# Can we see who acted on a post in a particular way?
|
|
|
|
def can_see_post_actors?(topic, post_action_type_id)
|
2014-08-07 13:12:35 -04:00
|
|
|
return true if is_admin?
|
2014-01-09 18:25:14 -05:00
|
|
|
return false unless topic
|
|
|
|
|
|
|
|
type_symbol = PostActionType.types[post_action_type_id]
|
2016-10-19 02:36:35 -04:00
|
|
|
|
2014-01-09 18:25:14 -05:00
|
|
|
return false if type_symbol == :bookmark
|
2016-10-19 02:36:35 -04:00
|
|
|
return false if type_symbol == :notify_user && !is_moderator?
|
|
|
|
|
2014-01-09 18:25:14 -05:00
|
|
|
return can_see_flags?(topic) if PostActionType.is_flag?(type_symbol)
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_delete_all_posts?(user)
|
2014-07-28 13:17:37 -04:00
|
|
|
is_staff? && user && !user.admin? &&
|
2021-08-25 00:44:22 -04:00
|
|
|
(
|
|
|
|
is_admin? ||
|
|
|
|
(
|
2023-01-09 07:10:19 -05:00
|
|
|
(
|
2021-08-25 00:44:22 -04:00
|
|
|
user.first_post_created_at.nil? ||
|
|
|
|
user.first_post_created_at >= SiteSetting.delete_user_max_post_age.days.ago
|
|
|
|
) && user.post_count <= SiteSetting.delete_all_posts_max.to_i
|
2023-01-09 07:10:19 -05:00
|
|
|
)
|
|
|
|
)
|
2014-01-09 18:25:14 -05:00
|
|
|
end
|
|
|
|
|
2023-01-02 20:00:42 -05:00
|
|
|
def can_create_post?(topic)
|
|
|
|
return can_create_post_in_topic?(topic) if !topic
|
2018-01-23 13:11:39 -05:00
|
|
|
|
2023-01-02 20:00:42 -05:00
|
|
|
key = topic_memoize_key(topic)
|
|
|
|
@can_create_post ||= {}
|
|
|
|
|
|
|
|
@can_create_post.fetch(key) { @can_create_post[key] = can_create_post_in_topic?(topic) }
|
2014-01-09 18:25:14 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def can_edit_post?(post)
|
2014-07-29 10:40:02 -04:00
|
|
|
return false if Discourse.static_doc_topic_ids.include?(post.topic_id) && !is_admin?
|
|
|
|
|
2016-04-13 01:59:38 -04:00
|
|
|
return true if is_admin?
|
|
|
|
|
2018-01-25 15:38:40 -05:00
|
|
|
# Must be staff to edit a locked post
|
|
|
|
return false if post.locked? && !is_staff?
|
|
|
|
|
2018-02-22 20:39:24 -05:00
|
|
|
if (
|
|
|
|
is_staff? ||
|
|
|
|
(SiteSetting.trusted_users_can_edit_others? && @user.has_trust_level?(TrustLevel[4])) ||
|
2021-05-03 04:37:43 -04:00
|
|
|
is_category_group_moderator?(post.topic&.category)
|
2018-02-22 20:39:24 -05:00
|
|
|
)
|
2018-10-03 21:34:47 -04:00
|
|
|
return can_create_post?(post.topic)
|
2014-05-13 08:53:11 -04:00
|
|
|
end
|
|
|
|
|
2021-10-13 05:53:23 -04:00
|
|
|
return false if post.topic&.archived? || post.user_deleted || post.deleted_at
|
2023-01-09 07:10:19 -05:00
|
|
|
|
2021-04-14 01:54:09 -04:00
|
|
|
# Editing a shared draft.
|
2020-12-03 09:07:57 -05:00
|
|
|
if (
|
|
|
|
can_see_post?(post) && can_create_post?(post.topic) &&
|
|
|
|
post.topic.category_id == SiteSetting.shared_drafts_category.to_i &&
|
|
|
|
can_see_category?(post.topic.category) && can_see_shared_draft?
|
|
|
|
)
|
|
|
|
return true
|
2023-01-09 07:10:19 -05:00
|
|
|
end
|
2020-12-03 09:07:57 -05:00
|
|
|
|
2014-05-13 08:53:11 -04:00
|
|
|
if post.wiki && (@user.trust_level >= SiteSetting.min_trust_to_edit_wiki_post.to_i)
|
2017-05-08 16:23:11 -04:00
|
|
|
return can_create_post?(post.topic)
|
2014-05-13 08:53:11 -04:00
|
|
|
end
|
|
|
|
|
2016-09-30 12:12:27 -04:00
|
|
|
return false if @user.trust_level < SiteSetting.min_trust_to_edit_post
|
|
|
|
|
2014-06-20 15:38:03 -04:00
|
|
|
if is_my_own?(post)
|
2018-08-15 00:29:36 -04:00
|
|
|
return false if @user.silenced?
|
|
|
|
|
2022-02-25 09:09:31 -05:00
|
|
|
return can_edit_hidden_post?(post) if post.hidden?
|
2014-06-20 15:38:03 -04:00
|
|
|
|
2021-04-14 01:54:09 -04:00
|
|
|
if post.is_first_post? && post.topic.category_allows_unlimited_owner_edits_on_first_post?
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2019-09-06 07:44:12 -04:00
|
|
|
return !post.edit_time_limit_expired?(@user)
|
2014-05-13 08:53:11 -04:00
|
|
|
end
|
|
|
|
|
2020-07-23 09:50:00 -04:00
|
|
|
if post.is_category_description?
|
|
|
|
return true if can_edit_category_description?(post.topic.category)
|
|
|
|
end
|
|
|
|
|
2014-05-13 08:53:11 -04:00
|
|
|
false
|
2014-01-09 18:25:14 -05:00
|
|
|
end
|
|
|
|
|
2022-02-25 09:09:31 -05:00
|
|
|
def can_edit_hidden_post?(post)
|
|
|
|
return false if post.nil?
|
|
|
|
post.hidden_at.nil? ||
|
|
|
|
post.hidden_at < SiteSetting.cooldown_minutes_after_hiding_posts.minutes.ago
|
|
|
|
end
|
|
|
|
|
2019-05-30 16:42:59 -04:00
|
|
|
def can_delete_post_or_topic?(post)
|
|
|
|
post.is_first_post? ? post.topic && can_delete_topic?(post.topic) : can_delete_post?(post)
|
|
|
|
end
|
|
|
|
|
2014-01-09 18:25:14 -05:00
|
|
|
def can_delete_post?(post)
|
2019-03-29 12:10:05 -04:00
|
|
|
return false if !can_see_post?(post)
|
2016-12-20 23:01:26 -05:00
|
|
|
|
2014-01-09 18:25:14 -05:00
|
|
|
# Can't delete the first post
|
2015-04-23 13:33:29 -04:00
|
|
|
return false if post.is_first_post?
|
2014-01-09 18:25:14 -05:00
|
|
|
|
2021-06-30 08:51:35 -04:00
|
|
|
return true if is_staff? || is_category_group_moderator?(post.topic&.category)
|
2021-01-08 10:05:13 -05:00
|
|
|
|
2023-01-19 21:31:51 -05:00
|
|
|
return true if SiteSetting.tl4_delete_posts_and_topics && user.has_trust_level?(TrustLevel[4])
|
|
|
|
|
2021-01-08 10:05:13 -05:00
|
|
|
# Can't delete posts in archived topics unless you are staff
|
|
|
|
return false if post.topic&.archived?
|
2014-01-17 17:42:12 -05:00
|
|
|
|
2014-01-09 18:25:14 -05:00
|
|
|
# You can delete your own posts
|
2021-01-08 10:05:13 -05:00
|
|
|
if is_my_own?(post)
|
|
|
|
if (
|
|
|
|
SiteSetting.max_post_deletions_per_minute < 1 ||
|
|
|
|
SiteSetting.max_post_deletions_per_day < 1
|
|
|
|
)
|
|
|
|
return false
|
2023-01-09 07:10:19 -05:00
|
|
|
end
|
2021-01-08 10:05:13 -05:00
|
|
|
return true if !post.user_deleted?
|
|
|
|
end
|
2014-01-09 18:25:14 -05:00
|
|
|
|
2021-01-08 10:05:13 -05:00
|
|
|
false
|
2014-01-09 18:25:14 -05:00
|
|
|
end
|
|
|
|
|
2021-10-13 05:53:23 -04:00
|
|
|
def can_permanently_delete_post?(post)
|
|
|
|
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 !post.deleted_at
|
|
|
|
if post.deleted_by_id == @user.id && post.deleted_at >= Post::PERMANENT_DELETE_TIMER.ago
|
|
|
|
return false
|
2023-01-09 07:10:19 -05:00
|
|
|
end
|
2021-10-13 05:53:23 -04:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2014-01-09 18:25:14 -05:00
|
|
|
def can_recover_post?(post)
|
2020-11-05 12:18:26 -05:00
|
|
|
return false unless post
|
|
|
|
|
2020-12-30 08:08:02 -05:00
|
|
|
# PERF, vast majority of the time topic will not be deleted
|
|
|
|
topic = (post.topic || Topic.with_deleted.find(post.topic_id)) if post.topic_id
|
2021-01-08 10:05:13 -05:00
|
|
|
return true if can_moderate_topic?(topic) && !!post.deleted_at
|
2020-11-05 12:18:26 -05:00
|
|
|
|
2021-01-08 10:05:13 -05:00
|
|
|
if is_my_own?(post)
|
|
|
|
if (
|
|
|
|
SiteSetting.max_post_deletions_per_minute < 1 ||
|
|
|
|
SiteSetting.max_post_deletions_per_day < 1
|
|
|
|
)
|
|
|
|
return false
|
2023-01-09 07:10:19 -05:00
|
|
|
end
|
2021-01-08 10:05:13 -05:00
|
|
|
return true if post.user_deleted && !post.deleted_at
|
2017-03-06 00:17:57 -05:00
|
|
|
end
|
2021-01-08 10:05:13 -05:00
|
|
|
|
|
|
|
false
|
2014-01-09 18:25:14 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def can_delete_post_action?(post_action)
|
2019-01-03 12:03:01 -05:00
|
|
|
return false unless is_my_own?(post_action) && !post_action.is_private_message?
|
|
|
|
|
2022-09-29 10:57:36 -04:00
|
|
|
post_action.created_at > SiteSetting.post_undo_action_window_mins.minutes.ago &&
|
|
|
|
!post_action.post&.topic&.archived?
|
2014-01-09 18:25:14 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def can_see_post?(post)
|
2015-09-10 16:01:23 -04:00
|
|
|
return false if post.blank?
|
|
|
|
return true if is_admin?
|
2023-01-02 20:00:42 -05:00
|
|
|
return false unless can_see_post_topic?(post)
|
2015-09-21 18:50:52 -04:00
|
|
|
unless post.user == @user || Topic.visible_post_types(@user).include?(post.post_type)
|
|
|
|
return false
|
2023-01-09 07:10:19 -05:00
|
|
|
end
|
2021-06-21 12:10:02 -04:00
|
|
|
return true if is_moderator? || is_category_group_moderator?(post.topic.category)
|
2023-01-09 01:12:10 -05:00
|
|
|
return true if !post.trashed? || can_see_deleted_post?(post)
|
2021-06-21 12:10:02 -04:00
|
|
|
false
|
2014-01-09 18:25:14 -05:00
|
|
|
end
|
|
|
|
|
2023-01-09 01:12:10 -05:00
|
|
|
def can_see_deleted_post?(post)
|
|
|
|
return false if !post.trashed?
|
|
|
|
return false if @user.anonymous?
|
|
|
|
return true if is_staff?
|
|
|
|
post.deleted_by_id == @user.id && @user.has_trust_level?(TrustLevel[4])
|
|
|
|
end
|
|
|
|
|
2014-10-27 17:06:43 -04:00
|
|
|
def can_view_edit_history?(post)
|
2014-05-12 10:30:10 -04:00
|
|
|
return false unless post
|
2014-06-26 13:19:35 -04:00
|
|
|
|
|
|
|
if !post.hidden
|
2016-07-16 07:30:00 -04:00
|
|
|
return true if post.wiki || SiteSetting.edit_history_visible_to_public
|
2014-06-26 13:19:35 -04:00
|
|
|
end
|
2014-05-12 10:30:10 -04:00
|
|
|
|
2020-06-18 06:27:51 -04:00
|
|
|
authenticated? && (is_staff? || @user.id == post.user_id) && can_see_post?(post)
|
2014-01-09 18:25:14 -05:00
|
|
|
end
|
|
|
|
|
2014-03-27 21:28:14 -04:00
|
|
|
def can_change_post_owner?
|
2021-07-13 10:40:11 -04:00
|
|
|
return true if is_admin?
|
|
|
|
|
|
|
|
SiteSetting.moderators_change_post_ownership && is_staff?
|
2014-03-27 21:28:14 -04:00
|
|
|
end
|
2014-05-13 08:53:11 -04:00
|
|
|
|
2016-11-06 14:14:09 -05:00
|
|
|
def can_change_post_timestamps?
|
2019-02-22 04:03:52 -05:00
|
|
|
is_staff?
|
2016-11-06 14:14:09 -05:00
|
|
|
end
|
|
|
|
|
2016-01-11 10:26:00 -05:00
|
|
|
def can_wiki?(post)
|
|
|
|
return false unless authenticated?
|
2016-03-15 05:13:52 -04:00
|
|
|
return true if is_staff? || @user.has_trust_level?(TrustLevel[4])
|
|
|
|
|
|
|
|
if @user.has_trust_level?(SiteSetting.min_trust_to_allow_self_wiki) && is_my_own?(post)
|
|
|
|
return false if post.hidden?
|
2019-09-06 07:44:12 -04:00
|
|
|
return !post.edit_time_limit_expired?(@user)
|
2016-03-15 05:13:52 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
false
|
2014-05-13 08:53:11 -04:00
|
|
|
end
|
2014-07-16 15:04:55 -04:00
|
|
|
|
2014-09-10 17:08:33 -04:00
|
|
|
def can_change_post_type?
|
|
|
|
is_staff?
|
|
|
|
end
|
|
|
|
|
2014-09-11 10:04:40 -04:00
|
|
|
def can_rebake?
|
2015-02-03 12:19:01 -05:00
|
|
|
is_staff? || @user.has_trust_level?(TrustLevel[4])
|
2014-09-11 10:04:40 -04:00
|
|
|
end
|
|
|
|
|
2014-07-16 15:04:55 -04:00
|
|
|
def can_see_flagged_posts?
|
|
|
|
is_staff?
|
|
|
|
end
|
|
|
|
|
2020-11-05 12:18:26 -05:00
|
|
|
def can_see_deleted_posts?(category = nil)
|
2023-01-19 21:31:51 -05:00
|
|
|
is_staff? || is_category_group_moderator?(category) ||
|
|
|
|
(SiteSetting.tl4_delete_posts_and_topics && @user.has_trust_level?(TrustLevel[4]))
|
2014-07-16 15:04:55 -04:00
|
|
|
end
|
2014-09-22 12:55:13 -04:00
|
|
|
|
2014-11-12 08:49:42 -05:00
|
|
|
def can_view_raw_email?(post)
|
2022-09-13 21:57:12 -04:00
|
|
|
post && is_staff?
|
2014-10-17 15:18:29 -04:00
|
|
|
end
|
|
|
|
|
2014-09-22 12:55:13 -04:00
|
|
|
def can_unhide?(post)
|
|
|
|
post.try(:hidden) && is_staff?
|
|
|
|
end
|
2018-08-09 20:48:30 -04:00
|
|
|
|
|
|
|
def can_skip_bump?
|
2019-01-03 08:13:36 -05:00
|
|
|
is_staff? || @user.has_trust_level?(TrustLevel[4])
|
2018-08-09 20:48:30 -04:00
|
|
|
end
|
2023-01-02 20:00:42 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def can_create_post_in_topic?(topic)
|
|
|
|
if !SiteSetting.enable_system_message_replies? && topic.try(:subtype) == "system_message"
|
|
|
|
return false
|
2023-01-09 07:10:19 -05:00
|
|
|
end
|
2023-01-02 20:00:42 -05:00
|
|
|
|
|
|
|
(
|
|
|
|
!SpamRule::AutoSilence.prevent_posting?(@user) ||
|
|
|
|
(!!topic.try(:private_message?) && topic.allowed_users.include?(@user))
|
|
|
|
) &&
|
|
|
|
(
|
|
|
|
!topic || !topic.category ||
|
|
|
|
Category.post_create_allowed(self).where(id: topic.category.id).count == 1
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def topic_memoize_key(topic)
|
|
|
|
# Milliseconds precision on Topic#updated_at so that we don't use memoized results after topic has been updated.
|
|
|
|
"#{topic.id}-#{(topic.updated_at.to_f * 1000).to_i}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_see_post_topic?(post)
|
|
|
|
topic = post.topic
|
|
|
|
return false if !topic
|
|
|
|
|
|
|
|
key = topic_memoize_key(topic)
|
|
|
|
@can_see_post_topic ||= {}
|
|
|
|
|
|
|
|
@can_see_post_topic.fetch(key) { @can_see_post_topic[key] = can_see_topic?(topic) }
|
|
|
|
end
|
2014-01-17 17:42:12 -05:00
|
|
|
end
|