resq/catalyst/api-bundle/Entity/User.php
2023-02-13 09:00:58 +00:00

207 lines
3.9 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
{
/**
* @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;
// rider linked to user
// NOTE: we're directly linking this only because we don't have to care about other apps using this library
/**
* @ORM\OneToOne(targetEntity="App\Entity\Rider", mappedBy="api_user")
*/
protected $rider;
/**
* @ORM\Column(type="json")
*/
protected $metadata;
// third party job order source linked to api_user
/**
* @ORM\ManyToOne(targetEntity="App\Entity\JobOrderSource", inversedBy="api_users")
* @ORM\JoinColumn(name="source_id", referencedColumnName="id")
*/
protected $source;
public function __construct()
{
parent::__construct();
// generate keys
$this->setAPIKey($this->generateAPIKey())
->setSecretKey($this->generateSecretKey());
// set date created
$this->date_create = new DateTime();
$this->metadata = [];
$this->source = null;
}
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');
}
public function setMetadata($meta)
{
$this->metadata = $meta;
return $this;
}
public function getMetadata()
{
if ($this->metadata == null)
return [];
return $this->metadata;
}
protected function generateKey($prefix = '')
{
return md5(uniqid($prefix, true));
}
public function setRider($rider)
{
$this->rider = $rider;
return $this;
}
public function getRider()
{
return $this->rider;
}
public function setSource($source = null)
{
$this->source = $source;
return $this;
}
public function getSource()
{
return $this->source;
}
}