1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OAuth\Repository; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityRepository; |
6
|
|
|
use Exception; |
7
|
|
|
use League\OAuth2\Server\Entities\ClientEntityInterface; |
8
|
|
|
use League\OAuth2\Server\Entities\ScopeEntityInterface; |
9
|
|
|
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface; |
10
|
|
|
use OAuth\Client; |
11
|
|
|
use OAuth\Scope; |
12
|
|
|
|
13
|
|
|
class ScopeRepository extends EntityRepository implements ScopeRepositoryInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @param string $identifier |
17
|
|
|
* @return null|Scope |
18
|
|
|
*/ |
19
|
|
|
public function getScopeEntityByIdentifier($identifier) |
20
|
|
|
{ |
21
|
|
|
/** @var Scope $scope */ |
22
|
|
|
$scope = $this->findOneBy(['identifier' => $identifier]); |
23
|
|
|
return $scope; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param array $scopes |
28
|
|
|
* @param string $grantType |
29
|
|
|
* @param ClientEntityInterface $clientEntity |
30
|
|
|
* @param null|string $userIdentifier |
31
|
|
|
* @return ScopeEntityInterface[] |
32
|
|
|
* @throws Exception |
33
|
|
|
*/ |
34
|
|
|
public function finalizeScopes(array $scopes, $grantType, ClientEntityInterface $clientEntity, $userIdentifier = null) |
35
|
|
|
{ |
36
|
|
|
/** @var Client $clientEntity */ |
37
|
|
|
$clientScopes = $clientEntity->getScopes()->getValues(); |
38
|
|
|
$finalScopes = array_uintersect($scopes, $clientScopes, function($a, $b) { |
39
|
|
|
return strcmp(spl_object_hash($a), spl_object_hash($b)); |
40
|
|
|
}); |
41
|
|
|
|
42
|
|
|
if (count($finalScopes) < count($scopes)) { |
43
|
|
|
throw new Exception('Scopes not authorised.', 403); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return $finalScopes; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param Scope $scope |
51
|
|
|
* @return Scope |
52
|
|
|
* @throws \Doctrine\ORM\OptimisticLockException |
53
|
|
|
*/ |
54
|
|
|
public function create(Scope $scope) |
55
|
|
|
{ |
56
|
|
|
$this->_em->persist($scope); |
57
|
|
|
$this->_em->flush($scope); |
58
|
|
|
return $scope; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param Scope $scope |
63
|
|
|
* @return Scope |
64
|
|
|
* @throws \Doctrine\ORM\OptimisticLockException |
65
|
|
|
*/ |
66
|
|
|
public function save(Scope $scope) |
67
|
|
|
{ |
68
|
|
|
$this->_em->flush($scope); |
69
|
|
|
return $scope; |
70
|
|
|
} |
71
|
|
|
} |