2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-02-13 10:42:35 -06:00
|
|
|
#mixin for all guardian methods dealing with post permissions
|
2014-05-12 09:30:10 -05:00
|
|
|
module PostGuardian
|
2015-03-31 11:58:56 -05:00
|
|
|
|
2018-06-13 13:57:32 -05: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 19:23:54 -05:00
|
|
|
elsif SiteSetting.allowed_link_domains.present?
|
2018-06-13 13:57:32 -05:00
|
|
|
'limited'
|
|
|
|
else
|
|
|
|
'none'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_post_link?(host: nil)
|
|
|
|
return false if host.blank?
|
|
|
|
|
|
|
|
unrestricted_link_posting? ||
|
2020-07-26 19:23:54 -05:00
|
|
|
SiteSetting.allowed_link_domains.split('|').include?(host)
|
2018-02-08 11:56:10 -06:00
|
|
|
end
|
|
|
|
|
2014-01-09 17:25:14 -06: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 00:07:22 -05: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 22:01:26 -06:00
|
|
|
|
|
|
|
# no warnings except for staff
|
2020-03-11 07:03:20 -05:00
|
|
|
return false if action_key == :notify_user && (post.user.blank? || (!is_staff? && opts[:is_warning].present? && opts[:is_warning] == 'true'))
|
2016-12-20 22:01:26 -06:00
|
|
|
|
2014-01-09 17:25:14 -06:00
|
|
|
taken = opts[:taken_actions].try(:keys).to_a
|
2020-08-19 18:31:40 -05:00
|
|
|
is_flag = PostActionType.notify_flag_types[action_key] || PostActionType.custom_types[action_key]
|
2014-01-09 17:25:14 -06:00
|
|
|
already_taken_this_action = taken.any? && taken.include?(PostActionType.types[action_key])
|
2018-02-27 21:22:51 -06:00
|
|
|
already_did_flagging = taken.any? && (taken & PostActionType.notify_flag_types.values).any?
|
2014-01-09 17:25:14 -06:00
|
|
|
|
2015-04-07 21:29:43 -05:00
|
|
|
result = if authenticated? && post && !@user.anonymous?
|
2018-08-14 10:43:39 -05:00
|
|
|
|
2018-08-17 10:06:01 -05:00
|
|
|
# Silenced users can't flag
|
|
|
|
return false if is_flag && @user.silenced?
|
2018-08-14 10:43:39 -05:00
|
|
|
|
2018-11-05 09:00:59 -06:00
|
|
|
# Hidden posts can't be flagged
|
|
|
|
return false if is_flag && post.hidden?
|
|
|
|
|
2018-02-12 13:56:21 -06:00
|
|
|
# post made by staff, but we don't allow staff flags
|
2018-02-14 14:46:04 -06:00
|
|
|
return false if is_flag &&
|
|
|
|
(!SiteSetting.allow_flagging_staff?) &&
|
2018-08-17 02:10:07 -05:00
|
|
|
post&.user&.staff?
|
2018-02-12 13:56:21 -06:00
|
|
|
|
2019-01-24 05:26:59 -06:00
|
|
|
if action_key == :notify_user &&
|
2018-02-27 21:22:51 -06:00
|
|
|
(!SiteSetting.enable_personal_messages? ||
|
|
|
|
!@user.has_trust_level?(SiteSetting.min_trust_to_send_messages))
|
|
|
|
|
|
|
|
return false
|
|
|
|
end
|
2014-12-19 15:47:39 -06:00
|
|
|
|
2014-03-10 10:48:27 -05:00
|
|
|
# we allow flagging for trust level 1 and higher
|
2015-01-08 09:06:43 -06:00
|
|
|
# always allowed for private messages
|
2018-02-06 16:12:27 -06:00
|
|
|
(is_flag && not(already_did_flagging) && (@user.has_trust_level?(TrustLevel[SiteSetting.min_trust_to_flag_posts]) || post.topic.private_message?)) ||
|
2014-01-09 17:25:14 -06:00
|
|
|
|
|
|
|
# not a flagging action, and haven't done it already
|
|
|
|
not(is_flag || already_taken_this_action) &&
|
|
|
|
|
2014-08-07 12:12:35 -05:00
|
|
|
# nothing except flagging on archived topics
|
2018-02-27 21:22:51 -06:00
|
|
|
not(post.topic&.archived?) &&
|
2014-01-09 17:25:14 -06:00
|
|
|
|
2014-08-07 12:12:35 -05:00
|
|
|
# nothing except flagging on deleted posts
|
|
|
|
not(post.trashed?) &&
|
|
|
|
|
2014-01-09 17:25:14 -06:00
|
|
|
# don't like your own stuff
|
2020-03-11 07:03:20 -05:00
|
|
|
not(action_key == :like && (post.user.blank? || is_my_own?(post)))
|
2014-01-09 17:25:14 -06:00
|
|
|
end
|
2015-04-07 21:29:43 -05:00
|
|
|
|
|
|
|
!!result
|
2014-01-09 17:25:14 -06:00
|
|
|
end
|
|
|
|
|
2018-01-25 14:38:40 -06:00
|
|
|
def can_lock_post?(post)
|
|
|
|
can_see_post?(post) && is_staff?
|
|
|
|
end
|
|
|
|
|
2014-01-09 17:25:14 -06: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 12:12:35 -05:00
|
|
|
return true if is_admin?
|
2014-01-09 17:25:14 -06:00
|
|
|
return false unless topic
|
|
|
|
|
|
|
|
type_symbol = PostActionType.types[post_action_type_id]
|
2016-10-19 01:36:35 -05:00
|
|
|
|
2014-01-09 17:25:14 -06:00
|
|
|
return false if type_symbol == :bookmark
|
2016-10-19 01:36:35 -05:00
|
|
|
return false if type_symbol == :notify_user && !is_moderator?
|
|
|
|
|
2014-01-09 17:25:14 -06:00
|
|
|
return can_see_flags?(topic) if PostActionType.is_flag?(type_symbol)
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_delete_all_posts?(user)
|
2014-07-28 12:17:37 -05:00
|
|
|
is_staff? &&
|
|
|
|
user &&
|
|
|
|
!user.admin? &&
|
|
|
|
(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
|
2014-01-09 17:25:14 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
# Creating Method
|
|
|
|
def can_create_post?(parent)
|
2018-01-23 12:11:39 -06:00
|
|
|
return false if !SiteSetting.enable_system_message_replies? && parent.try(:subtype) == "system_message"
|
|
|
|
|
2019-02-07 12:46:05 -06:00
|
|
|
(!SpamRule::AutoSilence.prevent_posting?(@user) || (!!parent.try(:private_message?) && parent.allowed_users.include?(@user))) && (
|
2014-04-18 11:42:31 -05:00
|
|
|
!parent ||
|
|
|
|
!parent.category ||
|
2017-07-27 20:20:09 -05:00
|
|
|
Category.post_create_allowed(self).where(id: parent.category.id).count == 1
|
2014-01-09 17:25:14 -06:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Editing Method
|
|
|
|
def can_edit_post?(post)
|
2014-07-29 09:40:02 -05:00
|
|
|
if Discourse.static_doc_topic_ids.include?(post.topic_id) && !is_admin?
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2016-04-13 00:59:38 -05:00
|
|
|
return true if is_admin?
|
|
|
|
|
2018-01-25 14:38:40 -06:00
|
|
|
# Must be staff to edit a locked post
|
|
|
|
return false if post.locked? && !is_staff?
|
|
|
|
|
2018-02-22 19:39:24 -06:00
|
|
|
return can_create_post?(post.topic) if (
|
|
|
|
is_staff? ||
|
|
|
|
(
|
|
|
|
SiteSetting.trusted_users_can_edit_others? &&
|
|
|
|
@user.has_trust_level?(TrustLevel[4])
|
2020-10-23 11:37:44 -05:00
|
|
|
) ||
|
|
|
|
is_category_group_moderator?(post.topic.category)
|
2018-02-22 19:39:24 -06:00
|
|
|
)
|
2014-05-13 07:53:11 -05:00
|
|
|
|
2018-10-03 20:34:47 -05:00
|
|
|
if post.topic&.archived? || post.user_deleted || post.deleted_at
|
2014-05-13 07:53:11 -05:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2020-12-03 08:07:57 -06:00
|
|
|
return true 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_create_shared_draft?
|
|
|
|
)
|
|
|
|
|
2014-05-13 07:53:11 -05:00
|
|
|
if post.wiki && (@user.trust_level >= SiteSetting.min_trust_to_edit_wiki_post.to_i)
|
2017-05-08 15:23:11 -05:00
|
|
|
return can_create_post?(post.topic)
|
2014-05-13 07:53:11 -05:00
|
|
|
end
|
|
|
|
|
2016-09-30 11:12:27 -05:00
|
|
|
if @user.trust_level < SiteSetting.min_trust_to_edit_post
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2014-06-20 14:38:03 -05:00
|
|
|
if is_my_own?(post)
|
2018-08-14 23:29:36 -05:00
|
|
|
|
|
|
|
return false if @user.silenced?
|
|
|
|
|
2014-09-16 10:20:31 -05:00
|
|
|
if post.hidden?
|
|
|
|
return false if post.hidden_at.present? &&
|
|
|
|
post.hidden_at >= SiteSetting.cooldown_minutes_after_hiding_posts.minutes.ago
|
|
|
|
|
|
|
|
# If it's your own post and it's hidden, you can still edit it
|
|
|
|
return true
|
|
|
|
end
|
2014-06-20 14:38:03 -05:00
|
|
|
|
2019-09-06 06:44:12 -05:00
|
|
|
return !post.edit_time_limit_expired?(@user)
|
2014-05-13 07:53:11 -05:00
|
|
|
end
|
|
|
|
|
2020-07-23 08:50:00 -05:00
|
|
|
if post.is_category_description?
|
|
|
|
return true if can_edit_category_description?(post.topic.category)
|
|
|
|
end
|
|
|
|
|
2014-05-13 07:53:11 -05:00
|
|
|
false
|
2014-01-09 17:25:14 -06:00
|
|
|
end
|
|
|
|
|
2019-05-30 15:42:59 -05: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 17:25:14 -06:00
|
|
|
# Deleting Methods
|
|
|
|
def can_delete_post?(post)
|
2019-03-29 11:10:05 -05:00
|
|
|
return false if !can_see_post?(post)
|
2016-12-20 22:01:26 -06:00
|
|
|
|
2014-01-09 17:25:14 -06:00
|
|
|
# Can't delete the first post
|
2015-04-23 12:33:29 -05:00
|
|
|
return false if post.is_first_post?
|
2014-01-09 17:25:14 -06:00
|
|
|
|
2014-01-17 16:42:12 -06:00
|
|
|
# Can't delete posts in archived topics unless you are staff
|
2020-11-05 11:18:26 -06:00
|
|
|
can_moderate = can_moderate_topic?(post.topic)
|
|
|
|
return false if !can_moderate && post.topic&.archived?
|
2014-01-17 16:42:12 -06:00
|
|
|
|
2014-01-09 17:25:14 -06:00
|
|
|
# You can delete your own posts
|
|
|
|
return !post.user_deleted? if is_my_own?(post)
|
|
|
|
|
2020-11-05 11:18:26 -06:00
|
|
|
can_moderate
|
2014-01-09 17:25:14 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
# Recovery Method
|
|
|
|
def can_recover_post?(post)
|
2020-11-05 11:18:26 -06:00
|
|
|
return false unless post
|
|
|
|
|
|
|
|
topic = Topic.with_deleted.find(post.topic_id) if post.topic_id
|
|
|
|
|
|
|
|
if can_moderate_topic?(topic)
|
2020-02-06 02:19:04 -06:00
|
|
|
!!post.deleted_at
|
2017-03-05 23:17:57 -06:00
|
|
|
else
|
|
|
|
is_my_own?(post) && post.user_deleted && !post.deleted_at
|
|
|
|
end
|
2014-01-09 17:25:14 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def can_delete_post_action?(post_action)
|
2019-01-03 11:03:01 -06:00
|
|
|
return false unless is_my_own?(post_action) && !post_action.is_private_message?
|
|
|
|
|
|
|
|
# Bookmarks do not have a time constraint
|
|
|
|
return true if post_action.is_bookmark?
|
2014-01-09 17:25:14 -06:00
|
|
|
|
|
|
|
post_action.created_at > SiteSetting.post_undo_action_window_mins.minutes.ago
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_see_post?(post)
|
2015-09-10 15:01:23 -05:00
|
|
|
return false if post.blank?
|
|
|
|
return true if is_admin?
|
|
|
|
return false unless can_see_topic?(post.topic)
|
2015-09-21 17:50:52 -05:00
|
|
|
return false unless post.user == @user || Topic.visible_post_types(@user).include?(post.post_type)
|
2020-11-05 11:18:26 -06:00
|
|
|
return false if !(is_moderator? || is_category_group_moderator?(post.topic.category)) && post.deleted_at.present?
|
2015-09-10 15:01:23 -05:00
|
|
|
|
|
|
|
true
|
2014-01-09 17:25:14 -06:00
|
|
|
end
|
|
|
|
|
2014-10-27 16:06:43 -05:00
|
|
|
def can_view_edit_history?(post)
|
2014-05-12 09:30:10 -05:00
|
|
|
return false unless post
|
2014-06-26 12:19:35 -05:00
|
|
|
|
|
|
|
if !post.hidden
|
2016-07-16 06:30:00 -05:00
|
|
|
return true if post.wiki || SiteSetting.edit_history_visible_to_public
|
2014-06-26 12:19:35 -05:00
|
|
|
end
|
2014-05-12 09:30:10 -05:00
|
|
|
|
2014-03-13 09:47:37 -05:00
|
|
|
authenticated? &&
|
2020-06-18 05:27:51 -05:00
|
|
|
(is_staff? || @user.id == post.user_id) &&
|
2014-05-12 09:30:10 -05:00
|
|
|
can_see_post?(post)
|
2014-01-09 17:25:14 -06:00
|
|
|
end
|
|
|
|
|
2014-03-27 20:28:14 -05:00
|
|
|
def can_change_post_owner?
|
|
|
|
is_admin?
|
|
|
|
end
|
2014-05-13 07:53:11 -05:00
|
|
|
|
2016-11-06 13:14:09 -06:00
|
|
|
def can_change_post_timestamps?
|
2019-02-22 03:03:52 -06:00
|
|
|
is_staff?
|
2016-11-06 13:14:09 -06:00
|
|
|
end
|
|
|
|
|
2016-01-11 09:26:00 -06:00
|
|
|
def can_wiki?(post)
|
|
|
|
return false unless authenticated?
|
2016-03-15 04:13:52 -05: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 06:44:12 -05:00
|
|
|
return !post.edit_time_limit_expired?(@user)
|
2016-03-15 04:13:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
false
|
2014-05-13 07:53:11 -05:00
|
|
|
end
|
2014-07-16 14:04:55 -05:00
|
|
|
|
2014-09-10 16:08:33 -05:00
|
|
|
def can_change_post_type?
|
|
|
|
is_staff?
|
|
|
|
end
|
|
|
|
|
2014-09-11 09:04:40 -05:00
|
|
|
def can_rebake?
|
2015-02-03 11:19:01 -06:00
|
|
|
is_staff? || @user.has_trust_level?(TrustLevel[4])
|
2014-09-11 09:04:40 -05:00
|
|
|
end
|
|
|
|
|
2014-07-16 14:04:55 -05:00
|
|
|
def can_see_flagged_posts?
|
|
|
|
is_staff?
|
|
|
|
end
|
|
|
|
|
2020-11-05 11:18:26 -06:00
|
|
|
def can_see_deleted_posts?(category = nil)
|
|
|
|
is_staff? || is_category_group_moderator?(category)
|
2014-07-16 14:04:55 -05:00
|
|
|
end
|
2014-09-22 11:55:13 -05:00
|
|
|
|
2014-11-12 07:49:42 -06:00
|
|
|
def can_view_raw_email?(post)
|
|
|
|
post && (is_staff? || post.user_id == @user.id)
|
2014-10-17 14:18:29 -05:00
|
|
|
end
|
|
|
|
|
2014-09-22 11:55:13 -05:00
|
|
|
def can_unhide?(post)
|
|
|
|
post.try(:hidden) && is_staff?
|
|
|
|
end
|
2018-08-09 19:48:30 -05:00
|
|
|
|
|
|
|
def can_skip_bump?
|
2019-01-03 07:13:36 -06:00
|
|
|
is_staff? || @user.has_trust_level?(TrustLevel[4])
|
2018-08-09 19:48:30 -05:00
|
|
|
end
|
2014-01-17 16:42:12 -06:00
|
|
|
end
|