<commit_before><?php

/**
 * Configure App specific behavior for 
 * Maestrano SSO
 */
class MnoSsoUser extends MnoSsoBaseUser
{
  /**
   * Database connection
   * @var PDO
   */
  public $connection = null;
  
  
  /**
   * Extend constructor to inialize app specific objects
   *
   * @param OneLogin_Saml_Response $saml_response
   *   A SamlResponse object from Maestrano containing details
   *   about the user being authenticated
   */
  public function __construct(OneLogin_Saml_Response $saml_response, &$session = array(), $opts = array())
  {
    // Call Parent
    parent::__construct($saml_response,$session);
    
    // Assign new attributes
    $this->connection = $opts['db_connection'];
  }
  
  
  /**
   * Sign the user in the application. 
   * Parent method deals with putting the mno_uid, 
   * mno_session and mno_session_recheck in session.
   *
   * @return boolean whether the user was successfully set in session or not
   */
  protected function setInSession()
  {
    
    if ($this->local_id) {
        $authentication = new Authentication();
        $authentication->setObject( $this->local_id );
        $authentication->Login($this->uid, '', 'USER_NAME', true);
        
        return true;
    } else {
        return false;
    }
  }
  
  
  /**
   * Used by createLocalUserOrDenyAccess to create a local user 
   * based on the sso user.
   * If the method returns null then access is denied
   *
   * @return the ID of the user created, null otherwise
   */
  protected function createLocalUser()
  {
    $lid = null;
    
    if ($this->accessScope() == 'private') {
      // First build the user
      $user = $this->buildLocalUser();
      
      // Then save the user and retrieve the local id
      $lid = $user->Save();
    }
    
    return $lid;
  }
  
  /**
   * Build a local user for creation
   *
   * @return a timetrex user
   */
  protected function buildLocalUser()
  {
    $user = TTnew( 'UserFactory' );
    
		$user->setCompany($this->getCompanyToAssign());
		$user->setStatus(10); //Active
		$user->setUserName($this->uid);
    $user->setPassword($this->generatePassword());

		$user->setEmployeeNumber($this->getEmployeeNumberToAssign());
		$user->setFirstName($this->name);
		$user->setLastName($this->surname);
		$user->setWorkEmail($this->email);

		if ( is_object( $user->getCompanyObject() ) ) {
			$user->setCountry( $user->getCompanyObject()->getCountry() );
			$user->setProvince( $user->getCompanyObject()->getProvince() );
			$user->setAddress1( $user->getCompanyObject()->getAddress1() );
			$user->setAddress2( $user->getCompanyObject()->getAddress2() );
			$user->setCity( $user->getCompanyObject()->getCity() );
			$user->setPostalCode( $user->getCompanyObject()->getPostalCode() );
			$user->setWorkPhone( $user->getCompanyObject()->getWorkPhone() );
			$user->setHomePhone( $user->getCompanyObject()->getWorkPhone() );

			if ( is_object( $user->getCompanyObject()->getUserDefaultObject() ) ) {
				$user->setCurrency( $user->getCompanyObject()->getUserDefaultObject()->getCurrency() );
			}
		}
    
    $user->setPermissionControl( $this->getRoleIdToAssign() );
    
    return $user;
  }
  
  /**
   * Return the ID of the default company to assign to the
   * user
   *
   * @return integer the ID of the company to assign
   */
  protected function getCompanyToAssign() {
    
    $result = $this->connection->Execute("SELECT id FROM company ORDER BY id ASC LIMIT 1");
    $result = $result->fields;
    
    if ($result && $result['id']) {
      return $result['id'];
    }
    
    return 0;
  }
  
  /**
   * Return the employee number to assign
   *
   * @return integer the next available employee number
   */
  protected function getEmployeeNumberToAssign() {
    
    $result = $this->connection->Execute("SELECT employee_number FROM users ORDER BY employee_number DESC LIMIT 1");
    $result = $result->fields;
    
    if ($result && $result['employee_number']) {
      $number = intval($result['employee_number']);
      return ($number + 1);
    }
    
    return 0;
  }
  <commit_msg>added fix to employee_number getter<commit_after><?php

/**
 * Configure App specific behavior for 
 * Maestrano SSO
 */
class MnoSsoUser extends MnoSsoBaseUser
{
  /**
   * Database connection
   * @var PDO
   */
  public $connection = null;
  
  
  /**
   * Extend constructor to inialize app specific objects
   *
   * @param OneLogin_Saml_Response $saml_response
   *   A SamlResponse object from Maestrano containing details
   *   about the user being authenticated
   */
  public function __construct(OneLogin_Saml_Response $saml_response, &$session = array(), $opts = array())
  {
    // Call Parent
    parent::__construct($saml_response,$session);
    
    // Assign new attributes
    $this->connection = $opts['db_connection'];
  }
  
  
  /**
   * Sign the user in the application. 
   * Parent method deals with putting the mno_uid, 
   * mno_session and mno_session_recheck in session.
   *
   * @return boolean whether the user was successfully set in session or not
   */
  protected function setInSession()
  {
    
    if ($this->local_id) {
        $authentication = new Authentication();
        $authentication->setObject( $this->local_id );
        $authentication->Login($this->uid, '', 'USER_NAME', true);
        
        return true;
    } else {
        return false;
    }
  }
  
  
  /**
   * Used by createLocalUserOrDenyAccess to create a local user 
   * based on the sso user.
   * If the method returns null then access is denied
   *
   * @return the ID of the user created, null otherwise
   */
  protected function createLocalUser()
  {
    $lid = null;
    
    if ($this->accessScope() == 'private') {
      // First build the user
      $user = $this->buildLocalUser();
      
      // Then save the user and retrieve the local id
      $lid = $user->Save();
    }
    
    return $lid;
  }
  
  /**
   * Build a local user for creation
   *
   * @return a timetrex user
   */
  protected function buildLocalUser()
  {
    $user = TTnew( 'UserFactory' );
    
		$user->setCompany($this->getCompanyToAssign());
		$user->setStatus(10); //Active
		$user->setUserName($this->uid);
    $user->setPassword($this->generatePassword());

		$user->setEmployeeNumber($this->getEmployeeNumberToAssign());
		$user->setFirstName($this->name);
		$user->setLastName($this->surname);
		$user->setWorkEmail($this->email);

		if ( is_object( $user->getCompanyObject() ) ) {
			$user->setCountry( $user->getCompanyObject()->getCountry() );
			$user->setProvince( $user->getCompanyObject()->getProvince() );
			$user->setAddress1( $user->getCompanyObject()->getAddress1() );
			$user->setAddress2( $user->getCompanyObject()->getAddress2() );
			$user->setCity( $user->getCompanyObject()->getCity() );
			$user->setPostalCode( $user->getCompanyObject()->getPostalCode() );
			$user->setWorkPhone( $user->getCompanyObject()->getWorkPhone() );
			$user->setHomePhone( $user->getCompanyObject()->getWorkPhone() );

			if ( is_object( $user->getCompanyObject()->getUserDefaultObject() ) ) {
				$user->setCurrency( $user->getCompanyObject()->getUserDefaultObject()->getCurrency() );
			}
		}
    
    $user->setPermissionControl( $this->getRoleIdToAssign() );
    
    return $user;
  }
  
  /**
   * Return the ID of the default company to assign to the
   * user
   *
   * @return integer the ID of the company to assign
   */
  protected function getCompanyToAssign() {
    
    $result = $this->connection->Execute("SELECT id FROM company ORDER BY id ASC LIMIT 1");
    $result = $result->fields;
    
    if ($result && $result['id']) {
      return $result['id'];
    }
    
    return 0;
  }
  
  /**
   * Return the employee number to assign
   *
   * @return integer the next available employee number
   */
  protected function getEmployeeNumberToAssign() {
    # Note ::int is for typecasting on postgresql - will not work with mysql
    $result = $this->connection->Execute("SELECT employee_number FROM users ORDER BY employee_number::int DESC LIMIT 1");
    $result = $result->fields;
    
    if ($result && $result['employee_number']) {
      $number = intval($result['employee_number']);
      return ($number + 1);
    }
    
    return 0;
  }
  
{'fraction_non_alphanumeric': 0.13951798474022042, 'fraction_numerical': 0.0026644059585805985, 'mean_word_length': 3.0381418092909533, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 129, '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': '3375862', 'n_tokens_mistral': 2609, 'n_tokens_neox': 2501, 'n_words': 614}