Created ApiKey entity

This commit is contained in:
Alejandro Celaya 2016-08-06 12:50:44 +02:00
parent 270dbc6028
commit 2767a14101
3 changed files with 131 additions and 1 deletions

View file

@ -1,7 +1,8 @@
<?php <?php
return [ return [
'debug' => true,
'debug' => true,
'config_cache_enabled' => false, 'config_cache_enabled' => false,
]; ];

View file

@ -0,0 +1,12 @@
<?php
return [
'entity_manager' => [
'orm' => [
'entities_paths' => [
__DIR__ . '/../src/Entity',
],
],
],
];

View file

@ -0,0 +1,117 @@
<?php
namespace Shlinkio\Shlink\Rest\Entity;
use Doctrine\ORM\Mapping as ORM;
use Shlinkio\Shlink\Common\Entity\AbstractEntity;
use Shlinkio\Shlink\Common\Util\StringUtilsTrait;
/**
* Class ApiKey
* @author Shlink
* @link http://shlink.io
*
* @ORM\Entity()
* @ORM\Table(name="api_keys")
*/
class ApiKey extends AbstractEntity
{
use StringUtilsTrait;
/**
* @var string
* @ORM\Column(name="`key`", nullable=false, unique=true)
*/
protected $key;
/**
* @var \DateTime
* @ORM\Column(name="expiration_date", nullable=true, type="datetime")
*/
protected $expirationDate;
/**
* @var bool
* @ORM\Column(type="boolean")
*/
protected $enabled;
public function __construct()
{
$this->enabled = true;
$this->key = $this->generateV4Uuid();
}
/**
* @return string
*/
public function getKey()
{
return $this->key;
}
/**
* @param string $key
* @return $this
*/
public function setKey($key)
{
$this->key = $key;
return $this;
}
/**
* @return \DateTime
*/
public function getExpirationDate()
{
return $this->expirationDate;
}
/**
* @param \DateTime $expirationDate
* @return $this
*/
public function setExpirationDate($expirationDate)
{
$this->expirationDate = $expirationDate;
return $this;
}
/**
* @return bool
*/
public function isExpired()
{
if (! isset($this->expirationDate)) {
return false;
}
return $this->expirationDate >= new \DateTime();
}
/**
* @return boolean
*/
public function isEnabled()
{
return $this->enabled;
}
/**
* @param boolean $enabled
* @return $this
*/
public function setEnabled($enabled)
{
$this->enabled = $enabled;
return $this;
}
/**
* Tells if this api key is enabled and not expired
*
* @return bool
*/
public function isValid()
{
return $this->isEnabled() && ! $this->isExpired();
}
}