aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/UserBundle/Entity
diff options
context:
space:
mode:
Diffstat (limited to 'src/Wallabag/UserBundle/Entity')
-rw-r--r--src/Wallabag/UserBundle/Entity/User.php99
1 files changed, 71 insertions, 28 deletions
diff --git a/src/Wallabag/UserBundle/Entity/User.php b/src/Wallabag/UserBundle/Entity/User.php
index 3a167de7..48446e3c 100644
--- a/src/Wallabag/UserBundle/Entity/User.php
+++ b/src/Wallabag/UserBundle/Entity/User.php
@@ -4,37 +4,43 @@ namespace Wallabag\UserBundle\Entity;
4 4
5use Doctrine\Common\Collections\ArrayCollection; 5use Doctrine\Common\Collections\ArrayCollection;
6use Doctrine\ORM\Mapping as ORM; 6use Doctrine\ORM\Mapping as ORM;
7use FOS\UserBundle\Model\User as BaseUser;
8use JMS\Serializer\Annotation\Accessor;
9use JMS\Serializer\Annotation\Groups;
10use JMS\Serializer\Annotation\XmlRoot;
7use Scheb\TwoFactorBundle\Model\Email\TwoFactorInterface; 11use Scheb\TwoFactorBundle\Model\Email\TwoFactorInterface;
8use Scheb\TwoFactorBundle\Model\TrustedComputerInterface; 12use Scheb\TwoFactorBundle\Model\TrustedComputerInterface;
9use FOS\UserBundle\Model\User as BaseUser;
10use JMS\Serializer\Annotation\ExclusionPolicy;
11use JMS\Serializer\Annotation\Expose;
12use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; 13use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
13use Symfony\Component\Security\Core\User\UserInterface; 14use Symfony\Component\Security\Core\User\UserInterface;
14use Wallabag\ApiBundle\Entity\Client; 15use Wallabag\ApiBundle\Entity\Client;
15use Wallabag\CoreBundle\Entity\Config; 16use Wallabag\CoreBundle\Entity\Config;
16use Wallabag\CoreBundle\Entity\Entry; 17use Wallabag\CoreBundle\Entity\Entry;
18use Wallabag\CoreBundle\Helper\EntityTimestampsTrait;
17 19
18/** 20/**
19 * User. 21 * User.
20 * 22 *
23 * @XmlRoot("user")
21 * @ORM\Entity(repositoryClass="Wallabag\UserBundle\Repository\UserRepository") 24 * @ORM\Entity(repositoryClass="Wallabag\UserBundle\Repository\UserRepository")
22 * @ORM\Table(name="`user`") 25 * @ORM\Table(name="`user`")
23 * @ORM\HasLifecycleCallbacks() 26 * @ORM\HasLifecycleCallbacks()
24 * @ExclusionPolicy("all")
25 * 27 *
26 * @UniqueEntity("email") 28 * @UniqueEntity("email")
27 * @UniqueEntity("username") 29 * @UniqueEntity("username")
28 */ 30 */
29class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterface 31class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterface
30{ 32{
33 use EntityTimestampsTrait;
34
35 /** @Serializer\XmlAttribute */
31 /** 36 /**
32 * @var int 37 * @var int
33 * 38 *
34 * @Expose
35 * @ORM\Column(name="id", type="integer") 39 * @ORM\Column(name="id", type="integer")
36 * @ORM\Id 40 * @ORM\Id
37 * @ORM\GeneratedValue(strategy="AUTO") 41 * @ORM\GeneratedValue(strategy="AUTO")
42 *
43 * @Groups({"user_api", "user_api_with_client"})
38 */ 44 */
39 protected $id; 45 protected $id;
40 46
@@ -42,20 +48,40 @@ class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterf
42 * @var string 48 * @var string
43 * 49 *
44 * @ORM\Column(name="name", type="text", nullable=true) 50 * @ORM\Column(name="name", type="text", nullable=true)
51 *
52 * @Groups({"user_api", "user_api_with_client"})
45 */ 53 */
46 protected $name; 54 protected $name;
47 55
48 /** 56 /**
49 * @var date 57 * @var string
58 *
59 * @Groups({"user_api", "user_api_with_client"})
60 */
61 protected $username;
62
63 /**
64 * @var string
65 *
66 * @Groups({"user_api", "user_api_with_client"})
67 */
68 protected $email;
69
70 /**
71 * @var \DateTime
50 * 72 *
51 * @ORM\Column(name="created_at", type="datetime") 73 * @ORM\Column(name="created_at", type="datetime")
74 *
75 * @Groups({"user_api", "user_api_with_client"})
52 */ 76 */
53 protected $createdAt; 77 protected $createdAt;
54 78
55 /** 79 /**
56 * @var date 80 * @var \DateTime
57 * 81 *
58 * @ORM\Column(name="updated_at", type="datetime") 82 * @ORM\Column(name="updated_at", type="datetime")
83 *
84 * @Groups({"user_api", "user_api_with_client"})
59 */ 85 */
60 protected $updatedAt; 86 protected $updatedAt;
61 87
@@ -70,12 +96,35 @@ class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterf
70 protected $config; 96 protected $config;
71 97
72 /** 98 /**
99 * @var ArrayCollection
100 *
101 * @ORM\OneToMany(targetEntity="Wallabag\CoreBundle\Entity\SiteCredential", mappedBy="user", cascade={"remove"})
102 */
103 protected $siteCredentials;
104
105 /**
106 * @var ArrayCollection
107 *
108 * @ORM\OneToMany(targetEntity="Wallabag\ApiBundle\Entity\Client", mappedBy="user", cascade={"remove"})
109 */
110 protected $clients;
111
112 /**
113 * @see getFirstClient() below
114 *
115 * @Groups({"user_api_with_client"})
116 * @Accessor(getter="getFirstClient")
117 */
118 protected $default_client;
119
120 /**
73 * @ORM\Column(type="integer", nullable=true) 121 * @ORM\Column(type="integer", nullable=true)
74 */ 122 */
75 private $authCode; 123 private $authCode;
76 124
77 /** 125 /**
78 * @var bool Enabled yes/no 126 * @var bool
127 *
79 * @ORM\Column(type="boolean") 128 * @ORM\Column(type="boolean")
80 */ 129 */
81 private $twoFactorAuthentication = false; 130 private $twoFactorAuthentication = false;
@@ -85,11 +134,6 @@ class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterf
85 */ 134 */
86 private $trusted; 135 private $trusted;
87 136
88 /**
89 * @ORM\OneToMany(targetEntity="Wallabag\ApiBundle\Entity\Client", mappedBy="user", cascade={"remove"})
90 */
91 protected $clients;
92
93 public function __construct() 137 public function __construct()
94 { 138 {
95 parent::__construct(); 139 parent::__construct();
@@ -98,19 +142,6 @@ class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterf
98 } 142 }
99 143
100 /** 144 /**
101 * @ORM\PrePersist
102 * @ORM\PreUpdate
103 */
104 public function timestamps()
105 {
106 if (is_null($this->createdAt)) {
107 $this->createdAt = new \DateTime();
108 }
109
110 $this->updatedAt = new \DateTime();
111 }
112
113 /**
114 * Set name. 145 * Set name.
115 * 146 *
116 * @param string $name 147 * @param string $name
@@ -135,7 +166,7 @@ class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterf
135 } 166 }
136 167
137 /** 168 /**
138 * @return string 169 * @return \DateTime
139 */ 170 */
140 public function getCreatedAt() 171 public function getCreatedAt()
141 { 172 {
@@ -143,7 +174,7 @@ class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterf
143 } 174 }
144 175
145 /** 176 /**
146 * @return string 177 * @return \DateTime
147 */ 178 */
148 public function getUpdatedAt() 179 public function getUpdatedAt()
149 { 180 {
@@ -266,4 +297,16 @@ class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterf
266 { 297 {
267 return $this->clients; 298 return $this->clients;
268 } 299 }
300
301 /**
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).
303 *
304 * @return Client
305 */
306 public function getFirstClient()
307 {
308 if (!empty($this->clients)) {
309 return $this->clients->first();
310 }
311 }
269} 312}