* Add: #348 新規登録の上限人数 * Fix test * Fix test * Wip * Fix test * Add invite support * Wip * Fix test * Fix test * Fix test
This commit is contained in:
parent
d7cc6b788c
commit
e317edecb8
26 changed files with 362 additions and 6 deletions
|
@ -3,8 +3,10 @@
|
|||
module RegistrationHelper
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
include RegistrationLimitationHelper
|
||||
|
||||
def allowed_registration?(remote_ip, invite)
|
||||
!Rails.configuration.x.single_user_mode && !omniauth_only? && (registrations_open? || invite&.valid_for_use?) && !ip_blocked?(remote_ip)
|
||||
!Rails.configuration.x.single_user_mode && !omniauth_only? && ((registrations_open? && !reach_registrations_limit?) || invite&.valid_for_use?) && !ip_blocked?(remote_ip)
|
||||
end
|
||||
|
||||
def registrations_open?
|
||||
|
|
55
app/helpers/registration_limitation_helper.rb
Normal file
55
app/helpers/registration_limitation_helper.rb
Normal file
|
@ -0,0 +1,55 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module RegistrationLimitationHelper
|
||||
def reach_registrations_limit?
|
||||
return true unless registrations_in_time?
|
||||
|
||||
((Setting.registrations_limit.presence || 0).positive? && Setting.registrations_limit <= user_count_for_registration) ||
|
||||
((Setting.registrations_limit_per_day.presence || 0).positive? && Setting.registrations_limit_per_day <= today_increase_user_count)
|
||||
end
|
||||
|
||||
def user_count_for_registration
|
||||
Rails.cache.fetch('registrations:user_count') { User.confirmed.enabled.joins(:account).merge(Account.without_suspended).count }
|
||||
end
|
||||
|
||||
def today_increase_user_count
|
||||
today_date = Time.now.utc.beginning_of_day.to_i
|
||||
count = 0
|
||||
|
||||
if Rails.cache.fetch('registrations:today_date') { today_date } == today_date
|
||||
count = Rails.cache.fetch('registrations:today_increase_user_count') { today_increase_user_count_value }
|
||||
else
|
||||
count = today_increase_user_count_value
|
||||
Rails.cache.write('registrations:today_date', today_date)
|
||||
Rails.cache.write('registrations:today_increase_user_count', count)
|
||||
end
|
||||
|
||||
count
|
||||
end
|
||||
|
||||
def today_increase_user_count_value
|
||||
User.confirmed.enabled.where('users.created_at >= ?', Time.now.utc.beginning_of_day).joins(:account).merge(Account.without_suspended).count
|
||||
end
|
||||
|
||||
def registrations_in_time?
|
||||
start_hour = Setting.registrations_start_hour || 0
|
||||
end_hour = Setting.registrations_end_hour || 24
|
||||
secondary_start_hour = Setting.registrations_secondary_start_hour || 0
|
||||
secondary_end_hour = Setting.registrations_secondary_end_hour || 0
|
||||
|
||||
return true if start_hour >= end_hour && secondary_start_hour >= secondary_end_hour
|
||||
|
||||
current_hour = Time.now.utc.hour
|
||||
primary_permitted = false
|
||||
primary_permitted = start_hour <= current_hour && current_hour < end_hour if start_hour < end_hour && end_hour.positive?
|
||||
secondary_permitted = false
|
||||
secondary_permitted = secondary_start_hour <= current_hour && current_hour < secondary_end_hour if secondary_start_hour < secondary_end_hour && secondary_end_hour.positive?
|
||||
|
||||
primary_permitted || secondary_permitted
|
||||
end
|
||||
|
||||
def reset_registration_limit_caches!
|
||||
Rails.cache.delete('registrations:user_count')
|
||||
Rails.cache.delete('registrations:today_increase_user_count')
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue