2025-07-18 17:36:37 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2011-05-29 13:11:52 -07: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
|
2011-05-30 20:52:25 +02:00
|
|
|
#
|
2011-05-29 13:11:52 -07:00
|
|
|
# This program is free software; you can redistribute it and/or
|
2013-06-05 16:27:56 +02:00
|
|
|
# modify it under the terms of the GNU General Public License version 3.
|
2011-05-30 20:52:25 +02:00
|
|
|
#
|
2013-09-16 17:59:31 +02:00
|
|
|
# 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
|
2013-09-16 17:59:31 +02: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.
|
2011-05-29 13:11:52 -07:00
|
|
|
#++
|
|
|
|
|
|
2014-08-14 11:50:58 +02:00
|
|
|
require "open_project/repository_authentication"
|
|
|
|
|
|
2007-04-01 19:43:59 +00:00
|
|
|
class SysController < ActionController::Base
|
2024-06-25 08:42:12 +02:00
|
|
|
include Security::DefaultUrlOptions
|
|
|
|
|
|
2016-09-06 15:40:49 +02:00
|
|
|
before_action :check_enabled
|
|
|
|
|
before_action :require_basic_auth, only: [:repo_auth]
|
2007-04-01 19:43:59 +00:00
|
|
|
|
2025-04-22 14:48:28 +02:00
|
|
|
# disable CSRF protection since the Sys API does not use sessions
|
|
|
|
|
skip_before_action :verify_authenticity_token
|
|
|
|
|
|
2014-07-15 16:19:14 +02:00
|
|
|
def repo_auth
|
2014-07-21 16:04:41 +02:00
|
|
|
project = Project.find_by(identifier: params[:repository])
|
2015-08-11 15:04:52 +02:00
|
|
|
if project && authorized?(project, @authenticated_user)
|
2016-09-12 10:15:27 +02:00
|
|
|
render plain: "Access granted"
|
2014-07-21 16:04:41 +02:00
|
|
|
else
|
2016-09-12 10:15:27 +02:00
|
|
|
render plain: "Not allowed", status: :forbidden # default to deny
|
2014-07-15 16:19:14 +02:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2024-12-06 09:14:42 +01:00
|
|
|
def fetch_changesets
|
|
|
|
|
projects = []
|
|
|
|
|
if params[:id]
|
|
|
|
|
projects << Project.active.has_module(:repository).find_by!(identifier: params[:id])
|
|
|
|
|
else
|
|
|
|
|
projects = Project.active.has_module(:repository)
|
|
|
|
|
.includes(:repository).references(:repositories)
|
|
|
|
|
end
|
|
|
|
|
projects.each do |project|
|
|
|
|
|
if project.repository
|
|
|
|
|
project.repository.fetch_changesets
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
head :ok
|
|
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
|
head :not_found
|
|
|
|
|
end
|
|
|
|
|
|
2014-07-21 16:04:41 +02:00
|
|
|
private
|
|
|
|
|
|
|
|
|
|
def authorized?(project, user)
|
2015-08-11 15:04:52 +02:00
|
|
|
repository = project.repository
|
|
|
|
|
|
2015-09-18 14:27:04 +02:00
|
|
|
if repository && repository.class.authorization_policy
|
|
|
|
|
repository.class.authorization_policy.new(project, user).authorized?(params)
|
2014-07-21 16:04:41 +02:00
|
|
|
else
|
2015-08-11 15:04:52 +02:00
|
|
|
false
|
2014-07-15 16:19:14 +02:00
|
|
|
end
|
2014-07-21 16:04:41 +02:00
|
|
|
end
|
2007-04-01 19:43:59 +00:00
|
|
|
|
2009-02-10 22:03:25 +00:00
|
|
|
def check_enabled
|
|
|
|
|
User.current = nil
|
2009-12-20 09:44:28 +00:00
|
|
|
unless Setting.sys_api_enabled? && params[:key].to_s == Setting.sys_api_key
|
2016-09-26 14:20:19 +02:00
|
|
|
render plain: "Access denied. Repository management WS is disabled or key is invalid.",
|
|
|
|
|
status: :forbidden
|
2021-02-11 16:02:18 +01:00
|
|
|
false
|
2009-02-10 22:03:25 +00:00
|
|
|
end
|
2007-04-01 19:43:59 +00:00
|
|
|
end
|
2014-07-15 16:19:14 +02:00
|
|
|
|
|
|
|
|
def require_basic_auth
|
|
|
|
|
authenticate_with_http_basic do |username, password|
|
2024-10-25 17:37:07 +02:00
|
|
|
@authenticated_user = user_login(username, password)
|
2014-07-15 16:19:14 +02:00
|
|
|
return true if @authenticated_user
|
|
|
|
|
end
|
|
|
|
|
|
2014-11-03 21:26:12 +01:00
|
|
|
response.headers["WWW-Authenticate"] = 'Basic realm="Repository Authentication"'
|
2016-09-12 10:15:27 +02:00
|
|
|
render plain: "Authorization required", status: :unauthorized
|
2014-07-15 16:19:14 +02:00
|
|
|
false
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def user_login(username, password)
|
|
|
|
|
User.try_to_login(username, password)
|
|
|
|
|
end
|
2007-04-01 19:43:59 +00:00
|
|
|
end
|