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_source.rb 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2019 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. # Generic exception for when the AuthSource can not be reached
  19. # (eg. can not connect to the LDAP)
  20. class AuthSourceException < StandardError; end
  21. class AuthSourceTimeoutException < AuthSourceException; end
  22. class AuthSource < ActiveRecord::Base
  23. include Redmine::SafeAttributes
  24. include Redmine::SubclassFactory
  25. include Redmine::Ciphering
  26. has_many :users
  27. validates_presence_of :name
  28. validates_uniqueness_of :name, :case_sensitive => false
  29. validates_length_of :name, :maximum => 60
  30. safe_attributes(
  31. 'name',
  32. 'host',
  33. 'port',
  34. 'account',
  35. 'account_password',
  36. 'base_dn',
  37. 'attr_login',
  38. 'attr_firstname',
  39. 'attr_lastname',
  40. 'attr_mail',
  41. 'onthefly_register',
  42. 'tls',
  43. 'verify_peer',
  44. 'filter',
  45. 'timeout')
  46. def authenticate(login, password)
  47. end
  48. def test_connection
  49. end
  50. def auth_method_name
  51. "Abstract"
  52. end
  53. def account_password
  54. read_ciphered_attribute(:account_password)
  55. end
  56. def account_password=(arg)
  57. write_ciphered_attribute(:account_password, arg)
  58. end
  59. def searchable?
  60. false
  61. end
  62. def self.search(q)
  63. results = []
  64. AuthSource.all.each do |source|
  65. begin
  66. if source.searchable?
  67. results += source.search(q)
  68. end
  69. rescue AuthSourceException => e
  70. logger.error "Error while searching users in #{source.name}: #{e.message}"
  71. end
  72. end
  73. results
  74. end
  75. def allow_password_changes?
  76. self.class.allow_password_changes?
  77. end
  78. # Does this auth source backend allow password changes?
  79. def self.allow_password_changes?
  80. false
  81. end
  82. # Try to authenticate a user not yet registered against available sources
  83. def self.authenticate(login, password)
  84. AuthSource.where(:onthefly_register => true).each do |source|
  85. begin
  86. logger.debug "Authenticating '#{login}' against '#{source.name}'" if logger && logger.debug?
  87. attrs = source.authenticate(login, password)
  88. rescue => e
  89. logger.error "Error during authentication: #{e.message}"
  90. attrs = nil
  91. end
  92. return attrs if attrs
  93. end
  94. return nil
  95. end
  96. end