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

[#49140] Refactor project_folder_mode default behavior.

Pavel Balashou5e9db37f04b2
diff --git a/db/migrate/20230720192054_remove_project_folder_mode_default.rb b/db/migrate/20230720192054_remove_project_folder_mode_default.rb
new file mode 100644
index 00000000000..ef071a9c867
--- /dev/null
+++ b/db/migrate/20230720192054_remove_project_folder_mode_default.rb
@@ -0,0 +1,33 @@
+#-- 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.
+#++
+
+class RemoveProjectFolderModeDefault < ActiveRecord::Migration[7.0]
+ def change
+ change_column_default :projects_storages, :project_folder_mode, to: nil, from: :inactive
+ end
+end
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 0cde972fc93..f799642c778 100644
--- a/modules/storages/app/contracts/storages/project_storages/base_contract.rb
+++ b/modules/storages/app/contracts/storages/project_storages/base_contract.rb
@@ -52,10 +52,6 @@ module Storages::ProjectStorages
end
end
- def assignable_storages
- Storages::Storage.visible(user).where.not(id: @model.project.projects_storages.pluck(:storage_id))
- end
-
private
def project_folder_mode_manual?
diff --git a/modules/storages/app/controllers/storages/admin/projects_storages_controller.rb b/modules/storages/app/controllers/storages/admin/projects_storages_controller.rb
index ea9ab575708..3503e3715b0 100644
--- a/modules/storages/app/controllers/storages/admin/projects_storages_controller.rb
+++ b/modules/storages/app/controllers/storages/admin/projects_storages_controller.rb
@@ -61,23 +61,16 @@ 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 Storages
def new
+ @available_storages = available_storages
@project_storage = ::Storages::ProjectStorages::SetAttributesService
.new(user: current_user,
model: Storages::ProjectStorage.new,
contract_class: EmptyContract)
- .call(project: @project)
+ .call(project: @project,
+ storage: @available_storages.find_by(id: params.dig(:storages_project_storage, :storage_id)))
.result
-
- @available_storages = available_storages
@last_project_folders = {}
- storage_id = params.dig(:storages_project_storage, :storage_id)
- if storage_id.present?
- storage = available_storages.find_by(id: storage_id)
- @project_storage.storage = storage
- @project_storage.project_folder_mode = "automatic" if storage.automatically_managed?
- end
-
render template: '/storages/project_settings/new'
end
@@ -167,6 +160,6 @@ class Storages::Admin::ProjectsStoragesController < Projects::SettingsController
end
def available_storages
- Storages::ProjectStorages::CreateContract.new(@project_storage, current_user).assignable_storages
+ Storages::Storage.visible.not_enabled_for_project(@project)
end
end
diff --git a/modules/storages/app/models/storages/storage.rb b/modules/storages/app/models/storages/storage.rb
index 49e717d3a56..d35d106dd0a 100644
--- a/modules/storages/app/models/storages/storage.rb
+++ b/modules/storages/app/models/storages/storage.rb
@@ -68,7 +68,7 @@ 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) {
+ scope :visible, ->(user = User.current) do
if user.allowed_to_globally?(:manage_storages_in_project)
all
else
@@ -78,7 +78,11 @@ class Storages::Storage < ApplicationRecord
)
)
end
- }
+ end
+
+ scope :not_enabled_for_project, ->(project) do
+ where.not(id: project.projects_storages.pluck(:storage_id))
+ end
def self.shorten_provider_type(provider_type)
case /Storages::(?'provider_name'.*)Storage/.match(provider_type)
diff --git a/modules/storages/app/services/storages/project_storages/set_attributes_service.rb b/modules/storages/app/services/storages/project_storages/set_attributes_service.rb
index eaff48a35b6..a24a643a6ad 100644
--- a/modules/storages/app/services/storages/project_storages/set_attributes_service.rb
+++ b/modules/storages/app/services/storages/project_storages/set_attributes_service.rb
@@ -31,6 +31,13 @@ module Storages::ProjectStorages
class SetAttributesService < ::BaseServices::SetAttributes
def set_default_attributes(_params)
model.creator ||= user
+
+ model.project_folder_mode ||=
+ if model.storage.present? && model.storage.automatically_managed?
+ "automatic"
+ else
+ "inactive"
+ end
end
end
end
diff --git a/modules/storages/spec/contracts/storages/project_storages/create_contract_spec.rb b/modules/storages/spec/contracts/storages/project_storages/create_contract_spec.rb
index 4c1b9d2dd91..4e7a5d7116a 100644
--- a/modules/storages/spec/contracts/storages/project_storages/create_contract_spec.rb
+++ b/modules/storages/spec/contracts/storages/project_storages/create_contract_spec.rb
@@ -38,7 +38,8 @@ RSpec.describe Storages::ProjectStorages::CreateContract do
# current_user, project, storage and other objects defined in the shared_contract_examples
# that includes all the stuff shared between create and update.
let(:project_storage) do
- Storages::ProjectStorage.new(
+ build(
+ :project_storage,
creator: current_user,
project:,
storage:
diff --git a/modules/storages/spec/features/manage_project_storage_spec.rb b/modules/storages/spec/features/manage_project_storage_spec.rb
index 5f11348acf3..b20960579c8 100644
--- a/modules/storages/spec/features/manage_project_storage_spec.rb
+++ b/modules/storages/spec/features/manage_project_storage_spec.rb
@@ -31,7 +31,13 @@ require_relative '../spec_helper'
# Setup storages in Project -> Settings -> File Storages
# This tests assumes that a Storage has already been setup
# in the Admin section, tested by admin_storage_spec.rb.
-RSpec.describe 'Activation of storages in projects', js: true, webmock: true, with_flag: { storage_project_folders: true } do
+RSpec.describe(
+ 'Activation of storages in projects',
+ js: true,
+ webmock: true,
+ with_flag: { storage_project_folders: true,
+ managed_project_folders: true }
+) do
let(:user) { create(:user) }
# The first page is the Project -> Settings -> General page, so we need
# to provide the user with the edit_project permission in the role.
@@ -42,7 +48,7 @@ RSpec.describe 'Activation of storages in projects', js: true, webmock: true, wi
edit_project])
end
let(:oauth_application) { create(:oauth_application) }
- let(:storage) { create(:nextcloud_storage, oauth_application:) }
+ let(:storage) { create(:nextcloud_storage, :as_automatically_managed, oauth_application:) }
let(:project) do
create(:project,
members: { user => role },
@@ -112,6 +118,9 @@ RSpec.describe 'Activation of storages in projects', js: true, webmock: true, wi
expect(page).to have_select('storages_project_storage_storage_id', options: ['Storage 1 (nextcloud)'])
page.click_button('Continue')
+ # by default automatic have to be choosen if storage has automatic management enabled
+ expect(page).to have_checked_field("New folder with automatically managed permissions")
+
page.find_by_id('storages_project_storage_project_folder_mode_manual').click
# Select project folder
diff --git a/modules/storages/spec/models/project_storage_spec.rb b/modules/storages/spec/models/project_storage_spec.rb
index ee734bc2803..2c4ab2ef23b 100644
--- a/modules/storages/spec/models/project_storage_spec.rb
+++ b/modules/storages/spec/models/project_storage_spec.rb
@@ -36,7 +36,8 @@ RSpec.describe Storages::ProjectStorage do
{
storage:,
creator:,
- project:
+ project:,
+ project_folder_mode: :inactive
}
end