<commit_before><?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
* Name:			Override
*
* Description:	Allows admins to log in as another user
* 
*/

/**
 * OVERLOADING NAILS'S AUTH MODULE
 * 
 * Note the name of this class; done like this to allow apps to extend this class.
 * Read full explanation at the bottom of this file.
 * 
 **/

require_once '_auth.php';

class NAILS_Override extends NAILS_Auth_Controller
{
	/**
	 * Constructor
	 *
	 * @access	public
	 * @param	none
	 * @return	void
	 * @author	Pablo
	 **/
	public function __construct()
	{
		parent::__construct();
		
		// --------------------------------------------------------------------------
		
		//	If you're not a admin then you shouldn't be accessing this class
		if ( ! $this->user->was_admin() && ! $this->user->is_admin() ) :
		
			$this->session->set_flashdata( 'error', lang( 'auth_no_access' ) );
			redirect( '/' );
			
		endif;
	}
	
	
	// --------------------------------------------------------------------------
	
	
	/**
	 * Log in as another user
	 *
	 * @access	public
	 * @param	none
	 * @return	void
	 * @author	Pablo
	 **/
	public function login_as( )
	{
		//	Perform lookup of user
		$_hashid = $this->uri->segment( 4 );
		$_hashpw = $this->uri->segment( 5 );
		
		$_u = $this->user->get_user_by_hashes( $_hashid, $_hashpw, TRUE );
		
		if ( ! $_u ) :
		
			show_error( lang( 'auth_override_invalid' ) );
			
		endif;
		
		// --------------------------------------------------------------------------
		
		//	Check sign-in permissions; ignore if recovering.
		//	Users cannot:
		//	- Sign in as themselves
		//	- Sign in as superusers or admins (groups 0 and 1)
		//	- Sign in as someone of the same group
		
		if ( ! $this->session->userdata( 'admin_recovery' ) ) :
		
			$_cloning	= ( active_user( 'id' ) == $_u->id );
			$_superuser	= ( isset( $_u->acl['superuser'] ) && $_u->acl['superuser'] );
			$_group		= ( active_user( 'group_id' ) == $_u->group_id );
			
			if ( $_cloning || $_superuser || $_group ) :
			
				if ( $_cloning ) :
				
					show_error( lang( 'auth_override_fail_cloning' ) );
					
				elseif ( $_superuser ) :
				
					show_error( lang( 'auth_override_fail_superuser' ) );
				
				elseif ( $_group ) :
				
					show_error( lang( 'auth_override_fail_group' ) );
				
				endif;
				
			endif;
			
		endif;
		
		// --------------------------------------------------------------------------
		
		//	Prep recovery data
		$_recovery_data						= new stdClass();
		$_recovery_data->id					= md5( active_user( 'id' ) );
		$_recovery_data->hash				= md5( active_user( 'password' ) );
		$_recovery_data->email				= active_user( 'email' );
		$_recovery_data->name				= active_user( 'first_name' );
		$_recovery_data->now_where_was_i	= $this->input->get( 'return_to' );
		
		// --------------------------------------------------------------------------
		
		//	Replace current user's session data
		$this->user->set_login_data( $_u->id, $_u->email, $_u->group_id );
		
		$this->session->set_flashdata( 'success', lang( 'auth_override_ok', title_case( $_u->first_name . ' ' . $_u->last_name ) ) );<commit_msg>Adding the complete URL for returning to admin to the admin recovery_data (make life easy, eh)<commit_after><?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
* Name:			Override
*
* Description:	Allows admins to log in as another user
* 
*/

/**
 * OVERLOADING NAILS'S AUTH MODULE
 * 
 * Note the name of this class; done like this to allow apps to extend this class.
 * Read full explanation at the bottom of this file.
 * 
 **/

require_once '_auth.php';

class NAILS_Override extends NAILS_Auth_Controller
{
	/**
	 * Constructor
	 *
	 * @access	public
	 * @param	none
	 * @return	void
	 * @author	Pablo
	 **/
	public function __construct()
	{
		parent::__construct();
		
		// --------------------------------------------------------------------------
		
		//	If you're not a admin then you shouldn't be accessing this class
		if ( ! $this->user->was_admin() && ! $this->user->is_admin() ) :
		
			$this->session->set_flashdata( 'error', lang( 'auth_no_access' ) );
			redirect( '/' );
			
		endif;
	}
	
	
	// --------------------------------------------------------------------------
	
	
	/**
	 * Log in as another user
	 *
	 * @access	public
	 * @param	none
	 * @return	void
	 * @author	Pablo
	 **/
	public function login_as( )
	{
		//	Perform lookup of user
		$_hashid = $this->uri->segment( 4 );
		$_hashpw = $this->uri->segment( 5 );
		
		$_u = $this->user->get_user_by_hashes( $_hashid, $_hashpw, TRUE );
		
		if ( ! $_u ) :
		
			show_error( lang( 'auth_override_invalid' ) );
			
		endif;
		
		// --------------------------------------------------------------------------
		
		//	Check sign-in permissions; ignore if recovering.
		//	Users cannot:
		//	- Sign in as themselves
		//	- Sign in as superusers or admins (groups 0 and 1)
		//	- Sign in as someone of the same group
		
		if ( ! $this->session->userdata( 'admin_recovery' ) ) :
		
			$_cloning	= ( active_user( 'id' ) == $_u->id );
			$_superuser	= ( isset( $_u->acl['superuser'] ) && $_u->acl['superuser'] );
			$_group		= ( active_user( 'group_id' ) == $_u->group_id );
			
			if ( $_cloning || $_superuser || $_group ) :
			
				if ( $_cloning ) :
				
					show_error( lang( 'auth_override_fail_cloning' ) );
					
				elseif ( $_superuser ) :
				
					show_error( lang( 'auth_override_fail_superuser' ) );
				
				elseif ( $_group ) :
				
					show_error( lang( 'auth_override_fail_group' ) );
				
				endif;
				
			endif;
			
		endif;
		
		// --------------------------------------------------------------------------
		
		//	Prep recovery data
		$_recovery_data						= new stdClass();
		$_recovery_data->id					= md5( active_user( 'id' ) );
		$_recovery_data->hash				= md5( active_user( 'password' ) );
		$_recovery_data->email				= active_user( 'email' );
		$_recovery_data->name				= active_user( 'first_name' );
		$_recovery_data->now_where_was_i	= $this->input->get( 'return_to' );
		$_recovery_data->back_to_admin_url	= site_url( 'auth/override/login_as/' . $_recovery_data->id . '/' . $_recovery_data->hash );
		
		// --------------------------------------------------------------------------
		
		//	Replace current user's session data
		$this->user->set_login_data( $_u->id, $_u->email, $_u->group_id );
		
		$this->session->set_flashdata( 'success', lang( 'auth_override_ok', title_case( $_u->first_name . ' ' . $_u->last_name ) ) );
{'fraction_non_alphanumeric': 0.2528081243268195, 'fraction_numerical': 0.0018464379135251577, 'mean_word_length': 1.8546332894158981, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 74, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5985289', 'n_tokens_mistral': 2418, 'n_tokens_neox': 2015, 'n_words': 484}