384 lines
7.4 KiB
PHP

<?php
namespace Chill\MainBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
/**
* User
*
* @ORM\Entity(repositoryClass="Chill\MainBundle\Repository\UserRepository")
* @ORM\Table(name="users")
* @ORM\Cache(usage="NONSTRICT_READ_WRITE", region="acl_cache_region")
*/
class User implements AdvancedUserInterface {
/**
* @var integer
*
* @ORM\Id
* @ORM\Column(name="id", type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var string
*
* @ORM\Column(type="string", length=80)
*/
private $username;
/**
* @var string
*
* @ORM\Column(
* type="string",
* length=80,
* unique=true)
*/
private $usernameCanonical;
/**
* @var string
*
* @ORM\Column(type="string", length=150, nullable=true)
*/
private $email;
/**
* @var string
*
* @ORM\Column(
* type="string",
* length=150,
* nullable=true,
* unique=true)
*/
private $emailCanonical;
/**
* @var string
*
* @ORM\Column(type="string", length=255)
*/
private $password;
/**
* @var string
* @internal must be set to null if we use bcrypt
*
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $salt = null;
/**
* @var boolean
*
* @ORM\Column(type="boolean")
* sf4 check: in yml was false by default !?
*/
private $locked = true;
/**
* @var boolean
*
* @ORM\Column(type="boolean")
*/
private $enabled = true;
/**
* @var Collection
*
* @ORM\ManyToMany(
* targetEntity="Chill\MainBundle\Entity\GroupCenter",
* inversedBy="users")
* @ORM\Cache(usage="NONSTRICT_READ_WRITE")
*/
private $groupCenters;
/**
* Array where SAML attributes's data are stored
* @var array
*
* @ORM\Column(type="json_array", nullable=true)
*/
private $attributes;
/**
* User constructor.
*/
public function __construct()
{
$this->groupCenters = new ArrayCollection();
}
/**
* @return string
*/
public function __toString()
{
return $this->getUsername();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set username
*
* @param string $name
* @return Agent
*/
public function setUsername($name)
{
$this->username = $name;
return $this;
}
/**
* @return string
*/
public function getUsername()
{
return $this->username;
}
/**
*/
public function eraseCredentials() {}
/**
* @return array
*/
public function getRoles()
{
return array('ROLE_USER');
}
/**
* @return null|string
*/
public function getSalt()
{
return $this->salt;
}
/**
* @param $usernameCanonical
* @return $this
*/
public function setUsernameCanonical($usernameCanonical)
{
$this->usernameCanonical = $usernameCanonical;
return $this;
}
/**
* @return string
*/
public function getUsernameCanonical()
{
return $this->usernameCanonical;
}
/**
* @param $email
* @return $this
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* @param $emailCanonical
* @return $this
*/
public function setEmailCanonical($emailCanonical)
{
$this->emailCanonical = $emailCanonical;
return $this;
}
/**
* @return string
*/
public function getEmailCanonical()
{
return $this->emailCanonical;
}
/**
* @param $password
* @return $this
*/
function setPassword($password)
{
$this->password = $password;
return $this;
}
/**
* @return string
*/
public function getPassword()
{
return $this->password;
}
/**
* @param $salt
* @return $this
*/
function setSalt($salt)
{
$this->salt = $salt;
return $this;
}
/**
* @return bool
*/
public function isAccountNonExpired()
{
return true;
}
/**
* @return bool
*/
public function isAccountNonLocked()
{
return $this->locked;
}
/**
* @return bool
*/
public function isCredentialsNonExpired()
{
return true;
}
/**
* @return bool
*/
public function isEnabled()
{
return $this->enabled;
}
/**
* @param bool $enabled
*/
public function setEnabled($enabled)
{
$this->enabled = $enabled;
return $this;
}
/**
* @return GroupCenter
*/
public function getGroupCenters()
{
return $this->groupCenters;
}
/**
* @param \Chill\MainBundle\Entity\GroupCenter $groupCenter
* @return \Chill\MainBundle\Entity\User
*/
public function addGroupCenter(GroupCenter $groupCenter)
{
$this->groupCenters->add($groupCenter);
return $this;
}
/**
* @param \Chill\MainBundle\Entity\GroupCenter $groupCenter
* @throws \RuntimeException if the groupCenter is not in the collection
*/
public function removeGroupCenter(GroupCenter $groupCenter)
{
if ($this->groupCenters->removeElement($groupCenter) === FALSE) {
throw new \RuntimeException(sprintf("The groupCenter could not be removed, "
. "it seems not to be associated with the user. Aborting."));
}
}
/**
* This function check that groupCenter are present only once. The validator
* use this function to avoid a user to be associated to the same groupCenter
* more than once.
*/
public function isGroupCenterPresentOnce(ExecutionContextInterface $context)
{
$groupCentersIds = array();
foreach ($this->getGroupCenters() as $groupCenter) {
if (in_array($groupCenter->getId(), $groupCentersIds)) {
$context->buildViolation("The user has already those permissions")
->addViolation();
} else {
$groupCentersIds[] = $groupCenter->getId();
}
}
}
/**
* Set attributes
*
* @param array $attributes
*
* @return Report
*/
public function setAttributes($attributes)
{
$this->attributes = $attributes;
return $this;
}
/**
* Get attributes
*
* @return array
*/
public function getAttributes()
{
if ($this->attributes === null) {
$this->attributes = [];
}
return $this->attributes;
}
}