-
Notifications
You must be signed in to change notification settings - Fork 4
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
base: master
Are you sure you want to change the base?
Changes from all commits
0d18e35
7726cc8
6da4fa7
5df2234
fbe5ee9
b9859c5
7cbef38
ff7cc03
dd18af4
41c8766
51bb993
4ffa498
26d02dc
10832c8
1e227f8
d95d1de
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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
|
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! } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Co robi ten block? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nechcem rypat ale There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
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 |
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 |
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 |
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 |
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 |
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, ' '.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> |
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.' } %>"); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
$('#<%= @registration.html_id %>').replaceWith('<%= j render_registration_form(model: @registration) %>'); |
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">×</span></button> | ||
<%= message %> | ||
</div> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, ' '.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"> | ||
|
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
😆
v produkcii sa budu kluce citat z ENV samozrejme.
dame prec?
There was a problem hiding this comment.
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.