|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the login-cidadao project or it's bundles. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Guilherme Donato <guilhermednt on github> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace LoginCidadao\CoreBundle\Entity; |
|
12
|
|
|
|
|
13
|
|
|
use Doctrine\ORM\Mapping as ORM; |
|
14
|
|
|
use LoginCidadao\CoreBundle\Model\PersonInterface; |
|
15
|
|
|
use LoginCidadao\OAuthBundle\Entity\Client; |
|
16
|
|
|
use LoginCidadao\OAuthBundle\Model\ClientInterface; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @ORM\Entity(repositoryClass="LoginCidadao\CoreBundle\Entity\AuthorizationRepository") |
|
20
|
|
|
* @ORM\HasLifecycleCallbacks |
|
21
|
|
|
* @ORM\Table(name="auth",uniqueConstraints={@ORM\UniqueConstraint(name="unique_person_client", columns={"person_id", "client_id"})}) |
|
22
|
|
|
*/ |
|
23
|
|
|
class Authorization |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @ORM\Id |
|
27
|
|
|
* @ORM\Column(type="integer") |
|
28
|
|
|
* @ORM\GeneratedValue(strategy="AUTO") |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $id; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @ORM\Column(type="array") |
|
34
|
|
|
* @var array |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $scope; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @ORM\ManyToOne(targetEntity="Person", inversedBy="authorizations") |
|
40
|
|
|
* @ORM\JoinColumn(name="person_id", referencedColumnName="id", nullable=false) |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $person; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @ORM\ManyToOne(targetEntity="LoginCidadao\OAuthBundle\Entity\Client", inversedBy="authorizations") |
|
46
|
|
|
* @ORM\JoinColumn(name="client_id", referencedColumnName="id", nullable=false) |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $client; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @ORM\Column(name="created_at", type="datetime") |
|
52
|
|
|
* @var \DateTime |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $createdAt; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Authorization constructor. |
|
58
|
|
|
*/ |
|
59
|
15 |
|
public function __construct() |
|
60
|
|
|
{ |
|
61
|
15 |
|
$this->scope = []; |
|
62
|
15 |
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @return mixed |
|
66
|
|
|
*/ |
|
67
|
1 |
|
public function getId() |
|
68
|
|
|
{ |
|
69
|
1 |
|
return $this->id; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @return PersonInterface |
|
74
|
|
|
*/ |
|
75
|
4 |
|
public function getPerson() |
|
76
|
|
|
{ |
|
77
|
4 |
|
return $this->person; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param PersonInterface|null $person |
|
82
|
|
|
* @return Authorization |
|
83
|
|
|
*/ |
|
84
|
9 |
|
public function setPerson(PersonInterface $person = null) |
|
85
|
|
|
{ |
|
86
|
9 |
|
$this->person = $person; |
|
87
|
|
|
|
|
88
|
9 |
|
return $this; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @return Client |
|
93
|
|
|
*/ |
|
94
|
4 |
|
public function getClient() |
|
95
|
|
|
{ |
|
96
|
4 |
|
return $this->client; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param ClientInterface|null $client |
|
101
|
|
|
* @return Authorization |
|
102
|
|
|
*/ |
|
103
|
9 |
|
public function setClient(ClientInterface $client = null) |
|
104
|
|
|
{ |
|
105
|
9 |
|
$this->client = $client; |
|
106
|
|
|
|
|
107
|
9 |
|
return $this; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @return array |
|
112
|
|
|
*/ |
|
113
|
10 |
|
public function getScope() |
|
114
|
|
|
{ |
|
115
|
10 |
|
$scope = $this->enforcePublicProfileScope(array_filter($this->scope)); |
|
116
|
|
|
|
|
117
|
10 |
|
return array_unique($scope); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @param array|string $scope |
|
122
|
|
|
* @return Authorization |
|
123
|
|
|
*/ |
|
124
|
11 |
|
public function setScope($scope) |
|
125
|
|
|
{ |
|
126
|
11 |
|
$scope = $this->enforcePublicProfileScope(Authorization::enforceArray($scope)); |
|
127
|
11 |
|
$this->scope = $scope; |
|
128
|
|
|
|
|
129
|
11 |
|
return $this; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @param mixed $needed |
|
134
|
|
|
* @return boolean |
|
135
|
|
|
*/ |
|
136
|
2 |
|
public function hasScopes($needed) |
|
137
|
|
|
{ |
|
138
|
2 |
|
foreach (Authorization::enforceArray($needed) as $n) { |
|
139
|
2 |
|
if (array_search($n, $this->getScope()) === false) { |
|
140
|
2 |
|
return false; |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
2 |
|
return true; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
12 |
|
protected function enforcePublicProfileScope($scope) |
|
148
|
|
|
{ |
|
149
|
12 |
|
if (array_search('public_profile', $scope) === false) { |
|
150
|
12 |
|
$scope[] = 'public_profile'; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
12 |
|
return $scope; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
1 |
|
public function setCreatedAt(\DateTime $createdAt) |
|
157
|
|
|
{ |
|
158
|
1 |
|
$this->createdAt = $createdAt; |
|
159
|
|
|
|
|
160
|
1 |
|
return $this; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
1 |
|
public function getCreatedAt() |
|
164
|
|
|
{ |
|
165
|
1 |
|
return $this->createdAt; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @ORM\PrePersist |
|
170
|
|
|
*/ |
|
171
|
1 |
|
public function setCreatedAtValue() |
|
172
|
|
|
{ |
|
173
|
1 |
|
if (!($this->getCreatedAt() instanceof \DateTime)) { |
|
|
|
|
|
|
174
|
1 |
|
$this->createdAt = new \DateTime(); |
|
175
|
|
|
} |
|
176
|
1 |
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Enforces that a scope is an array |
|
180
|
|
|
* |
|
181
|
|
|
* @param $scope |
|
182
|
|
|
* @return array |
|
183
|
|
|
*/ |
|
184
|
12 |
|
public static function enforceArray($scope) |
|
185
|
|
|
{ |
|
186
|
12 |
|
if (is_array($scope)) { |
|
187
|
5 |
|
return $scope; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
8 |
|
if (is_bool($scope) || is_null($scope) || $scope === '') { |
|
191
|
3 |
|
return []; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
7 |
|
return explode(' ', $scope); |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
|