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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
[#48330] Check for authorization state of storages in project storage create form
Eric Schubertae565e874715
diff --git a/frontend/src/app/core/setup/global-dynamic-components.const.ts b/frontend/src/app/core/setup/global-dynamic-components.const.tsindex 02345e1a580..6844163b4f8 100644--- a/frontend/src/app/core/setup/global-dynamic-components.const.ts+++ b/frontend/src/app/core/setup/global-dynamic-components.const.ts@@ -221,6 +221,10 @@ import {} from 'core-app/shared/components/datepicker/basic-single-date-picker/basic-single-date-picker.component';import { SpotDropModalPortalComponent, spotDropModalPortalComponentSelector } from 'core-app/spot/components/drop-modal/drop-modal-portal.component';import { StaticAttributeHelpTextComponent, staticAttributeHelpTextSelector } from 'core-app/shared/components/attribute-help-texts/static-attribute-help-text.component';+import {+ StorageLoginButtonComponent,+ opStorageLoginButtonSelector,+} from 'core-app/shared/components/storages/storage-login-button/storage-login-button.component';export const globalDynamicComponents:OptionalBootstrapDefinition[] = [{ selector: appBaseSelector, cls: ApplicationBaseComponent },@@ -281,6 +285,7 @@ export const globalDynamicComponents:OptionalBootstrapDefinition[] = [{ selector: opModalOverlaySelector, cls: OpModalOverlayComponent },{ selector: spotDropModalPortalComponentSelector, cls: SpotDropModalPortalComponent },{ selector: spotSwitchSelector, cls: SpotSwitchComponent },+ { selector: opStorageLoginButtonSelector, cls: StorageLoginButtonComponent },{ selector: opModalSingleDatePickerSelector, cls: OpModalSingleDatePickerComponent, embeddable: true },{ selector: opBasicSingleDatePickerSelector, cls: OpBasicSingleDatePickerComponent, embeddable: true },diff --git a/frontend/src/app/shared/components/storages/storage-information/storage-information.html b/frontend/src/app/shared/components/storages/storage-information/storage-information.htmlindex 3c32f86ae08..b399aee676d 100644--- a/frontend/src/app/shared/components/storages/storage-information/storage-information.html+++ b/frontend/src/app/shared/components/storages/storage-information/storage-information.html@@ -20,6 +20,7 @@class="button-box"><op-storage-login-button+ class="op-files-tab--storage-info-box-button"[input]="content.login"></op-storage-login-button></div>diff --git a/frontend/src/app/shared/components/storages/storage-login-button/storage-login-button.component.html b/frontend/src/app/shared/components/storages/storage-login-button/storage-login-button.component.htmlindex 09b5bf83a27..45ccf0c060e 100644--- a/frontend/src/app/shared/components/storages/storage-login-button/storage-login-button.component.html+++ b/frontend/src/app/shared/components/storages/storage-login-button/storage-login-button.component.html@@ -1,5 +1,5 @@<button type="button"- class="button op-files-tab--storage-info-box-button"+ class="button button_no-margin"(click)="login()"><span>{{label}}</span>diff --git a/frontend/src/app/shared/components/storages/storage-login-button/storage-login-button.component.ts b/frontend/src/app/shared/components/storages/storage-login-button/storage-login-button.component.tsindex 471f5aa5cb9..c9535a4f211 100644--- a/frontend/src/app/shared/components/storages/storage-login-button/storage-login-button.component.ts+++ b/frontend/src/app/shared/components/storages/storage-login-button/storage-login-button.component.ts@@ -29,6 +29,7 @@import {ChangeDetectionStrategy,Component,+ ElementRef,Input,OnInit,} from '@angular/core';@@ -38,9 +39,12 @@ import { v4 as uuidv4 } from 'uuid';import { I18nService } from 'core-app/core/i18n/i18n.service';import { IStorageLoginInput } from 'core-app/shared/components/storages/storage-login-button/storage-login-input';import { storageLocaleString } from 'core-app/shared/components/storages/functions/storages.functions';+import { populateInputsFromDataset } from 'core-app/shared/components/dataset-inputs';++export const opStorageLoginButtonSelector = 'op-storage-login-button';@Component({- selector: 'op-storage-login-button',+ selector: opStorageLoginButtonSelector,templateUrl: 'storage-login-button.component.html',changeDetection: ChangeDetectionStrategy.OnPush,})@@ -50,9 +54,12 @@ export class StorageLoginButtonComponent implements OnInit {label:string;constructor(+ public elementRef:ElementRef,private readonly i18n:I18nService,private readonly cookieService:CookieService,- ) { }+ ) {+ populateInputsFromDataset(this);+ }ngOnInit():void {const storageType = this.i18n.t(storageLocaleString(this.input.storageType));diff --git a/frontend/src/stimulus/controllers/dynamic/project-storage-form.controller.ts b/frontend/src/stimulus/controllers/dynamic/project-storage-form.controller.tsindex 99d0271271f..a34c2502a8d 100644--- a/frontend/src/stimulus/controllers/dynamic/project-storage-form.controller.ts+++ b/frontend/src/stimulus/controllers/dynamic/project-storage-form.controller.ts@@ -29,15 +29,25 @@*/import { Controller } from '@hotwired/stimulus';-import { from, Observable } from 'rxjs';-import { filter, map, switchMap } from 'rxjs/operators';+import {+ combineLatest,+ from,+ Observable,+ of,+} from 'rxjs';+import {+ filter,+ map,+ switchMap,+} from 'rxjs/operators';+import { IStorage } from 'core-app/core/state/storages/storage.model';import { OpModalService } from 'core-app/shared/components/modal/modal.service';+import { IStorageFile } from 'core-app/core/state/storage-files/storage-file.model';+import { storageConnected } from 'core-app/shared/components/storages/storages-constants.const';import {LocationPickerModalComponent,} from 'core-app/shared/components/storages/location-picker-modal/location-picker-modal.component';-import { IStorage } from 'core-app/core/state/storages/storage.model';-import { IStorageFile } from 'core-app/core/state/storage-files/storage-file.model';export default class ProjectStorageFormController extends Controller {static targets = [@@ -45,24 +55,28 @@ export default class ProjectStorageFormController extends Controller {'projectFolderIdInput','projectFolderIdValidation','selectedFolderText',+ 'selectProjectFolderButton',+ 'loginButton','storage',- 'storageSelector',];static values = {folderMode: String,placeholderFolderName: String,+ notLoggedInValidation: String,};declare folderModeValue:string;declare placeholderFolderNameValue:string;- declare readonly storageTargets:HTMLElement[];+ declare notLoggedInValidationValue:string;++ declare readonly storageTarget:HTMLElement;- declare readonly storageSelectorTarget:HTMLSelectElement;+ declare readonly selectProjectFolderButtonTarget:HTMLButtonElement;- declare readonly hasStorageSelectorTarget:boolean;+ declare readonly loginButtonTarget:HTMLButtonElement;declare readonly projectFolderSectionTarget:HTMLElement;@@ -77,17 +91,24 @@ export default class ProjectStorageFormController extends Controller {declare readonly hasProjectFolderSectionTarget:boolean;connect():void {- this.toggleFolderDisplay(this.folderModeValue);- this.selectedFolderTextTarget.innerText = this.placeholderFolderNameValue;-- const href = this.projectFolderHref;- if (href !== null) {- void fetch(href)- .then((data) => data.json())- .then((file:IStorageFile) => {- this.selectedFolderTextTarget.innerText = file.name;- });- }+ combineLatest([+ this.fetchStorageAuthorizationState(),+ this.fetchProjectFolder(),+ ]).subscribe(([isConnected, projectFolder]) => {+ if (isConnected) {+ this.selectProjectFolderButtonTarget.style.display = 'inline-block';+ this.loginButtonTarget.style.display = 'none';+ this.selectedFolderTextTarget.innerText = projectFolder === null+ ? this.placeholderFolderNameValue+ : projectFolder.name;+ } else {+ this.selectProjectFolderButtonTarget.style.display = 'none';+ this.loginButtonTarget.style.display = 'inline-block';+ this.selectedFolderTextTarget.innerText = this.notLoggedInValidationValue;+ }++ this.toggleFolderDisplay(this.folderModeValue);+ });}selectProjectFolder(_evt:Event):void {@@ -125,11 +146,7 @@ export default class ProjectStorageFormController extends Controller {}private get storage():IStorage {- const storageElement = this.hasStorageSelectorTarget- ? this.storageSelectorTarget.options[this.storageSelectorTarget.selectedIndex]- : this.storageTargets[0];-- return JSON.parse(storageElement.dataset.storage as string) as IStorage;+ return JSON.parse(this.storageTarget.dataset.storage as string) as IStorage;}private get projectFolderHref():string|null {@@ -142,6 +159,33 @@ export default class ProjectStorageFormController extends Controller {return `${this.storage._links.self.href}/files/${projectFolderId}`;}+ private fetchStorageAuthorizationState():Observable<boolean> {+ return from(fetch(this.storage._links.self.href)+ .then((data) => data.json()))+ .pipe(+ map((storage:IStorage) => storage._links.authorizationState.href === storageConnected),+ );+ }++ private fetchProjectFolder():Observable<IStorageFile|null> {+ const href = this.projectFolderHref;+ if (href === null) {+ return of(null);+ }++ return from(fetch(href).then((data) => data.json()))+ .pipe(+ map((file) => {+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access+ if (file._type === 'StorageFile') {+ return file as IStorageFile;+ }++ return null;+ }),+ );+ }+private toggleFolderDisplay(value:string):void {// If the manual radio button is selected, show the manual folder selection sectionif (value === 'manual') {diff --git a/modules/storages/app/controllers/storages/admin/projects_storages_controller.rb b/modules/storages/app/controllers/storages/admin/projects_storages_controller.rbindex 5877b200fee..e06ee1e2071 100644--- a/modules/storages/app/controllers/storages/admin/projects_storages_controller.rb+++ b/modules/storages/app/controllers/storages/admin/projects_storages_controller.rb@@ -61,9 +61,6 @@ class Storages::Admin::ProjectsStoragesController < Projects::SettingsController# Show a HTML page with a form in order to create a new ProjectStorage# Called by: When a user clicks on the "+New" button in Project -> Settings -> File Storagesdef new- # Create an empty ProjectStorage object, but don't save it to the database yet.- # @project was calculated in before_action (see comments above).- # @project_storage is used in the view in order to render the form for a new object@project_storage = ::Storages::ProjectStorages::SetAttributesService.new(user: current_user,model: Storages::ProjectStorage.new,@@ -71,11 +68,13 @@ class Storages::Admin::ProjectsStoragesController < Projects::SettingsController.call(project: @project).result- # Calculate the list of available Storage objects, subtracting already enabled storages.@available_storages = available_storages+ storage_id = params.dig(:storages_project_storage, :storage_id)+ if storage_id.present?+ @project_storage.storage = available_storages.find_by(id: storage_id)+ end- # Show the HTML form to create the object.- render '/storages/project_settings/new'+ render template: '/storages/project_settings/new'end# Create a new ProjectStorage object.diff --git a/modules/storages/app/helpers/storage_login_helper.rb b/modules/storages/app/helpers/storage_login_helper.rbnew file mode 100644index 00000000000..b588670db7c--- /dev/null+++ b/modules/storages/app/helpers/storage_login_helper.rb@@ -0,0 +1,42 @@+#-- 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 StorageLoginHelper+ def storage_login_input(storage)+ return {} if storage&.oauth_client.blank?++ connection_manager = ::OAuthClients::ConnectionManager.new(user: current_user, oauth_client: storage.oauth_client)++ {+ storageType: API::V3::Storages::STORAGE_TYPE_URN_MAP[storage.provider_type],+ authorizationLink: {+ href: connection_manager.get_authorization_uri+ }+ }+ end+enddiff --git a/modules/storages/app/views/storages/project_settings/_form.html.erb b/modules/storages/app/views/storages/project_settings/_project_folder_form.html.erbsimilarity index 64%rename from modules/storages/app/views/storages/project_settings/_form.html.erbrename to modules/storages/app/views/storages/project_settings/_project_folder_form.html.erbindex 61094fe1434..9c711f0bc04 100644--- a/modules/storages/app/views/storages/project_settings/_form.html.erb+++ b/modules/storages/app/views/storages/project_settings/_project_folder_form.html.erb@@ -30,66 +30,32 @@ See COPYRIGHT and LICENSE files for more details.<% content_controller 'project-storage-form',dynamic: true,'project-storage-form-folder-mode-value': @project_storage.project_folder_mode,- 'project-storage-form-placeholder-folder-name-value': t(:"storages.label_no_selected_folder")+ 'project-storage-form-placeholder-folder-name-value': t(:"storages.label_no_selected_folder"),+ 'project-storage-form-empty-project-folder-validation-value': t(:"storages.instructions.empty_project_folder_validation"),+ 'project-storage-form-not-logged-in-validation-value': t(:"storages.instructions.not_logged_into_storage")%><div class="form--field -required">- <% case action_name %>- <% when 'new', 'create' %>- <%= f.select :storage_id,- (@available_storages.map do |storage|- [- "#{storage.name} (#{storage.short_provider_type})",- storage.id,- data: {- 'project-storage-form-target': 'storage',- storage: {- name: storage.name,- id: storage.id,- _links: {- self: { href: api_v3_paths.storage(storage.id) },- type: { href: API::V3::Storages::URN_STORAGE_TYPE_NEXTCLOUD }- }- },- }- ]- end),- {- class: '-bold',- selected: @available_storages.first.id.to_s,- container_class: '-slim'- },- data: {- 'project-storage-form-target': 'storageSelector',- } %>- <% if User.current.admin? %>- <span class="form--field-instructions">- <%= t(:"storages.instructions.setting_up_additional_storages") %>- <%= link_to "#{t(:label_administration)} / #{t(:project_module_storages)}", controller: '/storages/admin/storages', action: :index %>- .- </span>- <% end %>- <% when 'edit', 'update' %>- <%= f.label :storage_id, class: "form--label -bold" do %>- <%= t(:"storages.label_storage") %>- <% end %>- <div class="form--field-container">- <%= content_tag(:span,- @project_storage.storage.name,- id: 'active_storage',- data: {- 'project-storage-form-target': 'storage',- storage: {- name: @project_storage.storage.name,- id: @project_storage.storage.id,- _links: {- self: { href: api_v3_paths.storage(@project_storage.storage.id) },- type: { href: API::V3::Storages::URN_STORAGE_TYPE_NEXTCLOUD }- }- },- }) %>- </div>+ <%= f.label :storage_id, class: "form--label -bold" do %>+ <%= t(:"storages.label_storage") %><% end %>+ <div class="form--field-container">+ <%= f.hidden_field :storage_id %>+ <% storage = @project_storage.storage %>+ <%= content_tag(:span,+ storage.name,+ data: {+ 'project-storage-form-target': 'storage',+ storage: {+ name: storage.name,+ id: storage.id,+ _links: {+ self: { href: api_v3_paths.storage(storage.id) },+ type: { href: API::V3::Storages::URN_STORAGE_TYPE_NEXTCLOUD }+ }+ },+ }) %>+ </div></div><% if OpenProject::FeatureDecisions.storage_project_folders_active? %>@@ -139,20 +105,29 @@ See COPYRIGHT and LICENSE files for more details.data: {'project-storage-form-target': "projectFolderIdInput"} %>- <%= styled_button_tag id: 'open_location_picker_button',- class: 'button_no-margin',+ <%= styled_button_tag class: 'button_no-margin',data: {+ 'project-storage-form-target': "selectProjectFolderButton",'action': "project-storage-form#selectProjectFolder"},type: 'button' do %><%= spot_icon('folder-open') %><%= content_tag :span, t(:"storages.buttons.select_folder") %><% end %>+ <%= angular_component_tag 'op-storage-login-button',+ inputs: {+ input: storage_login_input(@project_storage.storage)+ },+ style: 'display: inline-block',+ data: {+ 'project-storage-form-target': "loginButton"+ }+ %><spandata-project-storage-form-target="selectedFolderText"- ></span>+ >...</span></div>- <% if @errors.present? && @errors.any? %>+ <% if @errors&.any? { |error| error.attribute == :project_folder_id } %><%= content_tag :span,t(:"storages.instructions.empty_project_folder_validation"),class: 'form--field-error -with-bottom-spacing',diff --git a/modules/storages/app/views/storages/project_settings/_storage_select_form.html.erb b/modules/storages/app/views/storages/project_settings/_storage_select_form.html.erbnew file mode 100644index 00000000000..505382c004d--- /dev/null+++ b/modules/storages/app/views/storages/project_settings/_storage_select_form.html.erb@@ -0,0 +1,55 @@+<%#-- 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.++++#%>++<div class="form--field -required">+ <%= f.select :storage_id,+ @available_storages.map { |storage| ["#{storage.name} (#{storage.short_provider_type})", storage.id] },+ class: '-bold',+ selected: @available_storages.first.id.to_s,+ container_class: '-slim'+ %>+ <% if User.current.admin? %>+ <span class="form--field-instructions">+ <%= t(:"storages.instructions.setting_up_additional_storages") %>+ <%= link_to "#{t(:label_administration)} / #{t(:project_module_storages)}", controller: '/storages/admin/storages', action: :index %>+ .+ </span>+ <% end %>+</div>++<div class="form--field">+ <%= styled_button_tag class: "-highlight" do %>+ <%= spot_icon('checkmark') %>+ <%= content_tag :span, submit_button_label %>+ <% end %>+ <%= link_to project_settings_projects_storages_path(@project), class: 'button' do %>+ <%= spot_icon('cancel') %>+ <%= content_tag :span, t(:button_cancel) %>+ <% end %>+</div>diff --git a/modules/storages/app/views/storages/project_settings/edit.html.erb b/modules/storages/app/views/storages/project_settings/edit.html.erbindex 18f885750d3..78a69cd1339 100644--- a/modules/storages/app/views/storages/project_settings/edit.html.erb+++ b/modules/storages/app/views/storages/project_settings/edit.html.erb@@ -33,7 +33,7 @@ See COPYRIGHT and LICENSE files for more details.<%= labelled_tabular_form_for @project_storage, url: project_settings_projects_storage_path(project_id: @project_storage.project, id: @project_storage) do |f| -%><section class="form--section">- <%= render partial: '/storages/project_settings/form',+ <%= render partial: '/storages/project_settings/project_folder_form',locals: { f: f, available_storages: @available_storages, submit_button_label: t('button_save') }%></section>diff --git a/modules/storages/app/views/storages/project_settings/new.html.erb b/modules/storages/app/views/storages/project_settings/new.html.erbindex c2c8abf6ffa..cf5f8ee2b85 100644--- a/modules/storages/app/views/storages/project_settings/new.html.erb+++ b/modules/storages/app/views/storages/project_settings/new.html.erb@@ -31,14 +31,24 @@ See COPYRIGHT and LICENSE files for more details.<% local_assigns[:additional_breadcrumb] = t('storages.label_new_storage') %><%= toolbar title: t("storages.page_titles.project_settings.new") %>-<%= labelled_tabular_form_for @project_storage, url: project_settings_projects_storages_path do |f| -%>- <section class="form--section">- <% if @available_storages.any? %>- <%= render partial: '/storages/project_settings/form',- locals: { f: f, available_storages: @available_storages, submit_button_label: t('button_add') }- %>+<section class="form--section">+ <% if @available_storages.any? %>+ <% if @project_storage.storage.blank? %>+ <%= labelled_tabular_form_for @project_storage,+ url: new_project_settings_projects_storage_path,+ html: { method: :get } do |f| -%>+ <%= render partial: '/storages/project_settings/storage_select_form',+ locals: { f: f, available_storages: @available_storages, submit_button_label: t('button_continue') }+ %>+ <% end %><% else %>- <%= render partial: '/storages/project_settings/toast_all_available_storages_already_added' %>+ <%= labelled_tabular_form_for @project_storage, url: project_settings_projects_storages_path do |f| -%>+ <%= render partial: '/storages/project_settings/project_folder_form',+ locals: { f: f, submit_button_label: t('button_add') }+ %>+ <% end %><% end %>- </section>-<% end %>+ <% else %>+ <%= render partial: '/storages/project_settings/toast_all_available_storages_already_added' %>+ <% end %>+</section>diff --git a/modules/storages/config/locales/en.yml b/modules/storages/config/locales/en.ymlindex 2612a98cada..3481e45d561 100644--- a/modules/storages/config/locales/en.yml+++ b/modules/storages/config/locales/en.yml@@ -68,6 +68,7 @@ en:no_storage_set_up: "There are no file storages set up yet."no_specific_folder: "By default, each user will start at their own home folder when they upload a file."empty_project_folder_validation: "Selecting a folder is mandatory to proceed."+ not_logged_into_storage: "To select a project folder, please first login"existing_manual_folder: >You can designate an existing folder as the root folder for this project. The permissions are however not automatically managed, theadministrator needs to manually ensure relevant users have access. The selected folder can be used by multiple projects.