about summary refs log tree commit diff
path: root/app/models/concerns/ldap_authenticable.rb
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2019-03-14 02:13:42 +0100
committerGitHub <noreply@github.com>2019-03-14 02:13:42 +0100
commit9e33174604952490136a6f8cce2c9bd3ca03a26c (patch)
tree251eb7e328924f4bedcbca7d60e834647500eb18 /app/models/concerns/ldap_authenticable.rb
parentdfb9efae81d029d26a9ef09acd891254529a0f53 (diff)
Refactor User model, extract PamAuthenticable, LdapAuthenticable (#10217)
Diffstat (limited to 'app/models/concerns/ldap_authenticable.rb')
-rw-r--r--app/models/concerns/ldap_authenticable.rb25
1 files changed, 25 insertions, 0 deletions
diff --git a/app/models/concerns/ldap_authenticable.rb b/app/models/concerns/ldap_authenticable.rb
new file mode 100644
index 000000000..e1b5e3832
--- /dev/null
+++ b/app/models/concerns/ldap_authenticable.rb
@@ -0,0 +1,25 @@
+# frozen_string_literal: true
+
+module LdapAuthenticable
+  extend ActiveSupport::Concern
+
+  def ldap_setup(_attributes)
+    self.confirmed_at = Time.now.utc
+    self.admin        = false
+
+    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