Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

add mention replacements for groups

ulfertsa0ad5eed3949
diff --git a/app/services/users/replace_mentions_service.rb b/app/services/users/replace_mentions_service.rb
index c9574f19301..85b40870408 100644
--- a/app/services/users/replace_mentions_service.rb
+++ b/app/services/users/replace_mentions_service.rb
@@ -95,7 +95,7 @@ module Users
attr_accessor :replacements
def check_input(from, to)
- raise ArgumentError unless from.is_a?(User) && to.is_a?(User)
+ raise ArgumentError unless (from.is_a?(User) || from.is_a?(Group)) && to.is_a?(User)
end
def rewrite(from, to)
@@ -142,8 +142,24 @@ module Users
end
def condition_sql(from)
+ mention = '<mention_*data-id="%i"_*</mention>'
+ hash = if from.is_a?(User)
+ 'user#((%i)|("%s")|("%s"))\s'
+ else
+ 'group#%i\s'
+ end
+
+ hash_values = if from.is_a?(User)
+ [
+ sanitize_sql_like(from.mail),
+ sanitize_sql_like(from.login)
+ ]
+ else
+ []
+ end
+
sql = <<~SQL.squish
- #{table_name}.#{column_name} SIMILAR TO '_*((<mention_*data-id="%i"_*</mention>)|(user#((%i)|("%s")|("%s")\s)))_*'
+ #{table_name}.#{column_name} SIMILAR TO '_*((#{mention})|(#{hash}))_*'
SQL
if condition
@@ -152,9 +168,7 @@ module Users
sanitize_sql_for_conditions [sql,
from.id,
- from.id,
- sanitize_sql_like(from.mail),
- sanitize_sql_like(from.login)]
+ from.id] + hash_values
end
def mention_tag_replace(from, to)
@@ -170,14 +184,31 @@ module Users
end
def hash_replace(source, from, to)
+ search = if from.is_a?(User)
+ 'user#((%i)|("%s")|("%s"))\s'
+ else
+ 'group#%i\s'
+ end
+
+ values = if from.is_a?(User)
+ [
+ from.id,
+ sanitize_sql_like(from.login),
+ sanitize_sql_like(from.mail),
+ to.id
+ ]
+ else
+ [
+ from.id,
+ to.id
+ ]
+ end
+
regexp_replace(
source,
- 'user#((%i)|("%s")|("%s")\s)',
+ search,
'user#%i ',
- [from.id,
- sanitize_sql_like(from.login),
- sanitize_sql_like(from.mail),
- to.id]
+ values
)
end
diff --git a/app/workers/principals/delete_job.rb b/app/workers/principals/delete_job.rb
index 10aba98f798..7c4f6d1695d 100644
--- a/app/workers/principals/delete_job.rb
+++ b/app/workers/principals/delete_job.rb
@@ -53,8 +53,8 @@ class Principals::DeleteJob < ApplicationJob
def replace_mentions(principal)
# Breaking abstraction here.
# Doing the replacement is a very costly operation while at the same time,
- # groups and placeholder users can't be mentioned.
- return unless principal.is_a?(User)
+ # placeholder users can't be mentioned.
+ return unless principal.is_a?(User) || principal.is_a?(Group)
Users::ReplaceMentionsService
.new
diff --git a/spec/services/users/replace_mentions_service_integration_spec.rb b/spec/services/users/replace_mentions_service_integration_spec.rb
index 3b9dbd98540..bcb6278b0ba 100644
--- a/spec/services/users/replace_mentions_service_integration_spec.rb
+++ b/spec/services/users/replace_mentions_service_integration_spec.rb
@@ -29,16 +29,19 @@
require 'spec_helper'
describe Users::ReplaceMentionsService, 'integration' do
- subject(:service_call) { instance.call(from: user, to: to_user) }
+ subject(:service_call) { instance.call(from: principal, to: to_user) }
shared_let(:other_user) { create(:user, firstname: 'Frank', lastname: 'Herbert') }
shared_let(:user) { create(:user, firstname: 'Isaac', lastname: 'Asimov') }
+ shared_let(:group) { create(:group, lastname: 'Sci-Fi') }
shared_let(:to_user) { create :user, firstname: 'Philip K.', lastname: 'Dick' }
let(:instance) do
described_class.new
end
+ let(:principal) { user }
+
it 'is successful' do
expect(service_call)
.to be_success
@@ -63,6 +66,8 @@ describe Users::ReplaceMentionsService, 'integration' do
end
context 'with the replaced user in mention tags' do
+ let(:principal) { user }
+
it_behaves_like 'text replacement', attribute do
let(:text) do
<<~TEXT
@@ -86,6 +91,8 @@ describe Users::ReplaceMentionsService, 'integration' do
end
context 'with a different user in mention tags' do
+ let(:principal) { user }
+
it_behaves_like 'text replacement', attribute do
let(:text) do
<<~TEXT
@@ -109,6 +116,8 @@ describe Users::ReplaceMentionsService, 'integration' do
end
context 'with the replaced user in a user#ID notation' do
+ let(:principal) { user }
+
it_behaves_like 'text replacement', attribute do
let(:text) do
<<~TEXT
@@ -132,6 +141,8 @@ describe Users::ReplaceMentionsService, 'integration' do
end
context 'with a different user in a user#ID notation' do
+ let(:principal) { user }
+
it_behaves_like 'text replacement', attribute do
let(:text) do
<<~TEXT
@@ -155,6 +166,8 @@ describe Users::ReplaceMentionsService, 'integration' do
end
context 'with the replaced user in a user#"LOGIN" notation' do
+ let(:principal) { user }
+
it_behaves_like 'text replacement', attribute do
let(:text) do
<<~TEXT
@@ -178,6 +191,8 @@ describe Users::ReplaceMentionsService, 'integration' do
end
context 'with a different user in a user#"LOGIN" notation' do
+ let(:principal) { user }
+
it_behaves_like 'text replacement', attribute do
let(:text) do
<<~TEXT
@@ -201,6 +216,8 @@ describe Users::ReplaceMentionsService, 'integration' do
end
context 'with the replaced user in a user#"MAIL" notation' do
+ let(:principal) { user }
+
it_behaves_like 'text replacement', attribute do
let(:text) do
<<~TEXT
@@ -224,6 +241,8 @@ describe Users::ReplaceMentionsService, 'integration' do
end
context 'with a different user in a user#"MAIL" notation' do
+ let(:principal) { user }
+
it_behaves_like 'text replacement', attribute do
let(:text) do
<<~TEXT
@@ -245,6 +264,56 @@ describe Users::ReplaceMentionsService, 'integration' do
end
end
end
+
+ context 'with the replaced group in mention tags' do
+ let(:principal) { group }
+
+ it_behaves_like 'text replacement', attribute do
+ let(:text) do
+ <<~TEXT
+ <mention class="mention"
+ data-id="#{group.id}"
+ data-type="group"
+ data-text="@#{group.name}">
+ @#{group.name}
+ </mention>
+ TEXT
+ end
+ let(:expected_text) do
+ <<~TEXT.squish
+ <mention class="mention"
+ data-id="#{to_user.id}"
+ data-type="user"
+ data-text="@#{to_user.name}">@#{to_user.name}</mention>
+ TEXT
+ end
+ end
+ end
+
+ context 'with the replaced group in a group#ID notation' do
+ let(:principal) { group }
+
+ it_behaves_like 'text replacement', attribute do
+ let(:text) do
+ <<~TEXT
+ Lorem ipsum
+
+ group##{group.id} Lorem ipsum
+
+ Lorem ipsum
+ TEXT
+ end
+ let(:expected_text) do
+ <<~TEXT
+ Lorem ipsum
+
+ user##{to_user.id} Lorem ipsum
+
+ Lorem ipsum
+ TEXT
+ end
+ end
+ end
end
context 'for work package description' do
@@ -252,8 +321,6 @@ describe Users::ReplaceMentionsService, 'integration' do
end
context 'for work package description with dangerous mails' do
- subject(:service_call) { instance.call(from: dangerous_user, to: to_user) }
-
let(:dangerous_user) do
build(:user,
firstname: 'Dangerous',
@@ -262,7 +329,7 @@ describe Users::ReplaceMentionsService, 'integration' do
user.save(validate: false)
end
end
- let(:user) { dangerous_user }
+ let(:principal) { dangerous_user }
it 'escapes the malicious input' do
expect { service_call }
@@ -340,15 +407,6 @@ describe Users::ReplaceMentionsService, 'integration' do
it_behaves_like 'rewritten mention', :journal_wiki_content_journal, :text
end
- context 'for a group for from' do
- subject(:service_call) { instance.call(from: create(:group), to: to_user) }
-
- it 'raises an error' do
- expect { service_call }
- .to raise_error ArgumentError
- end
- end
-
context 'for a group for to' do
subject(:service_call) { instance.call(from: user, to: create(:group)) }
diff --git a/spec/workers/principals/delete_job_integration_spec.rb b/spec/workers/principals/delete_job_integration_spec.rb
index 8eb73d4bac5..69b1731fbb0 100644
--- a/spec/workers/principals/delete_job_integration_spec.rb
+++ b/spec/workers/principals/delete_job_integration_spec.rb
@@ -422,6 +422,7 @@ describe Principals::DeleteJob, type: :model do
it_behaves_like 'removes the principal'
it_behaves_like 'work_package handling'
it_behaves_like 'member handling'
+ it_behaves_like 'mention rewriting'
context 'with user only in project through group' do
let(:user) do