Refactor redirect logic and update not_permitted text
Dieser Commit ist enthalten in:
Ursprung
c098a24cb3
Commit
b71ec64a9f
6 geänderte Dateien mit 39 neuen und 33 gelöschten Zeilen
|
@ -2,7 +2,7 @@
|
||||||
{{wizard-no-access text=(i18n 'wizard.completed') wizardId=wizardId}}
|
{{wizard-no-access text=(i18n 'wizard.completed') wizardId=wizardId}}
|
||||||
{{else}}
|
{{else}}
|
||||||
{{#if notPermitted}}
|
{{#if notPermitted}}
|
||||||
{{wizard-no-access text=(i18n 'wizard.completed' level=minTrust) wizardId=wizardId}}
|
{{wizard-no-access text=(i18n 'wizard.not_permitted' level=minTrust) wizardId=wizardId}}
|
||||||
{{/if}}
|
{{/if}}
|
||||||
{{#if noWizard}}
|
{{#if noWizard}}
|
||||||
{{wizard-no-access text=(i18n 'wizard.none') wizardId=wizardId}}
|
{{wizard-no-access text=(i18n 'wizard.none') wizardId=wizardId}}
|
||||||
|
|
|
@ -41,8 +41,8 @@ class CustomWizard::WizardController < ::ApplicationController
|
||||||
wizard_id = params[:wizard_id]
|
wizard_id = params[:wizard_id]
|
||||||
|
|
||||||
user = current_user
|
user = current_user
|
||||||
wizard_data = PluginStore.get('custom_wizard', wizard_id.underscore)
|
wizard_template = PluginStore.get('custom_wizard', wizard_id.underscore)
|
||||||
wizard = CustomWizard::Wizard.new(user, wizard_data)
|
wizard = CustomWizard::Wizard.new(user, wizard_template)
|
||||||
|
|
||||||
if wizard.required && !wizard.completed? && wizard.permitted?
|
if wizard.required && !wizard.completed? && wizard.permitted?
|
||||||
return render json: { error: I18n.t('wizard.no_skip') }
|
return render json: { error: I18n.t('wizard.no_skip') }
|
||||||
|
|
|
@ -3,11 +3,13 @@ module Jobs
|
||||||
def execute(args)
|
def execute(args)
|
||||||
if CustomWizard::Wizard.find(args[:wizard_id])
|
if CustomWizard::Wizard.find(args[:wizard_id])
|
||||||
user_ids = []
|
user_ids = []
|
||||||
User.human_users.each do |u|
|
|
||||||
u.custom_fields['redirect_to_wizard'] = args[:wizard_id]
|
User.human_users.each do |user|
|
||||||
u.save_custom_fields(true)
|
if CustomWizard::Wizard.set_wizard_redirect(user, args[:wizard_id])
|
||||||
user_ids.push(u.id)
|
user_ids.push(user.id)
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
MessageBus.publish "/redirect_to_wizard", args[:wizard_id], user_ids: user_ids
|
MessageBus.publish "/redirect_to_wizard", args[:wizard_id], user_ids: user_ids
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -168,10 +168,25 @@ class CustomWizard::Wizard
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.find(wizard_id)
|
def self.find(wizard_id)
|
||||||
PluginStoreRow.find_by(plugin_name: 'custom_wizard', key: wizard_id)
|
PluginStore.get('custom_wizard', wizard_id)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.set_redirect(user, wizard_id, url)
|
def self.create(user, wizard_id)
|
||||||
|
CustomWizard::Wizard.new(user, self.find(wizard_id).to_h)
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.set_submission_redirect(user, wizard_id, url)
|
||||||
PluginStore.set("#{wizard_id.underscore}_submissions", user.id, [{ redirect_to: url }])
|
PluginStore.set("#{wizard_id.underscore}_submissions", user.id, [{ redirect_to: url }])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.set_wizard_redirect(user, wizard_id)
|
||||||
|
wizard = CustomWizard::Wizard.create(user, wizard_id)
|
||||||
|
|
||||||
|
if wizard.permitted?
|
||||||
|
user.custom_fields['redirect_to_wizard'] = wizard_id
|
||||||
|
user.save_custom_fields(true)
|
||||||
|
else
|
||||||
|
false
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -7,27 +7,17 @@ require_dependency 'wizard/step'
|
||||||
wizard_result = self.new(user).requires_completion?
|
wizard_result = self.new(user).requires_completion?
|
||||||
return wizard_result if wizard_result
|
return wizard_result if wizard_result
|
||||||
|
|
||||||
custom_redirect = nil
|
custom_redirect = false
|
||||||
|
|
||||||
if user && wizard_id = CustomWizard::Wizard.after_signup
|
if user && wizard_id = CustomWizard::Wizard.after_signup
|
||||||
custom_redirect = wizard_id
|
wizard = CustomWizard::Wizard.create(user, wizard_id)
|
||||||
|
|
||||||
wizard = CustomWizard::Wizard.new(user, id: wizard_id)
|
if !wizard.completed? && wizard.permitted?
|
||||||
|
custom_redirect = true
|
||||||
data = PluginStore.get('custom_wizard', wizard_id)
|
CustomWizard::Wizard.set_wizard_redirect(user, wizard_id)
|
||||||
|
|
||||||
if data['required']
|
|
||||||
user.custom_fields['redirect_to_wizard'] = wizard_id
|
|
||||||
user.save_custom_fields(true)
|
|
||||||
end
|
|
||||||
|
|
||||||
if CustomWizard::Wizard.new(user, id: wizard_id).completed?
|
|
||||||
custom_redirect = nil
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
$redis.set('custom_wizard_redirect', custom_redirect)
|
|
||||||
|
|
||||||
!!custom_redirect
|
!!custom_redirect
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
17
plugin.rb
17
plugin.rb
|
@ -75,7 +75,7 @@ after_initialize do
|
||||||
|
|
||||||
::UsersController.class_eval do
|
::UsersController.class_eval do
|
||||||
def wizard_path
|
def wizard_path
|
||||||
if custom_wizard_redirect = $redis.get('custom_wizard_redirect')
|
if custom_wizard_redirect = current_user.custom_fields['redirect_to_wizard']
|
||||||
"#{Discourse.base_url}/w/#{custom_wizard_redirect.dasherize}"
|
"#{Discourse.base_url}/w/#{custom_wizard_redirect.dasherize}"
|
||||||
else
|
else
|
||||||
"#{Discourse.base_url}/wizard"
|
"#{Discourse.base_url}/wizard"
|
||||||
|
@ -86,10 +86,10 @@ after_initialize do
|
||||||
module InvitesControllerCustomWizard
|
module InvitesControllerCustomWizard
|
||||||
def path(url)
|
def path(url)
|
||||||
if Wizard.user_requires_completion?(@user)
|
if Wizard.user_requires_completion?(@user)
|
||||||
wizard_id = $redis.get('custom_wizard_redirect')
|
wizard_id = @user.custom_fields['custom_wizard_redirect']
|
||||||
|
|
||||||
if wizard_id && url != '/'
|
if wizard_id && url != '/'
|
||||||
CustomWizard::Wizard.set_redirect(@user, wizard_id, url)
|
CustomWizard::Wizard.set_submission_redirect(@user, wizard_id, url)
|
||||||
url = "/w/#{wizard_id.dasherize}"
|
url = "/w/#{wizard_id.dasherize}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -111,16 +111,16 @@ after_initialize do
|
||||||
before_action :redirect_to_wizard_if_required, if: :current_user
|
before_action :redirect_to_wizard_if_required, if: :current_user
|
||||||
|
|
||||||
def redirect_to_wizard_if_required
|
def redirect_to_wizard_if_required
|
||||||
@wizard_id ||= current_user.custom_fields['redirect_to_wizard']
|
wizard_id = current_user.custom_fields['redirect_to_wizard']
|
||||||
@excluded_routes ||= SiteSetting.wizard_redirect_exclude_paths.split('|') + ['/w/']
|
@excluded_routes ||= SiteSetting.wizard_redirect_exclude_paths.split('|') + ['/w/']
|
||||||
url = request.referer || request.original_url
|
url = request.referer || request.original_url
|
||||||
|
|
||||||
if request.format === 'text/html' && !@excluded_routes.any? { |str| /#{str}/ =~ url } && @wizard_id
|
if request.format === 'text/html' && !@excluded_routes.any? { |str| /#{str}/ =~ url } && wizard_id
|
||||||
if request.referer !~ /\/w\// && request.referer !~ /\/invites\//
|
if request.referer !~ /\/w\// && request.referer !~ /\/invites\//
|
||||||
CustomWizard::Wizard.set_redirect(current_user, @wizard_id, request.referer)
|
CustomWizard::Wizard.set_submission_redirect(current_user, wizard_id, request.referer)
|
||||||
end
|
end
|
||||||
|
|
||||||
redirect_to "/w/#{@wizard_id.dasherize}"
|
redirect_to "/w/#{wizard_id.dasherize}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -148,8 +148,7 @@ after_initialize do
|
||||||
|
|
||||||
DiscourseEvent.on(:user_approved) do |user|
|
DiscourseEvent.on(:user_approved) do |user|
|
||||||
if wizard_id = CustomWizard::Wizard.after_signup
|
if wizard_id = CustomWizard::Wizard.after_signup
|
||||||
user.custom_fields['redirect_to_wizard'] = wizard_id
|
CustomWizard::Wizard.set_wizard_redirect(user, wizard_id)
|
||||||
user.save_custom_fields(true)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Laden …
In neuem Issue referenzieren