Skip to content

Commit df10ffb

Browse files
committed
Add preview token system to surveys component
1 parent 1d1985e commit df10ffb

10 files changed

Lines changed: 18 additions & 9 deletions

File tree

decidim-core/app/controllers/decidim/components/base_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class BaseController < Decidim::ApplicationController
3030
:current_manifest
3131

3232
before_action do
33-
enforce_permission_to :read, :component, component: current_component
33+
enforce_permission_to :read, :component, component: current_component, preview_token: params[:token]
3434
end
3535

3636
before_action :redirect_unless_feature_private

decidim-core/app/permissions/decidim/permissions.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ def component_public_action?
4545
return unless permission_action.subject == :component &&
4646
permission_action.action == :read
4747

48+
return allow! if can_preview_component?
4849
return allow! if component.published?
4950
return allow! if user_can_admin_component?
5051
return allow! if user_can_admin_component_via_space?
@@ -163,6 +164,10 @@ def user_can_admin_component?
163164
nil
164165
end
165166

167+
def can_preview_component?
168+
allow! if component.settings.preview_token == context[:preview_token]
169+
end
170+
166171
def user_can_admin_component_via_space?
167172
Decidim.participatory_space_manifests.any? do |manifest|
168173
new_permission_action = Decidim::PermissionAction.new(

decidim-forms/app/controllers/decidim/forms/admin/concerns/has_questionnaire.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def after_update_url
7777

7878
# Implement this method in your controller to set the URL
7979
# where the questionnaire can be answered.
80-
def public_url
80+
def public_url(params)
8181
raise "#{self.class.name} is expected to implement #public_url"
8282
end
8383

decidim-forms/app/views/decidim/forms/admin/questionnaires/_form.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<%= t(".title") %>
55
<% if allowed_to? :preview, :questionnaire %>
66
<div class="button--title">
7-
<%= link_to t(".preview"), public_url, class: "button tiny button--simple", target: :_blank %>
7+
<%= link_to t(".preview"), public_url(token: questionnaire_for.try(:component).try(:settings).try(:preview_token)), class: "button tiny button--simple", target: :_blank %>
88
</div>
99
<% end %>
1010
<div class="button--title">

decidim-forms/app/views/decidim/forms/questionnaires/show.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<div class="columns large-<%= columns %> medium-centered">
2121
<div class="card">
2222
<div class="card__content">
23-
<% unless questionnaire_for.try(:component)&.try(:published?) %>
23+
<% unless questionnaire_for.try(:component).try(:published?) %>
2424
<div class="section">
2525
<div class="callout warning">
2626
<p><%= t(".questionnaire_not_published.body") %></p>

decidim-meetings/app/controllers/decidim/meetings/admin/registration_form_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def after_update_url
2020
end
2121

2222
def public_url
23-
Decidim::EngineRouter.main_proxy(current_component).join_meeting_registration_path(meeting)
23+
Decidim::EngineRouter.main_proxy(current_component).join_meeting_registration_path(meeting, **params)
2424
end
2525

2626
private

decidim-surveys/app/controllers/decidim/surveys/admin/surveys_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ def questionnaire_for
1111
survey
1212
end
1313

14-
def public_url
15-
Decidim::EngineRouter.main_proxy(current_component).survey_path(survey)
14+
def public_url(params)
15+
Decidim::EngineRouter.main_proxy(current_component).survey_path(survey, **params)
1616
end
1717

1818
private

decidim-surveys/app/controllers/decidim/surveys/surveys_controller.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ class SurveysController < Decidim::Surveys::ApplicationController
88
include Decidim::ComponentPathHelper
99
helper Decidim::Surveys::SurveyHelper
1010

11-
delegate :allow_unregistered?, to: :current_settings
12-
1311
before_action :check_permissions
1412

1513
def check_permissions
@@ -25,6 +23,10 @@ def questionnaire_for
2523
def allow_answers?
2624
!current_component.published? || current_settings.allow_answers?
2725
end
26+
27+
def allow_unregistered?
28+
!current_component.published? || current_settings.allow_unregistered?
29+
end
2830

2931
def form_path
3032
main_component_path(current_component)

decidim-surveys/config/locales/en.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ en:
2222
global:
2323
announcement: Announcement
2424
clean_after_publish: Delete answers when publishing the survey
25+
preview_token: Preview token
2526
step:
2627
allow_answers: Allow answers
2728
allow_unregistered: Allow unregistered users to answer the survey

decidim-surveys/lib/decidim/surveys/component.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
component.settings(:global) do |settings|
5858
settings.attribute :announcement, type: :text, translated: true, editor: true
5959
settings.attribute :clean_after_publish, type: :boolean, default: true
60+
settings.attribute :preview_token, type: :string, default: Digest::MD5.hexdigest(Time.now.to_s)
6061
end
6162

6263
component.settings(:step) do |settings|

0 commit comments

Comments
 (0)