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
Show file tree
Hide file tree
Changes from 5 commits
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
4 changes: 4 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ GTM_ID=
ROLLBAR_ACCESS_TOKEN=
NEWRELIC_LICENSE_KEY=
SECRET_KEY_BASE=
RECAPTCHA_SITE_KEY_V3=
RECAPTCHA_SECRET_KEY_V3=
RECAPTCHA_SITE_KEY_V2_CHECKBOX=
michal-rohacek marked this conversation as resolved.
Show resolved Hide resolved
RECAPTCHA_SECRET_KEY_V2_CHECKBOX=
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ gem 'rollbar'
# Utilities
gem 'redis-rails'
gem 'rest-client'
gem 'recaptcha'

group :development, :test do
gem 'dotenv-rails'
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ GEM
rb-fsevent (0.10.4)
rb-inotify (0.10.1)
ffi (~> 1.0)
recaptcha (5.5.0)
json
redis (4.2.2)
redis-actionpack (5.2.0)
actionpack (>= 5, < 7)
Expand Down Expand Up @@ -293,6 +295,7 @@ DEPENDENCIES
rack-mini-profiler
rails (~> 5.2.4.4)
rails_12factor
recaptcha
redis-rails
rest-client
rollbar
Expand Down
39 changes: 39 additions & 0 deletions app/controllers/registrations_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
class RegistrationsController < ApplicationController
include RegistrationsHelper

def create
render :create and return unless registration.valid?(:submit)
michal-rohacek marked this conversation as resolved.
Show resolved Hide resolved

if recaptcha_valid?
registration.finish and render :success
else
registration.checkbox_captcha! and render :create
end
end

private

def recaptcha_valid?
if registration.checkbox_captcha
verify_checkbox_captcha
else
verify_invisible_captcha
end
end

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)
end

def registration_params
params.require(:registration).permit(:service, :email, :checkbox_captcha)
michal-rohacek marked this conversation as resolved.
Show resolved Hide resolved
end

def registration
@registration ||= Registration.new(registration_params)
end
end
2 changes: 2 additions & 0 deletions app/controllers/services/datahub_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class Services::DatahubController < ContentController
include RegistrationsHelper

def index
@page.title = 'Vyčistené, štruktúrované dáta nielen o firmách. &middot; Datahub'.html_safe
@page.og.image = view_context.image_url('fb-datahub.png')
Expand Down
25 changes: 25 additions & 0 deletions app/helpers/registrations_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module RegistrationsHelper
def registration_form_id(service = controller_name)
"#{service}_registration_form"
end

def recaptcha_action
"#{controller_name}_registration"
end

def controller_specific_register_path
polymorphic_path([:services, controller_name, :index])
end

def render_registration_form(service: controller_name, registration: nil)
michal-rohacek marked this conversation as resolved.
Show resolved Hide resolved
registration ||= Registration.new(service: service)

if registration.valid?(:render)
render partial: 'registrations/form', object: registration
end
end

def translate_field(field_name)
michal-rohacek marked this conversation as resolved.
Show resolved Hide resolved
translate "registrations.#{field_name}", default: ''
end
end
51 changes: 51 additions & 0 deletions app/models/registration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
class Registration
include ActiveModel::Model
include ActiveModel::Attributes

SUBMIT_MAPPINGS = HashWithIndifferentAccess.new(
michal-rohacek marked this conversation as resolved.
Show resolved Hide resolved
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

def finish
michal-rohacek marked this conversation as resolved.
Show resolved Hide resolved
args = attributes.transform_keys { |key| mappings[key] }.symbolize_keys!

RestClient.post(data[:url], **args)
end

private

def checkbox_captcha!
checkbox_captcha = true
end

def data
SUBMIT_MAPPINGS[service]
end

def attributes
other_fields.reverse_merge(email: email)
end
end
37 changes: 37 additions & 0 deletions app/views/registrations/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<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 } %>
<% end %>
<% end %>

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

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

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

<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>
</div>
<% end %>
</div>
</div>
1 change: 1 addition & 0 deletions app/views/registrations/create.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$('#<%= registration_form_id(@registration.service) %>').html("<%= j render partial: 'registrations/form', object: @registration %>");
1 change: 1 addition & 0 deletions app/views/registrations/success.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$('#<%= registration_form_id(@registration.service) %>').html("<%= j render 'services/share/flash_message', { type: :notice, message: 'Ďakujeme za Váš záujem. Budeme Vás kontaktovať cez zadaný email.' } %>");
47 changes: 25 additions & 22 deletions app/views/services/autoform/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -157,29 +157,32 @@

<div id="autoform_flash"></div>
<div class="row" id="autoform_client_form_wrapper">
<iframe name="form-result" style="display: none;"></iframe>
<%= form_tag 'https://docs.google.com/forms/d/1TpYNJfBQVGt4lmKP-wrXGkok2bo-Y6mzpmeUsJTnRis/formResponse', target: 'form-result', id: 'autoform-form' do %>
<div class="col-md-3 col-md-pad">
<div class="form-group">
<%= label_tag 'entry.204431983', 'Email', class: 'control-label' %>
<%= email_field_tag 'entry.204431983', nil, class: 'form-control input-lg', id: 'autoform-email' %>
</div>
</div>
<div class="col-md-3 col-md-pad">
<div class="form-group">
<%= label_tag 'entry.1349114640', 'Doména', class: 'control-label' %>
<%= text_field_tag 'entry.1349114640', nil, class: 'form-control input-lg', id: 'autoform-domain' %>
<p class="help-block">Napríklad: www.websupport.sk</p>
</div>
</div>
<div class="col-md-4 col-md-pad">
<%= label_tag :a, '&nbsp'.html_safe %> <br>
<%= button_tag id: 'submit_to_autoform', class: 'btn btn-default btn-lg' do %>
<strong>Zaregistrovať</strong>
<% end %>
</div>
<% end %>
<!-- <iframe name="form-result" style="display: none;"></iframe>-->
<%#= form_tag 'https://docs.google.com/forms/d/1TpYNJfBQVGt4lmKP-wrXGkok2bo-Y6mzpmeUsJTnRis/formResponse', target: 'form-result', id: 'autoform-form' do %>
<!-- <div class="col-md-3 col-md-pad">-->
<!-- <div class="form-group">-->
<%#= label_tag 'entry.204431983', 'Email', class: 'control-label' %>
<%#= email_field_tag 'entry.204431983', nil, class: 'form-control input-lg', id: 'autoform-email' %>
<!-- </div>-->
<!-- </div>-->
<!-- <div class="col-md-3 col-md-pad">-->
<!-- <div class="form-group">-->
<%#= label_tag 'entry.1349114640', 'Doména', class: 'control-label' %>
<%#= text_field_tag 'entry.1349114640', nil, class: 'form-control input-lg', id: 'autoform-domain' %>
<!-- <p class="help-block">Napríklad: www.websupport.sk</p>-->
<!-- </div>-->
<!-- </div>-->
<!-- <div class="col-md-4 col-md-pad">-->
<!-- <%#= label_tag :a, '&nbsp'.html_safe %> <br>-->
<%#= button_tag id: 'submit_to_autoform', class: 'btn btn-default btn-lg' do %>
<!-- <strong>Zaregistrovať</strong>-->
<%# end %>
<!-- </div>-->
<%# end %>
</div>

<%= render_registration_form %>
jsuchal marked this conversation as resolved.
Show resolved Hide resolved

<div class="row">
<div class="col-md-10 col-md-pad">
<br>
Expand Down
24 changes: 1 addition & 23 deletions app/views/services/datahub/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -132,30 +132,8 @@
<p class="lead">Jednoduchá registrácia na prístup k SQL rozhraniu a API.
<em>Kompletné dáta zdrojových registrov.</em> API na konsolidované a&nbsp;prepojené dáta. <em>256 dopytov mesačne pre <strong>kohokoľvek</strong></em>, viac po dohode.</p>

<div id="datahub-error" style="display: none;">
<%= render 'services/share/flash_message', {type: :alert, message: 'Vyplňte prosím email a skúste znova.'} %>
</div>
<div id="datahub-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>
<%= render_registration_form %>

<iframe name="form-result" style="display: none;"></iframe>
<div class="row">
<%= form_tag 'https://docs.google.com/forms/d/e/1FAIpQLSdgW4Hf2fEhX3cpTkoYJTaIVs8pWrTFrItt9Hj_9ZD36yPLZQ/formResponse', target: 'form-result', id: 'datahub-form' do %>
<div class="col-md-3 col-md-pad">
<div class="form-group">
<%= label_tag 'entry.1902802364', 'Email', class: 'control-label' %>
<%= email_field_tag 'entry.1902802364', nil, class: 'form-control input-lg', id: 'datahub-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>Zaregistrovať</strong>
<% end %>
</div>
<% end %>
</div>
<div class="row">
<div class="col-md-7 col-md-pad">
<br>
Expand Down
5 changes: 1 addition & 4 deletions app/views/services/share/_flash_message.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
<%
css_class = (type == :alert) ? 'danger' : 'success'
%>
<div class="alert alert-<%= css_class %> alert-dismissible" role="alert">
<div class="alert alert-<%= (type == :alert) ? 'danger' : 'success' %> alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<%= message %>
</div>
36 changes: 19 additions & 17 deletions app/views/services/slovensko_sk_api/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -88,23 +88,25 @@
<%= 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>
<!-- <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>

<section id="clients">
Expand Down
4 changes: 4 additions & 0 deletions config/initializers/recaptcha.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Recaptcha.configure do |config|
config.site_key = ENV.fetch('RECAPTCHA_SITE_KEY_V3')
config.secret_key = ENV.fetch('RECAPTCHA_SECRET_KEY_V3')
end
4 changes: 4 additions & 0 deletions config/locales/docs/sk.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
sk:
registrations:
email: Email
domain: Doména

docs:
timestamp_columns: &timestamp_columns
created_at:
Expand Down
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
resources :slovensko_sk_api, path: 'slovensko-sk-api'
end

resources :registrations

resource 'open_data', path: 'otvorene-data'
resource 'open_api', path: 'otvorene-api'

Expand Down