From f83e532fb1e5888aef0ef4d621af03551afa556d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Erik=20=C3=95unapuu?= Date: Thu, 1 Oct 2020 18:37:04 +0300 Subject: [PATCH] Make Registrant/Registrar use same TARA controller --- app/controllers/registrant/tara_controller.rb | 33 --------------- app/controllers/registrar/tara_controller.rb | 33 --------------- app/controllers/sso/tara_controller.rb | 40 +++++++++++++++++++ config/initializers/devise.rb | 2 - config/routes.rb | 32 +++++++-------- 5 files changed, 56 insertions(+), 84 deletions(-) delete mode 100644 app/controllers/registrant/tara_controller.rb delete mode 100644 app/controllers/registrar/tara_controller.rb create mode 100644 app/controllers/sso/tara_controller.rb diff --git a/app/controllers/registrant/tara_controller.rb b/app/controllers/registrant/tara_controller.rb deleted file mode 100644 index a09e4665b..000000000 --- a/app/controllers/registrant/tara_controller.rb +++ /dev/null @@ -1,33 +0,0 @@ -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 diff --git a/app/controllers/registrar/tara_controller.rb b/app/controllers/registrar/tara_controller.rb deleted file mode 100644 index e02aa52a5..000000000 --- a/app/controllers/registrar/tara_controller.rb +++ /dev/null @@ -1,33 +0,0 @@ -class Registrar - class TaraController < ApplicationController - skip_authorization_check - - # rubocop:disable Style/AndOr - def callback - session[:omniauth_hash] = user_hash - @api_user = ApiUser.from_omniauth(user_hash) - - if @api_user - flash[:notice] = t(:signed_in_successfully) - sign_in_and_redirect(:registrar_user, @api_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_registrar_user_session_url, alert: t(:no_such_user) - end - - private - - def user_hash - request.env['omniauth.auth'] - end - end -end diff --git a/app/controllers/sso/tara_controller.rb b/app/controllers/sso/tara_controller.rb new file mode 100644 index 000000000..1571f45b3 --- /dev/null +++ b/app/controllers/sso/tara_controller.rb @@ -0,0 +1,40 @@ +module Sso + class TaraController < ApplicationController + skip_authorization_check + + def registrant_callback + user = RegistrantUser.find_or_create_by_omniauth_data(user_hash) + callback(user, registrar: false) + end + + def registrar_callback + user = ApiUser.from_omniauth(user_hash) + callback(user, registrar: true) + end + + # rubocop:disable Style/AndOr + def callback(user, registrar: true) + session[:omniauth_hash] = user_hash + (show error and return) unless user + + flash[:notice] = t(:signed_in_successfully) + sign_in_and_redirect(registrar ? :registrar_user : :registrant_user, user) + end + # rubocop:enable Style/AndOr + + def cancel + redirect_to root_path, notice: t(:sign_in_cancelled) + end + + def show_error(registrar: true) + path = registrar ? new_registrar_user_session_url : new_registrant_user_session_url + redirect_to path, alert: t(:no_such_user) + end + + private + + def user_hash + request.env['omniauth.auth'] + end + end +end diff --git a/config/initializers/devise.rb b/config/initializers/devise.rb index 6631a0239..eb0465796 100644 --- a/config/initializers/devise.rb +++ b/config/initializers/devise.rb @@ -280,6 +280,4 @@ Devise.setup do |config| # When using OmniAuth, Devise cannot automatically set OmniAuth path, # so you need to do it manually. For the users scope, it would be: # config.omniauth_path_prefix = '/my_engine/users/auth' - - routes = [nil, :new, :destroy] end diff --git a/config/routes.rb b/config/routes.rb index 5d6b3d907..7df315f88 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -76,12 +76,6 @@ Rails.application.routes.draw do devise_for :users, path: '', class_name: 'ApiUser', skip: %i[sessions] - devise_scope :registrar_user do - match '/open_id/callback', via: %i[get post], to: 'tara#callback', as: :tara_callback - match '/open_id/cancel', via: %i[get post delete], to: 'tara#cancel', - as: :tara_cancel - end - resources :invoices, except: %i[new create edit update destroy] do resource :delivery, controller: 'invoices/delivery', only: %i[new create] @@ -158,6 +152,22 @@ Rails.application.routes.draw do post 'sessions', to: 'registrar/sessions#create', as: :registrar_user_session delete 'sign_out', to: 'registrar/sessions#destroy', as: :destroy_registrar_user_session + + # TARA + match '/open_id/callback', via: %i[get post], to: 'sso/tara#registrar_callback' + match '/open_id/cancel', via: %i[get post delete], to: 'sso/tara#cancel' + end + end + + scope :registrant do + devise_scope :registrant_user do + get 'sign_in', to: 'registrant/sessions#new', as: :new_registrant_user_session + post 'sessions', to: 'registrant/sessions#create', as: :registrant_user_session + delete 'sign_out', to: 'registrant/sessions#destroy', as: :destroy_registrant_user_session + + # TARA + match '/open_id/callback', via: %i[get post], to: 'sso/tara#registrant_callback' + match '/open_id/cancel', via: %i[get post delete], to: 'sso/tara#cancel' end end @@ -166,16 +176,6 @@ Rails.application.routes.draw do # POST /registrant/sign_in is not used devise_for :users, path: '', class_name: 'RegistrantUser' - devise_scope :registrant_user do - get 'login/mid' => 'sessions#login_mid' - post 'login/mid' => 'sessions#mid' - post 'login/mid_status' => 'sessions#mid_status' - post 'mid' => 'sessions#mid' - - match '/open_id/callback', via: %i[get post], to: 'tara#callback', as: :tara_registrant_callback - match '/open_id/cancel', via: %i[get post delete], to: 'tara#cancel', - as: :tara_registrant_cancel - end resources :registrars, only: :show resources :domains, only: %i[index show] do