about summary refs log tree commit diff
path: root/app/models/concerns/ldap_authenticable.rb
blob: 84ff84c4b05c856c30cf7190db6ed392df3dd063 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# frozen_string_literal: true

module LdapAuthenticable
  extend ActiveSupport::Concern

  def ldap_setup(_attributes)
    self.confirmed_at = Time.now.utc
    self.admin        = false
    self.external     = true

    save!
  end

  class_methods do
    def ldap_get_user(attributes = {})
      resource = joins(:account).find_by(accounts: { username: attributes[Devise.ldap_uid.to_sym].first })

      if resource.blank?
        resource = new(email: attributes[:mail].first, agreement: true, account_attributes: { username: attributes[Devise.ldap_uid.to_sym].first })
        resource.ldap_setup(attributes)
      end

      resource
    end
  end
end