Skip to content
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

Add an impersonate flow (#552) #798

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,23 @@ def confirm_email
end
end

# Development Only

def impersonate
raise unless Rails.env.development?

@all_users = User.all
end

def impersonate_login
raise unless Rails.env.development?

user = User.find(params[:user])

reset_session
set_session_and_redirect_returning_users(user)
end

private

def set_session_and_redirect_returning_users(user)
Expand Down
14 changes: 14 additions & 0 deletions app/helpers/sessions_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module SessionsHelper
def users_grouped_by_role_for_select(users)
users_by_state = users.group_by do |user|
user.is_admin? ? "Admin" : user.state.humanize
end

grouped_options = users_by_state.keys.sort.map do |state|
options = users_by_state[state].map { |user| [user.name, user.id] }
[state, options]
end

return grouped_options_for_select(grouped_options)
end
end
5 changes: 5 additions & 0 deletions app/views/sessions/impersonate.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
%h2 (DEV) Impersonate User

= form_tag impersonate_login_path, class: "form", method: :post do |f|
= select_tag :user, users_grouped_by_role_for_select(@all_users)
= submit_tag "Impersonate", class: "btn btn-primary"
3 changes: 3 additions & 0 deletions app/views/sessions/login.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
%p.ib= link_to 'Sign in with GitHub', github_login_path, class: 'btn btn-primary btn-lg mr-20'
%p.ib= link_to 'Sign in with Google', google_login_path, class: 'btn btn-primary btn-lg'

- if Rails.env.development?
%p= link_to '(DEV) Impersonate User', impersonate_path, class: 'btn btn-default'

- if Configurable[:accepting_applications]
%p To start an application, first authenticate with GitHub or Google. If you have any questions, email us at #{ mail_to JOIN_EMAIL }.

5 changes: 5 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@
get "get_email" => "sessions#get_email"
post "confirm_email" => "sessions#confirm_email"

if Rails.env.development?
get "impersonate" => "sessions#impersonate"
post "impersonate_login" => "sessions#impersonate_login"
end

post "add_github_auth" => "authentications#add_github_auth"
post "add_google_auth" => "authentications#add_google_auth"

Expand Down