src/Submarine/UsersBundle/Entity/User.php line 20

Open in your IDE?
  1. <?php
  2. namespace Submarine\UsersBundle\Entity;
  3. use Doctrine\Common\Collections\Collection;
  4. use Doctrine\ORM\Mapping as ORM;
  5. use Submarine\CoreBundle\Entity\SubmarineEntityInterface;
  6. use Submarine\CoreBundle\Uploader\AbstractUploadEntity;
  7. use Doctrine\Common\Collections\ArrayCollection;
  8. use Symfony\Component\Security\Core\User\UserInterface;
  9. use Symfony\Component\Validator\Constraints as Assert;
  10. use Symfony\Component\HttpFoundation\File\File;
  11. /**
  12.  * Пользователь
  13.  *
  14.  * @ORM\Table(name="submarine_auth_users")
  15.  * @ORM\Entity()
  16.  */
  17. class User extends AbstractUploadEntity implements \SerializableSubmarineEntityInterfaceUserInterface
  18. {
  19.     /**
  20.      * @ORM\Column(name="id", type="integer")
  21.      * @ORM\GeneratedValue(strategy="AUTO")
  22.      * @ORM\Id
  23.      */
  24.     protected $id;
  25.     /**
  26.      * @ORM\Column(name="username",type="string", length=255, unique=false)
  27.      * @Assert\NotBlank()
  28.      * @Assert\Length(min="3")
  29.      */
  30.     protected $username;
  31.     /**
  32.      * @ORM\Column(name="salt", type="string", length=32)
  33.      */
  34.     protected $salt;
  35.     /**
  36.      * @ORM\Column(name="password", type="string", length=128)
  37.      * @Assert\Length(min="6")
  38.      */
  39.     protected $password;
  40.     /**
  41.      * @ORM\Column(name="email", type="string", length=255, unique=true)
  42.      * @Assert\NotBlank()
  43.      * @Assert\Email
  44.      */
  45.     protected $email;
  46.     /**
  47.      * @ORM\Column(name="is_active", type="boolean", nullable=true)
  48.      */
  49.     protected $enabled;
  50.     /**
  51.      * Аккаунт подтвержден?
  52.      * @ORM\Column(name="is_confirmed", type="boolean", nullable=true)
  53.      */
  54.     protected $confirmed;
  55.     /**
  56.      * @ORM\Column(type="text", nullable=true)
  57.      */
  58.     protected $description;
  59.     /**
  60.      * @ORM\Column(type="datetime", unique=false, nullable=true)
  61.      */
  62.     protected $dateTimeCreated;
  63.     /**
  64.      * @Assert\File(maxSize = "5M", mimeTypes={"image/png", "image/jpeg", "image/pjpeg", "image/gif"})
  65.      * @var File $image
  66.      */
  67.     protected $file;
  68.     /**
  69.      * @ORM\Column(type="string", name="image",  length=255, unique=false, nullable=true)
  70.      */
  71.     protected $image;
  72.     /**
  73.      * @ORM\ManyToMany(targetEntity="Group", inversedBy="users")
  74.      * @ORM\JoinTable(name="submarine_auth_groups_map")
  75.      * @var Group[]|ArrayCollection
  76.      */
  77.     protected $groups;
  78.     /**
  79.      * @var ArrayCollection|UserAuthAccount[]
  80.      *
  81.      * @ORM\OneToMany(targetEntity="Submarine\UsersBundle\Entity\UserAuthAccount", mappedBy="user", cascade={"persist", "remove"})
  82.      */
  83.     protected $authAccounts;
  84.     /**
  85.      * @var Collection
  86.      *
  87.      * One product has many features. This is the inverse side.
  88.      * @ORM\OneToMany(targetEntity="Submarine\OrdersBundle\Entity\Order", mappedBy="user")
  89.      */
  90.     private $orders;
  91.     /**
  92.      * Имя сущности
  93.      * @return string
  94.      */
  95.     static public function entityName()
  96.     {
  97.         return __CLASS__;
  98.     }
  99.     public function __construct()
  100.     {
  101.         $this->salt md5(uniqid());
  102.         $this->enabled true;
  103.         $this->groups = new ArrayCollection();
  104.         $this->dateTimeCreated = new \DateTime();
  105.         $this->confirmed true;
  106.         $this->authAccounts = new ArrayCollection();
  107.     }
  108.     public function eraseCredentials()
  109.     {
  110.     }
  111.     /**
  112.      * ID пользователя
  113.      * @return int
  114.      */
  115.     public function getId()
  116.     {
  117.         return $this->id;
  118.     }
  119.     /**
  120.      * ID пользователя
  121.      * @param int $id
  122.      */
  123.     public function setId($id)
  124.     {
  125.         $this->id = (int)$id;
  126.     }
  127.     /**
  128.      * Соль
  129.      * @param string $salt
  130.      */
  131.     public function setSalt($salt)
  132.     {
  133.         $this->salt $salt;
  134.     }
  135.     /**
  136.      * Соль
  137.      * @return string
  138.      */
  139.     public function getSalt()
  140.     {
  141.         return $this->salt;
  142.     }
  143.     /**
  144.      * Пароль (зашифрованный)
  145.      * @return string
  146.      */
  147.     public function getPassword()
  148.     {
  149.         return $this->password;
  150.     }
  151.     /**
  152.      * Пароль зашифрованный
  153.      * @param string $password
  154.      */
  155.     public function setPassword($password)
  156.     {
  157.         $this->password $password;
  158.     }
  159.     /**
  160.      * Роли пользователя
  161.      * @return Role[]
  162.      */
  163.     public function getRoles()
  164.     {
  165.         $roles = [];
  166.         foreach ($this->groups as $group) {
  167.             $roles[] = $group->getRole();
  168.         }
  169.         return $roles;
  170.     }
  171.     /**
  172.      * Группы пользователя
  173.      * @return Group[]|ArrayCollection
  174.      */
  175.     public function getGroups()
  176.     {
  177.         return $this->groups;
  178.     }
  179.     /**
  180.      * @return ArrayCollection|UserAuthAccount[]
  181.      */
  182.     public function getAuthAccounts()
  183.     {
  184.         return $this->authAccounts;
  185.     }
  186.     /**
  187.      * Имя пользователя
  188.      * @return string
  189.      */
  190.     public function getUsername()
  191.     {
  192.         return $this->username;
  193.     }
  194.     /**
  195.      * Имя пользователя
  196.      * @param string $username
  197.      */
  198.     public function setUsername($username)
  199.     {
  200.         $this->username $username;
  201.     }
  202.     /**
  203.      * Подтвержен пользователь?
  204.      * Используется для подтверждения e-mail
  205.      * @param bool $confirmed
  206.      */
  207.     public function setConfirmed($confirmed)
  208.     {
  209.         $this->confirmed $confirmed;
  210.     }
  211.     /**
  212.      * Подтвержен пользователь?
  213.      * Используется для подтверждения e-mail
  214.      * @return bool
  215.      */
  216.     public function isConfirmed()
  217.     {
  218.         return $this->confirmed;
  219.     }
  220.     /**
  221.      * Пользователь включен?
  222.      * Если включен, но не подтвержден - false
  223.      * @return boolean
  224.      */
  225.     public function isEnabled()
  226.     {
  227.         if (!$this->confirmed) {
  228.             return false;
  229.         }
  230.         return $this->enabled;
  231.     }
  232.     /**
  233.      * Пользователь включен
  234.      * @param boolean
  235.      */
  236.     public function setEnabled($isEnabled)
  237.     {
  238.         $this->enabled = (bool)$isEnabled;
  239.     }
  240.     /**
  241.      * E-mail
  242.      * @return string
  243.      */
  244.     public function getEmail()
  245.     {
  246.         return $this->email;
  247.     }
  248.     /**
  249.      * E-mail
  250.      * @param string $email
  251.      */
  252.     public function setEmail($email)
  253.     {
  254.         $this->email $email;
  255.     }
  256.     /**
  257.      * Описание
  258.      * @return string
  259.      */
  260.     public function getDescription()
  261.     {
  262.         return $this->description;
  263.     }
  264.     /**
  265.      * Описание
  266.      * @param string $description
  267.      */
  268.     public function setDescription($description)
  269.     {
  270.         $this->description $description;
  271.     }
  272.     /**
  273.      * Время создания
  274.      * @return \DateTime
  275.      */
  276.     public function getDateTimeCreated()
  277.     {
  278.         return $this->dateTimeCreated;
  279.     }
  280.     /**
  281.      * Время создания
  282.      * @param \DateTime $dateTimeCreated
  283.      */
  284.     public function setDateTimeCreated(\DateTime $dateTimeCreated)
  285.     {
  286.         $this->dateTimeCreated $dateTimeCreated;
  287.     }
  288.     //---------- Image ----------------
  289.     /**
  290.      * @param string $image
  291.      */
  292.     public function setImage($image)
  293.     {
  294.         $this->image $image;
  295.     }
  296.     /**
  297.      * @return string
  298.      */
  299.     public function getImage()
  300.     {
  301.         return $this->image;
  302.     }
  303.     /**
  304.      * @param File $file
  305.      */
  306.     public function setFile(File $file null)
  307.     {
  308.         $this->file $file;
  309.     }
  310.     /**
  311.      * Получение файла
  312.      * @return File
  313.      */
  314.     function getFile()
  315.     {
  316.         return $this->file;
  317.     }
  318.     /**
  319.      * Путь к файлу
  320.      * @param string $filePath
  321.      */
  322.     function setFilePath($filePath)
  323.     {
  324.         $this->image $filePath;
  325.     }
  326.     /**
  327.      * @return Collection
  328.      */
  329.     public function getOrders(): Collection
  330.     {
  331.         return $this->orders;
  332.     }
  333.     /**
  334.      * @param Collection $orders
  335.      */
  336.     public function setOrders(Collection $orders): void
  337.     {
  338.         $this->orders $orders;
  339.     }
  340.     /**
  341.      * Путь к файлу
  342.      * @return string
  343.      */
  344.     function getFilePath()
  345.     {
  346.         return $this->image;
  347.     }
  348.     public function getPathPublic()
  349.     {
  350.         return 'media/user/';
  351.     }
  352.     // --------------- Serializable ----------
  353.     /**
  354.      * (PHP 5 &gt;= 5.1.0)<br/>
  355.      * String representation of object
  356.      * @link http://php.net/manual/en/serializable.serialize.php
  357.      * @return string the string representation of the object or null
  358.      */
  359.     public function serialize()
  360.     {
  361.         return serialize(
  362.             [
  363.                 'username' => $this->username,
  364.                 'email' => $this->email,
  365.                 'confirmed' => $this->confirmed,
  366.                 'enabled' => $this->enabled,
  367.                 'dateTimeCreated' => $this->dateTimeCreated,
  368.                 'id' => $this->id,
  369.                 'image' => $this->image,
  370.                 'groups' => serialize($this->groups),
  371.                 'password' => $this->password,
  372.                 'salt' => $this->salt,
  373.                 'authAccounts' => serialize($this->authAccounts),
  374.             ]
  375.         );
  376.     }
  377.     /**
  378.      * (PHP 5 &gt;= 5.1.0)<br/>
  379.      * Constructs the object
  380.      * @link http://php.net/manual/en/serializable.unserialize.php
  381.      * @param string $serialized <p>
  382.      * The string representation of the object.
  383.      * </p>
  384.      * @return void
  385.      */
  386.     public function unserialize($serialized)
  387.     {
  388.         $data unserialize($serialized);
  389.         $this->username $data['username'];
  390.         $this->email $data['email'];
  391.         $this->confirmed $data['confirmed'];
  392.         $this->enabled $data['enabled'];
  393.         $this->dateTimeCreated $data['dateTimeCreated'];
  394.         $this->id $data['id'];
  395.         $this->image $data['image'];
  396.         $this->groups unserialize($data['groups']);
  397.         $this->password $data['password'];
  398.         $this->salt $data['salt'];
  399.         $authAccounts = @unserialize($data['authAccounts']);
  400.         $this->authAccounts $authAccounts === false ? new ArrayCollection() : $authAccounts;
  401.     }
  402. }