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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2017 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. class AuthSourcesController < ApplicationController
  18. layout 'admin'
  19. self.main_menu = false
  20. menu_item :ldap_authentication
  21. before_action :require_admin
  22. before_action :build_new_auth_source, :only => [:new, :create]
  23. before_action :find_auth_source, :only => [:edit, :update, :test_connection, :destroy]
  24. require_sudo_mode :update, :destroy
  25. def index
  26. @auth_source_pages, @auth_sources = paginate AuthSource, :per_page => 25
  27. end
  28. def new
  29. end
  30. def create
  31. if @auth_source.save
  32. flash[:notice] = l(:notice_successful_create)
  33. redirect_to auth_sources_path
  34. else
  35. render :action => 'new'
  36. end
  37. end
  38. def edit
  39. end
  40. def update
  41. @auth_source.safe_attributes = params[:auth_source]
  42. if @auth_source.save
  43. flash[:notice] = l(:notice_successful_update)
  44. redirect_to auth_sources_path
  45. else
  46. render :action => 'edit'
  47. end
  48. end
  49. def test_connection
  50. begin
  51. @auth_source.test_connection
  52. flash[:notice] = l(:notice_successful_connection)
  53. rescue Exception => e
  54. flash[:error] = l(:error_unable_to_connect, e.message)
  55. end
  56. redirect_to auth_sources_path
  57. end
  58. def destroy
  59. unless @auth_source.users.exists?
  60. @auth_source.destroy
  61. flash[:notice] = l(:notice_successful_delete)
  62. else
  63. flash[:error] = l(:error_can_not_delete_auth_source)
  64. end
  65. redirect_to auth_sources_path
  66. end
  67. def autocomplete_for_new_user
  68. results = AuthSource.search(params[:term])
  69. render :json => results.map {|result| {
  70. 'value' => result[:login],
  71. 'label' => "#{result[:login]} (#{result[:firstname]} #{result[:lastname]})",
  72. 'login' => result[:login].to_s,
  73. 'firstname' => result[:firstname].to_s,
  74. 'lastname' => result[:lastname].to_s,
  75. 'mail' => result[:mail].to_s,
  76. 'auth_source_id' => result[:auth_source_id].to_s
  77. }}
  78. end
  79. private
  80. def build_new_auth_source
  81. @auth_source = AuthSource.new_subclass_instance(params[:type] || 'AuthSourceLdap')
  82. if @auth_source
  83. @auth_source.safe_attributes = params[:auth_source]
  84. else
  85. render_404
  86. end
  87. end
  88. def find_auth_source
  89. @auth_source = AuthSource.find(params[:id])
  90. rescue ActiveRecord::RecordNotFound
  91. render_404
  92. end
  93. end