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

[#49622] File picker broken on intermediate project folder states

Eric Schubert0b4d7b7d0c63
diff --git a/frontend/src/app/features/hal/resources/error-resource.ts b/frontend/src/app/features/hal/resources/error-resource.ts
index 1d9e4b2897a..8d9c79297e2 100644
--- a/frontend/src/app/features/hal/resources/error-resource.ts
+++ b/frontend/src/app/features/hal/resources/error-resource.ts
@@ -31,6 +31,7 @@ import { HttpErrorResponse } from '@angular/common/http';
export const v3ErrorIdentifierQueryInvalid = 'urn:openproject-org:api:v3:errors:InvalidQuery';
export const v3ErrorIdentifierMultipleErrors = 'urn:openproject-org:api:v3:errors:MultipleErrors';
+export const v3ErrorIdentifierOutboundRequestForbidden = 'urn:openproject-org:api:v3:errors:OutboundRequest:Forbidden';
export interface IHalErrorBase {
_type:string;
diff --git a/frontend/src/app/shared/components/storages/file-picker-base-modal/file-picker-base-modal.component.ts b/frontend/src/app/shared/components/storages/file-picker-base-modal/file-picker-base-modal.component.ts
index 7a15054bd89..877fd358887 100644
--- a/frontend/src/app/shared/components/storages/file-picker-base-modal/file-picker-base-modal.component.ts
+++ b/frontend/src/app/shared/components/storages/file-picker-base-modal/file-picker-base-modal.component.ts
@@ -60,6 +60,12 @@ import {
getIconForStorageType,
makeFilesCollectionLink,
} from 'core-app/shared/components/storages/functions/storages.functions';
+import {
+ IHalErrorBase,
+ v3ErrorIdentifierOutboundRequestForbidden,
+} from 'core-app/features/hal/resources/error-resource';
+
+type Alert = 'none'|'noAccess'|'managedFolderNoAccess'|'managedFolderNotFound';
@Directive()
export abstract class FilePickerBaseModalComponent extends OpModalComponent implements OnInit, OnDestroy {
@@ -79,7 +85,7 @@ export abstract class FilePickerBaseModalComponent extends OpModalComponent impl
navigate: () => {},
}]);
- public noAccessWarning = new BehaviorSubject(false);
+ public showAlert:BehaviorSubject<Alert> = new BehaviorSubject('none');
public listItems$:Observable<StorageFileListItem[]> = this.storageFiles$
.pipe(
@@ -128,8 +134,8 @@ export abstract class FilePickerBaseModalComponent extends OpModalComponent impl
this.storageFilesResourceService.reset();
}
- public closeWarning():void {
- this.noAccessWarning.next(false);
+ public closeAlert():void {
+ this.showAlert.next('none');
}
protected abstract storageFileToListItem(file:IStorageFile, index:number):StorageFileListItem;
@@ -163,7 +169,12 @@ export abstract class FilePickerBaseModalComponent extends OpModalComponent impl
}
private entryLocation():Observable<string> {
- if (this.locals.projectFolderHref === null) {
+ if (this.locals.projectFolderMode === 'inactive') {
+ return of('/');
+ }
+
+ if (this.locals.projectFolderMode === 'automatic' && this.locals.projectFolderHref === null) {
+ this.showAlert.next('managedFolderNotFound');
return of('/');
}
@@ -171,12 +182,19 @@ export abstract class FilePickerBaseModalComponent extends OpModalComponent impl
.file(this.locals.projectFolderHref as string)
.pipe(
map((file) => file.location),
- catchError((error:HttpErrorResponse) => {
- if (error.status !== 403) {
- throw new Error(error.message);
+ catchError((response:HttpErrorResponse) => {
+ const isForbiddenOnStorage = response.status === 500
+ && (response.error as IHalErrorBase).errorIdentifier === v3ErrorIdentifierOutboundRequestForbidden;
+
+ if (!isForbiddenOnStorage) {
+ throw new Error(response.message);
}
- this.noAccessWarning.next(true);
+ if (this.locals.projectFolderMode === 'automatic') {
+ this.showAlert.next('managedFolderNoAccess');
+ } else {
+ this.showAlert.next('noAccess');
+ }
return of('/');
}),
);
diff --git a/frontend/src/app/shared/components/storages/file-picker-modal/file-picker-modal.component.html b/frontend/src/app/shared/components/storages/file-picker-modal/file-picker-modal.component.html
index a5992e14384..1bc9f4f738b 100644
--- a/frontend/src/app/shared/components/storages/file-picker-modal/file-picker-modal.component.html
+++ b/frontend/src/app/shared/components/storages/file-picker-modal/file-picker-modal.component.html
@@ -10,14 +10,14 @@
<div class="spot-modal--body spot-container op-file-picker--modal-body">
<div
- *ngIf="noAccessWarning | async"
+ *ngIf="(showAlert | async) !== 'none'"
class="op-toast -warning"
>
<div class="op-toast--content">
- <p>{{text.warningNoAccess}}</p>
+ <p>{{alertText | async}}</p>
<button
class="spot-link op-toast--close"
- (click)="closeWarning()"
+ (click)="closeAlert()"
>
<span class="spot-icon spot-icon_close"></span>
</button>
diff --git a/frontend/src/app/shared/components/storages/file-picker-modal/file-picker-modal.component.ts b/frontend/src/app/shared/components/storages/file-picker-modal/file-picker-modal.component.ts
index a0dd86e18c1..d26e3d71262 100644
--- a/frontend/src/app/shared/components/storages/file-picker-modal/file-picker-modal.component.ts
+++ b/frontend/src/app/shared/components/storages/file-picker-modal/file-picker-modal.component.ts
@@ -29,7 +29,9 @@
import {
ChangeDetectionStrategy, ChangeDetectorRef, Component, ElementRef, Inject,
} from '@angular/core';
-import { take } from 'rxjs/operators';
+import { HttpErrorResponse } from '@angular/common/http';
+import { Observable } from 'rxjs';
+import { map, take } from 'rxjs/operators';
import { I18nService } from 'core-app/core/i18n/i18n.service';
import { TimezoneService } from 'core-app/core/datetime/timezone.service';
@@ -49,7 +51,6 @@ import {
import {
FilePickerBaseModalComponent,
} from 'core-app/shared/components/storages/file-picker-base-modal/file-picker-base-modal.component';
-import { HttpErrorResponse } from '@angular/common/http';
@Component({
templateUrl: 'file-picker-modal.component.html',
@@ -58,7 +59,9 @@ import { HttpErrorResponse } from '@angular/common/http';
export class FilePickerModalComponent extends FilePickerBaseModalComponent {
public readonly text = {
header: this.i18n.t('js.storages.file_links.select'),
- warningNoAccess: this.i18n.t('js.storages.files.project_folder_no_access'),
+ alertNoAccess: this.i18n.t('js.storages.files.project_folder_no_access'),
+ alertNoManagedProjectFolder: this.i18n.t('js.storages.files.managed_project_folder_not_available'),
+ alertNoAccessToManagedProjectFolder: this.i18n.t('js.storages.files.managed_project_folder_no_access'),
content: {
empty: this.i18n.t('js.storages.files.empty_folder'),
emptyHint: this.i18n.t('js.storages.files.empty_folder_location_hint'),
@@ -87,6 +90,26 @@ export class FilePickerModalComponent extends FilePickerBaseModalComponent {
return this.i18n.t(storageLocaleString(this.storage._links.type.href));
}
+ public get alertText():Observable<string> {
+ return this.showAlert
+ .pipe(
+ map((alert) => {
+ switch (alert) {
+ case 'noAccess':
+ return this.text.alertNoAccess;
+ case 'managedFolderNoAccess':
+ return this.text.alertNoAccessToManagedProjectFolder;
+ case 'managedFolderNotFound':
+ return this.text.alertNoManagedProjectFolder;
+ case 'none':
+ return '';
+ default:
+ throw new Error('unknown alert type');
+ }
+ }),
+ );
+ }
+
public showSelectAll = false;
private readonly selection = new Set<string>();
diff --git a/frontend/src/app/shared/components/storages/location-picker-modal/location-picker-modal.component.html b/frontend/src/app/shared/components/storages/location-picker-modal/location-picker-modal.component.html
index 67a551fd6be..3c6f116d8f5 100644
--- a/frontend/src/app/shared/components/storages/location-picker-modal/location-picker-modal.component.html
+++ b/frontend/src/app/shared/components/storages/location-picker-modal/location-picker-modal.component.html
@@ -10,14 +10,14 @@
<div class="spot-modal--body spot-container op-file-picker--modal-body">
<div
- *ngIf="noAccessWarning | async"
+ *ngIf="(showAlert | async) !== 'none'"
class="op-toast -warning"
>
<div class="op-toast--content">
- <p>{{text.warningNoAccess}}</p>
+ <p>{{alertText | async}}</p>
<button
class="spot-link op-toast--close"
- (click)="closeWarning()"
+ (click)="closeAlert()"
>
<span class="spot-icon spot-icon_close"></span>
</button>
diff --git a/frontend/src/app/shared/components/storages/location-picker-modal/location-picker-modal.component.ts b/frontend/src/app/shared/components/storages/location-picker-modal/location-picker-modal.component.ts
index 8ce8be4e864..c43e0cc6c55 100644
--- a/frontend/src/app/shared/components/storages/location-picker-modal/location-picker-modal.component.ts
+++ b/frontend/src/app/shared/components/storages/location-picker-modal/location-picker-modal.component.ts
@@ -48,6 +48,8 @@ import {
import {
FilePickerBaseModalComponent,
} from 'core-app/shared/components/storages/file-picker-base-modal/file-picker-base-modal.component';
+import { Observable } from 'rxjs';
+import { map } from 'rxjs/operators';
@Component({
templateUrl: 'location-picker-modal.component.html',
@@ -58,7 +60,9 @@ export class LocationPickerModalComponent extends FilePickerBaseModalComponent {
public readonly text = {
header: this.i18n.t('js.storages.select_location'),
- warningNoAccess: this.i18n.t('js.storages.files.project_folder_no_access'),
+ alertNoAccess: this.i18n.t('js.storages.files.project_folder_no_access'),
+ alertNoManagedProjectFolder: this.i18n.t('js.storages.files.managed_project_folder_not_available'),
+ alertNoAccessToManagedProjectFolder: this.i18n.t('js.storages.files.managed_project_folder_no_access'),
content: {
empty: this.i18n.t('js.storages.files.empty_folder'),
emptyHint: this.i18n.t('js.storages.files.empty_folder_location_hint'),
@@ -97,6 +101,26 @@ export class LocationPickerModalComponent extends FilePickerBaseModalComponent {
return this.currentDirectory.permissions.some((value) => value === 'writeable');
}
+ public get alertText():Observable<string> {
+ return this.showAlert
+ .pipe(
+ map((alert) => {
+ switch (alert) {
+ case 'noAccess':
+ return this.text.alertNoAccess;
+ case 'managedFolderNoAccess':
+ return this.text.alertNoAccessToManagedProjectFolder;
+ case 'managedFolderNotFound':
+ return this.text.alertNoManagedProjectFolder;
+ case 'none':
+ return '';
+ default:
+ throw new Error('unknown alert type');
+ }
+ }),
+ );
+ }
+
constructor(
@Inject(OpModalLocalsToken) public locals:OpModalLocalsMap,
readonly elementRef:ElementRef,
diff --git a/frontend/src/app/shared/components/storages/storage/storage.component.ts b/frontend/src/app/shared/components/storages/storage/storage.component.ts
index 49aa8637545..989b966d3dd 100644
--- a/frontend/src/app/shared/components/storages/storage/storage.component.ts
+++ b/frontend/src/app/shared/components/storages/storage/storage.component.ts
@@ -178,17 +178,17 @@ export class StorageComponent extends UntilDestroyedMixin implements OnInit, OnD
return (this.resource.$links as unknown&{ addFileLink:IHalResourceLink }).addFileLink.href;
}
- private get projectFolderHref():string|null {
- if (this.projectStorage.projectFolderMode === 'inactive') {
- return null;
- }
-
- if (!this.projectStorage._links.projectFolder) {
- throw new Error(`Project folder id 'null' not allowed for project folder mode '${this.projectStorage.projectFolderMode}'.`);
- }
-
- return this.projectStorage._links.projectFolder.href;
- }
+ // private get projectFolderHref():string|null {
+ // if (this.projectStorage.projectFolderMode === 'inactive') {
+ // return null;
+ // }
+ //
+ // if (!this.projectStorage._links.projectFolder) {
+ // throw new Error(`Project folder id 'null' not allowed for project folder mode '${this.projectStorage.projectFolderMode}'.`);
+ // }
+ //
+ // return this.projectStorage._links.projectFolder.href;
+ // }
private onGlobalDragLeave:(_event:DragEvent) => void = (_event) => {
this.dragging = Math.max(this.dragging - 1, 0);
@@ -294,7 +294,8 @@ export class StorageComponent extends UntilDestroyedMixin implements OnInit, OnD
.subscribe(([storage, fileLinks, collectionKey]) => {
const locals = {
addFileLinksHref: this.addFileLinksHref,
- projectFolderHref: this.projectFolderHref,
+ projectFolderHref: this.projectStorage._links.projectFolder?.href || null,
+ projectFolderMode: this.projectStorage.projectFolderMode,
storage,
collectionKey,
fileLinks,
@@ -329,7 +330,8 @@ export class StorageComponent extends UntilDestroyedMixin implements OnInit, OnD
private selectUploadLocation(storage:IStorage):Observable<LocationData> {
const locals = {
- projectFolderHref: this.projectFolderHref,
+ projectFolderHref: this.projectStorage._links.projectFolder?.href,
+ projectFolderMode: this.projectStorage.projectFolderMode,
storage,
};
diff --git a/lib/api/errors/outbound_request_forbidden.rb b/lib/api/errors/outbound_request_forbidden.rb
new file mode 100644
index 00000000000..af2a3ea4c8e
--- /dev/null
+++ b/lib/api/errors/outbound_request_forbidden.rb
@@ -0,0 +1,40 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2023 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 API
+ module Errors
+ class OutboundRequestForbidden < ErrorBase
+ identifier 'OutboundRequest:Forbidden'.freeze
+ code 500
+
+ def initialize
+ super I18n.t('api_v3.errors.code_500_outbound_request_failure', status_code: 403)
+ end
+ end
+ end
+end
diff --git a/modules/storages/app/common/storages/peripherals/storage_error_helper.rb b/modules/storages/app/common/storages/peripherals/storage_error_helper.rb
index 47b626fdf43..541cb4e7bb4 100644
--- a/modules/storages/app/common/storages/peripherals/storage_error_helper.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_error_helper.rb
@@ -37,7 +37,7 @@ module Storages::Peripherals
when :bad_request
raise API::Errors::BadRequest.new(error.log_message)
when :forbidden
- raise API::Errors::Unauthorized.new
+ raise API::Errors::OutboundRequestForbidden.new
else
raise API::Errors::InternalError.new
end
diff --git a/modules/storages/config/locales/js-en.yml b/modules/storages/config/locales/js-en.yml
index 275104d81a9..52f955438f9 100644
--- a/modules/storages/config/locales/js-en.yml
+++ b/modules/storages/config/locales/js-en.yml
@@ -43,6 +43,11 @@ en:
project_folder_no_access: >
You have no access to the project folder. Please, contact your administrator to get access or
upload the file in another location.
+ managed_project_folder_not_available: >
+ The automatically managed project folder was not yet found. Please wait a bit, reload the page to fetch the
+ newest data, and try again.
+ managed_project_folder_no_access: >
+ You have no access yet to the managed project folder. Please wait a bit and try again.
upload_keep_both: "Keep both"
upload_replace: "Replace"
diff --git a/modules/storages/lib/api/v3/storage_files/storage_files_api.rb b/modules/storages/lib/api/v3/storage_files/storage_files_api.rb
index 8cb356ff285..547c4adeac2 100644
--- a/modules/storages/lib/api/v3/storage_files/storage_files_api.rb
+++ b/modules/storages/lib/api/v3/storage_files/storage_files_api.rb
@@ -45,17 +45,23 @@ module API::V3::StorageFiles
route_param :file_id, type: String, desc: 'Storage file id' do
get do
- Storages::Peripherals::StorageRequests
- .new(storage: @storage)
- .files_info_query
- .call(user: current_user, file_ids: [params[:file_id]]).map(&:first)
- .map { |file_info| to_storage_file(file_info) }
- .match(
- on_success: ->(storage_file) {
- API::V3::StorageFiles::StorageFileRepresenter.new(storage_file, @storage, current_user:)
- },
- on_failure: ->(error) { raise_error(error) }
- )
+ service_result = Storages::Peripherals::StorageRequests
+ .new(storage: @storage)
+ .files_info_query
+ .call(user: current_user, file_ids: [params[:file_id]]).map(&:first)
+
+ if service_result.success? && service_result.result.status_code == 403
+ storage_error = Storages::StorageError.new(code: :forbidden, log_message: 'no access to file', data: nil)
+ service_result = ServiceResult.failure(result: :forbidden, errors: storage_error)
+ end
+
+ service_result.map { |file_info| to_storage_file(file_info) }
+ .match(
+ on_success: ->(storage_file) {
+ API::V3::StorageFiles::StorageFileRepresenter.new(storage_file, @storage, current_user:)
+ },
+ on_failure: ->(error) { raise_error(error) }
+ )
end
end
diff --git a/modules/storages/spec/requests/api/v3/storages/storage_files_spec.rb b/modules/storages/spec/requests/api/v3/storages/storage_files_spec.rb
index 4f5012823a6..b04c5f27b20 100644
--- a/modules/storages/spec/requests/api/v3/storages/storage_files_spec.rb
+++ b/modules/storages/spec/requests/api/v3/storages/storage_files_spec.rb
@@ -52,8 +52,7 @@ RSpec.describe 'API v3 storage files', content_type: :json, webmock: true do
end
before do
- allow(connection_manager).to receive(:get_authorization_uri).and_return(authorize_url)
- allow(connection_manager).to receive(:authorization_state).and_return(:connected)
+ allow(connection_manager).to receive_messages(get_authorization_uri: authorize_url, authorization_state: :connected)
allow(OAuthClients::ConnectionManager).to receive(:new).and_return(connection_manager)
project_storage
login_as current_user
@@ -167,6 +166,119 @@ RSpec.describe 'API v3 storage files', content_type: :json, webmock: true do
end
end
+ describe 'GET /api/v3/storages/:storage_id/files/:file_id' do
+ let(:file_id) { '42' }
+ let(:path) { api_v3_paths.storage_file(storage.id, file_id) }
+
+ context 'with successful response' do
+ let(:response) do
+ [
+ Storages::StorageFileInfo.new(
+ status: 'OK',
+ status_code: 200,
+ id: file_id,
+ name: "Documents",
+ last_modified_at: DateTime.now,
+ created_at: DateTime.now,
+ mime_type: 'application/x-op-directory',
+ size: 1108864,
+ owner_name: 'Darth Vader',
+ owner_id: 'darthvader',
+ trashed: false,
+ last_modified_by_name: 'Darth Sidious',
+ last_modified_by_id: 'palpatine',
+ permissions: 'RGDNVCK',
+ location: '/Documents'
+ )
+ ]
+ end
+
+ before do
+ storage_requests = instance_double(Storages::Peripherals::StorageRequests)
+ files_info_query = Proc.new do
+ ServiceResult.success(result: response)
+ end
+ allow(storage_requests).to receive(:files_info_query).and_return(files_info_query)
+ allow(Storages::Peripherals::StorageRequests).to receive(:new).and_return(storage_requests)
+ end
+
+ subject { last_response.body }
+
+ it 'responds with appropriate JSON' do
+ expect(subject).to be_json_eql('StorageFile'.to_json).at_path('_type')
+ expect(subject).to be_json_eql(response[0].id.to_json).at_path('id')
+ expect(subject).to be_json_eql(response[0].name.to_json).at_path('name')
+ expect(subject).to be_json_eql(response[0].size.to_json).at_path('size')
+ expect(subject).to be_json_eql(response[0].mime_type.to_json).at_path('mimeType')
+ expect(subject).to be_json_eql(response[0].owner_name.to_json).at_path('createdByName')
+ expect(subject).to be_json_eql(response[0].last_modified_by_name.to_json).at_path('lastModifiedByName')
+ expect(subject).to be_json_eql(response[0].location.to_json).at_path('location')
+ expect(subject).to be_json_eql(response[0].permissions.to_json).at_path('permissions')
+ end
+
+ context 'and storage file access is forbidden' do
+ let(:response) do
+ [
+ Storages::StorageFileInfo.new(
+ id: file_id,
+ status: 'Forbidden',
+ status_code: 403
+ )
+ ]
+ end
+
+ it 'fails with outbound request failure' do
+ expect(last_response.status).to be(500)
+
+ body = JSON.parse(last_response.body)
+ expect(body['message']).to eq(I18n.t('api_v3.errors.code_500_outbound_request_failure', status_code: 403))
+ expect(body['errorIdentifier']).to eq('urn:openproject-org:api:v3:errors:OutboundRequest:Forbidden')
+ end
+ end
+ end
+
+ context 'with query failed' do
+ before do
+ clazz = Storages::Peripherals::StorageInteraction::Nextcloud::FilesInfoQuery
+ instance = instance_double(clazz)
+ allow(clazz).to receive(:new).and_return(instance)
+ allow(instance).to receive(:call).and_return(
+ ServiceResult.failure(result: error, errors: Storages::StorageError.new(code: error))
+ )
+ end
+
+ context 'with authorization failure' do
+ let(:error) { :forbidden }
+
+ it 'fails with outbound request failure' do
+ expect(last_response.status).to be(500)
+
+ body = JSON.parse(last_response.body)
+ expect(body['message']).to eq(I18n.t('api_v3.errors.code_500_outbound_request_failure', status_code: 403))
+ expect(body['errorIdentifier']).to eq('urn:openproject-org:api:v3:errors:OutboundRequest:Forbidden')
+ end
+ end
+
+ context 'with internal error' do
+ let(:error) { :error }
+
+ it { expect(last_response.status).to be(500) }
+ end
+
+ context 'with not found' do
+ let(:error) { :not_found }
+
+ it 'fails with outbound request failure' do
+ expect(last_response.status).to be(500)
+
+ body = JSON.parse(last_response.body)
+ expect(body['message']).to eq(I18n.t('api_v3.errors.code_500_outbound_request_failure', status_code: 404))
+ expect(body['errorIdentifier']).to eq('urn:openproject-org:api:v3:errors:OutboundRequest:NotFound')
+ end
+ end
+ end
+ end
+
describe 'POST /api/v3/storages/:storage_id/files/prepare_upload' do
let(:permissions) { %i(view_work_packages view_file_links manage_file_links) }
let(:path) { api_v3_paths.prepare_upload(storage.id) }