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 all 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
5 changes: 4 additions & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
AUTOFORM_URL=https://example.com
DATAHUB_URL=
AUTOFORM_URL=
AUTOFORM_ACCESS_TOKEN=
AUTOFORM_PRIVATE_ACCESS_TOKEN=
GOVBOX_FORM_ENDPOINT=
Expand All @@ -7,3 +8,5 @@ GTM_ID=
ROLLBAR_ACCESS_TOKEN=
NEWRELIC_LICENSE_KEY=
SECRET_KEY_BASE=
RECAPTCHA_SITE_KEY_V3=6LcNQjAbAAAAAPNGbQNxDu0RCKOFOHRdopkJ4bU4
RECAPTCHA_SECRET_KEY_V3=6LcNQjAbAAAAAFvDD1DQJfwWVsF4npB9pI928TpU
Comment on lines +11 to +12
Copy link
Member

Choose a reason for hiding this comment

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

Toto by sme nemali davat do public repo a ani do private repo.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

su to specialne na testing kluce, povoleny maju len localhost..... Prislo mi to praktickejsie, ako predstava, ze ked si budes najblizsie lokalne pustat projekt, budes 15 minut riesit, preco ti tam ukazuje nic nehovoriacu chybu
image
😆
v produkcii sa budu kluce citat z ENV samozrejme.

dame prec?

Copy link
Member

Choose a reason for hiding this comment

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

Tak treba fixnut error hlasku.

7 changes: 7 additions & 0 deletions .env.test
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
DATAHUB_URL=https://example.com
AUTOFORM_URL=https://example.com
AUTOFORM_ACCESS_TOKEN=
AUTOFORM_PRIVATE_ACCESS_TOKEN=
GOVBOX_FORM_ENDPOINT=https://example.com
GA_TRACKING_ID=
GTM_ID=
ROLLBAR_ACCESS_TOKEN=
NEWRELIC_LICENSE_KEY=
SECRET_KEY_BASE=
RECAPTCHA_SITE_KEY_V3=6LcNQjAbAAAAAPNGbQNxDu0RCKOFOHRdopkJ4bU4
RECAPTCHA_SECRET_KEY_V3=6LcNQjAbAAAAAFvDD1DQJfwWVsF4npB9pI928TpU
michal-rohacek marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -176,6 +176,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 @@ -297,6 +299,7 @@ DEPENDENCIES
rack-mini-profiler
rails (~> 5.2.4.4)
rails_12factor
recaptcha
redis-rails
rest-client
rollbar
Expand Down
10 changes: 8 additions & 2 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@ class ApplicationController < ActionController::Base

before_action :store_referral

helper_method :rollout

def rollout
r = Rollout.new
r.add_feature(:govbox, Time.new(2017, 5, 18, 10, 0, 0))
r.activate!(params[:rollout].to_sym) if params[:rollout]
r
end

helper_method :rollout

def api_host
Environment.api_host
end

helper_method :api_host

def store_referral
if params[:ref]
cookies[:ref] = { value: params[:ref], expires: 30.days.from_now }
Expand Down
24 changes: 24 additions & 0 deletions app/controllers/registrations_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class RegistrationsController < ApplicationController
def create
@registration = Registration.build_from(registration_params)

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
render :new
end
end

private

def validate_captcha!
captcha_result = verify_recaptcha(minimum_score: 0.5, action: helpers.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']

captcha_result
end

def registration_params
params.require(:registration).permit(:email, :service, :score, :domain)
end
end
7 changes: 0 additions & 7 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,2 @@
module ApplicationHelper
def api_host
if Rails.env.staging?
'datahub.ekosystem.staging.slovensko.digital'
else
'datahub.ekosystem.slovensko.digital'
end
end
end
11 changes: 11 additions & 0 deletions app/helpers/registrations_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module RegistrationsHelper
def recaptcha_action(service = controller_name)
"#{service}_registration"
end

def render_registration_form(service: controller_name, model: nil)
model ||= Registration.build_from(service: service)

render partial: 'registrations/form', object: model
end
end
5 changes: 5 additions & 0 deletions app/lib/environment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Environment
def self.api_host
@host ||= URI.parse(ENV.fetch('DATAHUB_URL')).host
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
michal-rohacek marked this conversation as resolved.
Show resolved Hide resolved

def user_input_fields
super.append(:domain)
end

def mapping
super.merge(EXTRA_FIELDS)
end
end
58 changes: 58 additions & 0 deletions app/models/registration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
class Registration
include ActiveModel::Model

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

def save
return false unless valid?(:submit)
jsuchal marked this conversation as resolved.
Show resolved Hide resolved

if block_given?
return false unless yield
end

RestClient.post(FORM_URL, **build_request_params)
end

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

def user_input_fields
[:email]
end

def html_id
"#{service}_registration_form"
end

def mapping
REQUEST_MAPPING
end

private_constant :REQUEST_MAPPING

def self.build_from(args)
class_for(args[:service]).new(args)
end

def self.class_for(service)
case service
when 'autoform'
AutoformRegistration
when 'datahub', 'slovensko_sk_api'
Registration
else
raise "Service is nil or unknown: #{service}"
end
end
end
35 changes: 35 additions & 0 deletions app/views/registrations/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<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 %>

<%= recaptcha_v3(action: recaptcha_action(form.service)) %>

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

<%= f.hidden_field :service %>

<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>
<% end %>
</div>
</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.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) %>');
32 changes: 1 addition & 31 deletions app/views/services/autoform/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -148,38 +148,8 @@
<h2>Registrácia</h2>
<p class="lead">Jednoduchá registrácia, <em>až 256 vyhľadávaní mesačne <strong>zadarmo</strong></em>. Viac vyhľadávaní podľa <%= link_to 'cenníka', pricing_path(anchor: 'autoform') %>.</p>

<div id="autoform-error" style="display: none;">
<%= render 'services/share/flash_message', {type: :alert, message: 'Niektoré údaje ste nezadali správne. Opravte ich prosím a skúste znova.'} %>
</div>
<div id="autoform-form-sent" style="display: none;">
<%= render 'services/share/flash_message', {type: :notice, message: 'Žiadosť o registráciu bola odoslaná na schválenie. Ďakujeme za Váš záujem a budeme Vás kontaktovať.'} %>
</div>
<%= render_registration_form %>
jsuchal marked this conversation as resolved.
Show resolved Hide resolved

<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 %>
</div>
<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 @@ -130,30 +130,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>
26 changes: 1 addition & 25 deletions app/views/services/slovensko_sk_api/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -81,31 +81,7 @@
<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>

<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
22 changes: 22 additions & 0 deletions config/locales/docs/sk.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,26 @@
sk:
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
created_at:
Expand Down
Loading