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

Create BCF projects extension GET endpoint

Oliver Günther6778cd1de93d
diff --git a/modules/bcf/app/controllers/bcf/api/v2_1/project_extensions/api.rb b/modules/bcf/app/controllers/bcf/api/v2_1/project_extensions/api.rb
new file mode 100644
index 00000000000..f33a20811d6
--- /dev/null
+++ b/modules/bcf/app/controllers/bcf/api/v2_1/project_extensions/api.rb
@@ -0,0 +1,41 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is a project management system.
+# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)
+#
+# 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-2017 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 docs/COPYRIGHT.rdoc for more details.
+#++
+
+module Bcf::API::V2_1
+ module ProjectExtensions
+ class API < ::API::OpenProjectAPI
+
+ get :extensions do
+ mapper = Definitions.new(project: @project, user: current_user)
+ Representer.new(mapper)
+ end
+ end
+ end
+end
diff --git a/modules/bcf/app/controllers/bcf/api/v2_1/projects_api.rb b/modules/bcf/app/controllers/bcf/api/v2_1/projects_api.rb
index 06336eb7eb8..17d4e65b38d 100644
--- a/modules/bcf/app/controllers/bcf/api/v2_1/projects_api.rb
+++ b/modules/bcf/app/controllers/bcf/api/v2_1/projects_api.rb
@@ -53,6 +53,7 @@ module Bcf::API::V2_1
put &::Bcf::API::V2_1::Endpoints::Update.new(model: Project).mount
mount Bcf::API::V2_1::TopicsAPI
+ mount Bcf::API::V2_1::ProjectExtensions::API
end
end
end
diff --git a/modules/bcf/app/representers/bcf/api/v2_1/project_extensions/definitions.rb b/modules/bcf/app/representers/bcf/api/v2_1/project_extensions/definitions.rb
new file mode 100644
index 00000000000..649f06ce52c
--- /dev/null
+++ b/modules/bcf/app/representers/bcf/api/v2_1/project_extensions/definitions.rb
@@ -0,0 +1,115 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is a project management system.
+# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)
+#
+# 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-2017 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 docs/COPYRIGHT.rdoc for more details.
+#++
+
+module Bcf::API::V2_1
+ module ProjectExtensions
+ class Definitions
+ def initialize(project:, user:)
+ @project = project
+ @user = user
+ @allowed_service = ::Authorization::UserAllowedService.new(user)
+ end
+
+ def topic_type
+ OpenProject::Cache.fetch(project, :topic_type) do
+ project.types.pluck(:name)
+ end
+ end
+
+ # TODO: This returns all statuses regardless of workflow
+ def topic_status
+ OpenProject::Cache.fetch(Status.all.cache_key) do
+ Status.all.pluck(:name)
+ end
+ end
+
+ def priority
+ OpenProject::Cache.fetch(IssuePriority.all.cache_key) do
+ IssuePriority.all.pluck(:name)
+ end
+ end
+
+ def user_id_type
+ if allowed?(:view_members)
+ project.users.pluck(:mail)
+ else
+ []
+ end
+ end
+
+ # TODO: Labels do not yet exist
+ def topic_label
+ []
+ end
+
+ # TODO: Stage do not yet exist
+ def stage
+ []
+ end
+
+ # TODO: Snippet types do not exist
+ def snippet_type
+ []
+ end
+
+ def project_actions
+ [].tap do |actions|
+ actions << 'update' if allowed?(:edit_project)
+ actions << 'createTopic' if allowed?(:manage_bcf)
+ end
+ end
+
+ def topic_actions
+ if allowed?(:manage_bcf)
+ %w[update updateRelatedTopics updateFiles createComment createViewpoint]
+ else
+ []
+ end
+ end
+
+ def comment_actions
+ if allowed?(:manage_bcf)
+ %w[update]
+ else
+ []
+ end
+ end
+
+ private
+
+ attr_reader :project, :user, :allowed_service
+
+ def allowed?(permission)
+ allowed_service.call(permission, project).result
+ end
+ end
+ end
+end
+
diff --git a/modules/bcf/app/representers/bcf/api/v2_1/project_extensions/representer.rb b/modules/bcf/app/representers/bcf/api/v2_1/project_extensions/representer.rb
new file mode 100644
index 00000000000..f3461f4de9b
--- /dev/null
+++ b/modules/bcf/app/representers/bcf/api/v2_1/project_extensions/representer.rb
@@ -0,0 +1,44 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is a project management system.
+# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)
+#
+# 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-2017 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 docs/COPYRIGHT.rdoc for more details.
+#++
+
+module Bcf::API::V2_1
+ class ProjectExtensions::Representer < BaseRepresenter
+ property :topic_type
+ property :topic_status
+ property :topic_label
+ property :snippet_type
+ property :priority
+ property :user_id_type
+ property :stage
+ property :project_actions
+ property :topic_actions
+ property :comment_actions
+ end
+end
diff --git a/modules/bcf/spec/requests/api/bcf/v2_1/project_extensions_api_spec.rb b/modules/bcf/spec/requests/api/bcf/v2_1/project_extensions_api_spec.rb
new file mode 100644
index 00000000000..603fad1085a
--- /dev/null
+++ b/modules/bcf/spec/requests/api/bcf/v2_1/project_extensions_api_spec.rb
@@ -0,0 +1,99 @@
+#-- copyright
+# OpenProject is a project management system.
+# Copyright (C) 2012-2019 the OpenProject Foundation (OPF)
+#
+# 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-2017 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 docs/COPYRIGHT.rdoc for more details.
+#++
+
+require 'spec_helper'
+require 'rack/test'
+
+require_relative './shared_responses'
+
+describe 'BCF 2.1 project extensions resource', type: :request, content_type: :json do
+ include Rack::Test::Methods
+ shared_let(:type_task) { FactoryBot.create :type_task }
+ shared_let(:status) { FactoryBot.create :default_status }
+ shared_let(:project) { FactoryBot.create(:project, enabled_module_names: [:bcf], types: [type_task]) }
+ subject(:response) { JSON.parse(last_response.body) }
+
+ let(:path) { "/api/bcf/2.1/projects/#{project.id}/extensions" }
+
+ context 'with only view_project permissions' do
+ let(:current_user) do
+ FactoryBot.create(:user,
+ member_in_project: project,
+ member_with_permissions: [:view_project])
+ end
+
+ before do
+ login_as(current_user)
+ get path
+ end
+
+ it 'outputs read-only data', :aggregate_failures do
+ expect(response['topic_type']).to include type_task.name
+ expect(response['topic_status']).to include status.name
+
+ expect(response['user_id_type']).to be_empty
+
+ expect(response['project_actions']).to be_empty
+ expect(response['topic_actions']).to be_empty
+ expect(response['comment_actions']).to be_empty
+ end
+ end
+
+ context 'with edit permissions in project' do
+ let(:current_user) do
+ FactoryBot.create(:user,
+ member_in_project: project,
+ member_with_permissions: [:view_project, :edit_project, :manage_bcf, :view_members])
+ end
+
+ let(:other_user) {
+ FactoryBot.create(:user,
+ member_in_project: project,
+ member_with_permissions: [:view_project])
+ }
+
+ before do
+ other_user
+ login_as(current_user)
+ get path
+ end
+
+ it 'outputs all actions' do
+ expect(response['topic_type']).to include type_task.name
+ expect(response['topic_status']).to include status.name
+
+ expect(response['user_id_type']).to include(current_user.mail)
+ expect(response['user_id_type']).to include(other_user.mail)
+
+ expect(response['project_actions']).to eq %w[update createTopic]
+
+ expect(response['topic_actions']).to eq %w[update updateRelatedTopics updateFiles createComment createViewpoint]
+ expect(response['comment_actions']).to eq %w[update]
+ end
+ end
+end