Files
chill-bundles/src/Bundle/ChillMainBundle/Entity/User.php
2022-03-10 14:54:03 +01:00

433 lines
8.8 KiB
PHP

<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\MainBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use RuntimeException;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
use Symfony\Component\Serializer\Annotation as Serializer;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use function in_array;
/**
* User.
*
* @ORM\Entity
* @ORM\Table(name="users")
* @ORM\Cache(usage="NONSTRICT_READ_WRITE", region="acl_cache_region")
* @Serializer\DiscriminatorMap(typeProperty="type", mapping={
* "user": User::class
* })
*/
class User implements AdvancedUserInterface
{
/**
* @ORM\Id
* @ORM\Column(name="id", type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected ?int $id = null;
/**
* Array where SAML attributes's data are stored.
*
* @ORM\Column(type="json", nullable=true)
*/
private array $attributes;
/**
* @ORM\ManyToOne(targetEntity=Location::class)
*/
private ?Location $currentLocation = null;
/**
* @var string
*
* @ORM\Column(type="string", length=150, nullable=true)
*/
private ?string $email = null;
/**
* @ORM\Column(
* type="string",
* length=150,
* nullable=true,
* unique=true)
*/
private ?string $emailCanonical = null;
/**
* @ORM\Column(type="boolean")
*/
private bool $enabled = true;
/**
* @ORM\ManyToMany(
* targetEntity="Chill\MainBundle\Entity\GroupCenter",
* inversedBy="users")
* @ORM\Cache(usage="NONSTRICT_READ_WRITE")
*/
private Collection $groupCenters;
/**
* @ORM\Column(type="string", length=200)
*/
private string $label = '';
/**
* @ORM\Column(type="boolean")
* sf4 check: in yml was false by default !?
*/
private bool $locked = true;
/**
* @ORM\ManyToOne(targetEntity=Center::class)
*/
private ?Center $mainCenter = null;
/**
* @ORM\ManyToOne(targetEntity=Location::class)
*/
private ?Location $mainLocation = null;
/**
* @ORM\ManyToOne(targetEntity=Scope::class)
*/
private ?Scope $mainScope = null;
/**
* @ORM\Column(type="string", length=255)
*/
private string $password = '';
/**
* @ORM\Column(type="json")
*/
private array $roles = ['ROLE_USER'];
/**
* @internal must be set to null if we use bcrypt
*
* @ORM\Column(type="string", length=255, nullable=true)
*/
private ?string $salt = null;
/**
* @ORM\ManyToOne(targetEntity=UserJob::class)
*/
private ?UserJob $userJob = null;
/**
* @ORM\Column(type="string", length=80)
*/
private string $username = '';
/**
* @ORM\Column(
* type="string",
* length=80,
* unique=true,
* nullable=true)
*/
private ?string $usernameCanonical = null;
/**
* User constructor.
*/
public function __construct()
{
$this->groupCenters = new ArrayCollection();
}
public function __toString(): string
{
return $this->getLabel();
}
public function addGroupCenter(GroupCenter $groupCenter): self
{
$this->groupCenters->add($groupCenter);
return $this;
}
// empty function... remove?
public function eraseCredentials()
{
}
public function getAttributes(): ?array
{
if (null === $this->attributes) {
$this->attributes = [];
}
return $this->attributes;
}
public function getCurrentLocation(): ?Location
{
return $this->currentLocation;
}
public function getEmail(): ?string
{
return $this->email;
}
public function getEmailCanonical(): ?string
{
return $this->emailCanonical;
}
/**
* @return GroupCenter
*/
public function getGroupCenters()
{
return $this->groupCenters;
}
public function getId(): ?int
{
return $this->id;
}
public function getLabel(): string
{
return $this->label;
}
public function getMainCenter(): ?Center
{
return $this->mainCenter;
}
public function getMainLocation(): ?Location
{
return $this->mainLocation;
}
public function getMainScope(): ?Scope
{
return $this->mainScope;
}
public function getPassword(): string
{
return $this->password;
}
public function getRoles(): array
{
return array_unique($this->roles);
}
public function getSalt(): ?string
{
return $this->salt;
}
public function getUserJob(): ?UserJob
{
return $this->userJob;
}
public function getUsername(): string
{
return $this->username;
}
public function getUsernameCanonical(): ?string
{
return $this->usernameCanonical;
}
public function isAccountNonExpired(): bool
{
return true;
}
public function isAccountNonLocked(): bool
{
return $this->locked;
}
public function isCredentialsNonExpired(): bool
{
return true;
}
public function isEnabled(): bool
{
return $this->enabled;
}
/**
* 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 = [];
foreach ($this->getGroupCenters() as $groupCenter) {
if (in_array($groupCenter->getId(), $groupCentersIds, true)) {
$context->buildViolation('The user has already those permissions')
->addViolation();
} else {
$groupCentersIds[] = $groupCenter->getId();
}
}
}
/**
* @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('The groupCenter could not be removed, '
. 'it seems not to be associated with the user. Aborting.');
}
}
/**
* Set attributes.
*
* @param array $attributes
*
* @return Report
*/
public function setAttributes($attributes)
{
$this->attributes = $attributes;
return $this;
}
public function setCurrentLocation(?Location $currentLocation): self
{
$this->currentLocation = $currentLocation;
return $this;
}
public function setEmail($email): self
{
$this->email = $email;
return $this;
}
public function setEmailCanonical($emailCanonical): self
{
$this->emailCanonical = $emailCanonical;
return $this;
}
public function setEnabled(bool $enabled): self
{
$this->enabled = $enabled;
return $this;
}
public function setLabel(string $label): self
{
$this->label = $label;
return $this;
}
public function setMainCenter(?Center $mainCenter): self
{
$this->mainCenter = $mainCenter;
return $this;
}
public function setMainLocation(?Location $mainLocation): self
{
$this->mainLocation = $mainLocation;
return $this;
}
public function setMainScope(?Scope $mainScope): self
{
$this->mainScope = $mainScope;
return $this;
}
public function setPassword($password): self
{
$this->password = $password;
return $this;
}
public function setRoles($roles): self
{
$this->roles = $roles;
return $this;
}
public function setSalt($salt): self
{
$this->salt = $salt;
return $this;
}
public function setUserJob(?UserJob $userJob): self
{
$this->userJob = $userJob;
return $this;
}
/**
* Set username.
*
* @param string $name
*
* @return Agent
*/
public function setUsername($name)
{
$this->username = $name;
if (empty($this->getLabel())) {
$this->setLabel($name);
}
return $this;
}
public function setUsernameCanonical($usernameCanonical): self
{
$this->usernameCanonical = $usernameCanonical;
return $this;
}
}