|
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
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @ORM\Entity(repositoryClass="LoginCidadao\CoreBundle\Entity\AuthorizationRepository") |
|
19
|
|
|
* @ORM\HasLifecycleCallbacks |
|
20
|
|
|
* @ORM\Table(name="auth",uniqueConstraints={@ORM\UniqueConstraint(name="unique_person_client", columns={"person_id", "client_id"})}) |
|
21
|
|
|
*/ |
|
22
|
|
|
class Authorization |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @ORM\Id |
|
26
|
|
|
* @ORM\Column(type="integer") |
|
27
|
|
|
* @ORM\GeneratedValue(strategy="AUTO") |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $id; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @ORM\Column(type="array") |
|
33
|
|
|
* @var array |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $scope; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @ORM\ManyToOne(targetEntity="Person", inversedBy="authorizations") |
|
39
|
|
|
* @ORM\JoinColumn(name="person_id", referencedColumnName="id", nullable=false) |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $person; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @ORM\ManyToOne(targetEntity="LoginCidadao\OAuthBundle\Entity\Client", inversedBy="authorizations") |
|
45
|
|
|
* @ORM\JoinColumn(name="client_id", referencedColumnName="id", nullable=false) |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $client; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @ORM\Column(name="created_at", type="datetime") |
|
51
|
|
|
* @var \DateTime |
|
52
|
|
|
*/ |
|
53
|
|
|
protected $createdAt; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @return mixed |
|
57
|
|
|
*/ |
|
58
|
|
|
public function getId() |
|
59
|
|
|
{ |
|
60
|
|
|
return $this->id; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @return PersonInterface |
|
65
|
|
|
*/ |
|
66
|
2 |
|
public function getPerson() |
|
67
|
|
|
{ |
|
68
|
2 |
|
return $this->person; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param PersonInterface|null $person |
|
73
|
|
|
*/ |
|
74
|
4 |
|
public function setPerson(PersonInterface $person = null) |
|
75
|
|
|
{ |
|
76
|
4 |
|
$this->person = $person; |
|
77
|
4 |
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @return Client |
|
81
|
|
|
*/ |
|
82
|
2 |
|
public function getClient() |
|
83
|
|
|
{ |
|
84
|
2 |
|
return $this->client; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param \LoginCidadao\OAuthBundle\Entity\Client $client |
|
89
|
|
|
*/ |
|
90
|
4 |
|
public function setClient(Client $client = null) |
|
91
|
|
|
{ |
|
92
|
4 |
|
$this->client = $client; |
|
93
|
4 |
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @return array |
|
97
|
|
|
*/ |
|
98
|
1 |
|
public function getScope() |
|
99
|
|
|
{ |
|
100
|
1 |
|
$scope = $this->enforcePublicProfileScope(array_filter($this->scope)); |
|
101
|
|
|
|
|
102
|
1 |
|
return array_unique($scope); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param array $scope |
|
107
|
|
|
*/ |
|
108
|
2 |
|
public function setScope(array $scope) |
|
109
|
|
|
{ |
|
110
|
2 |
|
$scope = $this->enforcePublicProfileScope($scope); |
|
111
|
2 |
|
$this->scope = $scope; |
|
112
|
2 |
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @param mixed $needed |
|
116
|
|
|
* @return boolean |
|
117
|
|
|
*/ |
|
118
|
|
|
public function hasScopes($needed) |
|
119
|
|
|
{ |
|
120
|
|
|
if (!is_array($needed)) { |
|
121
|
|
|
$needed = array($needed); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
foreach ($needed as $n) { |
|
125
|
|
|
if (array_search($n, $this->getScope()) === false) { |
|
126
|
|
|
return false; |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
return true; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
2 |
|
protected function enforcePublicProfileScope($scope) |
|
134
|
|
|
{ |
|
135
|
2 |
|
if (array_search('public_profile', $scope) === false) { |
|
136
|
2 |
|
$scope[] = 'public_profile'; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
2 |
|
return $scope; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
public function setCreatedAt(\DateTime $createdAt) |
|
143
|
|
|
{ |
|
144
|
|
|
$this->createdAt = $createdAt; |
|
145
|
|
|
|
|
146
|
|
|
return $this; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
public function getCreatedAt() |
|
150
|
|
|
{ |
|
151
|
|
|
return $this->createdAt; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* @ORM\PrePersist |
|
156
|
|
|
*/ |
|
157
|
|
|
public function setCreatedAtValue() |
|
158
|
|
|
{ |
|
159
|
|
|
if (!($this->getCreatedAt() instanceof \DateTime)) { |
|
|
|
|
|
|
160
|
|
|
$this->createdAt = new \DateTime(); |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|