You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sys_controller.rb 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006- Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. class SysController < ActionController::Base
  19. include ActiveSupport::SecurityUtils
  20. before_action :check_enabled
  21. # Requests from repository WS clients don't contain CSRF tokens
  22. skip_before_action :verify_authenticity_token
  23. def projects
  24. p = Project.active.has_module(:repository).
  25. order("#{Project.table_name}.identifier").preload(:repository).to_a
  26. # extra_info attribute from repository breaks activeresource client
  27. render :json =>
  28. p.to_json(:only => [:id, :identifier, :name, :is_public, :status],
  29. :include => {:repository => {:only => [:id, :url]}})
  30. end
  31. def create_project_repository
  32. project = Project.find(params[:id])
  33. if project.repository
  34. head 409
  35. else
  36. logger.info "Repository for #{project.name} was reported to be created by #{request.remote_ip}."
  37. repository = Repository.factory(params[:vendor])
  38. repository.safe_attributes = params[:repository]
  39. repository.project = project
  40. if repository.save
  41. render :json => {repository.class.name.underscore.tr('/', '-') => {:id => repository.id, :url => repository.url}}, :status => 201
  42. else
  43. head 422
  44. end
  45. end
  46. end
  47. def fetch_changesets
  48. projects = []
  49. scope = Project.active.has_module(:repository)
  50. if params[:id]
  51. project = nil
  52. if /^\d*$/.match?(params[:id].to_s)
  53. project = scope.find(params[:id])
  54. else
  55. project = scope.find_by_identifier(params[:id])
  56. end
  57. raise ActiveRecord::RecordNotFound unless project
  58. projects << project
  59. else
  60. projects = scope.to_a
  61. end
  62. projects.each do |project|
  63. project.repositories.each do |repository|
  64. repository.fetch_changesets
  65. end
  66. end
  67. head 200
  68. rescue ActiveRecord::RecordNotFound
  69. head 404
  70. end
  71. protected
  72. def check_enabled
  73. User.current = nil
  74. unless Setting.sys_api_enabled? && secure_compare(params[:key].to_s, Setting.sys_api_key.to_s)
  75. render :plain => 'Access denied. Repository management WS is disabled or key is invalid.', :status => 403
  76. return false
  77. end
  78. end
  79. end