2019-11-07 15:51:43 +01:00
|
|
|
#-- copyright
|
2020-01-15 11:31:26 +01:00
|
|
|
# OpenProject is an open source project management software.
|
2024-07-30 13:42:36 +02:00
|
|
|
# Copyright (C) the OpenProject GmbH
|
2019-11-07 15:51:43 +01:00
|
|
|
#
|
|
|
|
|
# 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:
|
2021-01-13 17:47:45 +01:00
|
|
|
# Copyright (C) 2006-2013 Jean-Philippe Lang
|
2019-11-07 15:51:43 +01:00
|
|
|
# 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.
|
|
|
|
|
#
|
2021-09-02 21:49:06 +02:00
|
|
|
# See COPYRIGHT and LICENSE files for more details.
|
2019-11-07 15:51:43 +01:00
|
|
|
#++
|
|
|
|
|
|
|
|
|
|
# Root class of the API
|
|
|
|
|
# This is the place for all API wide configuration, helper methods, exceptions
|
2021-07-21 11:26:31 -04:00
|
|
|
# rescuing, mounting of different API versions etc.
|
2019-11-07 15:51:43 +01:00
|
|
|
|
|
|
|
|
require "open_project/authentication"
|
|
|
|
|
|
2019-11-08 11:37:18 +01:00
|
|
|
module API
|
|
|
|
|
class RootAPI < Grape::API
|
|
|
|
|
include OpenProject::Authentication::Scope
|
2022-05-31 13:47:40 +02:00
|
|
|
include ::API::AppsignalAPI
|
2019-11-08 11:37:18 +01:00
|
|
|
extend API::Utilities::GrapeHelper
|
2019-11-07 15:51:43 +01:00
|
|
|
|
2021-09-17 10:43:18 +02:00
|
|
|
insert_before Grape::Middleware::Error,
|
|
|
|
|
::GrapeLogging::Middleware::RequestLogger,
|
2025-09-22 17:58:43 +02:00
|
|
|
{ instrumentation_key: "openproject_grape_logger",
|
|
|
|
|
include: [API::Utilities::Loggers::EndpointName.new] }
|
2021-09-17 10:43:18 +02:00
|
|
|
|
2019-11-11 10:22:52 +01:00
|
|
|
content_type :json, "application/json; charset=utf-8"
|
2019-11-07 15:51:43 +01:00
|
|
|
|
2019-11-08 11:37:18 +01:00
|
|
|
helpers API::Caching::Helpers
|
2022-05-19 18:10:27 +02:00
|
|
|
module Helpers
|
2024-11-04 16:17:01 +01:00
|
|
|
include ::API::Helpers::RaiseQueryErrors
|
|
|
|
|
|
2019-11-07 15:51:43 +01:00
|
|
|
def current_user
|
|
|
|
|
User.current
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def warden
|
|
|
|
|
env["warden"]
|
|
|
|
|
end
|
|
|
|
|
|
2019-11-08 11:37:18 +01:00
|
|
|
##
|
|
|
|
|
# Helper to access only the declared
|
|
|
|
|
# params to avoid unvalidated access
|
|
|
|
|
# (e.g., in before blocks)
|
|
|
|
|
def declared_params
|
|
|
|
|
declared(params)
|
|
|
|
|
end
|
|
|
|
|
|
2019-11-07 15:51:43 +01:00
|
|
|
def request_body
|
|
|
|
|
env["api.request.body"]
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def authenticate
|
2020-03-24 16:17:54 +01:00
|
|
|
User.current = warden.authenticate! scope: authentication_scope
|
2019-11-07 15:51:43 +01:00
|
|
|
|
2023-04-19 15:52:48 +02:00
|
|
|
if Setting.login_required? && !logged_in? && !allowed_unauthenticated_route?
|
2019-11-07 15:51:43 +01:00
|
|
|
raise ::API::Errors::Unauthenticated
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2023-04-19 15:52:48 +02:00
|
|
|
def allowed_unauthenticated_route?
|
|
|
|
|
false
|
|
|
|
|
end
|
|
|
|
|
|
2019-11-07 15:51:43 +01:00
|
|
|
def set_localization
|
|
|
|
|
SetLocalizationService.new(User.current, env["HTTP_ACCEPT_LANGUAGE"]).call
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# Global helper to set allowed content_types
|
2021-07-21 11:26:31 -04:00
|
|
|
# This may be overridden when multipart is allowed (file uploads)
|
2019-11-07 15:51:43 +01:00
|
|
|
def allowed_content_types
|
2019-11-08 11:37:18 +01:00
|
|
|
%w(application/json application/hal+json)
|
2019-11-07 15:51:43 +01:00
|
|
|
end
|
|
|
|
|
|
2024-02-20 22:03:18 +01:00
|
|
|
# Prevent committing the session
|
|
|
|
|
# This prevents an unnecessary write when accessing the API
|
|
|
|
|
def skip_session_write
|
|
|
|
|
request.session_options[:skip] = true
|
|
|
|
|
end
|
|
|
|
|
|
2019-11-07 15:51:43 +01:00
|
|
|
def enforce_content_type
|
2024-01-15 11:40:40 -05:00
|
|
|
# Content-Type is not present in GET or DELETE requests
|
|
|
|
|
return if request.get? || request.delete?
|
2019-11-07 15:51:43 +01:00
|
|
|
|
|
|
|
|
# Raise if missing header
|
|
|
|
|
content_type = request.content_type
|
2022-06-20 09:12:06 +02:00
|
|
|
error!("Missing content-type header", 406, { "Content-Type" => "text/plain" }) if content_type.blank?
|
2019-11-07 15:51:43 +01:00
|
|
|
|
|
|
|
|
# Allow JSON and JSON+HAL per default
|
|
|
|
|
# and anything that each endpoint may optionally add to that
|
|
|
|
|
if content_type.present?
|
|
|
|
|
allowed_content_types.each do |mime|
|
|
|
|
|
# Content-Type header looks like this (e.g.,)
|
|
|
|
|
# application/json;encoding=utf8
|
|
|
|
|
return if content_type.start_with?(mime)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
bad_type = content_type.presence || I18n.t("api_v3.errors.missing_content_type")
|
|
|
|
|
message = I18n.t("api_v3.errors.invalid_content_type",
|
|
|
|
|
content_type: allowed_content_types.join(" "),
|
|
|
|
|
actual: bad_type)
|
|
|
|
|
|
|
|
|
|
fail ::API::Errors::UnsupportedMediaType, message
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def logged_in?
|
|
|
|
|
# An admin SystemUser is anonymous but still a valid user to be logged in.
|
|
|
|
|
current_user && (current_user.admin? || !current_user.anonymous?)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def authorize_by_with_raise(callable)
|
|
|
|
|
is_authorized = callable.respond_to?(:call) ? callable.call : callable
|
|
|
|
|
|
|
|
|
|
return true if is_authorized
|
|
|
|
|
|
|
|
|
|
if block_given?
|
|
|
|
|
yield
|
|
|
|
|
else
|
|
|
|
|
raise API::Errors::Unauthorized
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
false
|
|
|
|
|
end
|
|
|
|
|
|
2023-10-31 08:43:44 +01:00
|
|
|
# Checks that the current user has the given permission on the given project or raise {API::Errors::Unauthorized}.
|
|
|
|
|
#
|
|
|
|
|
# @param permission_or_permissions [String, [String], Hash] the permission name, an array of permissions or a hash
|
|
|
|
|
# with controller and action keys. When an array of permissions is given, the user needs to have at least one of
|
|
|
|
|
# those permissions, not all.
|
|
|
|
|
#
|
|
|
|
|
# @param project [Project] the project the permission needs to be checked on
|
|
|
|
|
#
|
|
|
|
|
# @raise [API::Errors::Unauthorized] when permission is not met
|
2024-03-01 18:17:35 -05:00
|
|
|
def authorize_in_project(permission_or_permissions, project:, user: current_user, &)
|
2023-10-30 12:36:30 +01:00
|
|
|
permissions = Array.wrap(permission_or_permissions)
|
2023-10-24 17:40:16 +02:00
|
|
|
authorized = permissions.any? do |permission|
|
|
|
|
|
user.allowed_in_project?(permission, project)
|
|
|
|
|
end
|
|
|
|
|
|
2024-03-01 18:17:35 -05:00
|
|
|
authorize_by_with_raise(authorized, &)
|
2023-10-24 17:40:16 +02:00
|
|
|
end
|
|
|
|
|
|
2023-10-31 08:43:44 +01:00
|
|
|
# Checks that the current user has the given permission in any of the given projects or raise {API::Errors::Unauthorized}.
|
|
|
|
|
#
|
|
|
|
|
# @param permission_or_permissions [String, [String], Hash] the permission name, an array of permissions or a hash
|
|
|
|
|
# with controller and action keys. When an array of permissions is given, the user needs to have at least one of
|
|
|
|
|
# those permissions, not all.
|
|
|
|
|
#
|
|
|
|
|
# @param projects [[Project]] the projects the permission needs to be checked on
|
|
|
|
|
#
|
|
|
|
|
# @raise [API::Errors::Unauthorized] when permission is not met
|
2024-03-01 18:17:35 -05:00
|
|
|
def authorize_in_projects(permission_or_permissions, projects:, user: current_user, &)
|
2023-10-24 17:40:16 +02:00
|
|
|
raise ArgumentError if projects.blank?
|
|
|
|
|
|
2023-10-30 12:36:30 +01:00
|
|
|
permissions = Array.wrap(permission_or_permissions)
|
2019-11-07 15:51:43 +01:00
|
|
|
|
|
|
|
|
projects = Array(projects)
|
|
|
|
|
|
|
|
|
|
authorized = permissions.any? do |permission|
|
2023-10-24 17:40:16 +02:00
|
|
|
allowed_projects = Project.allowed_to(user, permission)
|
2023-10-30 12:25:48 +01:00
|
|
|
projects.intersect?(allowed_projects)
|
2023-10-24 17:40:16 +02:00
|
|
|
end
|
|
|
|
|
|
2024-03-01 18:17:35 -05:00
|
|
|
authorize_by_with_raise(authorized, &)
|
2023-10-24 17:40:16 +02:00
|
|
|
end
|
|
|
|
|
|
2023-10-31 08:43:44 +01:00
|
|
|
# Checks that the current user has the given permission on any project or raise {API::Errors::Unauthorized}.
|
|
|
|
|
#
|
|
|
|
|
# @param permission_or_permissions [String, [String], Hash] the permission name, an array of permissions or a hash
|
|
|
|
|
# with controller and action keys. When an array of permissions is given, the user needs to have at least one of
|
|
|
|
|
# those permissions, not all.
|
|
|
|
|
#
|
|
|
|
|
# @raise [API::Errors::Unauthorized] when permission is not met
|
2024-03-01 18:17:35 -05:00
|
|
|
def authorize_in_any_project(permission_or_permissions, user: current_user, &)
|
2023-10-30 12:36:30 +01:00
|
|
|
permissions = Array.wrap(permission_or_permissions)
|
2023-10-24 17:40:16 +02:00
|
|
|
authorized = permissions.any? do |permission|
|
|
|
|
|
user.allowed_in_any_project?(permission)
|
|
|
|
|
end
|
|
|
|
|
|
2024-03-01 18:17:35 -05:00
|
|
|
authorize_by_with_raise(authorized, &)
|
2023-10-24 17:40:16 +02:00
|
|
|
end
|
|
|
|
|
|
2023-11-08 12:08:41 +01:00
|
|
|
# Checks that the current user has the given permission on any work package or project or raise {API::Errors::Unauthorized}.
|
|
|
|
|
#
|
|
|
|
|
# @param permission_or_permissions [String, [String], Hash] the permission name, an array of permissions or a hash
|
|
|
|
|
# with controller and action keys. When an array of permissions is given, the user needs to have at least one of
|
|
|
|
|
# those permissions, not all.
|
|
|
|
|
#
|
|
|
|
|
# @raise [API::Errors::Unauthorized] when permission is not met
|
2024-03-01 18:17:35 -05:00
|
|
|
def authorize_in_any_work_package(permission_or_permissions, user: current_user, in_project: nil, &)
|
2023-11-08 12:08:41 +01:00
|
|
|
permissions = Array.wrap(permission_or_permissions)
|
|
|
|
|
authorized = permissions.any? do |permission|
|
2023-11-15 11:22:34 +01:00
|
|
|
user.allowed_in_any_work_package?(permission, in_project:)
|
2023-11-08 12:08:41 +01:00
|
|
|
end
|
|
|
|
|
|
2024-03-01 18:17:35 -05:00
|
|
|
authorize_by_with_raise(authorized, &)
|
2023-11-08 12:08:41 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# Checks that the current user has the given permission on the given work package or raise {API::Errors::Unauthorized}.
|
|
|
|
|
#
|
|
|
|
|
# @param permission_or_permissions [String, [String], Hash] the permission name, an array of permissions or a hash
|
|
|
|
|
# with controller and action keys. When an array of permissions is given, the user needs to have at least one of
|
|
|
|
|
# those permissions, not all.
|
|
|
|
|
#
|
|
|
|
|
# @param work_package [Project] the work package the permission needs to be checked on
|
|
|
|
|
#
|
|
|
|
|
# @raise [API::Errors::Unauthorized] when permission is not met
|
2024-03-01 18:17:35 -05:00
|
|
|
def authorize_in_work_package(permission_or_permissions, work_package:, user: current_user, &)
|
2023-11-08 12:08:41 +01:00
|
|
|
permissions = Array.wrap(permission_or_permissions)
|
|
|
|
|
authorized = permissions.any? do |permission|
|
|
|
|
|
user.allowed_in_work_package?(permission, work_package)
|
|
|
|
|
end
|
|
|
|
|
|
2024-03-01 18:17:35 -05:00
|
|
|
authorize_by_with_raise(authorized, &)
|
2023-11-08 12:08:41 +01:00
|
|
|
end
|
|
|
|
|
|
2023-10-31 08:43:44 +01:00
|
|
|
# Checks that the current user has the given permission globally or raise {API::Errors::Unauthorized}.
|
|
|
|
|
#
|
|
|
|
|
# @param permission_or_permissions [String, [String], Hash] the permission name, an array of permissions or a hash
|
|
|
|
|
# with controller and action keys. When an array of permissions is given, the user needs to have at least one of
|
|
|
|
|
# those permissions, not all.
|
|
|
|
|
#
|
|
|
|
|
# @raise [API::Errors::Unauthorized] when permission is not met
|
2024-03-01 18:17:35 -05:00
|
|
|
def authorize_globally(permission_or_permissions, user: current_user, &)
|
2023-10-30 12:36:30 +01:00
|
|
|
permissions = Array.wrap(permission_or_permissions)
|
2023-10-24 17:40:16 +02:00
|
|
|
authorized = permissions.any? do |permission|
|
|
|
|
|
user.allowed_globally?(permission)
|
2019-11-07 15:51:43 +01:00
|
|
|
end
|
|
|
|
|
|
2024-03-01 18:17:35 -05:00
|
|
|
authorize_by_with_raise(authorized, &)
|
2019-11-07 15:51:43 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def authorize_admin
|
|
|
|
|
authorize_by_with_raise(current_user.admin? && (current_user.active? || current_user.is_a?(SystemUser)))
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def authorize_logged_in
|
|
|
|
|
authorize_by_with_raise((current_user.logged? && current_user.active?) || current_user.is_a?(SystemUser))
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def raise_invalid_query_on_service_failure
|
|
|
|
|
service = yield
|
|
|
|
|
|
|
|
|
|
if service.success?
|
|
|
|
|
service
|
|
|
|
|
else
|
2022-10-17 10:23:49 +02:00
|
|
|
raise_query_errors(service)
|
2019-11-07 15:51:43 +01:00
|
|
|
end
|
|
|
|
|
end
|
2025-11-25 16:28:08 +01:00
|
|
|
|
|
|
|
|
def guard_feature_flag(feature_key)
|
|
|
|
|
unless OpenProject::FeatureDecisions.public_send("#{feature_key}_active?")
|
|
|
|
|
raise API::Errors::NotFound
|
|
|
|
|
end
|
|
|
|
|
end
|
2019-11-07 15:51:43 +01:00
|
|
|
end
|
|
|
|
|
|
2022-05-19 18:10:27 +02:00
|
|
|
helpers Helpers
|
|
|
|
|
|
2019-11-07 15:51:43 +01:00
|
|
|
def self.auth_headers
|
|
|
|
|
lambda do
|
2026-02-09 16:42:49 +01:00
|
|
|
header = OpenProject::Authentication::WWWAuthenticate.response_header(scope: authentication_scope)
|
2019-11-07 15:51:43 +01:00
|
|
|
|
|
|
|
|
{ "WWW-Authenticate" => header }
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-11-13 09:18:58 +01:00
|
|
|
def self.error_representer(klass, content_type)
|
|
|
|
|
# Have the vars available in the instances via helpers.
|
|
|
|
|
helpers do
|
|
|
|
|
define_method(:error_representer, -> { klass })
|
|
|
|
|
define_method(:error_content_type, -> { content_type })
|
2019-11-11 10:22:52 +01:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-11-13 09:18:58 +01:00
|
|
|
def self.authentication_scope(sym)
|
|
|
|
|
# Have the scope available in the instances
|
|
|
|
|
# via a helper.
|
|
|
|
|
helpers do
|
|
|
|
|
define_method(:authentication_scope, -> { sym })
|
2019-11-11 16:49:22 +01:00
|
|
|
end
|
2019-11-07 15:51:43 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
error_response ActiveRecord::RecordNotFound, ::API::Errors::NotFound, log: false
|
|
|
|
|
error_response ActiveRecord::StaleObjectError, ::API::Errors::Conflict, log: false
|
2026-03-26 13:28:12 +01:00
|
|
|
|
|
|
|
|
# TODO: Where do we expect this to be raised and **not** be a programming error?
|
2019-11-20 14:02:25 +01:00
|
|
|
error_response NotImplementedError, ::API::Errors::NotImplemented, log: false
|
2019-11-07 15:51:43 +01:00
|
|
|
|
|
|
|
|
error_response MultiJson::ParseError, ::API::Errors::ParseError
|
|
|
|
|
|
|
|
|
|
error_response ::API::Errors::Unauthenticated, headers: auth_headers, log: false
|
|
|
|
|
error_response ::API::Errors::ErrorBase, rescue_subclasses: true, log: false
|
|
|
|
|
|
2019-11-08 11:37:18 +01:00
|
|
|
# Handle grape validation errors
|
|
|
|
|
error_response ::Grape::Exceptions::ValidationErrors, ::API::Errors::BadRequest, log: false
|
|
|
|
|
|
|
|
|
|
# Handle connection timeouts with appropriate payload
|
|
|
|
|
error_response ActiveRecord::ConnectionTimeoutError,
|
|
|
|
|
::API::Errors::InternalError,
|
|
|
|
|
log: ->(exception) do
|
|
|
|
|
payload = ::OpenProject::Logging::ThreadPoolContextBuilder.build!
|
|
|
|
|
::OpenProject.logger.error exception, reference: :APIv3, payload:
|
|
|
|
|
end
|
|
|
|
|
|
2019-11-07 15:51:43 +01:00
|
|
|
# hide internal errors behind the same JSON response as all other errors
|
|
|
|
|
# only doing it in production to allow for easier debugging
|
|
|
|
|
if Rails.env.production?
|
|
|
|
|
error_response StandardError, ::API::Errors::InternalError, rescue_subclasses: true
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# run authentication before each request
|
2019-11-08 11:37:18 +01:00
|
|
|
after_validation do
|
2024-02-21 20:21:54 +01:00
|
|
|
skip_session_write
|
2019-11-08 11:37:18 +01:00
|
|
|
authenticate
|
2019-11-07 15:51:43 +01:00
|
|
|
set_localization
|
|
|
|
|
enforce_content_type
|
2022-06-09 16:20:51 +02:00
|
|
|
::OpenProject::Appsignal.tag_request(request:)
|
2019-11-07 15:51:43 +01:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|