61 lines
1.5 KiB
Ruby
61 lines
1.5 KiB
Ruby
class CreateAccount < ApplicationService
|
|
def initialize(args)
|
|
@username = args[:username]
|
|
@domain = args[:ou] || "kosmos.org"
|
|
@email = args[:email]
|
|
@password = args[:password]
|
|
@invitation = args[:invitation]
|
|
end
|
|
|
|
def call
|
|
user = create_user_in_database
|
|
add_ldap_document
|
|
|
|
if @invitation.present?
|
|
update_invitation(user.id)
|
|
exchange_xmpp_contacts
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def create_user_in_database
|
|
User.create!(
|
|
cn: @username,
|
|
ou: @domain,
|
|
email: @email,
|
|
password: @password,
|
|
password_confirmation: @password
|
|
)
|
|
end
|
|
|
|
def update_invitation(user_id)
|
|
@invitation.update! invited_user_id: user_id, used_at: DateTime.now
|
|
end
|
|
|
|
# TODO move to confirmation
|
|
def add_ldap_document
|
|
hashed_pw = Devise.ldap_auth_password_builder.call(@password)
|
|
CreateLdapUserJob.perform_later(@username, "kosmos.org", @email, hashed_pw)
|
|
end
|
|
|
|
def exchange_xmpp_contacts
|
|
#TODO enable in development when we have easy setup of ejabberd etc.
|
|
return if Rails.env.development?
|
|
|
|
ejabberd = EjabberdApiClient.new
|
|
inviter = @invitation.user
|
|
|
|
ejabberd.add_rosteritem({
|
|
"localuser": @username, "localhost": @domain,
|
|
"user": inviter.cn, "host": inviter.ou,
|
|
"nick": inviter.cn, "group": "Friends", "subs": "both"
|
|
})
|
|
ejabberd.add_rosteritem({
|
|
"localuser": inviter.cn, "localhost": inviter.ou,
|
|
"user": @username, "host": @domain,
|
|
"nick": @username, "group": "Friends", "subs": "both"
|
|
})
|
|
end
|
|
end
|