diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index 6b6f510..82b91da 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -30,7 +30,7 @@ class Admin::UsersController < Admin::BaseController amount = params[:amount].to_i notify_user = ActiveRecord::Type::Boolean.new.cast(params[:notify_user]) - CreateInvitations.call(user: @user, amount: amount, notify: notify_user) + UserManager::CreateInvitations.call(user: @user, amount: amount, notify: notify_user) redirect_to admin_user_path(@user.cn), flash: { success: "Added #{amount} invitations to #{@user.cn}'s account" diff --git a/app/controllers/signup_controller.rb b/app/controllers/signup_controller.rb index 236db54..631551d 100644 --- a/app/controllers/signup_controller.rb +++ b/app/controllers/signup_controller.rb @@ -96,7 +96,7 @@ class SignupController < ApplicationController session[:new_user] = nil session[:validation_error] = nil - CreateAccount.call(account: { + UserManager::CreateAccount.call(account: { username: @user.cn, domain: Setting.primary_domain, email: @user.email, diff --git a/app/services/create_account.rb b/app/services/create_account.rb deleted file mode 100644 index 12bf07b..0000000 --- a/app/services/create_account.rb +++ /dev/null @@ -1,54 +0,0 @@ -class CreateAccount < ApplicationService - def initialize(account:) - @username = account[:username] - @domain = account[:ou] || Setting.primary_domain - @email = account[:email] - @password = account[:password] - @invitation = account[:invitation] - @confirmed = account[:confirmed] - end - - def call - user = create_user_in_database - add_ldap_document - create_lndhub_account(user) if Setting.lndhub_enabled - - if @invitation.present? - update_invitation(user.id) - end - end - - private - - def create_user_in_database - User.create!( - cn: @username, - ou: @domain, - email: @email, - password: @password, - password_confirmation: @password, - confirmed_at: @confirmed ? DateTime.now : nil - ) - end - - def update_invitation(user_id) - @invitation.update! invited_user_id: user_id, used_at: DateTime.now - end - - def add_ldap_document - hashed_pw = Devise.ldap_auth_password_builder.call(@password) - CreateLdapUserJob.perform_later( - username: @username, - domain: @domain, - email: @email, - hashed_pw: hashed_pw, - confirmed: @confirmed - ) - end - - def create_lndhub_account(user) - #TODO enable in development when we have a local lndhub (mock?) API - return if Rails.env.development? - CreateLndhubAccountJob.perform_later(user) - end -end diff --git a/app/services/create_invitations.rb b/app/services/create_invitations.rb deleted file mode 100644 index 3003b1a..0000000 --- a/app/services/create_invitations.rb +++ /dev/null @@ -1,17 +0,0 @@ -class CreateInvitations < ApplicationService - def initialize(user:, amount:, notify: true) - @user = user - @amount = amount - @notify = notify - end - - def call - @amount.times do - Invitation.create(user: @user) - end - - if @notify - NotificationMailer.with(user: @user).new_invitations_available.deliver_later - end - end -end diff --git a/app/services/user_manager/create_account.rb b/app/services/user_manager/create_account.rb new file mode 100644 index 0000000..ca65451 --- /dev/null +++ b/app/services/user_manager/create_account.rb @@ -0,0 +1,56 @@ +module UserManager + class CreateAccount < UserManagerService + def initialize(account:) + @username = account[:username] + @domain = account[:ou] || Setting.primary_domain + @email = account[:email] + @password = account[:password] + @invitation = account[:invitation] + @confirmed = account[:confirmed] + end + + def call + user = create_user_in_database + add_ldap_document + create_lndhub_account(user) if Setting.lndhub_enabled + + if @invitation.present? + update_invitation(user.id) + end + end + + private + + def create_user_in_database + User.create!( + cn: @username, + ou: @domain, + email: @email, + password: @password, + password_confirmation: @password, + confirmed_at: @confirmed ? DateTime.now : nil + ) + end + + def update_invitation(user_id) + @invitation.update! invited_user_id: user_id, used_at: DateTime.now + end + + def add_ldap_document + hashed_pw = Devise.ldap_auth_password_builder.call(@password) + CreateLdapUserJob.perform_later( + username: @username, + domain: @domain, + email: @email, + hashed_pw: hashed_pw, + confirmed: @confirmed + ) + end + + def create_lndhub_account(user) + #TODO enable in development when we have a local lndhub (mock?) API + return if Rails.env.development? + CreateLndhubAccountJob.perform_later(user) + end + end +end diff --git a/app/services/user_manager/create_invitations.rb b/app/services/user_manager/create_invitations.rb new file mode 100644 index 0000000..67d2fe8 --- /dev/null +++ b/app/services/user_manager/create_invitations.rb @@ -0,0 +1,19 @@ +module UserManager + class CreateInvitations < UserManagerService + def initialize(user:, amount:, notify: true) + @user = user + @amount = amount + @notify = notify + end + + def call + @amount.times do + Invitation.create(user: @user) + end + + if @notify + NotificationMailer.with(user: @user).new_invitations_available.deliver_later + end + end + end +end diff --git a/app/services/user_manager_service.rb b/app/services/user_manager_service.rb new file mode 100644 index 0000000..74f9d37 --- /dev/null +++ b/app/services/user_manager_service.rb @@ -0,0 +1,2 @@ +class UserManagerService < ApplicationService +end diff --git a/spec/features/signup_spec.rb b/spec/features/signup_spec.rb index e668e9d..0691688 100644 --- a/spec/features/signup_spec.rb +++ b/spec/features/signup_spec.rb @@ -52,7 +52,7 @@ RSpec.describe "Signup", type: :feature do click_button "Continue" expect(page).to have_content("Choose a password") - expect(CreateAccount).to receive(:call) + expect(UserManager::CreateAccount).to receive(:call) .with(account: { username: "tony", domain: "kosmos.org", email: "tony@example.com", password: "a-valid-password", @@ -96,7 +96,7 @@ RSpec.describe "Signup", type: :feature do click_button "Create account" expect(page).to have_content("Password is too short") - expect(CreateAccount).to receive(:call) + expect(UserManager::CreateAccount).to receive(:call) .with(account: { username: "tony", domain: "kosmos.org", email: "tony@example.com", password: "a-valid-password", diff --git a/spec/services/create_account_spec.rb b/spec/services/user_manager/create_account_spec.rb similarity index 89% rename from spec/services/create_account_spec.rb rename to spec/services/user_manager/create_account_spec.rb index 4aaf139..3958b5a 100644 --- a/spec/services/create_account_spec.rb +++ b/spec/services/user_manager/create_account_spec.rb @@ -1,8 +1,8 @@ require 'rails_helper' -RSpec.describe CreateAccount, type: :model do +RSpec.describe UserManager::CreateAccount, type: :model do describe "#create_user_in_database" do - let(:service) { CreateAccount.new(account: { + let(:service) { described_class.new(account: { username: 'isaacnewton', email: 'isaacnewton@example.com', password: 'bright-ideas-in-autumn' @@ -19,7 +19,7 @@ RSpec.describe CreateAccount, type: :model do describe "#update_invitation" do let(:invitation) { create :invitation } - let(:service) { CreateAccount.new(account: { + let(:service) { described_class.new(account: { username: 'isaacnewton', email: 'isaacnewton@example.com', password: 'bright-ideas-in-autumn', @@ -42,7 +42,7 @@ RSpec.describe CreateAccount, type: :model do describe "#add_ldap_document" do include ActiveJob::TestHelper - let(:service) { CreateAccount.new(account: { + let(:service) { described_class.new(account: { username: 'halfinney', email: 'halfinney@example.com', password: 'remember-remember-the-5th-of-november' @@ -68,7 +68,7 @@ RSpec.describe CreateAccount, type: :model do describe "#add_ldap_document for pre-confirmed account" do include ActiveJob::TestHelper - let(:service) { CreateAccount.new(account: { + let(:service) { described_class.new(account: { username: 'halfinney', email: 'halfinney@example.com', password: 'remember-remember-the-5th-of-november', @@ -89,7 +89,7 @@ RSpec.describe CreateAccount, type: :model do describe "#create_lndhub_account" do include ActiveJob::TestHelper - let(:service) { CreateAccount.new(account: { + let(:service) { described_class.new(account: { username: 'halfinney', email: 'halfinney@example.com', password: 'bright-ideas-in-winter' })} diff --git a/spec/services/create_invitations_spec.rb b/spec/services/user_manager/create_invitations_spec.rb similarity index 84% rename from spec/services/create_invitations_spec.rb rename to spec/services/user_manager/create_invitations_spec.rb index 4e5de23..d994c6e 100644 --- a/spec/services/create_invitations_spec.rb +++ b/spec/services/user_manager/create_invitations_spec.rb @@ -1,13 +1,13 @@ require 'rails_helper' -RSpec.describe CreateInvitations, type: :model do +RSpec.describe UserManager::CreateInvitations, type: :model do include ActiveJob::TestHelper let(:user) { create :user } describe "#call" do before do - CreateInvitations.call(user: user, amount: 5) + described_class.call(user: user, amount: 5) end after(:each) { clear_enqueued_jobs } @@ -28,7 +28,7 @@ RSpec.describe CreateInvitations, type: :model do describe "#call with notification disabled" do before do - CreateInvitations.call(user: user, amount: 3, notify: false) + described_class.call(user: user, amount: 3, notify: false) end after(:each) { clear_enqueued_jobs }