<commit_before><?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

namespace BaseX\Symfony\Security;

use BaseX\Query\QueryResult;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\Role\Role;

/**
 * Description of User
 *
 * @author alxarch
 */
class User extends QueryResult implements UserInterface
{
  static private $salt = null;
  
  protected $username;
  protected $password;
  protected $last_login;
  protected $roles;
  
  public function setData($data) {
    parent::setData($data);
    $xml = @simplexml_load_string($data);
    
    $this->setUsername((string) $xml->username)
         ->setPassword((string) $xml->password)
         ->setLastLogin(strtotime((string) $xml->{'last-login'}));
         
    $this->roles = array();
    foreach ($xml->roles->role as $r)
    {
       $this->roles[] = new Role((string)$r);
    }
    
    return $this;
    
  }
  
  public function eraseCredentials()
  {
  }
  
  public function setPassword($pass)
  {
    $this->password = $pass;
    return $this;
  }
  
  public function getPassword() {
    return $this->password;
  }
  
  public function getRoles() {
    return $this->roles;
  }
  
  public function setRoles($roles) {
    $this->roles = array();
    
    foreach ($roles as $r)
    {
      $this->roles[] = new Role((string)$r);
    }
    
    return $this;
  }
  
  public function getUsername() {
    return $this->username;
  }
  
  public function setUsername($username) {
    if(!preg_match('/^[a-zA-Z0-9\.\-_]+$/', $username))
    {
      throw new \InvalidArgumentException('Invlid username.');
    }
    
    $this->username = $username;
    return $this;
  }
  
  public function getSalt()
  {
    return self::$salt;
  }
  
  public static function setSalt($salt)
  {
    self::$salt = $salt;
  }
  
  public function getLastLogin()
  {
    return $this->last_login;
  }
  
  public function setLastLogin($timestamp)
  {
    $this->last_login = (int) $timestamp;
    return $this;
  }
  
  public function getXML()
  {
    $xml = simplexml_load_string('<user xmlns=""/>');
    $xml->addChild('username', $this->getUsername());
    $xml->addChild('last-login', $this->getLastLogin());
    $xml->addChild('password', $this->getPassword());
    $xml->addChild('roles');
    foreach ($this->getRoles() as $role)
    {
      $xml->roles->addChild('role', $role->getRole());
    }
    
    return substr($xml->asXML(), strlen('<?xml version="1.0"?>'));
  }
  
  public function __toString() {
    return $this->getUsername();
  }
}
<commit_msg>It appears Role object messes things up<commit_after><?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

namespace BaseX\Symfony\Security;

use BaseX\Query\QueryResult;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\Role\Role;

/**
 * Description of User
 *
 * @author alxarch
 */
class User extends QueryResult implements UserInterface
{
  static private $salt = null;
  
  protected $username;
  protected $password;
  protected $last_login;
  protected $roles;
  
  public function setData($data) {
    parent::setData($data);
    $xml = @simplexml_load_string($data);
    
    $this->setUsername((string) $xml->username)
         ->setPassword((string) $xml->password)
         ->setLastLogin(strtotime((string) $xml->{'last-login'}));
         
    $this->setRoles($xml->roles->role);
    return $this;
    
  }
  
  public function eraseCredentials()
  {
  }
  
  public function setPassword($pass)
  {
    $this->password = $pass;
    return $this;
  }
  
  public function getPassword() {
    return $this->password;
  }
  
  public function getRoles() {
    return $this->roles;
  }
  
  public function setRoles($roles) {
    $this->roles = array();
    
    foreach ($roles as $r)
    {
      $this->roles[] = (string)$r;
    }
    
    return $this;
  }
  
  public function getUsername() {
    return $this->username;
  }
  
  public function setUsername($username) {
    if(!preg_match('/^[a-zA-Z0-9\.\-_]+$/', $username))
    {
      throw new \InvalidArgumentException('Invlid username.');
    }
    
    $this->username = $username;
    return $this;
  }
  
  public function getSalt()
  {
    return self::$salt;
  }
  
  public static function setSalt($salt)
  {
    self::$salt = $salt;
  }
  
  public function getLastLogin()
  {
    return $this->last_login;
  }
  
  public function setLastLogin($timestamp)
  {
    $this->last_login = (int) $timestamp;
    return $this;
  }
  
  public function getXML()
  {
    $xml = simplexml_load_string('<user xmlns=""/>');
    $xml->addChild('username', $this->getUsername());
    $xml->addChild('last-login', $this->getLastLogin());
    $xml->addChild('password', $<PASSWORD>-><PASSWORD>());
    $xml->addChild('roles');
    foreach ($this->getRoles() as $role)
    {
      $xml->roles->addChild('role', $role);
    }
    
    return substr($xml->asXML(), strlen('<?xml version="1.0"?>'));
  }
  
  public function __toString() {
    return $this->getUsername();
  }
}

{'fraction_non_alphanumeric': 0.16080792386871237, 'fraction_numerical': 0.001553699747523791, 'mean_word_length': 2.9133738601823707, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 2, '>': 71, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 32}, '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': '1866514', 'n_tokens_mistral': 1730, 'n_tokens_neox': 1712, 'n_words': 343}