From aa7dd168277a80df74f7a01021c4e599735f843a Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Wed, 19 Jun 2019 13:23:10 +0800 Subject: [PATCH] Handle guests on wizard routes --- .../wizard/controllers/custom.js.es6 | 2 +- .../javascripts/wizard/models/custom.js.es6 | 1 - .../wizard/routes/custom-index.js.es6 | 3 +++ .../javascripts/wizard/routes/custom.js.es6 | 2 +- .../wizard/templates/custom.index.hbs | 19 ++++++++------ config/locales/client.en.yml | 1 + controllers/wizard.rb | 25 ++++++++++--------- lib/builder.rb | 8 +++--- lib/wizard.rb | 10 ++++++-- lib/wizard_edits.rb | 6 ++++- 10 files changed, 49 insertions(+), 28 deletions(-) diff --git a/assets/javascripts/wizard/controllers/custom.js.es6 b/assets/javascripts/wizard/controllers/custom.js.es6 index 9e89a0b0..fc2eb947 100644 --- a/assets/javascripts/wizard/controllers/custom.js.es6 +++ b/assets/javascripts/wizard/controllers/custom.js.es6 @@ -1,3 +1,3 @@ export default Ember.Controller.extend({ queryParams: ['reset'] -}) +}); diff --git a/assets/javascripts/wizard/models/custom.js.es6 b/assets/javascripts/wizard/models/custom.js.es6 index 90bd8985..089d12c8 100644 --- a/assets/javascripts/wizard/models/custom.js.es6 +++ b/assets/javascripts/wizard/models/custom.js.es6 @@ -37,7 +37,6 @@ export function findCustomWizard(wizardId, opts = {}) { return ajax({ url, cache: false, dataType: 'json' }).then(result => { const wizard = result.wizard; - if (!wizard) return null; if (!wizard.completed) { diff --git a/assets/javascripts/wizard/routes/custom-index.js.es6 b/assets/javascripts/wizard/routes/custom-index.js.es6 index 5e7a2914..5e90807d 100644 --- a/assets/javascripts/wizard/routes/custom-index.js.es6 +++ b/assets/javascripts/wizard/routes/custom-index.js.es6 @@ -16,8 +16,11 @@ export default Ember.Route.extend({ const permitted = model.get('permitted'); const minTrust = model.get('min_trust'); const wizardId = model.get('id'); + const user = model.get('user'); controller.setProperties({ + requiresLogin: !user, + user, completed, notPermitted: !permitted, minTrust, diff --git a/assets/javascripts/wizard/routes/custom.js.es6 b/assets/javascripts/wizard/routes/custom.js.es6 index dcdfbcb7..0fd8e627 100644 --- a/assets/javascripts/wizard/routes/custom.js.es6 +++ b/assets/javascripts/wizard/routes/custom.js.es6 @@ -6,7 +6,7 @@ import { ajax } from 'wizard/lib/ajax'; export default Ember.Route.extend({ model(params) { let opts = {}; - if (params.reset == 'true') opts['reset'] = true; + if (params.reset === 'true') opts['reset'] = true; return findCustomWizard(params.wizard_id, opts); }, diff --git a/assets/javascripts/wizard/templates/custom.index.hbs b/assets/javascripts/wizard/templates/custom.index.hbs index 8c1a48ae..7bcf6f2f 100644 --- a/assets/javascripts/wizard/templates/custom.index.hbs +++ b/assets/javascripts/wizard/templates/custom.index.hbs @@ -1,10 +1,15 @@ -{{#if completed}} - {{wizard-no-access text=(i18n 'wizard.completed') wizardId=wizardId}} +{{#if noWizard}} + {{wizard-no-access text=(i18n 'wizard.none') wizardId=wizardId}} {{else}} - {{#if notPermitted}} - {{wizard-no-access text=(i18n 'wizard.not_permitted' level=minTrust) wizardId=wizardId}} - {{/if}} - {{#if noWizard}} - {{wizard-no-access text=(i18n 'wizard.none') wizardId=wizardId}} + {{#if requiresLogin}} + {{wizard-no-access text=(i18n 'wizard.requires_login' level=minTrust) wizardId=wizardId}} + {{else}} + {{#if notPermitted}} + {{wizard-no-access text=(i18n 'wizard.not_permitted' level=minTrust) wizardId=wizardId}} + {{else}} + {{#if completed}} + {{wizard-no-access text=(i18n 'wizard.completed') wizardId=wizardId}} + {{/if}} + {{/if}} {{/if}} {{/if}} diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 653be688..2897ffa9 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -181,6 +181,7 @@ en: not_permitted: "You need to be trust level {{level}} or higher to access this wizard." none: "There is no wizard here." return_to_site: "Return to {{siteName}}" + requires_login: "You need to be logged in to access this wizard." wizard_composer: show_preview: "Preview Post" diff --git a/controllers/wizard.rb b/controllers/wizard.rb index 3235209c..e9e08637 100644 --- a/controllers/wizard.rb +++ b/controllers/wizard.rb @@ -2,7 +2,6 @@ class CustomWizard::WizardController < ::ApplicationController prepend_view_path(Rails.root.join('plugins', 'discourse-custom-wizard', 'views')) layout 'wizard' - requires_login helper_method :wizard_page_title helper_method :theme_ids @@ -22,7 +21,6 @@ class CustomWizard::WizardController < ::ApplicationController respond_to do |format| format.json do builder = CustomWizard::Builder.new(current_user, params[:wizard_id].underscore) - builder_opts = {} builder_opts[:reset] = params[:reset] if params[:reset] @@ -52,19 +50,22 @@ class CustomWizard::WizardController < ::ApplicationController end result = success_json - submission = Array.wrap(PluginStore.get("#{wizard_id}_submissions", user.id)).last - if submission && submission['redirect_to'] - result.merge!(redirect_to: submission['redirect_to']) - end + if user + submission = Array.wrap(PluginStore.get("#{wizard_id}_submissions", user.id)).last - if submission && !wizard.save_submissions - PluginStore.remove("#{wizard_id}_submissions", user.id) - end + if submission && submission['redirect_to'] + result.merge!(redirect_to: submission['redirect_to']) + end - if user.custom_fields['redirect_to_wizard'] === wizard_id - user.custom_fields.delete('redirect_to_wizard') - user.save_custom_fields(true) + if submission && !wizard.save_submissions + PluginStore.remove("#{wizard_id}_submissions", user.id) + end + + if user.custom_fields['redirect_to_wizard'] === wizard_id + user.custom_fields.delete('redirect_to_wizard') + user.save_custom_fields(true) + end end render json: result diff --git a/lib/builder.rb b/lib/builder.rb index 13434d28..0e5c4fb1 100644 --- a/lib/builder.rb +++ b/lib/builder.rb @@ -2,14 +2,16 @@ class CustomWizard::Builder attr_accessor :wizard, :updater, :submissions - def initialize(user, wizard_id) + def initialize(user=nil, wizard_id) data = PluginStore.get('custom_wizard', wizard_id) - return if data.blank? @steps = data['steps'] @wizard = CustomWizard::Wizard.new(user, data) - @submissions = Array.wrap(PluginStore.get("#{wizard_id}_submissions", user.id)) + + if user + @submissions = Array.wrap(PluginStore.get("#{wizard_id}_submissions", user.id)) + end end def self.sorted_handlers diff --git a/lib/wizard.rb b/lib/wizard.rb index e98b933a..b47d68f1 100644 --- a/lib/wizard.rb +++ b/lib/wizard.rb @@ -18,7 +18,7 @@ class CustomWizard::Wizard :required, :prompt_completion - def initialize(user, attrs = {}) + def initialize(user=nil, attrs = {}) @steps = [] @user = user @first_step = nil @@ -54,6 +54,8 @@ class CustomWizard::Wizard end def start + return nil if !@user + if unfinished? && last_completed_step = ::UserHistory.where( acting_user_id: @user.id, action: ::UserHistory.actions[:custom_wizard_step], @@ -76,6 +78,8 @@ class CustomWizard::Wizard end def unfinished? + return nil if !@user + most_recent = ::UserHistory.where( acting_user_id: @user.id, action: ::UserHistory.actions[:custom_wizard_step], @@ -94,6 +98,8 @@ class CustomWizard::Wizard end def completed? + return nil if !@user + steps = CustomWizard::Wizard.step_ids(@id) history = ::UserHistory.where( @@ -112,7 +118,7 @@ class CustomWizard::Wizard end def permitted? - user.staff? || user.trust_level.to_i >= min_trust.to_i + user && (user.staff? || user.trust_level.to_i >= min_trust.to_i) end def reset diff --git a/lib/wizard_edits.rb b/lib/wizard_edits.rb index 9ca52331..c93ac214 100644 --- a/lib/wizard_edits.rb +++ b/lib/wizard_edits.rb @@ -64,7 +64,7 @@ class ::Wizard::Step end ::WizardSerializer.class_eval do - attributes :id, :background, :completed, :required, :min_trust, :permitted + attributes :id, :background, :completed, :required, :min_trust, :permitted, :user def id object.id @@ -123,6 +123,10 @@ end def include_required? object.respond_to?(:required) end + + def user + object.user + end end ::WizardStepSerializer.class_eval do