|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the TheAlternativeZurich/events project. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Florian Moser <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace App\Entity; |
|
13
|
|
|
|
|
14
|
|
|
use App\Entity\Base\BaseEntity; |
|
15
|
|
|
use App\Entity\Traits\ContactInformationTrait; |
|
16
|
|
|
use App\Entity\Traits\IdTrait; |
|
17
|
|
|
use App\Entity\Traits\TimeTrait; |
|
18
|
|
|
use App\Helper\HashHelper; |
|
19
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
20
|
|
|
use Doctrine\ORM\Mapping as ORM; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @ORM\Entity() |
|
24
|
|
|
* @ORM\HasLifecycleCallbacks |
|
25
|
|
|
*/ |
|
26
|
|
|
class User extends BaseEntity |
|
27
|
|
|
{ |
|
28
|
|
|
use IdTrait; |
|
29
|
|
|
use TimeTrait; |
|
30
|
|
|
use ContactInformationTrait; |
|
31
|
|
|
|
|
32
|
|
|
// can use any features & impersonate users |
|
33
|
|
|
const ROLE_ADMIN = 'ROLE_ADMIN'; |
|
34
|
|
|
|
|
35
|
|
|
// can use any features |
|
36
|
|
|
const ROLE_USER = 'ROLE_USER'; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var string|null |
|
40
|
|
|
* |
|
41
|
|
|
* @ORM\Column(type="text", nullable=true) |
|
42
|
|
|
*/ |
|
43
|
|
|
private $authenticationHash; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var bool |
|
47
|
|
|
* |
|
48
|
|
|
* @ORM\Column(type="boolean", options={"default": false}) |
|
49
|
|
|
*/ |
|
50
|
|
|
private $isAdminAccount = false; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var bool |
|
54
|
|
|
* |
|
55
|
|
|
* @ORM\Column(type="boolean", options={"default": false}) |
|
56
|
|
|
*/ |
|
57
|
|
|
private $isEmailConfirmed = false; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @var Registration[]|ArrayCollection |
|
61
|
|
|
* |
|
62
|
|
|
* @ORM\OneToMany (targetEntity="App\Entity\Registration", mappedBy="user") |
|
63
|
|
|
*/ |
|
64
|
|
|
private $registrations; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* constructor. |
|
68
|
|
|
*/ |
|
69
|
|
|
public function __construct() |
|
70
|
|
|
{ |
|
71
|
|
|
$this->registrations = new ArrayCollection(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public static function createFromRegistration(Registration $registration) |
|
75
|
|
|
{ |
|
76
|
|
|
$user = new User(); |
|
77
|
|
|
$user->fromOtherContactInformation($registration); |
|
78
|
|
|
|
|
79
|
|
|
return $user; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function updateFromRegistration(Registration $registration) |
|
83
|
|
|
{ |
|
84
|
|
|
$this->fromOtherContactInformation($registration); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @return Registration[]|ArrayCollection |
|
89
|
|
|
*/ |
|
90
|
|
|
public function getRegistrations() |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->registrations; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function generateAuthenticationHash() |
|
96
|
|
|
{ |
|
97
|
|
|
$this->authenticationHash = HashHelper::getHash(); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Returns the roles granted to the user. |
|
102
|
|
|
* |
|
103
|
|
|
* <code> |
|
104
|
|
|
* public function getRoles() |
|
105
|
|
|
* { |
|
106
|
|
|
* return array('ROLE_USER'); |
|
107
|
|
|
* } |
|
108
|
|
|
* </code> |
|
109
|
|
|
* |
|
110
|
|
|
* Alternatively, the roles might be stored on a ``roles`` property, |
|
111
|
|
|
* and populated in any number of different ways when the user object |
|
112
|
|
|
* is created. |
|
113
|
|
|
* |
|
114
|
|
|
* @return string[] The user roles |
|
115
|
|
|
*/ |
|
116
|
|
|
public function getRoles() |
|
117
|
|
|
{ |
|
118
|
|
|
if ($this->isAdminAccount) { |
|
119
|
|
|
return [self::ROLE_ADMIN]; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
return [self::ROLE_USER]; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
public function getAuthenticationHash(): ?string |
|
126
|
|
|
{ |
|
127
|
|
|
return $this->authenticationHash; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
public function getRegistrationFor(Event $event): ?Registration |
|
131
|
|
|
{ |
|
132
|
|
|
foreach ($this->getRegistrations() as $registration) { |
|
133
|
|
|
if ($registration->getEvent() === $event) { |
|
134
|
|
|
return $registration; |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
return null; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
public function getIsEmailConfirmed(): bool |
|
142
|
|
|
{ |
|
143
|
|
|
return $this->isEmailConfirmed; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
public function setIsEmailConfirmed(bool $isEmailConfirmed): void |
|
147
|
|
|
{ |
|
148
|
|
|
$this->isEmailConfirmed = $isEmailConfirmed; |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|