3 // This permits to have the LdapUserInterface even when fr3d/ldap-bundle is not
5 namespace FR3D\LdapBundle\Model
;
7 interface LdapUserInterface
9 public function setDn($dn);
10 public function getDn();
13 namespace Wallabag\UserBundle\Entity
;
15 use Doctrine\Common\Collections\ArrayCollection
;
16 use Doctrine\ORM\Mapping
as ORM
;
17 use FOS\UserBundle\Model\User
as BaseUser
;
18 use JMS\Serializer\Annotation\Accessor
;
19 use JMS\Serializer\Annotation\Groups
;
20 use JMS\Serializer\Annotation\XmlRoot
;
21 use Scheb\TwoFactorBundle\Model\Email\TwoFactorInterface
;
22 use Scheb\TwoFactorBundle\Model\TrustedComputerInterface
;
23 use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity
;
24 use Symfony\Component\Security\Core\User\UserInterface
;
25 use Wallabag\ApiBundle\Entity\Client
;
26 use Wallabag\CoreBundle\Entity\Config
;
27 use Wallabag\CoreBundle\Entity\Entry
;
28 use Wallabag\CoreBundle\Helper\EntityTimestampsTrait
;
29 use FR3D\LdapBundle\Model\LdapUserInterface
;
35 * @ORM\Entity(repositoryClass="Wallabag\UserBundle\Repository\UserRepository")
36 * @ORM\Table(name="`user`")
37 * @ORM\HasLifecycleCallbacks()
39 * @UniqueEntity("email")
40 * @UniqueEntity("username")
42 class User
extends BaseUser
implements TwoFactorInterface
, TrustedComputerInterface
, LdapUserInterface
44 use EntityTimestampsTrait
;
46 /** @Serializer\XmlAttribute */
50 * @ORM\Column(name="id", type="integer")
52 * @ORM\GeneratedValue(strategy="AUTO")
54 * @Groups({"user_api", "user_api_with_client"})
61 * @ORM\Column(name="name", type="text", nullable=true)
63 * @Groups({"user_api", "user_api_with_client"})
70 * @Groups({"user_api", "user_api_with_client"})
77 * @Groups({"user_api", "user_api_with_client"})
84 * @ORM\Column(name="dn", type="text", nullable=true)
91 * @ORM\Column(name="created_at", type="datetime")
93 * @Groups({"user_api", "user_api_with_client"})
100 * @ORM\Column(name="updated_at", type="datetime")
102 * @Groups({"user_api", "user_api_with_client"})
104 protected $updatedAt;
107 * @ORM\OneToMany(targetEntity="Wallabag\CoreBundle\Entity\Entry", mappedBy="user", cascade={"remove"})
112 * @ORM\OneToOne(targetEntity="Wallabag\CoreBundle\Entity\Config", mappedBy="user", cascade={"remove"})
117 * @var ArrayCollection
119 * @ORM\OneToMany(targetEntity="Wallabag\CoreBundle\Entity\SiteCredential", mappedBy="user", cascade={"remove"})
121 protected $siteCredentials;
124 * @var ArrayCollection
126 * @ORM\OneToMany(targetEntity="Wallabag\ApiBundle\Entity\Client", mappedBy="user", cascade={"remove"})
131 * @see getFirstClient() below
133 * @Groups({"user_api_with_client"})
134 * @Accessor(getter="getFirstClient")
136 protected $default_client;
139 * @ORM\Column(type="integer", nullable=true)
146 * @ORM\Column(type="boolean")
148 private $twoFactorAuthentication = false;
151 * @ORM\Column(type="json_array", nullable=true)
155 public function __construct()
157 parent
::__construct();
158 $this->entries
= new ArrayCollection();
159 $this->roles
= ['ROLE_USER'];
165 * @param string $name
169 public function setName($name)
181 public function getName()
189 public function getCreatedAt()
191 return $this->createdAt
;
197 public function getUpdatedAt()
199 return $this->updatedAt
;
203 * @param Entry $entry
207 public function addEntry(Entry
$entry)
209 $this->entries
[] = $entry;
215 * @return ArrayCollection<Entry>
217 public function getEntries()
219 return $this->entries
;
222 public function isEqualTo(UserInterface
$user)
224 return $this->username
=== $user->getUsername();
230 * @param Config $config
234 public function setConfig(Config
$config = null)
236 $this->config
= $config;
246 public function getConfig()
248 return $this->config
;
254 public function isTwoFactorAuthentication()
256 return $this->twoFactorAuthentication
;
260 * @param bool $twoFactorAuthentication
262 public function setTwoFactorAuthentication($twoFactorAuthentication)
264 $this->twoFactorAuthentication
= $twoFactorAuthentication;
267 public function isEmailAuthEnabled()
269 return $this->twoFactorAuthentication
;
272 public function getEmailAuthCode()
274 return $this->authCode
;
277 public function setEmailAuthCode($authCode)
279 $this->authCode
= $authCode;
282 public function addTrustedComputer($token, \DateTime
$validUntil)
284 $this->trusted
[$token] = $validUntil->format('r');
287 public function isTrustedComputer($token)
289 if (isset($this->trusted
[$token])) {
290 $now = new \
DateTime();
291 $validUntil = new \
DateTime($this->trusted
[$token]);
293 return $now < $validUntil;
300 * @param Client $client
304 public function addClient(Client
$client)
306 $this->clients
[] = $client;
312 * @return ArrayCollection<Entry>
314 public function getClients()
316 return $this->clients
;
320 * 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).
324 public function getFirstClient()
326 if (!empty($this->clients
)) {
327 return $this->clients
->first();
338 public function setDn($dn)
350 public function getDn()
355 public function isLdapUser()
357 return $this->dn
!== null;