|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright 2015 SURFnet B.V. |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
7
|
|
|
* you may not use this file except in compliance with the License. |
|
8
|
|
|
* You may obtain a copy of the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15
|
|
|
* See the License for the specific language governing permissions and |
|
16
|
|
|
* limitations under the License. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace OpenConext\ProfileBundle\Service; |
|
20
|
|
|
|
|
21
|
|
|
use OpenConext\Profile\Value\Consent; |
|
22
|
|
|
use OpenConext\Profile\Value\ConsentList; |
|
23
|
|
|
use OpenConext\ProfileBundle\Exception\RuntimeException; |
|
24
|
|
|
use OpenConext\ProfileBundle\Security\Authentication\Entity\User; |
|
25
|
|
|
use OpenConext\ProfileBundle\User\UserProvider; |
|
26
|
|
|
use Surfnet\SamlBundle\SAML2\Attribute\Filter\AttributeFilter; |
|
27
|
|
|
|
|
28
|
|
|
class ConsentListingService |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @var UserProvider |
|
32
|
|
|
*/ |
|
33
|
|
|
private $userProvider; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var ConsentService |
|
37
|
|
|
*/ |
|
38
|
|
|
private $consentService; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var AttributeFilter |
|
42
|
|
|
*/ |
|
43
|
|
|
private $attributeFilter; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param UserProvider $userProvider |
|
47
|
|
|
* @param ConsentService $consentService |
|
48
|
|
|
* @param AttributeFilter $attributeFilter |
|
49
|
|
|
*/ |
|
50
|
|
|
public function __construct( |
|
51
|
|
|
UserProvider $userProvider, |
|
52
|
|
|
ConsentService $consentService, |
|
53
|
|
|
AttributeFilter $attributeFilter |
|
54
|
|
|
) { |
|
55
|
|
|
$this->userProvider = $userProvider; |
|
56
|
|
|
$this->consentService = $consentService; |
|
57
|
|
|
$this->attributeFilter = $attributeFilter; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @return ConsentList |
|
62
|
|
|
*/ |
|
63
|
|
|
public function getConsentListing() |
|
64
|
|
|
{ |
|
65
|
|
|
if (!$this->userProvider->hasCurrentUser()) { |
|
66
|
|
|
throw new RuntimeException('Cannot get consent listing: no current user available'); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$user = $this->userProvider->getCurrentUser(); |
|
70
|
|
|
$consentList = $this->consentService->findAllFor($user); |
|
71
|
|
|
$filteredAttributes = $user->getAttributes()->apply($this->attributeFilter); |
|
72
|
|
|
|
|
73
|
|
|
$newConsents = []; |
|
74
|
|
|
|
|
75
|
|
|
/** @var Consent $consent */ |
|
76
|
|
|
foreach ($consentList as $consent) { |
|
|
|
|
|
|
77
|
|
|
$newConsent = $consent->givenFor($filteredAttributes); |
|
78
|
|
|
$newConsents[] = $newConsent; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return new ConsentList($newConsents); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.