Skip to content

Commit

Permalink
Consider user with moderator role in comment moderate? policy
Browse files Browse the repository at this point in the history
  • Loading branch information
krauselukas authored and danidoni committed Oct 2, 2023
1 parent 9a58e67 commit e245a71
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/api/app/policies/comment_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ def reply?
!(user.blank? || user.is_nobody? || record.user.is_nobody?)
end

# Only logged-in Admins of Staff members can moderate comments
# Only logged-in Admins/Staff members or user with moderator role can moderate comments
def moderate?
return false if record.user.is_nobody? # soft-deleted comments
return true if user.try(:is_admin?) || user.try(:is_staff?)
return true if user.try(:is_moderator?) || user.try(:is_admin?) || user.try(:is_staff?)

false
end
Expand Down
4 changes: 4 additions & 0 deletions src/api/spec/factories/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@
roles { [Role.find_by_title('Staff')] }
end

factory :moderator do
roles { [Role.find_by_title('Moderator')] }
end

factory :user_with_groups do
after(:create) do |user|
create(:group, users: [user])
Expand Down
8 changes: 8 additions & 0 deletions src/api/spec/policies/comment_policy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,14 @@
expect(subject).to permit(staff_user, comment)
end
end

context 'when the user has the moderator role assigned' do
let(:user_with_moderator_role) { create(:moderator) }

it 'can moderate comments' do
expect(subject).to permit(user_with_moderator_role, comment)
end
end
end
# rubocop:enable RSpec/RepeatedExample
end

0 comments on commit e245a71

Please sign in to comment.