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.

auth_sources_controller.rb 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2021 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 AuthSourcesController < ApplicationController
  19. layout 'admin'
  20. self.main_menu = false
  21. menu_item :ldap_authentication
  22. before_action :require_admin
  23. before_action :build_new_auth_source, :only => [:new, :create]
  24. before_action :find_auth_source, :only => [:edit, :update, :test_connection, :destroy]
  25. require_sudo_mode :update, :destroy
  26. def index
  27. @auth_source_pages, @auth_sources = paginate AuthSource, :per_page => 25
  28. end
  29. def new
  30. end
  31. def create
  32. if @auth_source.save
  33. flash[:notice] = l(:notice_successful_create)
  34. redirect_to auth_sources_path
  35. else
  36. render :action => 'new'
  37. end
  38. end
  39. def edit
  40. end
  41. def update
  42. @auth_source.safe_attributes = params[:auth_source]
  43. if @auth_source.save
  44. flash[:notice] = l(:notice_successful_update)
  45. redirect_to auth_sources_path
  46. else
  47. render :action => 'edit'
  48. end
  49. end
  50. def test_connection
  51. begin
  52. @auth_source.test_connection
  53. flash[:notice] = l(:notice_successful_connection)
  54. rescue => e
  55. flash[:error] = l(:error_unable_to_connect, e.message)
  56. end
  57. redirect_to auth_sources_path
  58. end
  59. def destroy
  60. unless @auth_source.users.exists?
  61. @auth_source.destroy
  62. flash[:notice] = l(:notice_successful_delete)
  63. else
  64. flash[:error] = l(:error_can_not_delete_auth_source)
  65. end
  66. redirect_to auth_sources_path
  67. end
  68. def autocomplete_for_new_user
  69. results = AuthSource.search(params[:term])
  70. json = results.map do |result|
  71. {
  72. 'value' => result[:login],
  73. 'label' => "#{result[:login]} (#{result[:firstname]} #{result[:lastname]})",
  74. 'login' => result[:login].to_s,
  75. 'firstname' => result[:firstname].to_s,
  76. 'lastname' => result[:lastname].to_s,
  77. 'mail' => result[:mail].to_s,
  78. 'auth_source_id' => result[:auth_source_id].to_s
  79. }
  80. end
  81. render :json => json
  82. end
  83. private
  84. def build_new_auth_source
  85. @auth_source = AuthSource.new_subclass_instance(params[:type] || 'AuthSourceLdap')
  86. if @auth_source
  87. @auth_source.safe_attributes = params[:auth_source]
  88. else
  89. render_404
  90. end
  91. end
  92. def find_auth_source
  93. @auth_source = AuthSource.find(params[:id])
  94. rescue ActiveRecord::RecordNotFound
  95. render_404
  96. end
  97. end