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
|
|
|
|
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
|
|
|
|
return false if (action_key == :notify_user && !is_staff? && opts[:is_warning].present? && opts[:is_warning] == 'true')
|
|
|
|
|
2014-01-09 17:25:14 -06:00
|
|
|
taken = opts[:taken_actions].try(:keys).to_a
|
2017-10-17 12:31:45 -05:00
|
|
|
is_flag = PostActionType.flag_types_without_custom[action_key]
|
2014-01-09 17:25:14 -06:00
|
|
|
already_taken_this_action = taken.any? && taken.include?(PostActionType.types[action_key])
|
2017-10-17 12:31:45 -05:00
|
|
|
already_did_flagging = taken.any? && (taken & PostActionType.flag_types_without_custom.values).any?
|
2014-01-09 17:25:14 -06:00
|
|
|
|
2015-04-07 21:29:43 -05:00
|
|
|
result = if authenticated? && post && !@user.anonymous?
|
2014-12-19 15:47:39 -06:00
|
|
|
|
2017-10-17 12:31:45 -05:00
|
|
|
return false if [:notify_user, :notify_moderators].include?(action_key) &&
|
|
|
|
!SiteSetting.enable_private_messages?
|
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
|
|
|
|
(is_flag && not(already_did_flagging) && (@user.has_trust_level?(TrustLevel[1]) || 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
|
2015-02-03 13:51:29 -06:00
|
|
|
not(post.topic.try(: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
|
|
|
|
not(action_key == :like && is_my_own?(post)) &&
|
|
|
|
|
2017-10-24 10:47:41 -05:00
|
|
|
# new users can't notify_user or notify_moderators because they are not allowed to send private messages
|
|
|
|
not((action_key == :notify_user || action_key == :notify_moderators) &&
|
|
|
|
!@user.has_trust_level?(SiteSetting.min_trust_to_send_messages)) &&
|
2014-03-10 10:48:27 -05:00
|
|
|
|
2014-01-09 17:25:14 -06:00
|
|
|
# no voting more than once on single vote topics
|
|
|
|
not(action_key == :vote && opts[:voted_in_topic] && post.topic.has_meta_data_boolean?(:single_vote))
|
|
|
|
end
|
2015-04-07 21:29:43 -05:00
|
|
|
|
|
|
|
!!result
|
2014-01-09 17:25:14 -06:00
|
|
|
end
|
|
|
|
|
2014-07-28 12:17:37 -05:00
|
|
|
def can_defer_flags?(post)
|
2016-12-20 22:01:26 -06:00
|
|
|
can_see_post?(post) && is_staff? && post
|
2014-01-09 17:25:14 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
# 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)
|
|
|
|
|
|
|
|
if type_symbol == :vote
|
|
|
|
# We can see votes if the topic allows for public voting
|
|
|
|
return false if topic.has_meta_data_boolean?(:private_poll)
|
|
|
|
end
|
|
|
|
|
|
|
|
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)
|
2016-01-22 11:54:18 -06:00
|
|
|
(!SpamRule::AutoBlock.block?(@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?
|
|
|
|
|
2014-09-05 00:20:39 -05:00
|
|
|
if is_staff? || @user.has_trust_level?(TrustLevel[4])
|
2016-04-13 00:59:38 -05:00
|
|
|
return can_create_post?(post.topic)
|
2014-05-13 07:53:11 -05:00
|
|
|
end
|
|
|
|
|
2016-03-30 13:18:42 -05:00
|
|
|
if post.topic.archived? || post.user_deleted || post.deleted_at
|
2014-05-13 07:53:11 -05:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
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)
|
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
|
|
|
|
|
|
|
return !post.edit_time_limit_expired?
|
2014-05-13 07:53:11 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
false
|
2014-01-09 17:25:14 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
# Deleting Methods
|
|
|
|
def can_delete_post?(post)
|
2016-12-20 22:01:26 -06:00
|
|
|
can_see_post?(post)
|
|
|
|
|
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
|
|
|
|
|
|
|
# Can't delete after post_edit_time_limit minutes have passed
|
|
|
|
return false if !is_staff? && post.edit_time_limit_expired?
|
|
|
|
|
2014-01-17 16:42:12 -06:00
|
|
|
# Can't delete posts in archived topics unless you are staff
|
|
|
|
return false if !is_staff? && post.topic.archived?
|
|
|
|
|
2014-01-09 17:25:14 -06:00
|
|
|
# You can delete your own posts
|
|
|
|
return !post.user_deleted? if is_my_own?(post)
|
|
|
|
|
|
|
|
is_staff?
|
|
|
|
end
|
|
|
|
|
|
|
|
# Recovery Method
|
|
|
|
def can_recover_post?(post)
|
2017-03-05 23:17:57 -06:00
|
|
|
if is_staff?
|
|
|
|
post.deleted_at && post.user
|
|
|
|
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)
|
|
|
|
# You can only undo your own actions
|
|
|
|
is_my_own?(post_action) && not(post_action.is_private_message?) &&
|
|
|
|
|
|
|
|
# Make sure they want to delete it within the window
|
|
|
|
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)
|
2015-09-10 15:01:23 -05:00
|
|
|
return false if !is_moderator? && post.deleted_at.present?
|
|
|
|
|
|
|
|
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? &&
|
2014-09-05 00:20:39 -05:00
|
|
|
(is_staff? || @user.has_trust_level?(TrustLevel[4]) || @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
|
|
|
|
|
2017-07-27 20:20:09 -05:00
|
|
|
def can_vote?(post, opts = {})
|
2017-09-08 00:07:22 -05:00
|
|
|
post_can_act?(post, :vote, opts: opts)
|
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?
|
|
|
|
is_admin?
|
|
|
|
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?
|
|
|
|
return !post.edit_time_limit_expired?
|
|
|
|
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
|
|
|
|
|
|
|
|
def can_see_deleted_posts?
|
|
|
|
is_staff?
|
|
|
|
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
|
2014-01-17 16:42:12 -06:00
|
|
|
end
|