Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Labco\CoreBundle\Entity;
- use Doctrine\Common\Collections\ArrayCollection;
- use Doctrine\Common\Util\Debug;
- use Doctrine\ORM\Mapping as ORM;
- use Labco\CoreBundle\Entity\User\LocalCode;
- use Labco\CoreBundle\Entity\User\SamplingDeposit;
- use Labco\CoreBundle\Entity\User\Speciality;
- use Labco\CoreBundle\Entity\User\Status;
- use Symfony\Component\Security\Core\User\UserInterface;
- use Labco\CoreBundle\Entity\Relations\UserSel;
- use Labco\CoreBundle\Entity\User\Circuit;
- use Symfony\Component\Validator\Constraints as Assert;
- use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
- use JMS\Serializer\Annotation\Groups;
- use Symfony\Component\Validator\Context\ExecutionContextInterface;
- use JMS\Serializer\Annotation as JMS;
- /**
- * User
- *
- * @ORM\Table(indexes={@ORM\Index(name="date_updated_idx", columns={"date_updated"})})
- * @ORM\Entity(repositoryClass="Labco\CoreBundle\Repository\UserRepository")
- * @UniqueEntity("email",message="user.profil_exist", repositoryMethod="labcoFindEmail")
- */
- class User extends Historisation implements UserInterface {
- const ROOT_EMAIL_ADDRESS = "admin@root.com";
- /**
- * @var integer
- * @Groups({"listUser", "detailsUser","detailsClusAck"})
- * @ORM\Column(name="id", type="integer")
- * @ORM\Id
- * @ORM\GeneratedValue(strategy="AUTO")
- */
- protected $id;
- /**
- * @var string
- *
- * @Groups({"listUser", "detailsUser"})
- * @ORM\Column(name="email", type="string", length=255)
- * @Assert\NotBlank()
- * @Assert\Email()
- */
- protected $email;
- /**
- * @var boolean
- *
- * @Groups({"detailsUser"})
- * @ORM\Column(name="enabled", type="boolean")
- */
- protected $enabled;
- /**
- * @var string
- *
- * @Groups({"detailsUser"})
- * @ORM\Column(name="salt", type="string", length=255)
- */
- protected $salt;
- /**
- * @var string
- *
- * @Groups({"detailsUser"})
- * @ORM\Column(name="password", type="string", length=255)
- */
- protected $password;
- /**
- * @var array
- *
- * @ORM\Column(name="roles", type="array")
- * @Groups({"none"})
- */
- protected $roles;
- /**
- * @var string
- *
- * @Groups({"detailsUser"})
- * @ORM\Column(name="firstname", type="string", length=255)
- * @Assert\NotBlank()
- */
- protected $firstname;
- /**
- * @var string
- *
- * @Groups({"detailsUser"})
- * @ORM\Column(name="lastname", type="string", length=255)
- * @Assert\NotBlank()
- */
- protected $lastname;
- /**
- * @ORM\OneToMany(targetEntity="Labco\CoreBundle\Entity\Relations\UserSel", mappedBy="user")
- */
- protected $userSels;
- /**
- * @Groups({"detailsUser"})
- *
- * @ORM\ManyToOne(targetEntity="Labco\CoreBundle\Entity\Lang")
- * @ORM\JoinColumn(nullable=false)
- */
- protected $lang;
- /**
- * @var $status Status
- *
- * @ORM\ManyToOne(targetEntity="Labco\CoreBundle\Entity\User\Status")
- * @ORM\JoinColumn(nullable=false)
- */
- protected $status;
- /**
- * @var $speciality Speciality
- *
- * @ORM\ManyToOne(targetEntity="Labco\CoreBundle\Entity\User\Speciality")
- * @ORM\JoinColumn(nullable=true)
- */
- protected $speciality;
- /**
- * @var string
- *
- * @ORM\Column(name="adeli", type="string", length=10, nullable = true)
- */
- protected $adeli;
- /**
- * @ORM\OneToMany(targetEntity="Labco\CoreBundle\Entity\Analysis\AckAnalysis", mappedBy="user")
- */
- private $ackNotifications;
- /**
- * @var $circuits ArrayCollection
- *
- * @ORM\OneToMany(targetEntity="Labco\CoreBundle\Entity\User\Circuit", mappedBy="user")
- */
- protected $circuits;
- /**
- * @var $samplingDeposits ArrayCollection
- *
- * @ORM\OneToMany(targetEntity="Labco\CoreBundle\Entity\User\SamplingDeposit", mappedBy="user")
- */
- protected $samplingDeposits;
- /**
- * @var $clusUserVersions ArrayCollection
- * @ORM\OneToMany(targetEntity="Labco\CoreBundle\Entity\ClusAck", mappedBy="user")
- */
- protected $clusAcks;
- /**
- * @var $localCodes ArrayCollection
- * @ORM\OneToMany(targetEntity="Labco\CoreBundle\Entity\User\LocalCode", mappedBy="user")
- */
- protected $localCodes;
- /**
- * @ORM\OneToMany(targetEntity="Labco\CoreBundle\Entity\Relations\UserPatient", mappedBy="user")
- */
- protected $userPatients;
- /**
- * @ORM\ManyToOne(targetEntity="Labco\CoreBundle\Entity\Sel")
- * @ORM\JoinColumn(name="default_sel_id", nullable=true)
- */
- protected $defaultSel;
- public function __construct()
- {
- parent::__construct();
- $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
- $randomString = '';
- for ($i = 0; $i < 60; $i++) {
- $randomString .= $characters[rand(0, strlen($characters) - 1)];
- }
- $this->enabled = true;
- $this->salt = base_convert(sha1(uniqid(mt_rand(), true)), 16, 36);
- $this->password = $randomString;
- $this->roles = array('ROLE_USER');
- $this->sels = new ArrayCollection();
- $this->ackNotifications = new ArrayCollection;
- $this->circuits = new ArrayCollection();
- $this->samplingDeposits = new ArrayCollection();
- $this->clusAcks = new ArrayCollection();
- $this->localCodes = new ArrayCollection();
- $this->patients = new ArrayCollection();
- $this->userPatients = new ArrayCollection();
- }
- public function clearAllCollections()
- {
- $this->circuits = new ArrayCollection();
- $this->samplingDeposits = new ArrayCollection();
- $this->clusAcks = new ArrayCollection();
- $this->localCodes = new ArrayCollection();
- $this->userPatients = new ArrayCollection();
- }
- /**
- * @Assert\Callback
- */
- public function validate(ExecutionContextInterface $context)
- {
- if ($this->getStatus()->getHasAdeliNumber() && empty($this->adeli)) {
- $context->buildViolation("form.user.error_adeli")->setTranslationDomain("messages")->atPath("adeli")->addViolation();
- }
- if ($this->getStatus()->getHasSpeciality() && $this->getSpeciality()->getCode() == "unspecified") {
- $context->buildViolation('form.user.error_speciality')->setTranslationDomain("messages")->atPath("speciality")->addViolation();
- }
- }
- /**
- * Get id
- *
- * @return integer
- */
- public function getId()
- {
- return $this->id;
- }
- /**
- * Set email
- *
- * @param string $email
- * @return User
- */
- public function setEmail($email)
- {
- $this->email = $email;
- return $this;
- }
- /**
- * Get email
- *
- * @return string
- */
- public function getEmail()
- {
- return $this->email;
- }
- /**
- * Set enabled
- *
- * @param boolean $enabled
- * @return User
- */
- public function setEnabled($enabled)
- {
- $this->enabled = $enabled;
- return $this;
- }
- /**
- * Get enabled
- *
- * @return boolean
- */
- public function getEnabled()
- {
- return $this->enabled;
- }
- /**
- * Set salt
- *
- * @param string $salt
- * @return User
- */
- public function setSalt($salt)
- {
- $this->salt = $salt;
- return $this;
- }
- /**
- * Get salt
- *
- * @return string
- */
- public function getSalt()
- {
- return $this->salt;
- }
- /**
- * Set password
- *
- * @param string $password
- * @return User
- */
- public function setPassword($password)
- {
- $this->password = $password;
- return $this;
- }
- /**
- * Get password
- *
- * @return string
- */
- public function getPassword()
- {
- return $this->password;
- }
- /**
- * Set roles
- *
- * @param array $roles
- * @return User
- */
- public function setRoles($roles)
- {
- $this->roles = $roles;
- return $this;
- }
- public function addRole($role)
- {
- $role = strtoupper($role);
- if ($role === 'ROLE_USER') {
- return $this;
- }
- if (!in_array($role, $this->roles, true)) {
- $this->roles[] = $role;
- }
- return $this;
- }
- public function removeRole($role)
- {
- if (false !== $key = array_search(strtoupper($role), $this->roles, true)) {
- unset($this->roles[$key]);
- $this->roles = array_values($this->roles);
- }
- return $this;
- }
- /**
- * Get roles
- *
- * @return array
- */
- public function getRoles()
- {
- return $this->roles;
- }
- /**
- * Set firstname
- *
- * @param string $firstname
- * @return User
- */
- public function setFirstname($firstname)
- {
- $this->firstname = $firstname;
- return $this;
- }
- /**
- * Get firstname
- *
- * @return string
- */
- public function getFirstname()
- {
- return $this->firstname;
- }
- /**
- * Set lastname
- *
- * @param string $lastname
- * @return User
- */
- public function setLastname($lastname)
- {
- $this->lastname = $lastname;
- return $this;
- }
- /**
- * Get lastname
- *
- * @return string
- */
- public function getLastname()
- {
- return $this->lastname;
- }
- /**
- * Returns the username used to authenticate the user.
- *
- * @return string The username
- */
- public function getUsername()
- {
- return $this->email;
- }
- /**
- * @param mixed $lang
- */
- public function setLang($lang)
- {
- $this->lang = $lang;
- }
- /**
- * @return mixed
- */
- public function getLang()
- {
- return $this->lang;
- }
- /**
- * @param string $adeli
- */
- public function setAdeli($adeli)
- {
- $this->adeli = $adeli;
- }
- /**
- * @return string
- */
- public function getAdeli()
- {
- return $this->adeli;
- }
- /**
- * Removes sensitive data from the user.
- *
- * This is important if, at any given point, sensitive information like
- * the plain-text password is stored on this object.
- */
- public function eraseCredentials()
- {
- }
- /**
- * @param \Labco\CoreBundle\Entity\User\Speciality $speciality
- */
- public function setSpeciality($speciality)
- {
- $this->speciality = $speciality;
- }
- /**
- * @return \Labco\CoreBundle\Entity\User\Speciality
- */
- public function getSpeciality()
- {
- return $this->speciality;
- }
- /**
- * @param Status $status
- */
- public function setStatus($status)
- {
- $this->status = $status;
- }
- /**
- * @return Status
- */
- public function getStatus()
- {
- return $this->status;
- }
- /**
- * @param mixed $userSels
- */
- public function setUserSels($userSels)
- {
- $this->userSels = $userSels;
- }
- /**
- * Add userSels
- *
- * @param \Labco\CoreBundle\Entity\Relations\UserSel $userSels
- * @return User
- */
- public function addUserSel(\Labco\CoreBundle\Entity\Relations\UserSel $userSels)
- {
- $this->userSels[] = $userSels;
- return $this;
- }
- /**
- * Remove userSels
- *
- * @param \Labco\CoreBundle\Entity\Relations\UserSel $userSels
- */
- public function removeUserSel(\Labco\CoreBundle\Entity\Relations\UserSel $userSels)
- {
- $this->userSels->removeElement($userSels);
- }
- /**
- * Get userSels
- *
- * @return \Doctrine\Common\Collections\Collection
- */
- public function getUserSels()
- {
- $userSels = $this->filterWithHistorisation($this->userSels);
- return $userSels;
- }
- /**
- * @param mixed $ackNotifications
- */
- public function setAckNotifications($ackNotifications)
- {
- $this->ackNotifications = $ackNotifications;
- }
- /**
- * Add ackNotifications
- *
- * @param \Labco\CoreBundle\Entity\Analysis\AckAnalysis $ackNotifications
- * @return User
- */
- public function addAckNotifications(\Labco\CoreBundle\Entity\Analysis\AckAnalysis $ackNotifications)
- {
- $this->ackNotifications[] = $ackNotifications;
- return $this;
- }
- /**
- * Remove ackNotifications
- *
- * @param \Labco\CoreBundle\Entity\Analysis\AckAnalysis $ackNotifications
- */
- public function removeAckNotifications(\Labco\CoreBundle\Entity\Analysis\AckAnalysis $ackNotifications)
- {
- $this->ackNotifications->removeElement($ackNotifications);
- }
- /**
- * Get ackNotifications
- *
- * @return \Doctrine\Common\Collections\Collection
- */
- public function getAckNotifications()
- {
- return $this->ackNotifications;
- }
- /**
- * @param mixed $conservations
- */
- public function setConservations($conservations)
- {
- $this->conservations = $conservations;
- }
- /**
- * @return mixed
- */
- public function getCircuits()
- {
- $this->circuits = $this->filterWithHistorisation($this->circuits);
- return $this->circuits;
- }
- /**
- * @param mixed $circuits
- */
- public function setCircuits($circuits)
- {
- $this->circuits = $circuits;
- }
- public function removeCircuit(Circuit $circuit)
- {
- $this->circuits->removeElement($circuit);
- }
- /**
- * @return mixed
- */
- public function getSamplingDeposits()
- {
- $this->samplingDeposits = $this->filterWithHistorisation($this->samplingDeposits);
- return $this->samplingDeposits;
- }
- /**
- * @param mixed $samplingDeposits
- */
- public function setSamplingDeposits($samplingDeposits)
- {
- $this->samplingDeposits = $samplingDeposits;
- }
- public function removeSamplingDeposit(SamplingDeposit $samplingDeposit)
- {
- $this->samplingDeposits->removeElement($samplingDeposit);
- }
- /**
- * @return ArrayCollection
- */
- public function getClusAcks()
- {
- $this->clusAcks = $this->filterWithHistorisation($this->clusAcks);
- return $this->clusAcks;
- }
- public function addClusAck(ClusAck $clusAck)
- {
- $this->clusAcks[] = $clusAck;
- return $this;
- }
- public function removeClusAck(ClusAck $clusAck)
- {
- $this->clusAcks->removeElement($clusAck);
- }
- /**
- * @return ArrayCollection
- */
- public function getLocalCodes()
- {
- $this->localCodes = $this->filterWithHistorisation($this->localCodes);
- return $this->localCodes;
- }
- public function addLocalCode(LocalCode $localCode)
- {
- $this->localCodes[] = $localCode;
- return $this;
- }
- public function removeLocalCode(LocalCode $localCode)
- {
- $this->localCodes->removeElement($localCode);
- }
- /**
- * @param mixed $localCodes
- */
- public function setLocalCodes($localCodes)
- {
- $this->localCodes = $localCodes;
- }
- /**
- * @param mixed $userPatients
- */
- public function setUserPatients($userPatients)
- {
- $this->userPatients = $userPatients;
- }
- /**
- * Add userPatients
- *
- * @param \Labco\CoreBundle\Entity\Relations\UserPatient $userPatients
- * @return Patient
- */
- public function addUserPatient(\Labco\CoreBundle\Entity\Relations\UserPatient $userPatients)
- {
- $this->userPatients[] = $userPatients;
- return $this;
- }
- /**
- * Remove userPatients
- *
- * @param \Labco\CoreBundle\Entity\Relations\UserPatient $userPatients
- */
- public function removeUserPatient(\Labco\CoreBundle\Entity\Relations\UserPatient $userPatients)
- {
- $this->userPatients->removeElement($userPatients);
- }
- /**
- * Get userPatients
- *
- * @return \Doctrine\Common\Collections\Collection
- */
- public function getUserPatients()
- {
- $this->userPatients = $this->filterWithHistorisation($this->userPatients);
- return $this->userPatients;
- }
- /**
- * @return mixed
- */
- public function getDefaultSel()
- {
- return $this->defaultSel;
- }
- /**
- * @param mixed $defaultSel
- * @return User
- */
- public function setDefaultSel($defaultSel)
- {
- $this->defaultSel = $defaultSel;
- return $this;
- }
- //API
- /**
- * @JMS\VirtualProperty
- * @JMS\SerializedName("isNurse")
- * @JMS\Groups({"detailsUser"})
- * @return boolean
- */
- public function isNurse()
- {
- if ($this->getStatus() == null) {
- return false;
- }
- return $this->getStatus()->getIsNurse();
- }
- /**
- * @JMS\VirtualProperty
- * @JMS\SerializedName("user_sels")
- * @JMS\Groups({"detailsUser"})
- *
- * @return String
- */
- public function getApiUserSels()
- {
- $userSels = $this->filterWithHistorisation($this->userSels);
- return $userSels;
- }
- /**
- * @JMS\VirtualProperty
- * @JMS\SerializedName("clus_acks")
- * @JMS\Groups({"userClusAck"})
- * @return ArrayCollection
- */
- public function getApiClusAcks()
- {
- $clusAcks = null;
- if (!$this->isGlobal()) {
- $clusAcks = $this->filterWithHistorisationClusAcks($this->clusAcks);
- }
- return $clusAcks;
- }
- public function filterWithHistorisationClusAcks($collection){
- $entities = new ArrayCollection();
- if(!is_object($collection) || $collection->isEmpty()) {
- return $entities;
- }
- foreach($collection as $entity){
- if( $entity->getDateOut() > new \DateTime() &&
- $entity->getDateDeleted() == NULL &&
- $entity->getDateIn() <= new \DateTime('+1 seconds')){
- $entities->add($entity);
- }
- }
- return $entities;
- }
- /**
- * @JMS\VirtualProperty
- * @JMS\SerializedName("is_global")
- * @JMS\Groups({"detailsUser"})
- * @return mixed
- */
- public function getApiIsGlobal($roles = null)
- {
- $roles = ($roles == null) ? $this->roles : $roles;
- if (in_array('ROLE_DATA_MANAGER', $roles, true)) {
- return true;
- } elseif (in_array('ROLE_GLOBAL_ADMIN', $roles, true)) {
- return true;
- }
- return false;
- }
- /**
- * Get Sels
- * @return ArrayCollection
- */
- public function getSels()
- {
- $sels = new ArrayCollection();
- foreach ($this->getUserSels() as $userSel) {
- /** @var $userSel UserSel */
- $sels->add($userSel->getSel());
- }
- return $sels;
- }
- /**
- * Get Patients
- * @return ArrayCollection
- */
- public function getPatients()
- {
- $patients = new ArrayCollection();
- foreach ($this->getUserPatients() as $userPatient) {
- /** @var $userPatient UserPatient */
- $patients->add($userPatient->getPatient());
- }
- return $patients;
- }
- public function isRoot()
- {
- return $this->email == User::ROOT_EMAIL_ADDRESS;
- }
- public function isGlobal($roles = null)
- {
- $roles = ($roles == null) ? $this->roles : $roles;
- if (in_array('ROLE_DATA_MANAGER', $roles, true)) {
- return true;
- } elseif (in_array('ROLE_GLOBAL_ADMIN', $roles, true)) {
- return true;
- }
- return false;
- }
- public function copy()
- {
- $newUser = new User();
- $newUser->setFirstname($this->getFirstname());
- $newUser->setLastname($this->getLastname());
- $newUser->setEmail($this->getEmail());
- $newUser->setPassword($this->getPassword());
- $newUser->setRoles($this->getRoles());
- return $newUser;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement