Add account creation service

This commit is contained in:
2020-11-29 17:31:08 +01:00
parent fd2ebc4ad3
commit 18df8fe449
3 changed files with 81 additions and 0 deletions

View File

@@ -0,0 +1,5 @@
class ApplicationService
def self.call(*args, &block)
new(*args, &block).call
end
end

View File

@@ -0,0 +1,43 @@
class CreateAccount < ApplicationService
def initialize(args)
@username = args[:username]
@email = args[:email]
@password = args[:password]
@invited_by_id = args[:invited_by_id]
end
def call
add_ldap_document
end
private
def add_ldap_document
dn = "cn=#{@username},ou=kosmos.org,cn=users,dc=kosmos,dc=org"
attr = {
objectclass: ["top", "account", "person", "extensibleObject"],
cn: @username,
sn: @username,
uid: @username,
mail: @email,
userPassword: Devise.ldap_auth_password_builder.call(@password)
}
ldap_client.add(dn: dn, attributes: attr)
end
def ldap_client
ldap_client ||= Net::LDAP.new host: ldap_config['host'],
port: ldap_config['port'],
encryption: ldap_config['ssl'],
auth: {
method: :simple,
username: ldap_config['admin_user'],
password: ldap_config['admin_password']
}
end
def ldap_config
ldap_config ||= YAML.load(ERB.new(File.read("#{Rails.root}/config/ldap.yml")).result)[Rails.env]
end
end