0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-22 09:20:29 +01:00

Fix completed? for reused wizards && ensure users with no access can get back to site

Dieser Commit ist enthalten in:
Angus McLeod 2018-05-09 15:06:43 +10:00
Ursprung a295f6fe20
Commit 9a3ae6406f
12 geänderte Dateien mit 65 neuen und 28 gelöschten Zeilen

Datei anzeigen

@ -0,0 +1,13 @@
import CustomWizard from '../models/custom';
export default Ember.Component.extend({
siteName: function() {
return Wizard.SiteSettings.title;
}.property(),
actions: {
skip() {
CustomWizard.skip(this.get('wizardId'));
}
}
})

Datei anzeigen

@ -8,6 +8,7 @@ export default {
const ApplicationRoute = requirejs('wizard/routes/application').default; const ApplicationRoute = requirejs('wizard/routes/application').default;
const ajax = requirejs('wizard/lib/ajax').ajax; const ajax = requirejs('wizard/lib/ajax').ajax;
const StepModel = requirejs('wizard/models/step').default; const StepModel = requirejs('wizard/models/step').default;
const CustomWizard = requirejs('discourse/plugins/discourse-custom-wizard/wizard/models/custom').default;
const WizardStep = requirejs('wizard/components/wizard-step').default; const WizardStep = requirejs('wizard/components/wizard-step').default;
const WizardField = requirejs('wizard/components/wizard-field').default; const WizardField = requirejs('wizard/components/wizard-field').default;
const getUrl = requirejs('discourse-common/lib/get-url').default; const getUrl = requirejs('discourse-common/lib/get-url').default;
@ -84,7 +85,7 @@ export default {
this.get('step').save() this.get('step').save()
.then(response => { .then(response => {
if (this.get('finalStep')) { if (this.get('finalStep')) {
this.get('wizard').finished(response); CustomWizard.finished(response);
} else { } else {
this.sendAction('goNext', response); this.sendAction('goNext', response);
} }

Datei anzeigen

@ -9,10 +9,16 @@ const CustomWizard = Ember.Object.extend({
totalSteps: length => length, totalSteps: length => length,
skip() { skip() {
if (this.get('required')) return; if (this.get('required') && (!this.get('completed') && this.get('permitted'))) return;
const id = this.get('id'); const id = this.get('id');
ajax({ url: `/w/${id}/skip`, type: 'PUT' }).then((result) => { CustomWizard.skip(id);
this.finished(result); }
});
CustomWizard.reopenClass({
skip(wizardId) {
ajax({ url: `/w/${wizardId}/skip`, type: 'PUT' }).then((result) => {
CustomWizard.finished(result);
}); });
}, },
@ -23,7 +29,7 @@ const CustomWizard = Ember.Object.extend({
} }
window.location.href = getUrl(url); window.location.href = getUrl(url);
} }
}); })
export function findCustomWizard(wizardId) { export function findCustomWizard(wizardId) {
return ajax({ url: `/w/${wizardId}` }).then(result => { return ajax({ url: `/w/${wizardId}` }).then(result => {

Datei anzeigen

@ -15,10 +15,13 @@ export default Ember.Route.extend({
const completed = model.get('completed'); const completed = model.get('completed');
const permitted = model.get('permitted'); const permitted = model.get('permitted');
const minTrust = model.get('min_trust'); const minTrust = model.get('min_trust');
const wizardId = model.get('id');
controller.setProperties({ controller.setProperties({
completed, completed,
notPermitted: !permitted, notPermitted: !permitted,
minTrust minTrust,
wizardId
}); });
} else { } else {
controller.set('noWizard', true); controller.set('noWizard', true);

Datei anzeigen

@ -0,0 +1,7 @@
<div>{{text}}</div>
<div class="no-access-gutter">
<button class='wizard-btn primary return-to-site' {{action 'skip'}}>
{{i18n 'wizard.return_to_site' siteName=siteName}}
{{d-icon "sign-out"}}
</button>
</div>

Datei anzeigen

@ -1,10 +1,10 @@
{{#if completed}} {{#if completed}}
{{i18n 'wizard.completed'}} {{wizard-no-access text=(i18n 'wizard.completed') wizardId=wizardId}}
{{else}} {{else}}
{{#if notPermitted}} {{#if notPermitted}}
{{i18n 'wizard.not_permitted' level=minTrust}} {{wizard-no-access text=(i18n 'wizard.completed' level=minTrust) wizardId=wizardId}}
{{/if}} {{/if}}
{{#if noWizard}} {{#if noWizard}}
{{i18n 'wizard.none'}} {{wizard-no-access text=(i18n 'wizard.none') wizardId=wizardId}}
{{/if}} {{/if}}
{{/if}} {{/if}}

Datei anzeigen

@ -1,5 +1,11 @@
@import 'wizard_variables'; @import 'wizard_variables';
.no-access-gutter {
margin-top: 10px;
display: flex;
justify-content: flex-end;
}
.custom-wizard { .custom-wizard {
background-color: initial; background-color: initial;
font-size: 1.1em; font-size: 1.1em;

Datei anzeigen

@ -159,6 +159,7 @@ en:
completed: "You have completed this wizard." completed: "You have completed this wizard."
not_permitted: "You need to be trust level {{level}} or higher to access this wizard." not_permitted: "You need to be trust level {{level}} or higher to access this wizard."
none: "There is no wizard here." none: "There is no wizard here."
return_to_site: "Return to {{siteName}}"
wizard_composer: wizard_composer:
show_preview: "Preview Your Post" show_preview: "Preview Your Post"

Datei anzeigen

@ -22,6 +22,7 @@ class CustomWizard::WizardController < ::ApplicationController
respond_to do |format| respond_to do |format|
format.json do format.json do
builder = CustomWizard::Builder.new(current_user, params[:wizard_id].underscore) builder = CustomWizard::Builder.new(current_user, params[:wizard_id].underscore)
if builder.wizard.present? if builder.wizard.present?
wizard = builder.build wizard = builder.build
render_serialized(wizard, WizardSerializer) render_serialized(wizard, WizardSerializer)
@ -35,15 +36,18 @@ class CustomWizard::WizardController < ::ApplicationController
## clean up if user skips wizard ## clean up if user skips wizard
def skip def skip
params.require(:wizard_id)
wizard_id = params[:wizard_id] wizard_id = params[:wizard_id]
wizard = PluginStore.get('custom_wizard', wizard_id.underscore) user = current_user
wizard_data = PluginStore.get('custom_wizard', wizard_id.underscore)
wizard = CustomWizard::Wizard.new(user, wizard_data)
if wizard['required'] 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') }
end end
user = current_user
result = success_json result = success_json
submission = Array.wrap(PluginStore.get("#{wizard_id}_submissions", user.id)).last submission = Array.wrap(PluginStore.get("#{wizard_id}_submissions", user.id)).last
@ -51,8 +55,8 @@ class CustomWizard::WizardController < ::ApplicationController
result.merge!(redirect_to: submission['redirect_to']) result.merge!(redirect_to: submission['redirect_to'])
end end
if submission && !wizard['save_submissions'] if submission && !wizard.save_submissions
PluginStore.remove("#{wizard['id']}_submissions", user.id) PluginStore.remove("#{wizard_id}_submissions", user.id)
end end
if user.custom_fields['redirect_to_wizard'] === wizard_id if user.custom_fields['redirect_to_wizard'] === wizard_id

Datei anzeigen

@ -8,17 +8,7 @@ class CustomWizard::Builder
return if data.blank? return if data.blank?
@steps = data['steps'] @steps = data['steps']
@wizard = CustomWizard::Wizard.new(user, @wizard = CustomWizard::Wizard.new(user, data)
id: wizard_id,
save_submissions: data['save_submissions'],
multiple_submissions: data['multiple_submissions'],
background: data["background"],
name: data["name"],
after_time: data["after_time"],
after_signup: data["after_signup"],
required: data["required"],
min_trust: data["min_trust"]
)
@submissions = Array.wrap(PluginStore.get("#{wizard_id}_submissions", user.id)) @submissions = Array.wrap(PluginStore.get("#{wizard_id}_submissions", user.id))
end end

Datei anzeigen

@ -13,6 +13,7 @@ class CustomWizard::Wizard
:multiple_submissions, :multiple_submissions,
:min_trust, :min_trust,
:after_time, :after_time,
:after_time_scheduled,
:after_signup, :after_signup,
:required, :required,
:prompt_completion :prompt_completion
@ -99,8 +100,8 @@ class CustomWizard::Wizard
context: @id context: @id
) )
if @completed_after if @after_time
history.where("updated_at > ?", @completed_after) history = history.where("updated_at > ?", @after_time_scheduled)
end end
completed = history.distinct.order(:subject).pluck(:subject) completed = history.distinct.order(:subject).pluck(:subject)
@ -165,4 +166,8 @@ class CustomWizard::Wizard
wizard = ::JSON.parse(json) wizard = ::JSON.parse(json)
PluginStore.set('custom_wizard', wizard["id"], wizard) PluginStore.set('custom_wizard', wizard["id"], wizard)
end end
def self.set_redirect(user, wizard_id, url)
PluginStore.set("#{wizard_id}_submissions", user.id, [{ redirect_to: url }])
end
end end

Datei anzeigen

@ -88,7 +88,7 @@ after_initialize do
if Wizard.user_requires_completion?(@user) if Wizard.user_requires_completion?(@user)
wizard_path = $redis.get('custom_wizard_redirect') wizard_path = $redis.get('custom_wizard_redirect')
unless url === '/' unless url === '/'
PluginStore.set("#{wizard_path.underscore}_submissions", @user.id, [{ redirect_to: url }]) CustomWizard::Wizard.set_redirect(@user, wizard_id, url)
end end
url = "/w/#{wizard_path}" url = "/w/#{wizard_path}"
end end
@ -112,6 +112,7 @@ after_initialize do
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']
if @wizard_id && request.referer !~ /w/ && request.referer !~ /admin/ if @wizard_id && request.referer !~ /w/ && request.referer !~ /admin/
CustomWizard::Wizard.set_redirect(current_user, @wizard_id, request.referer)
redirect_to "/w/#{@wizard_id}" redirect_to "/w/#{@wizard_id}"
end end
end end