-
-
Save dennismonsewicz/8fc54e22519510c02b95 to your computer and use it in GitHub Desktop.
Sign In As Controller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SignInAsController < ApplicationController | |
before_filter :authenticate_user! | |
include SignInAs::RememberContributor | |
def create | |
# Let's remember the contributor ID for use in the Warden::Strategy | |
self.remember_contributor_id = original_user.try(:id) || current_user.id | |
# Sign out current user | |
sign_out(current_user) | |
# If original_user and original_user ID eql params[:id].to_i, log original_user back in | |
if original_user && original_user.id == params[:id].to_i | |
sign_in(:user, original_user) | |
redirect_to user_root_path | |
else | |
# Else pass off request to custom Warden::Strategy | |
handle_request | |
end | |
end | |
private | |
def handle_request | |
# IF Warden autheticates using Devise::Strategies::SignInAs, redirect them to the correct path | |
if env['warden'].authenticate(:sign_in_as) | |
redirect_to user_root_path | |
else | |
# ELSE sign the contributor back into their account, and tell them they no have rights, they go home | |
sign_in(:user, User.find(remember_contributor_id)) | |
redirect_to user_root_path, notice: "You do not have sufficient rights" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment