<?php
/**
 * Mantis auth backend
 *
 * Uses external Trust mechanism to check against Mantis'
 * user cookie.
 *
 * @author    Victor Boctor (http://www.futureware.biz)
 */
 
require_once( MANTIS_ROOT . 'core.php' );
 
#dbg($GLOBALS);
 
class auth_mantis extends auth_basic {
  /**
   * Constructor.
   *
   * Sets additional capabilities and config strings
   */
  function auth_mantis(){
    $this->cando['external'] = true;
  }
 
  /**
   * Authenticates the user using Mantis APIs.
   */
  function trustExternal($user,$pass,$sticky=false){
    global $USERINFO;
    global $conf;
 
    if ( auth_is_user_authenticated() ) {
      // okay we're logged in - set the globals
      $USERINFO['pass'] = current_user_get_field( 'password' );
      $USERINFO['name'] = current_user_get_field( 'username' );
      $USERINFO['mail'] = current_user_get_field( 'email' );
 
//      $t_project_name = getNS( getID() );
//      $t_project_id = project_get_id_by_name( $t_project_name );
      $t_project_name = explode( ':', getNS( getID() ) );
      $t_project_id = project_get_id_by_name( str_replace('_',' ',$t_project_name[1]) );

      $t_access_level = access_get_project_level( $t_project_id );
      $t_access_level_string = strtoupper( get_enum_to_string( config_get( 'access_levels_enum_string' ),  $t_access_level ) );

 	$t_access_level_string_ex = strtoupper( $t_project_name[1] ) . '_' . $t_access_level_string; 

//      $USERINFO['grps'] = array( $t_access_level_string );
      $USERINFO['grps'] = array( $t_access_level_string, $t_access_level_string_ex );
 
      $_SERVER['REMOTE_USER'] = $USERINFO['name'];

      $_SESSION[$conf['title']]['auth']['user'] = $USERINFO['name'];
      $_SESSION[$conf['title']]['auth']['info'] = $USERINFO;
 
      return true;
    }
 
    // to be sure
    auth_logoff();
 
    return false;
  }
 
  /**
   * Logout from Mantis
   */
  function logOff(){
	auth_logout();
  }
}
?>