0
0
Fork 0

Admission-based registrations mode (#10250)

Fix #6856
Fix #6951
This commit is contained in:
Eugen Rochko 2019-03-14 05:28:30 +01:00 committed by GitHub
parent 6e3936aa6f
commit 51e154f5e8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
92 changed files with 282 additions and 249 deletions

View file

@ -104,6 +104,8 @@ class Account < ApplicationRecord
:current_sign_in_ip,
:current_sign_in_at,
:confirmed?,
:approved?,
:pending?,
:admin?,
:moderator?,
:staff?,

View file

@ -22,7 +22,7 @@ class AccountFilter
def set_defaults!
params['local'] = '1' if params['remote'].blank?
params['active'] = '1' if params['suspended'].blank? && params['silenced'].blank?
params['active'] = '1' if params['suspended'].blank? && params['silenced'].blank? && params['pending'].blank?
end
def scope_for(key, value)
@ -35,6 +35,8 @@ class AccountFilter
Account.where(domain: value)
when 'active'
Account.without_suspended
when 'pending'
accounts_with_users.merge User.pending
when 'silenced'
Account.silenced
when 'suspended'

View file

@ -18,8 +18,8 @@ class Form::AdminSettings
:site_extended_description=,
:site_terms,
:site_terms=,
:open_registrations,
:open_registrations=,
:registrations_mode,
:registrations_mode=,
:closed_registrations_message,
:closed_registrations_message=,
:open_deletion,

View file

@ -37,6 +37,7 @@
# remember_token :string
# chosen_languages :string is an Array
# created_by_application_id :bigint(8)
# approved :boolean default(TRUE), not null
#
class User < ApplicationRecord
@ -79,6 +80,8 @@ class User < ApplicationRecord
validates :agreement, acceptance: { allow_nil: false, accept: [true, 'true', '1'] }, on: :create
scope :recent, -> { order(id: :desc) }
scope :pending, -> { where(approved: false) }
scope :approved, -> { where(approved: true) }
scope :confirmed, -> { where.not(confirmed_at: nil) }
scope :enabled, -> { where(disabled: false) }
scope :inactive, -> { where(arel_table[:current_sign_in_at].lt(ACTIVE_DURATION.ago)) }
@ -87,6 +90,7 @@ class User < ApplicationRecord
scope :emailable, -> { confirmed.enabled.joins(:account).merge(Account.searchable) }
before_validation :sanitize_languages
before_create :set_approved
# This avoids a deprecation warning from Rails 5.1
# It seems possible that a future release of devise-two-factor will
@ -124,7 +128,11 @@ class User < ApplicationRecord
super
prepare_new_user! if new_user
if new_user && approved?
prepare_new_user!
elsif new_user
notify_staff_about_pending_account!
end
end
def confirm!
@ -133,7 +141,26 @@ class User < ApplicationRecord
skip_confirmation!
save!
prepare_new_user! if new_user
prepare_new_user! if new_user && approved?
end
def pending?
!approved?
end
def active_for_authentication?
super && approved?
end
def inactive_message
!approved? ? :pending : super
end
def approve!
return if approved?
update!(approved: true)
prepare_new_user!
end
def update_tracked_fields!(request)
@ -236,6 +263,10 @@ class User < ApplicationRecord
private
def set_approved
self.approved = Setting.registrations_mode == 'open' || invited?
end
def sanitize_languages
return if chosen_languages.nil?
chosen_languages.reject!(&:blank?)
@ -253,6 +284,13 @@ class User < ApplicationRecord
regenerate_feed! if needs_feed_update?
end
def notify_staff_about_pending_account!
User.staff.includes(:account).each do |u|
next unless u.allows_report_emails?
AdminMailer.new_pending_account(u.account, self).deliver_later
end
end
def regenerate_feed!
return unless Redis.current.setnx("account:#{account_id}:regeneration", true)
Redis.current.expire("account:#{account_id}:regeneration", 1.day.seconds)