Commit 83347954 authored by Pavel Forkert's avatar Pavel Forkert

Add option to admin area to sign in as a specific user

Closes #2291
parent f2999fe8
......@@ -63,6 +63,12 @@ class Admin::UsersController < Admin::ApplicationController
end
end
def login_as
sign_in(user)
flash[:alert] = "Logged in as #{user.username}"
redirect_to root_path
end
def disable_two_factor
user.disable_two_factor!
redirect_to admin_user_path(user),
......
......@@ -72,7 +72,7 @@
= link_to 'New User', new_admin_user_path, class: "btn btn-new btn-sm"
%ul.well-list
- @users.each do |user|
%li
%li{ class: "user-#{user.id}" }
.list-item-name
- if user.blocked?
%i.fa.fa-lock.cred
......@@ -90,6 +90,7 @@
&nbsp;
= link_to 'Edit', edit_admin_user_path(user), id: "edit_#{dom_id(user)}", class: "btn btn-xs"
- unless user == current_user
= link_to 'Log in', login_as_admin_user_path(user), method: :put, class: "btn btn-xs btn-primary"
- if user.blocked?
= link_to 'Unblock', unblock_admin_user_path(user), method: :put, class: "btn btn-xs btn-success"
- else
......
......@@ -262,6 +262,7 @@ Gitlab::Application.routes.draw do
put :unblock
put :unlock
put :confirm
put :login_as
patch :disable_two_factor
delete 'remove/:email_id', action: 'remove_email', as: 'remove_email'
end
......
......@@ -7,6 +7,21 @@ describe Admin::UsersController do
sign_in(admin)
end
describe 'PUT login_as' do
let(:user) { create(:user) }
it 'logs admin as another user' do
expect(warden.authenticate(scope: :user)).not_to eq(user)
put :login_as, id: user.username
expect(warden.authenticate(scope: :user)).to eq(user)
end
it 'redirects user to homepage' do
put :login_as, id: user.username
expect(response).to redirect_to(root_path)
end
end
describe 'DELETE #user with projects' do
let(:user) { create(:user) }
let(:project) { create(:project, namespace: user.namespace) }
......
......@@ -166,4 +166,20 @@ describe "Admin::Users", feature: true do
end
end
end
it 'should be able to log in as another user' do
another_user = create(:user)
visit admin_users_path
page.within ".user-#{another_user.id}" do
click_link 'Log in'
end
expect(page).to have_content("Logged in as #{another_user.username}")
page.within '.sidebar-user .username' do
expect(page).to have_content(another_user.username)
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment