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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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
  29. validates_length_of :name, :maximum => 60
  30. safe_attributes 'name',
  31. 'host',
  32. 'port',
  33. 'account',
  34. 'account_password',
  35. 'base_dn',
  36. 'attr_login',
  37. 'attr_firstname',
  38. 'attr_lastname',
  39. 'attr_mail',
  40. 'onthefly_register',
  41. 'tls',
  42. 'verify_peer',
  43. 'filter',
  44. 'timeout'
  45. def authenticate(login, password)
  46. end
  47. def test_connection
  48. end
  49. def auth_method_name
  50. "Abstract"
  51. end
  52. def account_password
  53. read_ciphered_attribute(:account_password)
  54. end
  55. def account_password=(arg)
  56. write_ciphered_attribute(:account_password, arg)
  57. end
  58. def searchable?
  59. false
  60. end
  61. def self.search(q)
  62. results = []
  63. AuthSource.all.each do |source|
  64. begin
  65. if source.searchable?
  66. results += source.search(q)
  67. end
  68. rescue AuthSourceException => e
  69. logger.error "Error while searching users in #{source.name}: #{e.message}"
  70. end
  71. end
  72. results
  73. end
  74. def allow_password_changes?
  75. self.class.allow_password_changes?
  76. end
  77. # Does this auth source backend allow password changes?
  78. def self.allow_password_changes?
  79. false
  80. end
  81. # Try to authenticate a user not yet registered against available sources
  82. def self.authenticate(login, password)
  83. AuthSource.where(:onthefly_register => true).each do |source|
  84. begin
  85. logger.debug "Authenticating '#{login}' against '#{source.name}'" if logger && logger.debug?
  86. attrs = source.authenticate(login, password)
  87. rescue => e
  88. logger.error "Error during authentication: #{e.message}"
  89. attrs = nil
  90. end
  91. return attrs if attrs
  92. end
  93. return nil
  94. end
  95. end