diff options
author | Eugen Rochko <eugen@zeonfederated.com> | 2019-03-14 05:28:30 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-03-14 05:28:30 +0100 |
commit | 51e154f5e87968d6bb115e053689767ab33e80cd (patch) | |
tree | d86ba475bfc61ba7a774bf36e24704dc82f68991 /app/models/user.rb | |
parent | 6e3936aa6f4296ac202b54c0b178b4fa825d7885 (diff) |
Admission-based registrations mode (#10250)
Fix #6856 Fix #6951
Diffstat (limited to 'app/models/user.rb')
-rw-r--r-- | app/models/user.rb | 42 |
1 files changed, 40 insertions, 2 deletions
diff --git a/app/models/user.rb b/app/models/user.rb index a9d6adf70..9d0d49676 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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) |