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

check visibility on notifications

ulfertsebc25fd7cc92
diff --git a/app/models/notification.rb b/app/models/notification.rb
index 729264d6cad..c228a953403 100644
--- a/app/models/notification.rb
+++ b/app/models/notification.rb
@@ -25,5 +25,6 @@ class Notification < ApplicationRecord
scopes :unsent_reminders_before,
:mail_reminder_unsent,
:mail_alert_unsent,
- :recipient
+ :recipient,
+ :visible
end
diff --git a/app/models/notifications/scopes/visible.rb b/app/models/notifications/scopes/visible.rb
new file mode 100644
index 00000000000..6e36c6a9d82
--- /dev/null
+++ b/app/models/notifications/scopes/visible.rb
@@ -0,0 +1,45 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2021 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+module Notifications::Scopes
+ module Visible
+ extend ActiveSupport::Concern
+
+ class_methods do
+ # Return notifications visible to a user:
+ # * the user is the recipient
+ # * the user has the permission to see the associated resource
+ #
+ # As currently only notifications for work packages exist, the implementation is work package specific.
+ def visible(user)
+ recipient(user)
+ .where(resource_type: 'WorkPackage', resource_id: WorkPackage.visible(user).select(:id))
+ end
+ end
+ end
+end
diff --git a/app/workers/mails/reminder_job.rb b/app/workers/mails/reminder_job.rb
index fb3d39b6348..ed3472a544e 100644
--- a/app/workers/mails/reminder_job.rb
+++ b/app/workers/mails/reminder_job.rb
@@ -40,7 +40,10 @@ class Mails::ReminderJob < Mails::DeliverJob
def render_mail
# Have to cast to array since the update in the subsequent block
# will result in the notification to not be found via the .unsent_reminders_before scope.
- notification_ids = Notification.unsent_reminders_before(recipient: recipient, time: Time.current).pluck(:id)
+ notification_ids = Notification
+ .unsent_reminders_before(recipient: recipient, time: Time.current)
+ .visible(recipient)
+ .pluck(:id)
return nil if notification_ids.empty?
diff --git a/lib/api/v3/notifications/notifications_api.rb b/lib/api/v3/notifications/notifications_api.rb
index bbc0de914c9..107e5c8afef 100644
--- a/lib/api/v3/notifications/notifications_api.rb
+++ b/lib/api/v3/notifications/notifications_api.rb
@@ -44,7 +44,7 @@ module API
def notification_scope
::Notification
- .recipient(current_user)
+ .visible(current_user)
.includes(NotificationRepresenter.to_eager_load)
.where
.not(read_ian: nil)
diff --git a/spec/models/notifications/scopes/visible_spec.rb b/spec/models/notifications/scopes/visible_spec.rb
new file mode 100644
index 00000000000..7debac97544
--- /dev/null
+++ b/spec/models/notifications/scopes/visible_spec.rb
@@ -0,0 +1,80 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2021 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+require 'spec_helper'
+
+describe Notifications::Scopes::Visible, type: :model do
+ describe '.visible' do
+ subject(:scope) { ::Notification.visible(user) }
+
+ let(:user) do
+ FactoryBot.create(:user,
+ member_in_project: project,
+ member_with_permissions: permissions)
+ end
+
+ let(:notification) do
+ FactoryBot.create(:notification,
+ project: project,
+ resource: work_package,
+ recipient: notification_recipient)
+ end
+ let(:notification_recipient) { user }
+ let(:permissions) { %i[view_work_packages] }
+ let(:project) { FactoryBot.create(:project) }
+ let(:work_package) { FactoryBot.create(:work_package, project: project) }
+
+ let!(:notifications) { notification }
+
+ shared_examples_for 'is empty' do
+ it 'is empty' do
+ expect(scope)
+ .to be_empty
+ end
+ end
+
+ context 'with the user being recipient and being allowed to see the work package' do
+ it 'returns the notification' do
+ expect(scope)
+ .to match_array([notification])
+ end
+ end
+
+ context 'with the user being recipient and not being allowed to see the work package' do
+ let(:permissions) { [] }
+
+ it_behaves_like 'is empty'
+ end
+
+ context 'with the user not being recipient but being allowed to see the work package' do
+ let(:notification_recipient) { FactoryBot.create(:user) }
+
+ it_behaves_like 'is empty'
+ end
+ end
+end
diff --git a/spec/requests/api/v3/notifications/index_resource_spec.rb b/spec/requests/api/v3/notifications/index_resource_spec.rb
index 89363d21590..c507e9180cc 100644
--- a/spec/requests/api/v3/notifications/index_resource_spec.rb
+++ b/spec/requests/api/v3/notifications/index_resource_spec.rb
@@ -67,11 +67,15 @@ describe ::API::V3::Notifications::NotificationsAPI,
end
let(:parsed_response) { JSON.parse(last_response.body) }
+ let(:additional_setup) do
+ # To be overwritten by individual specs
+ end
before do
notifications
login_as current_user
+ additional_setup
send_request
end
@@ -267,11 +271,15 @@ describe ::API::V3::Notifications::NotificationsAPI,
end
context 'with a project groupBy' do
- let(:work_package2) { FactoryBot.create :work_package }
+ let(:other_project) do
+ FactoryBot.create(:project,
+ members: { recipient => recipient.members.first.roles })
+ end
+ let(:work_package2) { FactoryBot.create :work_package, project: other_project }
let(:other_project_notification) do
FactoryBot.create :notification,
resource: work_package2,
- project: work_package2.project,
+ project: other_project,
recipient: recipient,
reason: :responsible,
journal: work_package2.journals.first
@@ -292,7 +300,7 @@ describe ::API::V3::Notifications::NotificationsAPI,
expect(groups.count).to eq 2
keyed = groups.index_by { |el| el['value'] }
- expect(keyed.keys).to contain_exactly work_package2.project.name, work_package.project.name
+ expect(keyed.keys).to contain_exactly other_project.name, work_package.project.name
expect(keyed[work_package.project.name]['count']).to eq 2
expect(keyed[work_package2.project.name]['count']).to eq 1
@@ -300,6 +308,14 @@ describe ::API::V3::Notifications::NotificationsAPI,
.to eq "/api/v3/projects/#{work_package.project.id}"
end
end
+
+ context 'when having lost the permission to see the work package' do
+ let(:additional_setup) do
+ Member.where(principal: recipient).destroy_all
+ end
+
+ it_behaves_like 'API V3 collection response', 0, 0, 'Notification'
+ end
end
describe 'admin user' do
diff --git a/spec/requests/api/v3/notifications/read_ian_resource_spec.rb b/spec/requests/api/v3/notifications/read_ian_resource_spec.rb
index 0164e714763..d3b2004775d 100644
--- a/spec/requests/api/v3/notifications/read_ian_resource_spec.rb
+++ b/spec/requests/api/v3/notifications/read_ian_resource_spec.rb
@@ -35,8 +35,19 @@ describe ::API::V3::Notifications::NotificationsAPI,
content_type: :json do
include API::V3::Utilities::PathHelper
- shared_let(:recipient) { FactoryBot.create :user }
- shared_let(:notification) { FactoryBot.create :notification, recipient: recipient }
+ shared_let(:project) { FactoryBot.create(:project) }
+ shared_let(:work_package) { FactoryBot.create(:work_package, project: project) }
+ shared_let(:recipient) do
+ FactoryBot.create :user,
+ member_in_project: project,
+ member_with_permissions: %i[view_work_packages]
+ end
+ shared_let(:notification) do
+ FactoryBot.create :notification,
+ recipient: recipient,
+ resource: work_package,
+ project: project
+ end
let(:send_read) do
post api_v3_paths.notification_read_ian(notification.id)
diff --git a/spec/requests/api/v3/notifications/show_resource_examples.rb b/spec/requests/api/v3/notifications/show_resource_examples.rb
index 12f50cb0b34..ef687610e1e 100644
--- a/spec/requests/api/v3/notifications/show_resource_examples.rb
+++ b/spec/requests/api/v3/notifications/show_resource_examples.rb
@@ -28,7 +28,7 @@
# See COPYRIGHT and LICENSE files for more details.
shared_examples 'represents the notification' do
- it :aggregate_failures do
+ it 'represents the notification', :aggregate_failures do
expect(last_response.status).to eq(200)
expect(last_response.body)
.to(be_json_eql('Notification'.to_json).at_path('_type'))
diff --git a/spec/requests/api/v3/notifications/show_resource_spec.rb b/spec/requests/api/v3/notifications/show_resource_spec.rb
index a63f7d6c293..d782735911f 100644
--- a/spec/requests/api/v3/notifications/show_resource_spec.rb
+++ b/spec/requests/api/v3/notifications/show_resource_spec.rb
@@ -32,11 +32,18 @@ require_relative './show_resource_examples'
describe ::API::V3::Notifications::NotificationsAPI,
'show',
+ content_type: :json,
type: :request do
include API::V3::Utilities::PathHelper
- shared_let(:recipient) { FactoryBot.create :user }
- shared_let(:project) { FactoryBot.create :project }
+ shared_let(:recipient) do
+ FactoryBot.create :user
+ end
+ shared_let(:role) { FactoryBot.create(:role, permissions: %i(view_work_packages)) }
+ shared_let(:project) do
+ FactoryBot.create :project,
+ members: { recipient => role }
+ end
shared_let(:resource) { FactoryBot.create :work_package, project: project }
shared_let(:notification) do
FactoryBot.create :notification,
@@ -47,12 +54,9 @@ describe ::API::V3::Notifications::NotificationsAPI,
end
let(:send_request) do
- header "Content-Type", "application/json"
get api_v3_paths.notification(notification.id)
end
- let(:parsed_response) { JSON.parse(last_response.body) }
-
before do
login_as current_user
send_request
diff --git a/spec/workers/mails/reminder_job_spec.rb b/spec/workers/mails/reminder_job_spec.rb
index 76419a76cbd..a0958d58cc6 100644
--- a/spec/workers/mails/reminder_job_spec.rb
+++ b/spec/workers/mails/reminder_job_spec.rb
@@ -40,7 +40,7 @@ describe Mails::ReminderJob, type: :model do
let(:notification_ids) { [1, 2, 3] }
let!(:notifications) do
- instance_double(ActiveRecord::Relation).tap do |notifications|
+ class_double(Notification).tap do |notifications|
allow(Time)
.to receive(:current)
.and_return(Time.current)
@@ -50,6 +50,11 @@ describe Mails::ReminderJob, type: :model do
.with(recipient: recipient, time: Time.current)
.and_return(notifications)
+ allow(notifications)
+ .to receive(:visible)
+ .with(recipient)
+ .and_return(notifications)
+
allow(notifications)
.to receive(:pluck)
.with(:id)