155 lines
2.8 KiB
PHP
155 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace Catalyst\APIBundle\Entity;
|
|
|
|
use Catalyst\AuthBundle\Entity\User as BaseUser;
|
|
|
|
use Symfony\Component\Security\Core\User\UserInterface;
|
|
use Doctrine\ORM\Mapping as ORM;
|
|
use Doctrine\ORM\Mapping\JoinColumn;
|
|
use Doctrine\Common\Collections\ArrayCollection;
|
|
use DateTime;
|
|
|
|
/**
|
|
* @ORM\Entity
|
|
* @ORM\Table(name="api_user")
|
|
*/
|
|
class User extends BaseUser implements UserInterface
|
|
{
|
|
/**
|
|
* @ORM\Id
|
|
* @ORM\Column(type="integer")
|
|
* @ORM\GeneratedValue(strategy="AUTO")
|
|
*/
|
|
protected $id;
|
|
|
|
// api key
|
|
/**
|
|
* @ORM\Column(type="string", length=32)
|
|
*/
|
|
protected $api_key;
|
|
|
|
// secret key
|
|
/**
|
|
* @ORM\Column(type="string", length=32)
|
|
*/
|
|
protected $secret_key;
|
|
|
|
/**
|
|
* @ORM\Column(type="string", length=80)
|
|
*/
|
|
protected $name;
|
|
|
|
// date created
|
|
/**
|
|
* @ORM\Column(type="datetime")
|
|
*/
|
|
protected $date_create;
|
|
|
|
// roles
|
|
/**
|
|
* @ORM\ManyToMany(targetEntity="Role", inversedBy="users")
|
|
* @ORM\JoinTable(name="api_user_role")
|
|
*/
|
|
protected $roles;
|
|
|
|
// enabled
|
|
/**
|
|
* @ORM\Column(type="boolean")
|
|
*/
|
|
protected $enabled;
|
|
|
|
public function __construct()
|
|
{
|
|
// generate keys
|
|
$this->setAPIKey($this->generateAPIKey())
|
|
->setSecretKey($this->generateSecretKey());
|
|
|
|
// set date created
|
|
$this->date_create = new DateTime();
|
|
|
|
$this->roles = new ArrayCollection();
|
|
$this->enabled = true;
|
|
}
|
|
|
|
public function getID()
|
|
{
|
|
return $this->id;
|
|
}
|
|
|
|
public function setAPIKey($api_key)
|
|
{
|
|
$this->api_key = $api_key;
|
|
return $this;
|
|
}
|
|
|
|
public function getAPIKey()
|
|
{
|
|
return $this->api_key;
|
|
}
|
|
|
|
public function setSecretKey($key)
|
|
{
|
|
$this->secret_key = $key;
|
|
return $this;
|
|
}
|
|
|
|
public function getSecretKey()
|
|
{
|
|
return $this->secret_key;
|
|
}
|
|
|
|
public function setName($name)
|
|
{
|
|
$this->name = $name;
|
|
return $this;
|
|
}
|
|
|
|
public function getName()
|
|
{
|
|
return $this->name;
|
|
}
|
|
|
|
public function getDateCreate()
|
|
{
|
|
return $this->date_create;
|
|
}
|
|
|
|
public function getPassword()
|
|
{
|
|
// we don't need this for API
|
|
return 'notneeded';
|
|
}
|
|
|
|
public function getSalt()
|
|
{
|
|
return null;
|
|
}
|
|
|
|
public function getUsername()
|
|
{
|
|
// since it's an api, the api key IS the username
|
|
return $this->api_key;
|
|
}
|
|
|
|
public function eraseCredentials()
|
|
{
|
|
return;
|
|
}
|
|
|
|
public function generateAPIKey()
|
|
{
|
|
return $this->generateKey('api');
|
|
}
|
|
|
|
public function generateSecretKey()
|
|
{
|
|
return $this->generateKey('secret');
|
|
}
|
|
|
|
protected function generateKey($prefix = '')
|
|
{
|
|
return md5(uniqid($prefix, true));
|
|
}
|
|
}
|
|
|