app_sign_up_service.rb 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # frozen_string_literal: true
  2. class AppSignUpService < BaseService
  3. def call(app, remote_ip, params)
  4. @app = app
  5. @remote_ip = remote_ip
  6. @params = params
  7. raise Mastodon::NotPermittedError unless allowed_registrations?
  8. ApplicationRecord.transaction do
  9. create_user!
  10. create_access_token!
  11. end
  12. @access_token
  13. end
  14. private
  15. def create_user!
  16. @user = User.create!(
  17. 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)
  18. )
  19. end
  20. def create_access_token!
  21. @access_token = Doorkeeper::AccessToken.create!(
  22. application: @app,
  23. resource_owner_id: @user.id,
  24. scopes: @app.scopes,
  25. expires_in: Doorkeeper.configuration.access_token_expires_in,
  26. use_refresh_token: Doorkeeper.configuration.refresh_token_enabled?
  27. )
  28. end
  29. def user_params
  30. @params.slice(:email, :password, :agreement, :locale, :time_zone)
  31. end
  32. def account_params
  33. @params.slice(:username)
  34. end
  35. def invite_request_params
  36. { text: @params[:reason] }
  37. end
  38. def allowed_registrations?
  39. registrations_open? && !single_user_mode? && !omniauth_only? && !ip_blocked?
  40. end
  41. def registrations_open?
  42. Setting.registrations_mode != 'none'
  43. end
  44. def single_user_mode?
  45. Rails.configuration.x.single_user_mode
  46. end
  47. def omniauth_only?
  48. ENV['OMNIAUTH_ONLY'] == 'true'
  49. end
  50. def ip_blocked?
  51. IpBlock.where(severity: :sign_up_block).where('ip >>= ?', @remote_ip.to_s).exists?
  52. end
  53. end