<?php

/*
 * PiAnoS (Picture Annotation System), A fingerprint examiner training tool
 * Copyright (C) 2011-2021 University of Lausanne, Switzerland
 * https://ips-labs.unil.ch/pianos
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
 */

function switchToUser( $uid, $back = false )
{
    $ouid = $_SESSION[ 'user' ][ 'id' ];
    
    $uid = ( int ) $uid;
    
    $db = Application::instance()->getDB();
    
    $res = $db->query( "SELECT username, name FROM person WHERE id = " . $uid . ";" );
    
    if( count( $res ) == 0 )
        throw new Exception( "User not found" );
        
    $username = $res[ 0 ][ 'username' ];
    $displayName = $res[ 0 ][ 'name' ];
    
    unset( $_SESSION[ 'security' ][ 'login_challenge' ] );
    $_SESSION[ 'user' ] = array (
        'id' => $uid,
        'username' => $username,
        'name' => $displayName,
        'switchedUser' => ! $back,
        'originalUser' => $ouid,
    );
    
    // Set a isAdmin (i.e. isTutor) flag to the session variable - this is quite
    // often needed when rendering the interface. A user is said to be an admin
    // (or tutor) if at least one group he belongs to is a tutors group.
    $res = $db->query( "SELECT bool_or(pgroup.tutors) FROM membership,pgroup WHERE membership.person = " . ( int ) $uid . " AND membership.pgroup = pgroup.id;" );
    $_SESSION[ 'user' ][ 'isadmin' ] = ( count( $res ) == 1 ) && ( $res[ 0 ][ 'bool_or' ] === 't' );
    
    // Load the base settings for this user. Also, check for first-time logins
    // and update the last login timestamp.
    $res = $db->query( "SELECT * FROM person WHERE id = " . ( int ) $_SESSION[ 'user' ][ 'id' ] . ";" );
    if( count( $res ) != 1 )
        throw new Exception( "Access denied" );
    
    $pdata = $res[ 0 ];
    $_SESSION[ 'is_first_login' ] = $pdata[ 'last_login' ] === NULL;
    $_SESSION[ 'settings' ] = array (
        'suitability_approach' => $pdata[ 'suitability_approach' ]
    );
    
    if( $pdata[ 'must_change_pwd' ] !== 'f' )
        $_SESSION[ 'security' ][ 'must_change_pwd' ] = 1;
        
    $db->query( "UPDATE person SET last_login = now() WHERE id = " . ( int ) $_SESSION[ 'user' ][ 'id' ] . ";" );
}

function changeUser( $uid = null, $exid = null, $step = null )
{
    $dest = "?";
    
	if( $_SESSION[ 'user' ][ 'isadmin' ] && $uid != null )
	{
	    switchToUser( $uid, false );
	} else if( isset( $_SESSION[ 'user' ][ 'originalUser' ] ) && $_SESSION[ 'user' ][ 'switchedUser' ] ) {
	    switchToUser( $_SESSION[ 'user' ][ 'originalUser' ], true );
	} else {
	    throw new Exception( "Access denied" );
	}
	
	// Destination to a particular exercise
	if( $exid != null )
	    $dest = "?action=take&id=" . ( int ) $exid;
    
    if( $step != null )
        $dest .= "&step=" . $step;
	
	// Do an HTTP redirect to the home page, so that the user can refresh the
	// page without re-sending the username/password.
	header( "Location: " . $dest );
	exit();
}
