From 0a0543c71513295d565a35b1f737fddbb208a799 Mon Sep 17 00:00:00 2001 From: Martin Lensment Date: Tue, 28 Jul 2015 17:56:01 +0300 Subject: [PATCH] Switch user in registrar #2754 --- app/controllers/registrar/sessions_controller.rb | 6 ++++++ app/views/layouts/registrar/application.haml | 9 ++++++++- config/locales/en.yml | 2 +- config/routes.rb | 1 + spec/features/registrar/domain_spec.rb | 16 ++++++++++++++++ 5 files changed, 32 insertions(+), 2 deletions(-) diff --git a/app/controllers/registrar/sessions_controller.rb b/app/controllers/registrar/sessions_controller.rb index 940e5f415..6875d9291 100644 --- a/app/controllers/registrar/sessions_controller.rb +++ b/app/controllers/registrar/sessions_controller.rb @@ -64,6 +64,12 @@ class Registrar::SessionsController < Devise::SessionsController # rubocop:enable Metrics/MethodLength # rubocop:enable Metrics/AbcSize + def switch_user + @api_user = ApiUser.find(params[:id]) + sign_in @api_user if @api_user.identity_code == current_user.identity_code + redirect_to :back + end + def id @user = ApiUser.find_by_idc_data(request.env['SSL_CLIENT_S_DN']) diff --git a/app/views/layouts/registrar/application.haml b/app/views/layouts/registrar/application.haml index 8b92a5b6d..d6c8cf30c 100644 --- a/app/views/layouts/registrar/application.haml +++ b/app/views/layouts/registrar/application.haml @@ -49,8 +49,15 @@ %li{class: active_class}= link_to t(:xml_console), registrar_xml_console_path %ul.nav.navbar-nav.navbar-right + %li.dropdown + %a.dropdown-toggle{"data-toggle" => "dropdown", href: "#"} + = "#{current_user} (#{current_user.roles.first}) - #{current_user.registrar}" + %span.caret + %ul.dropdown-menu{role: "menu"} + - ApiUser.where(identity_code: current_user.identity_code).includes(:registrar).each do |x| + %li= link_to "#{x} (#{x.roles.first}) - #{x.registrar}", "/registrar/switch_user/#{x.id}" - if user_signed_in? - %li= link_to t(:log_out, user: current_user), '/registrar/logout' + %li= link_to t(:log_out_), '/registrar/logout' .container = render 'shared/flash' diff --git a/config/locales/en.yml b/config/locales/en.yml index 2051e1e82..fc124e6a4 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -555,7 +555,6 @@ en: username: 'Username' password: 'Password' log_in: 'Log in' - log_out: 'Log out (%{user})' domains: 'Domains' register: 'Register' check: 'Check' @@ -879,3 +878,4 @@ en: notes: Notes active_price_for_this_operation_is: 'Active price for this operation is %{price}' active_price_missing_for_this_operation: 'Active price missing for this operation!' + log_out_: 'Log out' diff --git a/config/routes.rb b/config/routes.rb index 925947ea9..c1b7ad237 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -41,6 +41,7 @@ Rails.application.routes.draw do post 'sessions' => 'sessions#create' post 'id' => 'sessions#id' post 'mid' => 'sessions#mid' + get 'switch_user/:id' => 'sessions#switch_user' get 'logout' => '/devise/sessions#destroy' end diff --git a/spec/features/registrar/domain_spec.rb b/spec/features/registrar/domain_spec.rb index c72f50b84..057a23849 100644 --- a/spec/features/registrar/domain_spec.rb +++ b/spec/features/registrar/domain_spec.rb @@ -39,5 +39,21 @@ feature 'Domains', type: :feature do visit '/registrar/domains/new' current_path.should == '/registrar/domains/new' end + + it 'should switch user' do + d1 = Fabricate(:domain, registrar: @user.registrar) + user2 = Fabricate(:api_user, identity_code: @user.identity_code) + d2 = Fabricate(:domain, registrar: user2.registrar) + + visit '/registrar/domains' + + page.should have_text(d1.name) + page.should_not have_text(d2.name) + + click_link "#{user2} (#{user2.roles.first}) - #{user2.registrar}" + + page.should_not have_text(d1.name) + page.should have_text(d2.name) + end end end