<commit_before><?php

use Carbon\Carbon;
use Illuminate\Database\Capsule\Manager as DB;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Collection;

class User extends Model {
	public $perms = null;
	public $timestamps = true;

	public $activeChainMap = 0;

	protected $fillable = [
		'username',
		'password',
		'email',
		'active'
	];

	protected $hidden = [
		'password'
	];

	public function group()
	{
		return $this->hasOne('Group','id','groupID');
	}

	public function savePassword( $groupID, $pass )
	{
		$passes = DB::insert("REPLACE INTO user_group_passwords (user_id, group_id, group_password) VALUES(:user, :group, :pass)",
								[
									'user' => $this->id,
									'group' => $groupID,
									'pass' => $pass
								]);

		$this->recacheSavedPasswords();
	}

	public function groupPasswords()
	{
		return $this->hasMany('UserGroupPassword');
	}

	public function getSavedGroupPassword( $groupID )
	{
		$entry = $this->groupPasswords()->where('group_id',$groupID)->first();

		if($entry != null)
		{
			return $entry->group_password;
		}

		return '';
	}

	public function validateCorpChar()
	{
		if( $this->findSSOCharacter( $this->char_id ) == null )
		{
			return FALSE;
		}

		$character = Character::find( $this->char_id );
		if( $character == null )
		{
			return FALSE;
		}

		/* update the corp id */
		if( $character->corporation_id != $this->corp_id )
		{
			$this->corp_id = $character->corporation_id;
			$this->save();

			if( $this->id == Auth::$user->id )
			{
				Auth::$session->reloadUserSession();
			}
		}

		return TRUE;
	}

	protected static function boot()
	{
		parent::boot();
		
		static::creating(function ($model)
		{
			$model->password = <PASSWORD>($model->password);
			$model->active = TRUE;
		});
	}

	public function getActiveSSOCharacter()
	{
		return $this->findSSOCharacter($this->char_id);
	}

	public function findSSOCharacter(int $characterId)
	{
		$char = $this->ssoCharacters()->where('character_id', $characterId)->first();

		return $char;
	}

	public function ssoCharacters()
	{
		return $this->hasMany('UserSSOCharacter');
	}

	public function removeSSOCharacter(int $characterId)
	{
		$this->ssoCharacters()->where('character_id', $characterId)->delete();

		return TRUE;
	}

	public function updateSSOCharacter(int $characterId, $token, $refreshToken, $expiration)
	{
		$char = $this->ssoCharacters()->where('character_id', $characterId)->first();
		$data = [
			'access_token' => $token,
			'access_token_expiration' => $expiration,
			'refresh_token' => $refreshToken,
			'valid' => 1
		];

		$char->fill($data);
		$char->save();
		
		return TRUE;
	}

	public function addSSOCharacter($hash, $characterId, $token, $expiration, $refreshToken)
	{
		$insert = [
			'user_id' => $this->id,
			'character_owner_hash' => $hash,
			'character_id' => $characterId,
			'access_token' => $token,
			'access_token_expiration' => $expiration,
			'refresh_token' => $refreshToken,
			'valid' => 1
		];

		$this->ssoCharacters()->create($insert);

		return TRUE;
	}

	public static function findByUsername(string $username)
	{
		return self::whereRaw('LOWER(username) = ?', [$username])->first();
	}

	public static function findByEmail(string $email)
	{
		return self::whereRaw('LOWER(email) = ?', [$username])->first();
	}

	public function perms()
	{
		if($this->perms == null)
		{
			$perms = UserGroupPermission::findByUser($this->id);

			$this->perms = $perms;
		}

		return $this->perms;
	}

	public function updatePassword($newPass)
	{
		$this->password = Auth::hash($newPass);<commit_msg>Fix reset password<commit_after><?php

use Carbon\Carbon;
use Illuminate\Database\Capsule\Manager as DB;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Collection;

class User extends Model {
	public $perms = null;
	public $timestamps = true;

	public $activeChainMap = 0;

	protected $fillable = [
		'username',
		'password',
		'email',
		'active'
	];

	protected $hidden = [
		'password'
	];

	public function group()
	{
		return $this->hasOne('Group','id','groupID');
	}

	public function savePassword( $groupID, $pass )
	{
		$passes = DB::insert("REPLACE INTO user_group_passwords (user_id, group_id, group_password) VALUES(:user, :group, :pass)",
								[
									'user' => $this->id,
									'group' => $groupID,
									'pass' => $pass
								]);

		$this->recacheSavedPasswords();
	}

	public function groupPasswords()
	{
		return $this->hasMany('UserGroupPassword');
	}

	public function getSavedGroupPassword( $groupID )
	{
		$entry = $this->groupPasswords()->where('group_id',$groupID)->first();

		if($entry != null)
		{
			return $entry->group_password;
		}

		return '';
	}

	public function validateCorpChar()
	{
		if( $this->findSSOCharacter( $this->char_id ) == null )
		{
			return FALSE;
		}

		$character = Character::find( $this->char_id );
		if( $character == null )
		{
			return FALSE;
		}

		/* update the corp id */
		if( $character->corporation_id != $this->corp_id )
		{
			$this->corp_id = $character->corporation_id;
			$this->save();

			if( $this->id == Auth::$user->id )
			{
				Auth::$session->reloadUserSession();
			}
		}

		return TRUE;
	}

	protected static function boot()
	{
		parent::boot();
		
		static::creating(function ($model)
		{
			$model->password = <PASSWORD>::hash($model->password);
			$model->active = TRUE;
		});
	}

	public function getActiveSSOCharacter()
	{
		return $this->findSSOCharacter($this->char_id);
	}

	public function findSSOCharacter(int $characterId)
	{
		$char = $this->ssoCharacters()->where('character_id', $characterId)->first();

		return $char;
	}

	public function ssoCharacters()
	{
		return $this->hasMany('UserSSOCharacter');
	}

	public function removeSSOCharacter(int $characterId)
	{
		$this->ssoCharacters()->where('character_id', $characterId)->delete();

		return TRUE;
	}

	public function updateSSOCharacter(int $characterId, $token, $refreshToken, $expiration)
	{
		$char = $this->ssoCharacters()->where('character_id', $characterId)->first();
		$data = [
			'access_token' => $token,
			'access_token_expiration' => $expiration,
			'refresh_token' => $refreshToken,
			'valid' => 1
		];

		$char->fill($data);
		$char->save();
		
		return TRUE;
	}

	public function addSSOCharacter($hash, $characterId, $token, $expiration, $refreshToken)
	{
		$insert = [
			'user_id' => $this->id,
			'character_owner_hash' => $hash,
			'character_id' => $characterId,
			'access_token' => $token,
			'access_token_expiration' => $expiration,
			'refresh_token' => $refreshToken,
			'valid' => 1
		];

		$this->ssoCharacters()->create($insert);

		return TRUE;
	}

	public static function findByUsername(string $username)
	{
		return self::whereRaw('LOWER(username) = ?', [strtolower($username)])->first();
	}

	public static function findByEmail(string $email)
	{
		return self::whereRaw('LOWER(email) = ?', [strtolower($email)])->first();
	}

	public function perms()
	{
		if($this->perms == null)
		{
			$perms = UserGroupPermission::findByUser($this->id);

			$this->perms = $perms;
		}

		return $this->perms;
	}

	public function updatePassword($newPass)
	{
		$this->password = Auth::hash($newPass);
{'fraction_non_alphanumeric': 0.1724137931034483, 'fraction_numerical': 0.0008342602892102336, 'mean_word_length': 4.054813773717498, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 125, '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': '4199604', 'n_tokens_mistral': 2848, 'n_tokens_neox': 2481, 'n_words': 455}