Prevent forum messages to be moved to forums in different projects

This commit is contained in:
Klaus Zanders
2026-02-16 14:16:54 +01:00
parent 435c1087a6
commit 7b812af2cf
3 changed files with 50 additions and 0 deletions
+15
View File
@@ -31,5 +31,20 @@
# TODO: This is but a stub
module Messages
class UpdateContract < BaseContract
validate :moving_message_to_another_forum
private
def moving_message_to_another_forum
return if !model.forum_id_changed?
return if model.forum_id_was.nil?
old_forum = Forum.find_by(id: model.forum_id_was)
return if old_forum.nil?
return if old_forum.project_id == model.forum.project_id
errors.add(:forum_id, :cannot_move_message_to_forum_of_different_project)
end
end
end
+2
View File
@@ -1740,6 +1740,8 @@ en:
not_unique: "is already in use. Please select another name."
meeting:
error_conflict: "Unable to save because the meeting was updated by someone else in the meantime. Please reload the page."
message:
cannot_move_message_to_forum_of_different_project: "A message cannot be moved to a forum of a different project."
notifications:
at_least_one_channel: "At least one channel for sending notifications needs to be specified."
attributes:
@@ -53,4 +53,37 @@ RSpec.describe Messages::UpdateContract do
end
end
end
context "when moving a message to another forum" do
let(:project) { create(:project) }
let(:forum) { create(:forum, project: project) }
let(:other_forum) { create(:forum, project: project) }
let(:message) { create(:message, forum: forum) }
let(:forum_in_other_project) { create(:forum) }
let(:current_user) { create(:user, member_with_permissions: { project => [:edit_messages] }) }
subject(:contract) { described_class.new(message, current_user) }
context "when moving the message to another forum in the same project" do
before do
message.forum = other_forum
end
it "is valid" do
expect(contract).to be_valid
end
end
context "when moving the message to a forum in another project" do
before do
message.forum = forum_in_other_project
end
it "is invalid" do
expect(contract).not_to be_valid
expect(contract.errors[:forum_id]).to include("A message cannot be moved to a forum of a different project.")
end
end
end
end