]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Entity/User.php
implement FosUser
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Entity / User.php
1 <?php
2
3 namespace Wallabag\CoreBundle\Entity;
4
5 use Doctrine\Common\Collections\ArrayCollection;
6 use Doctrine\ORM\Mapping as ORM;
7 use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
8 use Symfony\Component\Security\Core\User\UserInterface;
9 use Symfony\Component\Security\Core\User\AdvancedUserInterface;
10 use Symfony\Component\Validator\Constraints as Assert;
11 use JMS\Serializer\Annotation\ExclusionPolicy;
12 use JMS\Serializer\Annotation\Expose;
13 use FOS\UserBundle\Model\User as BaseUser;
14
15 /**
16 * User.
17 *
18 * @ORM\Entity(repositoryClass="Wallabag\CoreBundle\Repository\UserRepository")
19 * @ORM\Table
20 * @ORM\HasLifecycleCallbacks()
21 * @ExclusionPolicy("all")
22 *
23 * @UniqueEntity("email")
24 * @UniqueEntity("username")
25 */
26 class User extends BaseUser implements AdvancedUserInterface, \Serializable
27 {
28 /**
29 * @var int
30 *
31 * @Expose
32 * @ORM\Column(name="id", type="integer")
33 * @ORM\Id
34 * @ORM\GeneratedValue(strategy="AUTO")
35 */
36 protected $id;
37
38 /**
39 * @var string
40 *
41 * @ORM\Column(name="name", type="text", nullable=true)
42 */
43 protected $name;
44
45 /**
46 * @var date
47 *
48 * @ORM\Column(name="created_at", type="datetime")
49 */
50 protected $createdAt;
51
52 /**
53 * @var date
54 *
55 * @ORM\Column(name="updated_at", type="datetime")
56 */
57 protected $updatedAt;
58
59 /**
60 * @ORM\OneToMany(targetEntity="Entry", mappedBy="user", cascade={"remove"})
61 */
62 protected $entries;
63
64 /**
65 * @ORM\OneToOne(targetEntity="Config", mappedBy="user")
66 */
67 protected $config;
68
69 /**
70 * @ORM\OneToMany(targetEntity="Tag", mappedBy="user", cascade={"remove"})
71 */
72 protected $tags;
73
74 public function __construct()
75 {
76 parent::__construct();
77 $this->entries = new ArrayCollection();
78 $this->tags = new ArrayCollection();
79 }
80
81 /**
82 * @ORM\PrePersist
83 * @ORM\PreUpdate
84 */
85 public function timestamps()
86 {
87 if (is_null($this->createdAt)) {
88 $this->createdAt = new \DateTime();
89 }
90
91 $this->updatedAt = new \DateTime();
92 }
93
94 /**
95 * Set password.
96 *
97 * @param string $password
98 *
99 * @return User
100 */
101 public function setPassword($password)
102 {
103 if (!$password && 0 === strlen($password)) {
104 return;
105 }
106
107 $this->password = sha1($password.$this->getUsername().$this->getSalt());
108
109 return $this;
110 }
111
112 /**
113 * Set name.
114 *
115 * @param string $name
116 *
117 * @return User
118 */
119 public function setName($name)
120 {
121 $this->name = $name;
122
123 return $this;
124 }
125
126 /**
127 * Get name.
128 *
129 * @return string
130 */
131 public function getName()
132 {
133 return $this->name;
134 }
135
136 /**
137 * @return string
138 */
139 public function getCreatedAt()
140 {
141 return $this->createdAt;
142 }
143
144 /**
145 * @return string
146 */
147 public function getUpdatedAt()
148 {
149 return $this->updatedAt;
150 }
151
152 /**
153 * @param Entry $entry
154 *
155 * @return User
156 */
157 public function addEntry(Entry $entry)
158 {
159 $this->entries[] = $entry;
160
161 return $this;
162 }
163
164 /**
165 * @return ArrayCollection<Entry>
166 */
167 public function getEntries()
168 {
169 return $this->entries;
170 }
171
172 /**
173 * @param Entry $entry
174 *
175 * @return User
176 */
177 public function addTag(Tag $tag)
178 {
179 $this->tags[] = $tag;
180
181 return $this;
182 }
183
184 /**
185 * @return ArrayCollection<Tag>
186 */
187 public function getTags()
188 {
189 return $this->tags;
190 }
191
192 public function isEqualTo(UserInterface $user)
193 {
194 return $this->username === $user->getUsername();
195 }
196
197 /**
198 * Set config.
199 *
200 * @param \Wallabag\CoreBundle\Entity\Config $config
201 *
202 * @return User
203 */
204 public function setConfig(\Wallabag\CoreBundle\Entity\Config $config = null)
205 {
206 $this->config = $config;
207
208 return $this;
209 }
210
211 /**
212 * Get config.
213 *
214 * @return \Wallabag\CoreBundle\Entity\Config
215 */
216 public function getConfig()
217 {
218 return $this->config;
219 }
220 }