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
Add methods to publish and unpublish a project list based on permission
Klaus Zandersca6e583cdbdb
diff --git a/app/components/projects/index_page_header_component.html.erb b/app/components/projects/index_page_header_component.html.erbindex 88189b02fea..dff8c5e76b1 100644--- a/app/components/projects/index_page_header_component.html.erb+++ b/app/components/projects/index_page_header_component.html.erb@@ -81,6 +81,28 @@endif query.persisted?+ if can_publish?+ if query.public?+ menu.with_item(+ label: t(:button_unpublish),+ scheme: :danger,+ href: unpublish_projects_query_path(query),+ content_arguments: { data: { method: :post } }+ ) do |item|+ item.with_leading_visual_icon(icon: 'eye-closed')+ end+ else+ menu.with_item(+ label: t(:button_publish),+ scheme: :default,+ href: publish_projects_query_path(query),+ content_arguments: { data: { method: :post } }+ ) do |item|+ item.with_leading_visual_icon(icon: 'eye')+ end+ end+ end+menu.with_item(label: t(:button_delete),scheme: :danger,@@ -92,7 +114,6 @@endend%>-<%= render(Projects::ConfigureViewModalComponent.new(query:)) %><%= render(Projects::DeleteListModalComponent.new(query:)) if query.persisted? %><%= render(Projects::ExportListModalComponent.new(query:)) %>diff --git a/app/components/projects/index_page_header_component.rb b/app/components/projects/index_page_header_component.rbindex fc7f6939bef..15f19a7ba0c 100644--- a/app/components/projects/index_page_header_component.rb+++ b/app/components/projects/index_page_header_component.rb@@ -74,6 +74,10 @@ class Projects::IndexPageHeaderComponent < ApplicationComponentdef can_rename? = may_save_as? && query.persisted? && query.user == current_user && !query.changed?+ def can_publish?+ current_user.allowed_globally?(:manage_public_project_queries) && query.persisted?+ end+def show_state?state == :showenddiff --git a/app/components/projects/row_component.rb b/app/components/projects/row_component.rbindex 6dd80c99c2c..11b55719c5a 100644--- a/app/components/projects/row_component.rb+++ b/app/components/projects/row_component.rb@@ -46,19 +46,19 @@ module Projectsdef favoredrender(Primer::Beta::IconButton.new(- icon: currently_favored? ? "star-fill" : "star",- scheme: :invisible,- mobile_icon: currently_favored? ? "star-fill" : "star",- size: :medium,- tag: :a,- tooltip_direction: :e,- href: helpers.build_favorite_path(project, format: :html),- data: { method: currently_favored? ? :delete : :post },- classes: currently_favored? ? "op-primer--star-icon " : "op-project-row-component--favorite",- label: currently_favored? ? I18n.t(:button_unfavorite) : I18n.t(:button_favorite),- aria: { label: currently_favored? ? I18n.t(:button_unfavorite) : I18n.t(:button_favorite) },- test_selector: 'project-list-favorite-button'- ))+ icon: currently_favored? ? "star-fill" : "star",+ scheme: :invisible,+ mobile_icon: currently_favored? ? "star-fill" : "star",+ size: :medium,+ tag: :a,+ tooltip_direction: :e,+ href: helpers.build_favorite_path(project, format: :html),+ data: { method: currently_favored? ? :delete : :post },+ classes: currently_favored? ? "op-primer--star-icon " : "op-project-row-component--favorite",+ label: currently_favored? ? I18n.t(:button_unfavorite) : I18n.t(:button_favorite),+ aria: { label: currently_favored? ? I18n.t(:button_unfavorite) : I18n.t(:button_favorite) },+ test_selector: "project-list-favorite-button"+ ))enddef currently_favored?@@ -197,7 +197,7 @@ module Projectsdef additional_css_class(column)if column.attribute == :name"project--hierarchy #{project.archived? ? 'archived' : ''}"- elsif [:status_explanation, :description].include?(column.attribute)+ elsif %i[status_explanation description].include?(column.attribute)"project-long-text-container"elsif custom_field_column?(column)cf = column.custom_field@@ -254,7 +254,7 @@ module Projectshref: helpers.build_favorite_path(project, format: :html),data: { method: :post },label: I18n.t(:button_favorite),- aria: { label: I18n.t(:button_favorite) },+ aria: { label: I18n.t(:button_favorite) }}end@@ -269,7 +269,7 @@ module Projectsdata: { method: :delete },classes: "op-primer--star-icon",label: I18n.t(:button_unfavorite),- aria: { label: I18n.t(:button_unfavorite) },+ aria: { label: I18n.t(:button_unfavorite) }}end@@ -302,7 +302,7 @@ module Projectsscheme: :default,icon: :check,label: I18n.t(:label_project_activity),- href: project_activity_index_path(project, event_types: ["project_attributes"]),+ href: project_activity_index_path(project, event_types: ["project_attributes"])}endend@@ -317,7 +317,7 @@ module Projectsdata: {confirm: t("project.archive.are_you_sure", name: project.name),method: :post- },+ }}endend@@ -340,7 +340,7 @@ module Projectsscheme: :default,icon: :copy,label: I18n.t(:button_copy),- href: copy_project_path(project),+ href: copy_project_path(project)}endend@@ -351,7 +351,7 @@ module Projectsscheme: :danger,icon: :trash,label: I18n.t(:button_delete),- href: confirm_destroy_project_path(project),+ href: confirm_destroy_project_path(project)}endend@@ -361,7 +361,7 @@ module Projectsenddef custom_field_column?(column)- column.is_a?(Queries::Projects::Selects::CustomField)+ column.is_a?(::Queries::Projects::Selects::CustomField)endendenddiff --git a/app/components/projects/table_component.rb b/app/components/projects/table_component.rbindex 3c053819422..a2dea976cff 100644--- a/app/components/projects/table_component.rb+++ b/app/components/projects/table_component.rb@@ -64,11 +64,18 @@ module Projects# We don't return the project row# but the [project, level] array from the helperdef rows+<<<<<<< HEAD@rows ||=beginprojects_enumerator = ->(model) { to_enum(:projects_with_levels_order_sensitive, model).to_a }instance_exec(model, &projects_enumerator)end+=======+ @rows ||= begin+ projects_enumerator = ->(model) { to_enum(:projects_with_levels_order_sensitive, model).to_a }+ instance_exec(model, &projects_enumerator)+ end+>>>>>>> 0e1a80b408 (Add methods to publish and unpublish a project list based on permission)enddef initialize_sorted_model@@ -113,12 +120,11 @@ module Projectsenddef columns- @columns ||=- begin- columns = query.selects.reject { |select| select.is_a?(Queries::Selects::NotExistingSelect) }+ @columns ||= begin+ columns = query.selects.reject { |select| select.is_a?(::Queries::Selects::NotExistingSelect) }- index = columns.index { |column| column.attribute == :name }- columns.insert(index, Queries::Projects::Selects::Default.new(:hierarchy)) if index+ index = columns.index { |column| column.attribute == :name }+ columns.insert(index, ::Queries::Projects::Selects::Default.new(:hierarchy)) if indexcolumnsend@@ -156,7 +162,7 @@ module Projectsenddef favored_project_ids- @favored_projects ||= Favorite.where(user: current_user, favored_type: 'Project').pluck(:favored_id)+ @favored_projects ||= Favorite.where(user: current_user, favored_type: "Project").pluck(:favored_id)enddef sorted_by_lft?diff --git a/app/contracts/projects/queries/publish_contract.rb b/app/contracts/projects/queries/publish_contract.rbnew file mode 100644index 00000000000..ccf53f5ff9b--- /dev/null+++ b/app/contracts/projects/queries/publish_contract.rb@@ -0,0 +1,39 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2024 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 Projects::Queries::PublishContract < BaseContract+ validate :allowed_to_modify_public_flag++ protected++ def allowed_to_modify_public_flag+ unless user.allowed_globally?(:manage_public_project_queries)+ errors.add :base, :error_unauthorized+ end+ end+enddiff --git a/app/controllers/projects/queries_controller.rb b/app/controllers/projects/queries_controller.rbindex 273733778fe..23439331915 100644--- a/app/controllers/projects/queries_controller.rb+++ b/app/controllers/projects/queries_controller.rb@@ -31,7 +31,7 @@ class Projects::QueriesController < ApplicationController# No need for a more specific authorization check. That is carried out in the contracts.before_action :require_login- before_action :find_query, only: %i[rename update destroy]+ before_action :find_query, only: %i[rename update destroy publish unpublish]before_action :build_query_or_deny_access, only: %i[new create]current_menu_item [:new, :rename, :create, :update] do@@ -55,17 +55,7 @@ class Projects::QueriesController < ApplicationController.new(from: @query, user: current_user).call(permitted_query_params)- if call.success?- flash[:notice] = I18n.t("lists.create.success")-- redirect_to projects_path(query_id: call.result.id)- else- flash[:error] = I18n.t("lists.create.failure", errors: call.errors.full_messages.join("\n"))-- render template: "/projects/index",- layout: "global",- locals: { query: call.result, state: :edit }- end+ render_result(call, success_i18n_key: "lists.create.success", error_i18n_key: "lists.create.failure")enddef update@@ -73,17 +63,23 @@ class Projects::QueriesController < ApplicationController.new(user: current_user, model: @query).call(permitted_query_params)- if call.success?- flash[:notice] = I18n.t("lists.update.success")+ render_result(call, success_i18n_key: "lists.update.success", error_i18n_key: "lists.update.failure")+ end- redirect_to projects_path(query_id: call.result.id)- else- flash[:error] = I18n.t("lists.update.failure", errors: call.errors.full_messages.join("\n"))+ def publish+ call = Queries::Projects::ProjectQueries::PublishService+ .new(user: current_user, model: @query)+ .call(public: true)- render template: "/projects/index",- layout: "global",- locals: { query: call.result, state: :edit }- end+ render_result(call, success_i18n_key: "lists.publish.success", error_i18n_key: "lists.publish.failure")+ end++ def unpublish+ call = Queries::Projects::ProjectQueries::PublishService+ .new(user: current_user, model: @query)+ .call(public: false)++ render_result(call, success_i18n_key: "lists.unpublish.success", error_i18n_key: "lists.unpublish.failure")enddef destroy@@ -95,6 +91,20 @@ class Projects::QueriesController < ApplicationControllerprivate+ def render_result(service_call, success_i18n_key:, error_i18n_key:)+ if service_call.success?+ flash[:notice] = I18n.t(success_i18n_key)++ redirect_to projects_path(query_id: service_call.result.id)+ else+ flash[:error] = I18n.t(error_i18n_key, errors: service_call.errors.full_messages.join("\n"))++ render template: "/projects/index",+ layout: "global",+ locals: { query: service_call.result, state: :edit }+ end+ end+def find_query@query = Queries::Projects::ProjectQuery.find(params[:id])enddiff --git a/app/controllers/projects/query_loading.rb b/app/controllers/projects/query_loading.rbindex 1d0edfa44ea..c7679d9b9b8 100644--- a/app/controllers/projects/query_loading.rb+++ b/app/controllers/projects/query_loading.rb@@ -30,10 +30,10 @@ module Projectsprivatedef load_query(duplicate:)- Queries::Projects::Factory.find(params[:query_id],- params: permitted_query_params,- user: current_user,- duplicate:)+ ::Queries::Projects::Factory.find(params[:query_id],+ params: permitted_query_params,+ user: current_user,+ duplicate:)enddef load_query_or_deny_access@@ -55,7 +55,7 @@ module Projectsquery_params.merge!(params.require(:query).permit(:name))end- query_params.merge!(Queries::ParamsParser.parse(params))+ query_params.merge!(::Queries::ParamsParser.parse(params))query_params.with_indifferent_accessenddiff --git a/app/services/queries/projects/project_queries/publish_service.rb b/app/services/queries/projects/project_queries/publish_service.rbnew file mode 100644index 00000000000..ad68a1bc45d--- /dev/null+++ b/app/services/queries/projects/project_queries/publish_service.rb@@ -0,0 +1,50 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2024 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 Queries::Projects::ProjectQueries::PublishService < BaseServices::BaseContracted+ include Contracted++ def initialize(user:, model:, contract_class: Projects::Queries::PublishContract)+ super(user:, contract_class:)+ self.model = model+ end++ private++ def after_validate(params, service_call)+ model.public = params[:public]++ service_call+ end++ def persist(service_call)+ model.save++ service_call+ end+enddiff --git a/config/routes.rb b/config/routes.rbindex 7fda512c194..80c29d6a84e 100644--- a/config/routes.rb+++ b/config/routes.rb@@ -184,6 +184,9 @@ Rails.application.routes.draw doresources :queries, only: %i[new create update destroy] domember doget :rename++ post :publish+ post :unpublishendendend