18
18
19
19
namespace Surfnet \StepupMiddleware \ApiBundle \Identity \Service ;
20
20
21
+ use Psr \Log \LoggerInterface ;
21
22
use Surfnet \Stepup \Configuration \Value \InstitutionRole ;
22
23
use Surfnet \Stepup \Identity \Value \IdentityId ;
23
24
use Surfnet \StepupMiddleware \ApiBundle \Authorization \Service \AuthorizationContextService ;
24
- use Surfnet \StepupMiddleware \ApiBundle \Identity \Repository \ InstitutionListingRepository ;
25
+ use Surfnet \StepupMiddleware \ApiBundle \Identity \Entity \ RaListing ;
25
26
use Surfnet \StepupMiddleware \ApiBundle \Identity \Repository \RaListingRepository ;
27
+ use Surfnet \StepupMiddleware \ApiBundle \Identity \Value \AuthorityRole ;
26
28
use Surfnet \StepupMiddleware \ApiBundle \Identity \Value \AuthorizedInstitutionCollection ;
27
29
use Surfnet \StepupMiddleware \ApiBundle \Identity \Value \Profile ;
30
+ use function sprintf ;
28
31
29
32
class ProfileService extends AbstractSearchService
30
33
{
@@ -43,14 +46,21 @@ class ProfileService extends AbstractSearchService
43
46
*/
44
47
private $ authorizationService ;
45
48
49
+ /**
50
+ * @var LoggerInterface
51
+ */
52
+ private $ logger ;
53
+
46
54
public function __construct (
47
55
RaListingRepository $ raListingRepository ,
48
56
IdentityService $ identityService ,
49
- AuthorizationContextService $ institutionAuthorizationService
57
+ AuthorizationContextService $ institutionAuthorizationService ,
58
+ LoggerInterface $ logger
50
59
) {
51
60
$ this ->raListingRepository = $ raListingRepository ;
52
61
$ this ->identityService = $ identityService ;
53
62
$ this ->authorizationService = $ institutionAuthorizationService ;
63
+ $ this ->logger = $ logger ;
54
64
}
55
65
56
66
/**
@@ -72,29 +82,73 @@ public function __construct(
72
82
public function createProfile ($ identityId )
73
83
{
74
84
$ identity = $ this ->identityService ->find ($ identityId );
85
+
75
86
if ($ identity === null ) {
87
+ $ this ->logger ->notice (sprintf ('No Identity found with IdentityId %s ' , $ identityId ));
76
88
return null ;
77
89
}
90
+ $ this ->logger ->notice (sprintf ('Found IdentityId "%s" NameId "%s" ' , $ identityId , $ identity ->nameId ));
78
91
79
- $ authorizationContextRa = $ this ->authorizationService ->buildInstitutionAuthorizationContext (
80
- new IdentityId ($ identityId ),
81
- InstitutionRole::useRa ()
92
+ $ raListing = $ this ->raListingRepository ->findByIdentityId (new IdentityId ($ identityId ));
93
+ $ isRa = $ this ->getRoleFromListing ($ raListing , AuthorityRole::ROLE_RA );
94
+ $ isRaa = $ this ->getRoleFromListing ($ raListing , AuthorityRole::ROLE_RAA );
95
+
96
+ $ this ->logger ->notice (
97
+ sprintf (
98
+ 'Based on RaListing Identity %s has roles(RA: %s, RAA: %s) ' ,
99
+ $ identityId ,
100
+ $ isRa ? "YES " : "NO " ,
101
+ $ isRaa ? "YES " : "NO "
102
+ )
82
103
);
83
104
84
- $ authorizationContextRaa = $ this ->authorizationService ->buildInstitutionAuthorizationContext (
105
+
106
+ if ($ raListing === null ) {
107
+ $ this ->logger ->notice (sprintf ('No RA listing found for IdentityId %s ' , $ identityId ));
108
+ return null ;
109
+ }
110
+
111
+ $ authorizationContextRa = $ this ->authorizationService ->buildInstitutionAuthorizationContext (
85
112
new IdentityId ($ identityId ),
86
- InstitutionRole::useRaa ()
113
+ InstitutionRole::useRa ()
87
114
);
88
-
89
115
$ authorizations = AuthorizedInstitutionCollection::from (
90
- $ authorizationContextRa ->getInstitutions (),
91
- $ authorizationContextRaa ->getInstitutions ()
116
+ $ authorizationContextRa ->getInstitutions ()
92
117
);
93
118
119
+ $ this ->logger ->notice (sprintf ('IdentityId "%s" is RA for: %s ' , $ identityId , json_encode ($ authorizationContextRa ->getInstitutions ()->jsonSerialize ())));
120
+
121
+ if ($ isRaa ) {
122
+ $ authorizationContextRaa = $ this ->authorizationService ->buildInstitutionAuthorizationContext (
123
+ new IdentityId ($ identityId ),
124
+ InstitutionRole::useRaa ()
125
+ );
126
+
127
+ $ this ->logger ->notice (sprintf ('IdentityId "%s" is RAA for: %s ' , $ identityId , json_encode ($ authorizationContextRaa ->getInstitutions ()->jsonSerialize ())));
128
+
129
+ $ authorizations = AuthorizedInstitutionCollection::from (
130
+ $ authorizationContextRa ->getInstitutions (),
131
+ $ authorizationContextRaa ->getInstitutions ()
132
+ );
133
+ }
134
+
94
135
return new Profile (
95
136
$ identity ,
96
137
$ authorizations ,
97
138
$ authorizationContextRa ->isActorSraa ()
98
139
);
99
140
}
141
+
142
+ /**
143
+ * @param array<int, RaListing> $raListing
144
+ */
145
+ private function getRoleFromListing (array $ raListing , string $ role ): bool
146
+ {
147
+ foreach ($ raListing as $ listing ) {
148
+ if ($ listing ->role ->getRole () === $ role ) {
149
+ return true ;
150
+ }
151
+ }
152
+ return false ;
153
+ }
100
154
}
0 commit comments