Skip to content

Commit

Permalink
restrict surveys unless code
Browse files Browse the repository at this point in the history
  • Loading branch information
microstudi committed Apr 4, 2024
1 parent 4dd10fc commit 9a37566
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

module Decidim
module AnonymousCodes
module SurveysControllerOverride
extend ActiveSupport::Concern

included do
before_action do
unless token_groups.any? && current_token.present?
flash.now[:alert] = I18n.t("decidim.anonymous_codes.invalid_code") if params.has_key?(:token)
render "decidim/anonymous_codes/surveys/code_required"
end
end

private

def token_groups
@token_groups ||= Decidim::AnonymousCodes::Group.where(resource: survey)
end

def current_token
@current_token ||= Decidim::AnonymousCodes::Token.where(group: token_groups).find_by(token: token_param)
end

def token_param
@token_param ||= begin
session[:anonymous_codes_token] = params[:token] if params.has_key?(:token)
session[:anonymous_codes_token]
end
end
end
end
end
end
50 changes: 50 additions & 0 deletions app/views/decidim/anonymous_codes/surveys/code_required.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<% add_decidim_meta_tags({
title: translated_attribute(questionnaire.title),
description: translated_attribute(questionnaire.description),
}) %>
<%= render partial: "decidim/shared/component_announcement" if current_component.manifest_name == "surveys" %>

<div class="row columns">
<h2 class="section-heading"><%= translated_attribute questionnaire.title %></h2>
<div class="row">
<div class="columns large-6 medium-centered lead">
<%= decidim_sanitize_editor_admin translated_attribute questionnaire.description %>
</div>
</div>
</div>

<div class="row">
<div class="columns large-6 medium-centered">
<div class="card">
<div class="card__content">
<% unless questionnaire_for.try(:component)&.try(:published?) %>
<div class="section">
<div class="callout warning">
<p><%= t("questionnaire_not_published.body", scope: "decidim.forms.questionnaires.show") %></p>
</div>
</div>
<% end %>

<div class="section">
<div class="callout warning">
<h3 class="heading4"><%= t(".title") %></h3>
<p><%= t(".body") %></p>
</div>
<fi3eldset class="field">
<label class="field__label" for="code"><%= t(".label") %></label>
<div class="field__input">
<form method="get">
<%= text_field_tag :token, nil, class: "input", required: true %>
<button type="submit" class="button"><%= t(".submit") %></button>
</form>
</div>
</div>
</div>
</div>
</div>
</div>

<% content_for :js_content do %>
<%= javascript_pack_tag "decidim_forms" %>
<% end %>
7 changes: 7 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,10 @@ en:
still apply if you choose to use this option.
groups: 'Edit groups:'
new_group: "\U0001F449 Create answer codes here"
invalid_code: The introduced code is invalid. Please try again.
surveys:
code_required:
title: Form restricted
body: This form can only be accessed using a valid code. If you have a valid code, please enter it below.
label: Code
submit: Continue
36 changes: 34 additions & 2 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,46 @@
print "Creating seeds for decidim_anonymous_codes...\n" unless Rails.env.test?

organization = Decidim::Organization.first
admin = Decidim::User.where(admin: true).first
participatory_processes = Decidim::ParticipatoryProcess.where(organization: organization)
survey_components = Decidim::Component.where(manifest_name: "surveys", participatory_space: participatory_processes)
survey_components.each do |component|
form = OpenStruct.new(name: component.name, weight: component.weight, settings: component.settings, default_step_settings: component.default_step_settings,
step_settings: component.step_settings)
form.step_settings.each do |key, _value|
form.step_settings[key]["allow_answers"] = true
form.step_settings[key]["allow_unregistered"] = true
end
Decidim::Admin::UpdateComponent.call(form, component, admin) do
on(:ok) do
puts "Component #{component.id} updated"
end

on(:invalid) do
puts "ERROR: Component #{component.id} not updated"
end
end
end

[survey1, survey2].each do |survey|
if survey.component.participatory_space.respond_to? :steps
steps = survey.component.participatory_space.steps.pluck(:id)
steps.each do |step|
survey.component.attributes["settings"]["steps"] = { step.to_s => { "allow_answers" => true, "allow_unregistered" => true } }
end
else
survey.component.attributes["settings"]["default_step"] = { "allow_answers" => true, "allow_unregistered" => true }
end
survey.save!
end

group1 = Decidim::AnonymousCodes::Group.create!(
title: { en: "First Group" },
organization: organization,
expires_at: 1.week.from_now,
active: true,
max_reuses: 1,
resource: Decidim::Surveys::Survey.first
resource: survey1
)

group2 = Decidim::AnonymousCodes::Group.create!(
Expand All @@ -20,7 +52,7 @@
expires_at: 1.week.from_now,
active: true,
max_reuses: 1,
resource: Decidim::Surveys::Survey.second
resource: survey2
)

5.times do
Expand Down
6 changes: 3 additions & 3 deletions lib/decidim/anonymous_codes/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ class Engine < ::Rails::Engine
isolate_namespace Decidim::AnonymousCodes

initializer "decidim_anonymous_codes.overrides", after: "decidim.action_controller" do
# config.to_prepare do

# end
config.to_prepare do
Decidim::Surveys::SurveysController.include(Decidim::AnonymousCodes::SurveysControllerOverride)
end
end

initializer "decidim-anonymous_codes.webpacker.assets_path" do
Expand Down

0 comments on commit 9a37566

Please sign in to comment.