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

admins and storage admins can see all storages

Christophe Bliarddaed5e1bee8c
diff --git a/modules/storages/app/contracts/storages/project_storages/base_contract.rb b/modules/storages/app/contracts/storages/project_storages/base_contract.rb
index fd866fdcc94..cc6043127df 100644
--- a/modules/storages/app/contracts/storages/project_storages/base_contract.rb
+++ b/modules/storages/app/contracts/storages/project_storages/base_contract.rb
@@ -50,7 +50,7 @@ module Storages::ProjectStorages
end
def assignable_storages
- Storages::Storage.where.not(id: @model.project.projects_storages.pluck(:storage_id))
+ Storages::Storage.visible(user).where.not(id: @model.project.projects_storages.pluck(:storage_id))
end
def validate_creator_is_user
diff --git a/modules/storages/app/models/storages/storage.rb b/modules/storages/app/models/storages/storage.rb
index e31b38ff7f8..dd14f9b0632 100644
--- a/modules/storages/app/models/storages/storage.rb
+++ b/modules/storages/app/models/storages/storage.rb
@@ -63,10 +63,14 @@ class Storages::Storage < ApplicationRecord
# Creates a scope of all storages, which belong to a project the user is a member
# and has the permission ':view_file_links'
scope :visible, ->(user = User.current) {
- where(
- projects_storages: ::Storages::ProjectStorage.where(
- project: Project.allowed_to(user, :view_file_links)
+ if user.admin? || Project.allowed_to(user, :manage_storages_in_project).any?
+ all
+ else
+ where(
+ projects_storages: ::Storages::ProjectStorage.where(
+ project: Project.allowed_to(user, :view_file_links)
+ )
)
- )
+ end
}
end
diff --git a/modules/storages/spec/requests/api/v3/storages/storages_spec.rb b/modules/storages/spec/requests/api/v3/storages/storages_spec.rb
index 9c8332fc709..03ae5650a54 100644
--- a/modules/storages/spec/requests/api/v3/storages/storages_spec.rb
+++ b/modules/storages/spec/requests/api/v3/storages/storages_spec.rb
@@ -42,36 +42,56 @@ describe 'API v3 storages resource', type: :request, content_type: :json do
create(:storage, creator: current_user)
end
- let(:project_storage) { create(:project_storage, project: project, storage: storage) }
-
- subject(:response) { last_response }
+ subject(:last_response) do
+ get path
+ end
before do
- project_storage
login_as current_user
end
+ shared_examples_for 'successful storage response' do
+ include_examples 'successful response'
+
+ describe 'response body' do
+ subject { last_response.body }
+
+ it { is_expected.to be_json_eql('Storage'.to_json).at_path('_type') }
+ it { is_expected.to be_json_eql(storage.id.to_json).at_path('id') }
+ end
+ end
+
describe 'GET /api/v3/storages/:storage_id' do
let(:path) { api_v3_paths.storage(storage.id) }
- before do
- get path
- end
+ context 'when user belongs to a project using the given storage' do
+ let!(:project_storage) { create(:project_storage, project: project, storage: storage) }
+
+ it_behaves_like 'successful storage response'
+
+ context 'if user is missing permission view_file_links' do
+ let(:permissions) { [] }
+
+ it_behaves_like 'not found'
+ end
+
+ context 'if no storage with that id exists' do
+ let(:path) { api_v3_paths.storage(1337) }
- it 'is successful' do
- expect(subject.status).to be 200
+ it_behaves_like 'not found'
+ end
end
- context 'if user has no permissions' do
- let(:permissions) { [] }
+ context 'when user has :manage_storages_in_project permission in any project' do
+ let(:permissions) { %i(manage_storages_in_project) }
- it_behaves_like 'not found'
+ it_behaves_like 'successful storage response'
end
- context 'if no storage with that id exists' do
- let(:path) { api_v3_paths.storage(1337) }
+ context 'as admin' do
+ let(:current_user) { create(:admin) }
- it_behaves_like 'not found'
+ it_behaves_like 'successful storage response'
end
end
end
diff --git a/spec/requests/api/v3/support/response_examples.rb b/spec/requests/api/v3/support/response_examples.rb
index 56300077735..f7fd3802aef 100644
--- a/spec/requests/api/v3/support/response_examples.rb
+++ b/spec/requests/api/v3/support/response_examples.rb
@@ -28,6 +28,18 @@
require 'spec_helper'
+shared_examples_for 'successful response' do |code = 200|
+ it "has the status code #{code}" do
+ expect(last_response.status).to eq(code)
+ end
+
+ it 'has a HAL+JSON Content-Type' do
+ expected_content_type = 'application/hal+json; charset=utf-8'
+ expect(last_response.headers).to include 'Content-Type'
+ expect(last_response.headers['Content-Type'].downcase).to eql expected_content_type
+ end
+end
+
shared_examples_for 'error response' do |code, id, provided_message = nil|
let(:expected_message) do
provided_message || message
@@ -115,10 +127,10 @@ shared_examples_for 'unauthorized access' do
end
shared_examples_for 'not found' do |message = I18n.t('api_v3.errors.code_404')|
- it_behaves_like 'error response',
- 404,
- 'NotFound',
- message
+ include_examples 'error response',
+ 404,
+ 'NotFound',
+ message
end
shared_examples_for 'param validation error' do