app_sign_up_service.rb 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # frozen_string_literal: true
  2. class AppSignUpService < BaseService
  3. include RegistrationHelper
  4. def call(app, remote_ip, params)
  5. @app = app
  6. @remote_ip = remote_ip
  7. @params = params
  8. raise Mastodon::NotPermittedError unless allowed_registration?(remote_ip, invite)
  9. ApplicationRecord.transaction do
  10. create_user!
  11. create_access_token!
  12. end
  13. @access_token
  14. end
  15. private
  16. def create_user!
  17. @user = User.create!(
  18. user_params.merge(created_by_application: @app, sign_up_ip: @remote_ip, password_confirmation: user_params[:password], account_attributes: account_params, invite_request_attributes: invite_request_params)
  19. )
  20. end
  21. def create_access_token!
  22. @access_token = Doorkeeper::AccessToken.create!(
  23. application: @app,
  24. resource_owner_id: @user.id,
  25. scopes: @app.scopes,
  26. expires_in: Doorkeeper.configuration.access_token_expires_in,
  27. use_refresh_token: Doorkeeper.configuration.refresh_token_enabled?
  28. )
  29. end
  30. def invite
  31. Invite.find_by(code: @params[:invite_code]) if @params[:invite_code].present?
  32. end
  33. def user_params
  34. @params.slice(:email, :password, :agreement, :locale, :time_zone, :invite_code)
  35. end
  36. def account_params
  37. @params.slice(:username)
  38. end
  39. def invite_request_params
  40. { text: @params[:reason] }
  41. end
  42. end