This repository has been archived on 2025-02-26. You can view files and clone it, but cannot push or open issues or pull requests.
szurubooru/src/Models/Entities/TokenEntity.php
2014-05-09 21:29:16 +02:00

84 lines
1.3 KiB
PHP

<?php
final class TokenEntity extends AbstractEntity implements IValidatable
{
private $userId;
private $token;
private $used;
private $expires;
public function fillNew()
{
$this->used = false;
$tokenText = '';
while (true)
{
$tokenText = md5(mt_rand() . uniqid());
$token = TokenModel::tryGetByToken($tokenText);
if (!$token)
break;
}
$this->token = $tokenText;
}
public function fillFromDatabase($row)
{
$this->id = (int) $row['id'];
$this->userId = (int) $row['user_id'];
$this->token = $row['token'];
$this->used = (bool) $row['used'];
$this->expires = $row['expires'];
}
public function validate()
{
if (empty($this->token))
throw new Exception('Trying to save empty token');
}
public function getText()
{
return $this->token;
}
public function setText($tokenText)
{
$this->token = $tokenText;
}
public function isUsed()
{
return $this->used;
}
public function setUsed($used)
{
$this->used = $used;
}
public function getExpirationTime()
{
return $this->expires;
}
public function setExpirationTime($unixTime)
{
$this->expires = $unixTime;
}
public function getUser()
{
return UserModel::getById($this->userId);
}
public function getUserId()
{
return $this->userId;
}
public function setUser($user)
{
$this->userId = $user ? $user->getId() : null;
}
}