3 namespace Wallabag\UserBundle\Entity
;
5 use Doctrine\Common\Collections\ArrayCollection
;
6 use Doctrine\ORM\Mapping
as ORM
;
7 use JMS\Serializer\Annotation\Groups
;
8 use JMS\Serializer\Annotation\XmlRoot
;
9 use JMS\Serializer\Annotation\Accessor
;
10 use Scheb\TwoFactorBundle\Model\Email\TwoFactorInterface
;
11 use Scheb\TwoFactorBundle\Model\TrustedComputerInterface
;
12 use FOS\UserBundle\Model\User
as BaseUser
;
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\GroupBundle\Entity\Group
;
19 use Wallabag\GroupBundle\Entity\UserGroup
;
25 * @ORM\Entity(repositoryClass="Wallabag\UserBundle\Repository\UserRepository")
26 * @ORM\Table(name="`user`")
27 * @ORM\HasLifecycleCallbacks()
29 * @UniqueEntity("email")
30 * @UniqueEntity("username")
32 class User
extends BaseUser
implements TwoFactorInterface
, TrustedComputerInterface
34 /** @Serializer\XmlAttribute */
38 * @ORM\Column(name="id", type="integer")
40 * @ORM\GeneratedValue(strategy="AUTO")
42 * @Groups({"user_api", "user_api_with_client"})
49 * @ORM\Column(name="name", type="text", nullable=true)
51 * @Groups({"user_api", "user_api_with_client"})
58 * @Groups({"user_api", "user_api_with_client"})
65 * @Groups({"user_api", "user_api_with_client"})
72 * @ORM\Column(name="created_at", type="datetime")
74 * @Groups({"user_api", "user_api_with_client"})
81 * @ORM\Column(name="updated_at", type="datetime")
83 * @Groups({"user_api", "user_api_with_client"})
88 * @ORM\OneToMany(targetEntity="Wallabag\CoreBundle\Entity\Entry", mappedBy="user", cascade={"remove"})
93 * @ORM\OneToOne(targetEntity="Wallabag\CoreBundle\Entity\Config", mappedBy="user", cascade={"remove"})
98 * @ORM\Column(type="integer", nullable=true)
103 * @var ArrayCollection
105 * @ORM\OneToMany(targetEntity="Wallabag\GroupBundle\Entity\UserGroup", mappedBy="user", cascade={"persist", "remove"})
107 protected $userGroups;
110 * @var bool Enabled yes/no
111 * @ORM\Column(type="boolean")
113 private $twoFactorAuthentication = false;
116 * @ORM\Column(type="json_array", nullable=true)
121 * @var ArrayCollection
123 * @ORM\OneToMany(targetEntity="Wallabag\ApiBundle\Entity\Client", mappedBy="user", cascade={"remove"})
128 * @see getFirstClient() below
130 * @Groups({"user_api_with_client"})
131 * @Accessor(getter="getFirstClient")
133 protected $default_client;
135 public function __construct()
137 parent
::__construct();
138 $this->entries
= new ArrayCollection();
139 $this->userGroups
= new ArrayCollection();
140 $this->roles
= ['ROLE_USER'];
147 public function timestamps()
149 if (is_null($this->createdAt
)) {
150 $this->createdAt
= new \
DateTime();
153 $this->updatedAt
= new \
DateTime();
159 * @param string $name
163 public function setName($name)
175 public function getName()
183 public function getCreatedAt()
185 return $this->createdAt
;
191 public function getUpdatedAt()
193 return $this->updatedAt
;
197 * @param Entry $entry
201 public function addEntry(Entry
$entry)
203 $this->entries
[] = $entry;
209 * @return ArrayCollection<Entry>
211 public function getEntries()
213 return $this->entries
;
216 public function isEqualTo(UserInterface
$user)
218 return $this->username
=== $user->getUsername();
224 * @param Config $config
228 public function setConfig(Config
$config = null)
230 $this->config
= $config;
240 public function getConfig()
242 return $this->config
;
248 public function isTwoFactorAuthentication()
250 return $this->twoFactorAuthentication
;
254 * @param bool $twoFactorAuthentication
256 public function setTwoFactorAuthentication($twoFactorAuthentication)
258 $this->twoFactorAuthentication
= $twoFactorAuthentication;
261 public function isEmailAuthEnabled()
263 return $this->twoFactorAuthentication
;
266 public function getEmailAuthCode()
268 return $this->authCode
;
271 public function setEmailAuthCode($authCode)
273 $this->authCode
= $authCode;
276 public function addTrustedComputer($token, \DateTime
$validUntil)
278 $this->trusted
[$token] = $validUntil->format('r');
281 public function isTrustedComputer($token)
283 if (isset($this->trusted
[$token])) {
284 $now = new \
DateTime();
285 $validUntil = new \
DateTime($this->trusted
[$token]);
287 return $now < $validUntil;
294 * @param Client $client
298 public function addClient(Client
$client)
300 $this->clients
[] = $client;
306 * @return ArrayCollection<Entry>
308 public function getClients()
310 return $this->clients
;
314 * 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).
318 public function getFirstClient()
320 if (!empty($this->clients
)) {
321 return $this->clients
->first();
327 * @param Group $group
331 public function getUserGroupFromGroup(Group
$group)
333 foreach ($this->userGroups
as $userGroup) {
334 if ($userGroup->getGroup() == $group) {
344 * @param Group $group
347 public function setGroupRole(Group
$group, $role)
349 if ($userGroup = $this->getUserGroupFromGroup($group)) {
350 $userGroup->setRole($role);
355 * @param Group $group
359 public function getGroupRoleForUser(Group
$group)
361 if ($userGroup = $this->getUserGroupFromGroup($group)) {
362 return $userGroup->getRole();
369 * @param Group $group
373 public function inGroup(Group
$group)
375 if ($group::ACCESS_REQUEST
=== $group->getAcceptSystem()) {
376 $userGroup = $this->getUserGroupFromGroup($group);
378 return $userGroup->isAccepted();
381 return null !== $this->getUserGroupFromGroup($group);
385 * @return ArrayCollection<Group>
387 public function getGroups()
389 $groups = new ArrayCollection();
390 foreach ($this->userGroups
as $userGroup) {
391 $groups->add($userGroup->getGroup());