Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ECO-124 Captcha na registracie #97

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Finalize registration forms and captcha mechanism
  • Loading branch information
michal-rohacek committed Jul 21, 2021
commit 7cbef38be486353d1676f0a5218029bf9faeb5f3
31 changes: 9 additions & 22 deletions app/controllers/registrations_controller.rb
Original file line number Diff line number Diff line change
@@ -2,38 +2,25 @@ class RegistrationsController < ApplicationController
include RegistrationsHelper

def create
render :create and return unless registration.valid?(:submit)
@registration ||= new_registration(registration_params)

if recaptcha_valid?
registration.finish and render :success
if @registration.save { validate_captcha! }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Co robi ten block?

Copy link
Contributor Author

@michal-rohacek michal-rohacek Jul 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

block sa pusti medzi validaciou fieldov (ak je nevalidny email/iny field, ani neposielame captcha request) a medzi finalnym registracnym POSTom

image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Toto je velmi zvlastny pattern, ja by som skor vytiahol von ten block aj ten post do controllera.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nechcem rypat ale
#97 (comment)
... teda, potom je save zbytocne nie?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mozno taketo cosi jedine. ale to asi nesedi na ten tvoj pattern ktory som linkol vyssie
image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No patterny su dva. bud sa to udeje v controlleri alebo v save. Ty si spravil nieco medzi co nebude fungovat ked tam ten block nedas.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Podla mna ten druhy pattern pouzime. Nech sa to deje v controlleri.

render :create
else
registration.checkbox_captcha! and render :create
render :new
end
end

private

def recaptcha_valid?
if registration.checkbox_captcha
verify_checkbox_captcha
else
verify_invisible_captcha
end
end
def validate_captcha!
captcha_result = verify_recaptcha(minimum_score: 0.5, action: recaptcha_action(@registration.service), model: @registration, message: 'Nastala chyba. Ak problém pretrváva aj v inom prehliadači alebo zariadení, kontaktujte nás.')
@registration.score = recaptcha_reply['score']

def verify_checkbox_captcha
verify_recaptcha(message: 'Skúste to ešte raz.', secret_key: ENV.fetch('RECAPTCHA_SECRET_KEY_V2_CHECKBOX'), model: registration)
end

def verify_invisible_captcha
verify_recaptcha(message: 'Potvrďte, prosím, že nie ste robot.', minimum_score: 0.5, action: recaptcha_action, model: registration)
captcha_result
end

def registration_params
params.require(:registration).permit(:service, :email, :checkbox_captcha)
end

def registration
@registration ||= Registration.new(registration_params)
params.require(:registration).permit(:email, :service, :score, :domain)
end
end
26 changes: 11 additions & 15 deletions app/helpers/registrations_helper.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
module RegistrationsHelper
def registration_form_id(service = controller_name)
"#{service}_registration_form"
def recaptcha_action(service = controller_name)
"#{service}_registration"
end

def recaptcha_action
"#{controller_name}_registration"
end
def new_registration(args)
raise unless args[:service]

def controller_specific_register_path
polymorphic_path([:services, controller_name, :index])
klass = (args[:service] == 'autoform') ? AutoformRegistration : Registration
klass.new(args)
end

def render_registration_form(service: controller_name, registration: nil)
registration ||= Registration.new(service: service)

if registration.valid?(:render)
render partial: 'registrations/form', object: registration
def render_registration_form(service: controller_name, model: nil)
unless model
model = new_registration(service: service)
return if model.invalid?(:render)
end
end

def translate_field(field_name)
translate "registrations.#{field_name}", default: ''
render partial: 'registrations/form', object: model
end
end
14 changes: 14 additions & 0 deletions app/models/autoform_registration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class AutoformRegistration < Registration
EXTRA_FIELDS = { domain: 'entry.591019594' }.freeze

attr_accessor :domain
validates :domain, presence: true, on: :submit

def user_input_fields
super.append(:domain)
end

def mapping
super.merge(EXTRA_FIELDS)
end
end
66 changes: 30 additions & 36 deletions app/models/registration.rb
Original file line number Diff line number Diff line change
@@ -2,50 +2,44 @@ class Registration
include ActiveModel::Model
include ActiveModel::Attributes

SUBMIT_MAPPINGS = HashWithIndifferentAccess.new(
autoform: {
url: 'https://docs.google.com/forms/d/1TpYNJfBQVGt4lmKP-wrXGkok2bo-Y6mzpmeUsJTnRis/formResponse',
email: 'entry.204431983',
other: { domain: 'entry.1349114640' }
},
slovensko_sk_api: {
url: 'https://docs.google.com/forms/d/e/1FAIpQLSfUuAjnqGjDvSc-Miy6bP0xODXsjr6g04hGAeYlYkJo-3Iu1Q/formResponse',
email: 'emailAddress',
},
datahub: {
url: 'https://docs.google.com/forms/d/e/1FAIpQLSdgW4Hf2fEhX3cpTkoYJTaIVs8pWrTFrItt9Hj_9ZD36yPLZQ/formResponse',
email: 'entry.1902802364',
}
)

attr_accessor :email, :service, :other_fields
attribute :checkbox_captcha, :boolean, default: false

validates :service, inclusion: SUBMIT_MAPPINGS.keys, on: :render
validates :email, format: { with: URI::MailTo::EMAIL_REGEXP, message: 'Zadajte email v správnom tvare' }, on: :submit
# validates :other_fields, each: { presence: true }, on: :submit

def mappings
data[:other].reverse_merge(email: data[:email])
end
FORM_URL = 'https://docs.google.com/forms/d/e/1FAIpQLScswqdDYxXtjUDW7Crw0aro3Au87R1dVmHIYyA5UH4jrZNZ5g/formResponse'

REQUEST_MAPPING = {
email: 'entry.1908289207',
service: 'entry.1504702132',
score: 'entry.324492615',
}.freeze

attr_accessor :email, :service, :score

validates :email, format: { with: URI::MailTo::EMAIL_REGEXP }, on: :submit
validates :service, inclusion: %w(datahub autoform slovensko_sk_api), on: :render

def finish
args = attributes.transform_keys { |key| mappings[key] }.symbolize_keys!
def save
return false unless valid?(:submit)

RestClient.post(data[:url], **args)
if block_given?
return false unless yield
end

RestClient.post(FORM_URL, **build_request_params)
end

private
def build_request_params
mapping.transform_keys { |attr| send(attr) }.invert.symbolize_keys
end

def checkbox_captcha!
checkbox_captcha = true
def user_input_fields
[:email]
end

def data
SUBMIT_MAPPINGS[service]
def html_id
"#{service}_registration_form"
end

def attributes
other_fields.reverse_merge(email: email)
def mapping
REQUEST_MAPPING
end

private_constant :REQUEST_MAPPING
end
54 changes: 26 additions & 28 deletions app/views/registrations/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,37 +1,35 @@
<div class="row" id="<%= registration_form_id %>">
<div class="col-md-12">
<%= form_with model: form do |f| %>
<% if form.errors.any? %>
<% form.errors.values.flatten.each do |message| %>
<%= render 'services/share/flash_message', { type: :alert, message: message } %>
<div id="<%= form.html_id %>">
<div class="row">
<div class="col-md-12">
<%= form_with model: form, scope: :registration do |f| %>
<% if form.errors.any? %>
<% form.errors.full_messages.each do |message| %>
<%= render 'services/share/flash_message', { type: :alert, message: message } %>
<% end %>
<% end %>
<% end %>

<% if form.checkbox_captcha %>
<%= recaptcha_tags(site_key: ENV.fetch('RECAPTCHA_SITE_KEY_V2_CHECKBOX')) %>
<% else %>
<%= recaptcha_v3(action: recaptcha_action) %>
<% end %>
<%= recaptcha_v3(action: recaptcha_action(form.service)) %>

<div class="form-group">
<% form.mappings.keys.each do |field| %>
<div class="row">
<%= f.label field, translate_field(field), class: 'control-label' %>
<div class="row">
<% form.user_input_fields.each do |field| %>
<div class="col-md-3">
<%= f.text_field field, class: 'form-control input-lg' %>
<div class="form-group">
<%= f.label field, t(field), class: 'control-label' %>
<%= f.text_field field, class: 'form-control input-lg' %>
</div>
</div>
</div>
<% end %>
<% end %>

<%= f.hidden_field :service, value: form.service %>
<%= f.hidden_field :checkbox_captcha, value: form.checkbox_captcha %>
<%= f.hidden_field :service %>

<div class="col-md-4 col-md-pad">
<%= button_tag :submit, class: 'btn btn-default btn-lg btn-strong', id: 'submit-button' do %>
<strong>Zaregistrovať</strong>
<% end %>
<div class="col-md-4 col-md-pad">
<%= label_tag :a, '&nbsp'.html_safe %><br>
<%= button_tag :submit, class: 'btn btn-default btn-lg btn-strong', id: 'submit-button' do %>
<strong>Zaregistrovať</strong>
<% end %>
</div>
</div>
</div>
<% end %>
<% end %>
</div>
</div>
</div>
</div>
2 changes: 1 addition & 1 deletion app/views/registrations/create.js.erb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
$('#<%= registration_form_id(@registration.service) %>').html("<%= j render partial: 'registrations/form', object: @registration %>");
$('#<%= @registration.html_id %>').replaceWith("<%= j render 'services/share/flash_message', { type: :notice, message: 'Ďakujeme za Váš záujem. Budeme Vás kontaktovať cez zadaný email.' } %>");
1 change: 1 addition & 0 deletions app/views/registrations/new.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$('#<%= @registration.html_id %>').replaceWith('<%= j render_registration_form(model: @registration) %>');
1 change: 0 additions & 1 deletion app/views/registrations/success.js.erb

This file was deleted.

26 changes: 0 additions & 26 deletions app/views/services/slovensko_sk_api/index.html.erb
Original file line number Diff line number Diff line change
@@ -80,32 +80,6 @@
<p class="lead">
Nechajte nám Váš emailový kontakt alebo nám napíšte na <a href="mailto:[email protected]">[email protected]</a>. <em>Tešíme sa na spoluprácu.</em>
</p>

<div id="sk-api-error" style="display: none;">
<%= render 'services/share/flash_message', {type: :alert, message: 'Vyplňte prosím email a skúste znova.'} %>
</div>
<div id="sk-api-form-sent" style="display: none;">
<%= render 'services/share/flash_message', {type: :notice, message: 'Ďakujeme, za Váš záujem. Budeme Vás kontaktovať cez zadaný email.'} %>
</div>

<!-- <iframe name="form-result" style="display: none;"></iframe>-->
<!-- <div class="row">-->
<%#= form_tag 'https://docs.google.com/forms/d/e/1FAIpQLSfUuAjnqGjDvSc-Miy6bP0xODXsjr6g04hGAeYlYkJo-3Iu1Q/formResponse', target: 'form-result', id: 'sk-api-form' do %>
<!-- <div class="col-md-3 col-md-pad">-->
<!-- <div class="form-group">-->
<%#= label_tag 'emailAddress', 'Email', class: 'control-label' %>
<%#= email_field_tag 'emailAddress', nil, class: 'form-control input-lg', id: 'sk-api-email' %>
<!-- </div>-->
<!-- </div>-->
<!-- <div class="col-md-4 col-md-pad">-->
<!-- <%#= label_tag :a, '&nbsp'.html_safe %> <br>-->
<%#= button_tag id: 'submit_to_datahub', class: 'btn btn-default btn-lg btn-strong' do %>
<!-- <strong>Odoslať</strong>-->
<%# end %>
<!-- </div>-->
<%# end %>
<!-- </div>-->

<%= render_registration_form %>
</section>

24 changes: 21 additions & 3 deletions config/locales/docs/sk.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
sk:
registrations:
email: Email
domain: Doména
email: Email
domain: Doména

activemodel:
attributes:
registration:
email: Email
autoform_registration:
domain: Doména
errors:
models:
registration:
attributes:
email:
invalid: je v nesprávnom tvare
autoform_registration:
attributes:
domain:
blank: nie je uvedená



docs:
timestamp_columns: &timestamp_columns
5 changes: 3 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -43,6 +43,7 @@
end

resources :registrations
resources :autoform_registrations, controller: :registrations, path: 'registrations'

resource 'open_data', path: 'otvorene-data'
resource 'open_api', path: 'otvorene-api'
@@ -72,8 +73,8 @@
get '422', to: 'errors#unacceptable'
get '500', to: 'errors#internal_error'

# redirect all /api request to to datahub.ekosystem
match '/api/(*endpoint)', to: redirect(host: 'datahub.ekosystem.slovensko.digital'), via: :all
# redirect all /api request to proper datahub.ekosystem api
match '/api/(*endpoint)', to: redirect(host: Environment.api_host), via: :all
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rozmyslam, ze na co toto tu vobec mame.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

napadaju ma len historicke dovody alebo zbludile requesty


# backward compatibility for generating url in views
concern :syncable do