* Rename banned and comment_banned roles * Add data update script to rename roles containing 'ban' * Add named error for Suspended users * Update unidiomatic method names * Rename misc banned to suspended * Apply suggestions from code review Co-authored-by: Michael Kohl <me@citizen428.net> * Add unit tests for suspended methods This commit also adds TODO comments for removing banned and comment_banned from the codebase after data update scripts have successfully run on all of our Forems. Co-authored-by: Michael Kohl <me@citizen428.net>
81 lines
1.3 KiB
Ruby
81 lines
1.3 KiB
Ruby
class CommentPolicy < ApplicationPolicy
|
|
def edit?
|
|
user_is_author?
|
|
end
|
|
|
|
def create?
|
|
!user_suspended? && !user.comment_suspended? && !user_is_blocked?
|
|
end
|
|
|
|
def update?
|
|
edit?
|
|
end
|
|
|
|
def destroy?
|
|
edit?
|
|
end
|
|
|
|
def delete_confirm?
|
|
edit?
|
|
end
|
|
|
|
def settings?
|
|
edit?
|
|
end
|
|
|
|
def preview?
|
|
true
|
|
end
|
|
|
|
def moderator_create?
|
|
!user_is_blocked? && (user_is_moderator? || minimal_admin?)
|
|
end
|
|
|
|
def hide?
|
|
user_is_commentable_author?
|
|
end
|
|
|
|
def unhide?
|
|
user_is_commentable_author?
|
|
end
|
|
|
|
def admin_delete?
|
|
minimal_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_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_is_moderator?
|
|
user.moderator_for_tags.present?
|
|
end
|
|
|
|
def user_is_author?
|
|
record.user_id == user.id
|
|
end
|
|
|
|
def user_is_blocked?
|
|
return false if user.blocked_by_count.zero?
|
|
|
|
UserBlock.blocking?(record.commentable.user_id, user.id)
|
|
end
|
|
|
|
def user_is_commentable_author?
|
|
record.commentable.present? && record.commentable.user_id == user.id
|
|
end
|
|
end
|