3 namespace Wallabag\UserBundle\Entity
;
5 use Doctrine\Common\Collections\ArrayCollection
;
6 use Doctrine\ORM\Mapping
as ORM
;
7 use FOS\UserBundle\Model\User
as BaseUser
;
8 use JMS\Serializer\Annotation\Accessor
;
9 use JMS\Serializer\Annotation\Groups
;
10 use JMS\Serializer\Annotation\XmlRoot
;
11 use Scheb\TwoFactorBundle\Model\Email\TwoFactorInterface
;
12 use Scheb\TwoFactorBundle\Model\TrustedComputerInterface
;
13 use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity
;
14 use Symfony\Component\Security\Core\User\UserInterface
;
15 use Wallabag\ApiBundle\Entity\Client
;
16 use Wallabag\CoreBundle\Entity\Config
;
17 use Wallabag\CoreBundle\Entity\Entry
;
18 use Wallabag\CoreBundle\Helper\EntityTimestampsTrait
;
24 * @ORM\Entity(repositoryClass="Wallabag\UserBundle\Repository\UserRepository")
25 * @ORM\Table(name="`user`")
26 * @ORM\HasLifecycleCallbacks()
28 * @UniqueEntity("email")
29 * @UniqueEntity("username")
31 class User
extends BaseUser
implements TwoFactorInterface
, TrustedComputerInterface
33 use EntityTimestampsTrait
;
35 /** @Serializer\XmlAttribute */
39 * @ORM\Column(name="id", type="integer")
41 * @ORM\GeneratedValue(strategy="AUTO")
43 * @Groups({"user_api", "user_api_with_client"})
50 * @ORM\Column(name="name", type="text", nullable=true)
52 * @Groups({"user_api", "user_api_with_client"})
59 * @Groups({"user_api", "user_api_with_client"})
66 * @Groups({"user_api", "user_api_with_client"})
73 * @ORM\Column(name="created_at", type="datetime")
75 * @Groups({"user_api", "user_api_with_client"})
82 * @ORM\Column(name="updated_at", type="datetime")
84 * @Groups({"user_api", "user_api_with_client"})
89 * @ORM\OneToMany(targetEntity="Wallabag\CoreBundle\Entity\Entry", mappedBy="user", cascade={"remove"})
94 * @ORM\OneToOne(targetEntity="Wallabag\CoreBundle\Entity\Config", mappedBy="user", cascade={"remove"})
99 * @var ArrayCollection
101 * @ORM\OneToMany(targetEntity="Wallabag\CoreBundle\Entity\SiteCredential", mappedBy="user", cascade={"remove"})
103 protected $siteCredentials;
106 * @var ArrayCollection
108 * @ORM\OneToMany(targetEntity="Wallabag\ApiBundle\Entity\Client", mappedBy="user", cascade={"remove"})
113 * @see getFirstClient() below
115 * @Groups({"user_api_with_client"})
116 * @Accessor(getter="getFirstClient")
118 protected $default_client;
121 * @ORM\Column(type="integer", nullable=true)
128 * @ORM\Column(type="boolean")
130 private $twoFactorAuthentication = false;
133 * @ORM\Column(type="json_array", nullable=true)
137 public function __construct()
139 parent
::__construct();
140 $this->entries
= new ArrayCollection();
141 $this->roles
= ['ROLE_USER'];
147 * @param string $name
151 public function setName($name)
163 public function getName()
171 public function getCreatedAt()
173 return $this->createdAt
;
179 public function getUpdatedAt()
181 return $this->updatedAt
;
185 * @param Entry $entry
189 public function addEntry(Entry
$entry)
191 $this->entries
[] = $entry;
197 * @return ArrayCollection<Entry>
199 public function getEntries()
201 return $this->entries
;
204 public function isEqualTo(UserInterface
$user)
206 return $this->username
=== $user->getUsername();
212 * @param Config $config
216 public function setConfig(Config
$config = null)
218 $this->config
= $config;
228 public function getConfig()
230 return $this->config
;
236 public function isTwoFactorAuthentication()
238 return $this->twoFactorAuthentication
;
242 * @param bool $twoFactorAuthentication
244 public function setTwoFactorAuthentication($twoFactorAuthentication)
246 $this->twoFactorAuthentication
= $twoFactorAuthentication;
249 public function isEmailAuthEnabled()
251 return $this->twoFactorAuthentication
;
254 public function getEmailAuthCode()
256 return $this->authCode
;
259 public function setEmailAuthCode($authCode)
261 $this->authCode
= $authCode;
264 public function addTrustedComputer($token, \DateTime
$validUntil)
266 $this->trusted
[$token] = $validUntil->format('r');
269 public function isTrustedComputer($token)
271 if (isset($this->trusted
[$token])) {
272 $now = new \
DateTime();
273 $validUntil = new \
DateTime($this->trusted
[$token]);
275 return $now < $validUntil;
282 * @param Client $client
286 public function addClient(Client
$client)
288 $this->clients
[] = $client;
294 * @return ArrayCollection<Entry>
296 public function getClients()
298 return $this->clients
;
302 * Only used by the API when creating a new user it'll also return the first client (which was also created at the same time).
306 public function getFirstClient()
308 if (!empty($this->clients
)) {
309 return $this->clients
->first();