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

add portfolio creation using existing project creation

ulferts64805d3bbaea
diff --git a/app/contracts/projects/create_contract.rb b/app/contracts/projects/create_contract.rb
index ac2480932da..cabb674b91d 100644
--- a/app/contracts/projects/create_contract.rb
+++ b/app/contracts/projects/create_contract.rb
@@ -76,9 +76,22 @@ module Projects
def without_custom_fields(changes) = changes.grep_v(/^custom_field_/)
def validate_user_allowed_to_manage
+ if model.project?
+ validate_user_allowed_to_manage_projects
+ elsif model.portfolio?
+ validate_user_allowed_to_manage_portfolios
+ end
+ end
+
+ def validate_user_allowed_to_manage_portfolios
+ unless user.allowed_globally?(:add_portfolios)
+ errors.add :base, :error_unauthorized
+ end
+ end
+
+ def validate_user_allowed_to_manage_projects
unless user.allowed_globally?(:add_project) ||
(model.parent && user.allowed_in_project?(:add_subprojects, model.parent))
-
errors.add :base, :error_unauthorized
end
end
diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb
index d0298ff6d58..e1836392639 100644
--- a/app/controllers/projects_controller.rb
+++ b/app/controllers/projects_controller.rb
@@ -184,7 +184,7 @@ class ProjectsController < ApplicationController
def from_template? = @template.present?
def new_blank
- @new_project = @parent&.children&.build || Project.new
+ @new_project = @parent&.children&.build(permitted_params.new_project) || Project.new(permitted_params.new_project)
render layout: "no_menu"
end
diff --git a/app/forms/projects/settings/type_form.rb b/app/forms/projects/settings/type_form.rb
index d17fb70540e..5ae034e2095 100644
--- a/app/forms/projects/settings/type_form.rb
+++ b/app/forms/projects/settings/type_form.rb
@@ -31,8 +31,7 @@ module Projects
module Settings
class TypeForm < ApplicationForm
form do |f|
- f.hidden name: :workspace_type,
- value: Project.workspace_types[:project]
+ f.hidden name: :workspace_type
end
end
end
diff --git a/app/helpers/projects_helper.rb b/app/helpers/projects_helper.rb
index 99469be7f72..9c17262edd3 100644
--- a/app/helpers/projects_helper.rb
+++ b/app/helpers/projects_helper.rb
@@ -88,4 +88,12 @@ module ProjectsHelper
def projects_query_params
safe_query_params(PROJECTS_QUERY_PARAM_NAMES)
end
+
+ def new_workspace_title(workspace)
+ if workspace.project?
+ I18n.t(:label_project_new)
+ elsif workspace.portfolio?
+ I18n.t(:label_portfolio_new)
+ end
+ end
end
diff --git a/app/views/portfolios/new.html.erb b/app/views/portfolios/new.html.erb
new file mode 100644
index 00000000000..ef38382a099
--- /dev/null
+++ b/app/views/portfolios/new.html.erb
@@ -0,0 +1,41 @@
+<%#-- copyright
+OpenProject is an open source project management software.
+Copyright (C) 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.
+
+++#%>
+<% html_title t("label_project_new") %>
+<%=
+ render(Primer::OpenProject::PageHeader.new) do |header|
+ header.with_title { t(:label_project_new) }
+ header.with_breadcrumbs([t(:label_project_new)])
+ end
+%>
+
+<%=
+ render Projects::NewComponent.new(
+ project: @new_project
+ )
+%>
diff --git a/app/views/projects/new.html.erb b/app/views/projects/new.html.erb
index eedf76c1ad1..d492f9b7436 100644
--- a/app/views/projects/new.html.erb
+++ b/app/views/projects/new.html.erb
@@ -26,11 +26,11 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
See COPYRIGHT and LICENSE files for more details.
++#%>
-<% html_title t("label_project_new") %>
+<% html_title new_workspace_title(@new_project) %>
<%=
render(Primer::OpenProject::PageHeader.new) do |header|
- header.with_title { t(:label_project_new) }
- header.with_breadcrumbs([t(:label_project_new)])
+ header.with_title { new_workspace_title(@new_project) }
+ header.with_breadcrumbs([new_workspace_title(@new_project)])
end
%>
diff --git a/config/initializers/feature_decisions.rb b/config/initializers/feature_decisions.rb
index 7998fcd34ba..c8bb7740e27 100644
--- a/config/initializers/feature_decisions.rb
+++ b/config/initializers/feature_decisions.rb
@@ -64,3 +64,6 @@ OpenProject::FeatureDecisions.add :minutes_styling_meeting_pdf,
OpenProject::FeatureDecisions.add :scored_list_custom_fields,
description: "Enables the use of the custom field type 'Scored List', which lets " \
"the admin add scores to list items."
+
+OpenProject::FeatureDecisions.add :portfolio_models,
+ description: "Enables the creation and management of portfolio and program work spaces."
diff --git a/config/initializers/permissions.rb b/config/initializers/permissions.rb
index 76ff2c26cd6..7a14c1f70eb 100644
--- a/config/initializers/permissions.rb
+++ b/config/initializers/permissions.rb
@@ -37,6 +37,11 @@ Rails.application.reloader.to_prepare do
require: :loggedin,
contract_actions: { projects: %i[create] }
+ map.permission :add_portfolios,
+ { projects: %i[new create] },
+ permissible_on: :global,
+ require: :loggedin
+
map.permission :archive_project,
{
"projects/archive": %i[create]
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 46251b2eec9..a6781b8f55c 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -3240,6 +3240,7 @@ en:
label_project_view_all: "View all projects"
label_project_show_details: "Show project details"
label_project_hide_details: "Hide project details"
+ label_portfolio_new: "New portfolio"
label_public_projects: "Public projects"
label_query_new: "New query"
label_query_plural: "Custom queries"
diff --git a/config/routes.rb b/config/routes.rb
index 40a422bc93c..4222ef1f59a 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -261,7 +261,13 @@ Rails.application.routes.draw do
resource :menu, only: %i[show]
end
- resources :projects, except: %i[show edit update] do
+ # Extracted from the resources definition right below so that the
+ # default parameters can be defined.
+ resources :projects,
+ only: %i[new],
+ defaults: { project: { workspace_type: "project" } }
+
+ resources :projects, except: %i[new show edit update] do
scope module: "projects" do
namespace "settings" do
resource :general, only: %i[show update], controller: "general" do
@@ -467,6 +473,11 @@ Rails.application.routes.draw do
end
end
+ resources :portfolios,
+ only: %i[new],
+ defaults: { project: { workspace_type: "portfolio" } },
+ controller: "projects"
+
resources :project_phases, only: [] do
member do
get "/hover_card" => "project_phases/hover_card#show", as: "hover_card"
diff --git a/spec/contracts/projects/create_contract_spec.rb b/spec/contracts/projects/create_contract_spec.rb
index ebe663d0927..c9253a92d0d 100644
--- a/spec/contracts/projects/create_contract_spec.rb
+++ b/spec/contracts/projects/create_contract_spec.rb
@@ -44,7 +44,7 @@ RSpec.describe Projects::CreateContract do
status_explanation: project_status_explanation,
workspace_type: project_workspace_type)
end
- let(:global_permissions) { [:add_project] }
+ let(:global_permissions) { %i[add_project add_portfolios] }
let(:validated_contract) do
contract.tap(&:validate)
end
@@ -57,6 +57,26 @@ RSpec.describe Projects::CreateContract do
it_behaves_like "contract is valid"
end
+ context "when having the 'project' workspace_type and lacking the add_project permission" do
+ let(:global_permissions) { [] }
+
+ it_behaves_like "contract is invalid", base: %i(error_unauthorized)
+ end
+
+ context "when having the 'portfolio' workspace_type and having the add_portfolios permission" do
+ let(:project_workspace_type) { "portfolio" }
+ let(:global_permissions) { [:add_portfolios] }
+
+ it_behaves_like "contract is valid"
+ end
+
+ context "when having the 'portfolio' workspace_type and lacking the add_portfolios permission" do
+ let(:project_workspace_type) { "portfolio" }
+ let(:global_permissions) { [] }
+
+ it_behaves_like "contract is invalid", base: %i(error_unauthorized)
+ end
+
context "if workspace_type is nil" do
let(:project_workspace_type) { nil }
diff --git a/spec/features/portfolios/create_spec.rb b/spec/features/portfolios/create_spec.rb
index c104753cb76..e1c13bfd6e2 100644
--- a/spec/features/portfolios/create_spec.rb
+++ b/spec/features/portfolios/create_spec.rb
@@ -37,17 +37,19 @@ RSpec.describe "Portfolios",
create(:user,
global_permissions: :add_portfolios)
end
+ # Role granted to creator on portfolio creation to be able to access the portfolio.
+ shared_let(:default_project_role) { create(:project_role) }
current_user { user_with_permissions }
let(:projects_page) { Pages::Projects::Index.new }
- it "can create a project", with_flags: { portfolio_models: true } do
+ it "can create a project", with_flag: { portfolio_models: true } do
# TODO: trigger this from a button
# e.g. on the project index page
- visit new_portfolio_page_path
+ visit new_portfolio_path
- expect(page).to have_heading "New project"
+ expect(page).to have_heading "New portfolio"
fill_in "Name", with: "Foo bar"
click_on "Create"