Skip to content

Instantly share code, notes, and snippets.

@dzhibas
Forked from karlingen/switch_user.php
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dzhibas/e7cb97284915875a3ed6 to your computer and use it in GitHub Desktop.
Save dzhibas/e7cb97284915875a3ed6 to your computer and use it in GitHub Desktop.
<?php
/**
* Login as another user in SugarCRM and switch back to admin user
*
* Simply put this file into a custom entry point file and
* browse to it with the parameters 'user_name' or 'back_to_sudo'
*
* Usage:
* http://xxxxxxxxx/index.php?entryPoint=my_awesome_entry_point&user_name=mylittlepony
* http://xxxxxxxxx/index.php?entryPoint=my_awesome_entry_point&back_to_sudo=1
*
* @author Karl Metum <karl.metum@codehacker.se>
* @link http://www.codehacker.se
*/
if(!empty($_GET['user_name']) && $GLOBALS['current_user']->is_admin)
{
$user_focus = BeanFactory::getBean('Users');
$user_focus->retrieve_by_string_fields(array('user_name' => $_GET['user_name']));
if(!empty($user_focus->id))
{
if(empty($_SESSION['sudo_user_id']))
$_SESSION['sudo_user_id'] = $GLOBALS['current_user']->id;
$GLOBALS['current_user'] = $user_focus;
$_SESSION['authenticated_user_id'] = $user_focus->id;
echo "Successfully logged in as " . $GLOBALS['current_user']->user_name;
return;
}
}
elseif(!empty($_GET['back_to_sudo']) && !empty($_SESSION['sudo_user_id']))
{
$user_focus = BeanFactory::getBean('Users');
$user_focus->retrieve($_SESSION['sudo_user_id']);
if($user_focus->is_admin)
{
$_SESSION['sudo_user_id'] = null;
$GLOBALS['current_user'] = $user_focus;
$_SESSION['authenticated_user_id'] = $user_focus->id;
echo "Successfully logged back to sudo user: " . $GLOBALS['current_user']->user_name;
return;
}
}
die("No dice.");
@dzhibas
Copy link
Author

dzhibas commented May 5, 2015

<?php
global $current_user, $db; 
if( ! $current_user->is_admin)
    if(empty($_SESSION['was_admin']))
        die('Need to be administrator to access'); 
if( ! empty($_GET['user_id']))
{
    $_SESSION['user_id'] = $_GET['user_id']; 
    $_SESSION['authenticated_user_id'] = $_GET['user_id'];
    $_SESSION['was_admin'] = true;

    $user = new User(); 
    $user->retrieve($_GET['user_id']);
    $current_user = $user; 
    header('Location: index.php?module=Home');
}
$sql = "SELECT id, user_name, first_name, last_name FROM users WHERE deleted = 0 AND is_group = 0";
$resultset = $db->query($sql);
while($row = $db->fetchByAssoc($resultset))
{
    echo '<a href="index.php?module=Administration&action=SwitchUser&user_id=' . $row['id'] . '"> ' . $row['user_name'] . '</a> '.$row['first_name'].' ' . $row['last_name'] .' <br/><br/>';
}
?>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment