Fix external user creation failing when invite request text is required (#15405)

* Fix external user creation failing when invite request text is required

Also fixes tootctl-based user creation.

* Add test about invites when invite request text is otherwise required

Co-authored-by: Claire <claire.github-309c@sitedethib.com>
master
ThibG 4 years ago committed by GitHub
parent 9915d11c0d
commit 1cf2c3a810
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 12
      app/models/user.rb
  2. 2
      lib/mastodon/accounts_cli.rb
  3. 2
      lib/tasks/mastodon.rake
  4. 5
      spec/controllers/auth/registrations_controller_spec.rb

@ -83,7 +83,7 @@ class User < ApplicationRecord
has_one :invite_request, class_name: 'UserInviteRequest', inverse_of: :user, dependent: :destroy has_one :invite_request, class_name: 'UserInviteRequest', inverse_of: :user, dependent: :destroy
accepts_nested_attributes_for :invite_request, reject_if: ->(attributes) { attributes['text'].blank? && !Setting.require_invite_text } accepts_nested_attributes_for :invite_request, reject_if: ->(attributes) { attributes['text'].blank? && !Setting.require_invite_text }
validates :invite_request, presence: true, on: :create, if: -> { Setting.require_invite_text && !invited? } validates :invite_request, presence: true, on: :create, if: :invite_text_required?
validates :locale, inclusion: I18n.available_locales.map(&:to_s), if: :locale? validates :locale, inclusion: I18n.available_locales.map(&:to_s), if: :locale?
validates_with BlacklistedEmailValidator, on: :create validates_with BlacklistedEmailValidator, on: :create
@ -128,7 +128,7 @@ class User < ApplicationRecord
to: :settings, prefix: :setting, allow_nil: false to: :settings, prefix: :setting, allow_nil: false
attr_reader :invite_code, :sign_in_token_attempt attr_reader :invite_code, :sign_in_token_attempt
attr_writer :external attr_writer :external, :bypass_invite_request_check
def confirmed? def confirmed?
confirmed_at.present? confirmed_at.present?
@ -429,6 +429,10 @@ class User < ApplicationRecord
!!@external !!@external
end end
def bypass_invite_request_check?
@bypass_invite_request_check
end
def sanitize_languages def sanitize_languages
return if chosen_languages.nil? return if chosen_languages.nil?
chosen_languages.reject!(&:blank?) chosen_languages.reject!(&:blank?)
@ -466,4 +470,8 @@ class User < ApplicationRecord
def validate_email_dns? def validate_email_dns?
email_changed? && !(Rails.env.test? || Rails.env.development?) email_changed? && !(Rails.env.test? || Rails.env.development?)
end end
def invite_text_required?
Setting.require_invite_text && !invited? && !external? && !bypass_invite_request_check?
end
end end

@ -77,7 +77,7 @@ module Mastodon
def create(username) def create(username)
account = Account.new(username: username) account = Account.new(username: username)
password = SecureRandom.hex password = SecureRandom.hex
user = User.new(email: options[:email], password: password, agreement: true, approved: true, admin: options[:role] == 'admin', moderator: options[:role] == 'moderator', confirmed_at: options[:confirmed] ? Time.now.utc : nil) user = User.new(email: options[:email], password: password, agreement: true, approved: true, admin: options[:role] == 'admin', moderator: options[:role] == 'moderator', confirmed_at: options[:confirmed] ? Time.now.utc : nil, bypass_invite_request_check: true)
if options[:reattach] if options[:reattach]
account = Account.find_local(username) || Account.new(username: username) account = Account.find_local(username) || Account.new(username: username)

@ -412,7 +412,7 @@ namespace :mastodon do
password = SecureRandom.hex(16) password = SecureRandom.hex(16)
user = User.new(admin: true, email: email, password: password, confirmed_at: Time.now.utc, account_attributes: { username: username }) user = User.new(admin: true, email: email, password: password, confirmed_at: Time.now.utc, account_attributes: { username: username }, bypass_invite_request_check: true)
user.save(validate: false) user.save(validate: false)
prompt.ok "You can login with the password: #{password}" prompt.ok "You can login with the password: #{password}"

@ -195,16 +195,19 @@ RSpec.describe Auth::RegistrationsController, type: :controller do
end end
end end
context 'approval-based registrations with valid invite' do context 'approval-based registrations with valid invite and required invite text' do
around do |example| around do |example|
registrations_mode = Setting.registrations_mode registrations_mode = Setting.registrations_mode
require_invite_text = Setting.require_invite_text
example.run example.run
Setting.require_invite_text = require_invite_text
Setting.registrations_mode = registrations_mode Setting.registrations_mode = registrations_mode
end end
subject do subject do
inviter = Fabricate(:user, confirmed_at: 2.days.ago) inviter = Fabricate(:user, confirmed_at: 2.days.ago)
Setting.registrations_mode = 'approved' Setting.registrations_mode = 'approved'
Setting.require_invite_text = true
request.headers["Accept-Language"] = accept_language request.headers["Accept-Language"] = accept_language
invite = Fabricate(:invite, user: inviter, max_uses: nil, expires_at: 1.hour.from_now) invite = Fabricate(:invite, user: inviter, max_uses: nil, expires_at: 1.hour.from_now)
post :create, params: { user: { account_attributes: { username: 'test' }, email: 'test@example.com', password: '12345678', password_confirmation: '12345678', 'invite_code': invite.code, agreement: 'true' } } post :create, params: { user: { account_attributes: { username: 'test' }, email: 'test@example.com', password: '12345678', password_confirmation: '12345678', 'invite_code': invite.code, agreement: 'true' } }

Loading…
Cancel
Save