<?php
namespace Submarine\UsersBundle\Entity;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Submarine\CoreBundle\Entity\SubmarineEntityInterface;
use Submarine\CoreBundle\Uploader\AbstractUploadEntity;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\File;
/**
* Пользователь
*
* @ORM\Table(name="submarine_auth_users")
* @ORM\Entity()
*/
class User extends AbstractUploadEntity implements \Serializable, SubmarineEntityInterface, UserInterface
{
/**
* @ORM\Column(name="id", type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Id
*/
protected $id;
/**
* @ORM\Column(name="username",type="string", length=255, unique=false)
* @Assert\NotBlank()
* @Assert\Length(min="3")
*/
protected $username;
/**
* @ORM\Column(name="salt", type="string", length=32)
*/
protected $salt;
/**
* @ORM\Column(name="password", type="string", length=128)
* @Assert\Length(min="6")
*/
protected $password;
/**
* @ORM\Column(name="email", type="string", length=255, unique=true)
* @Assert\NotBlank()
* @Assert\Email
*/
protected $email;
/**
* @ORM\Column(name="is_active", type="boolean", nullable=true)
*/
protected $enabled;
/**
* Аккаунт подтвержден?
* @ORM\Column(name="is_confirmed", type="boolean", nullable=true)
*/
protected $confirmed;
/**
* @ORM\Column(type="text", nullable=true)
*/
protected $description;
/**
* @ORM\Column(type="datetime", unique=false, nullable=true)
*/
protected $dateTimeCreated;
/**
* @Assert\File(maxSize = "5M", mimeTypes={"image/png", "image/jpeg", "image/pjpeg", "image/gif"})
* @var File $image
*/
protected $file;
/**
* @ORM\Column(type="string", name="image", length=255, unique=false, nullable=true)
*/
protected $image;
/**
* @ORM\ManyToMany(targetEntity="Group", inversedBy="users")
* @ORM\JoinTable(name="submarine_auth_groups_map")
* @var Group[]|ArrayCollection
*/
protected $groups;
/**
* @var ArrayCollection|UserAuthAccount[]
*
* @ORM\OneToMany(targetEntity="Submarine\UsersBundle\Entity\UserAuthAccount", mappedBy="user", cascade={"persist", "remove"})
*/
protected $authAccounts;
/**
* @var Collection
*
* One product has many features. This is the inverse side.
* @ORM\OneToMany(targetEntity="Submarine\OrdersBundle\Entity\Order", mappedBy="user")
*/
private $orders;
/**
* Имя сущности
* @return string
*/
static public function entityName()
{
return __CLASS__;
}
public function __construct()
{
$this->salt = md5(uniqid());
$this->enabled = true;
$this->groups = new ArrayCollection();
$this->dateTimeCreated = new \DateTime();
$this->confirmed = true;
$this->authAccounts = new ArrayCollection();
}
public function eraseCredentials()
{
}
/**
* ID пользователя
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* ID пользователя
* @param int $id
*/
public function setId($id)
{
$this->id = (int)$id;
}
/**
* Соль
* @param string $salt
*/
public function setSalt($salt)
{
$this->salt = $salt;
}
/**
* Соль
* @return string
*/
public function getSalt()
{
return $this->salt;
}
/**
* Пароль (зашифрованный)
* @return string
*/
public function getPassword()
{
return $this->password;
}
/**
* Пароль зашифрованный
* @param string $password
*/
public function setPassword($password)
{
$this->password = $password;
}
/**
* Роли пользователя
* @return Role[]
*/
public function getRoles()
{
$roles = [];
foreach ($this->groups as $group) {
$roles[] = $group->getRole();
}
return $roles;
}
/**
* Группы пользователя
* @return Group[]|ArrayCollection
*/
public function getGroups()
{
return $this->groups;
}
/**
* @return ArrayCollection|UserAuthAccount[]
*/
public function getAuthAccounts()
{
return $this->authAccounts;
}
/**
* Имя пользователя
* @return string
*/
public function getUsername()
{
return $this->username;
}
/**
* Имя пользователя
* @param string $username
*/
public function setUsername($username)
{
$this->username = $username;
}
/**
* Подтвержен пользователь?
* Используется для подтверждения e-mail
* @param bool $confirmed
*/
public function setConfirmed($confirmed)
{
$this->confirmed = $confirmed;
}
/**
* Подтвержен пользователь?
* Используется для подтверждения e-mail
* @return bool
*/
public function isConfirmed()
{
return $this->confirmed;
}
/**
* Пользователь включен?
* Если включен, но не подтвержден - false
* @return boolean
*/
public function isEnabled()
{
if (!$this->confirmed) {
return false;
}
return $this->enabled;
}
/**
* Пользователь включен
* @param boolean
*/
public function setEnabled($isEnabled)
{
$this->enabled = (bool)$isEnabled;
}
/**
* E-mail
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* E-mail
* @param string $email
*/
public function setEmail($email)
{
$this->email = $email;
}
/**
* Описание
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Описание
* @param string $description
*/
public function setDescription($description)
{
$this->description = $description;
}
/**
* Время создания
* @return \DateTime
*/
public function getDateTimeCreated()
{
return $this->dateTimeCreated;
}
/**
* Время создания
* @param \DateTime $dateTimeCreated
*/
public function setDateTimeCreated(\DateTime $dateTimeCreated)
{
$this->dateTimeCreated = $dateTimeCreated;
}
//---------- Image ----------------
/**
* @param string $image
*/
public function setImage($image)
{
$this->image = $image;
}
/**
* @return string
*/
public function getImage()
{
return $this->image;
}
/**
* @param File $file
*/
public function setFile(File $file = null)
{
$this->file = $file;
}
/**
* Получение файла
* @return File
*/
function getFile()
{
return $this->file;
}
/**
* Путь к файлу
* @param string $filePath
*/
function setFilePath($filePath)
{
$this->image = $filePath;
}
/**
* @return Collection
*/
public function getOrders(): Collection
{
return $this->orders;
}
/**
* @param Collection $orders
*/
public function setOrders(Collection $orders): void
{
$this->orders = $orders;
}
/**
* Путь к файлу
* @return string
*/
function getFilePath()
{
return $this->image;
}
public function getPathPublic()
{
return 'media/user/';
}
// --------------- Serializable ----------
/**
* (PHP 5 >= 5.1.0)<br/>
* String representation of object
* @link http://php.net/manual/en/serializable.serialize.php
* @return string the string representation of the object or null
*/
public function serialize()
{
return serialize(
[
'username' => $this->username,
'email' => $this->email,
'confirmed' => $this->confirmed,
'enabled' => $this->enabled,
'dateTimeCreated' => $this->dateTimeCreated,
'id' => $this->id,
'image' => $this->image,
'groups' => serialize($this->groups),
'password' => $this->password,
'salt' => $this->salt,
'authAccounts' => serialize($this->authAccounts),
]
);
}
/**
* (PHP 5 >= 5.1.0)<br/>
* Constructs the object
* @link http://php.net/manual/en/serializable.unserialize.php
* @param string $serialized <p>
* The string representation of the object.
* </p>
* @return void
*/
public function unserialize($serialized)
{
$data = unserialize($serialized);
$this->username = $data['username'];
$this->email = $data['email'];
$this->confirmed = $data['confirmed'];
$this->enabled = $data['enabled'];
$this->dateTimeCreated = $data['dateTimeCreated'];
$this->id = $data['id'];
$this->image = $data['image'];
$this->groups = unserialize($data['groups']);
$this->password = $data['password'];
$this->salt = $data['salt'];
$authAccounts = @unserialize($data['authAccounts']);
$this->authAccounts = $authAccounts === false ? new ArrayCollection() : $authAccounts;
}
}