Expand TARA auth flow to registrant portal

This commit is contained in:
Karl Erik Õunapuu 2020-10-01 17:01:54 +03:00
parent 248c984443
commit 04f0ef9a93
No known key found for this signature in database
GPG key ID: C9DD647298A34764
7 changed files with 85 additions and 27 deletions

View file

@ -0,0 +1,33 @@
class Registrant
class TaraController < ApplicationController
skip_authorization_check
# rubocop:disable Style/AndOr
def callback
session[:omniauth_hash] = user_hash
@registrant_user = RegistrantUser.find_or_create_by_omniauth_data(user_hash)
if @registrant_user
flash[:notice] = t(:signed_in_successfully)
sign_in_and_redirect(:registrant_user, @registrant_user)
else
show_error and return
end
end
# rubocop:enable Style/AndOr
def cancel
redirect_to root_path, notice: t(:sign_in_cancelled)
end
def show_error
redirect_to new_registrant_user_session_url, alert: t(:no_such_user)
end
private
def user_hash
request.env['omniauth.auth']
end
end
end

View file

@ -66,23 +66,19 @@ class RegistrantUser < User
find_or_create_by_user_data(user_data)
end
def find_or_create_by_mid_data(response)
user_data = { first_name: response.user_givenname, last_name: response.user_surname,
ident: response.user_id_code, country_code: response.user_country }
def find_or_create_by_omniauth_data(omniauth_hash)
uid = omniauth_hash['uid']
identity_code = uid.slice(2..-1)
country_code = uid.slice(0..1)
first_name = omniauth_hash.dig('info', 'first_name')
last_name = omniauth_hash.dig('info', 'last_name')
user_data = { first_name: first_name, last_name: last_name,
ident: identity_code, country_code: country_code }
find_or_create_by_user_data(user_data)
end
def find_by_id_card(id_card)
registrant_ident = "#{id_card.country_code}-#{id_card.personal_code}"
username = [id_card.first_name, id_card.last_name].join("\s")
user = find_or_initialize_by(registrant_ident: registrant_ident)
user.username = username
user.save!
user
end
private
def find_or_create_by_user_data(user_data = {})

View file

@ -8,11 +8,6 @@
<%= t '.hint' %>
</div>
<br/>
<%= link_to '/registrant/login/mid' do %>
<%= image_tag 'mid.gif' %>
<% end %>
<%= link_to registrant_id_card_sign_in_path, method: :post do %>
<%= image_tag 'id_card.gif' %>
<% end %>
<%= link_to t(:sign_in), "/auth/rant_tara", method: :post, class: 'btn btn-lg btn-primary btn-block' %>
</div>
</div>
</div>