docbrown/app/policies/comment_policy.rb
Anna Buianova 473594f192
Match spam role with existing suspended actions (#20477)
* Started matching spam and suspended roles

* Match spam and suspended roles in most cases

* Fixed check_suspended for unauthenticated users
2023-12-27 20:12:50 +00:00

93 lines
1.5 KiB
Ruby

class CommentPolicy < ApplicationPolicy
def edit?
return false if user.spam_or_suspended?
user_author?
end
def destroy?
user_author?
end
def create?
!user.spam_or_suspended? && !user.comment_suspended?
end
alias new? create?
alias update? edit?
alias delete_confirm? destroy?
alias settings? edit?
def preview?
true
end
def subscribe?
true
end
def unsubscribe?
true
end
def moderate?
return true if user.trusted?
moderator_create?
end
def moderator_create?
Authorizer.for(user: user).accesses_mod_response_templates?
end
def hide?
user_commentable_author? && !record.by_staff_account?
end
alias unhide? hide?
def admin_delete?
user_any_admin?
end
def permitted_attributes_for_update
%i[body_markdown receive_notifications]
end
def permitted_attributes_for_preview
%i[body_markdown]
end
def permitted_attributes_for_subscribe
%i[subscription_id comment_id article_id]
end
def permitted_attributes_for_unsubscribe
%i[subscription_id]
end
def permitted_attributes_for_create
%i[body_markdown commentable_id commentable_type parent_id]
end
def permitted_attributes_for_moderator_create
%i[commentable_id commentable_type parent_id]
end
private
def user_moderator?
user.moderator_for_tags.present?
end
def user_author?
record.user_id == user.id
end
def user_commentable_author?
record.commentable.present? && record.commentable.user_id == user.id
end
end