33import access .exception .InvalidInputException ;
44import access .exception .UserRestrictionException ;
55import access .manage .*;
6- import access .model .*;
6+ import access .model .EntityType ;
7+ import access .model .Environment ;
8+ import access .model .Institution ;
9+ import access .model .User ;
710import access .security .InstitutionAdmin ;
811import com .fasterxml .jackson .core .type .TypeReference ;
912import com .fasterxml .jackson .databind .ObjectMapper ;
3134
3235@ RestController
3336@ RequestMapping (value = {"/api/v1/manage" }, produces = MediaType .APPLICATION_JSON_VALUE )
34- public class ManageController implements UserAccessRights {
37+ public class ManageController implements UserAccessRights , PolicyAccessRights {
3538
3639 private static final Log LOG = LogFactory .getLog (ManageController .class );
3740
3841 private final MetaDataFeedParser metaDataFeedParser = new MetaDataFeedParser ();
3942 private final Manage manage ;
43+ private final ObjectMapper objectMapper ;
4044 private final Map <String , Object > arpInfo ;
4145 private final List <Map <String , Object >> privacyInfo ;
4246
4347 @ SneakyThrows
4448 public ManageController (Manage manage ,
4549 ObjectMapper objectMapper ) {
4650 this .manage = manage ;
51+ this .objectMapper = objectMapper ;
4752 this .arpInfo = objectMapper .readValue (new ClassPathResource ("/metadata/ARP.json" ).getInputStream (), new TypeReference <>() {
4853 });
4954 this .privacyInfo = objectMapper .readValue (new ClassPathResource ("/metadata/Privacy.json" ).getInputStream (), new TypeReference <>() {
@@ -97,24 +102,33 @@ public ResponseEntity<List<Map<String, Object>>> policies(User user,
97102 @ RequestParam ("entityId" ) String entityId ) {
98103 confirmInstitutionAdmin (user );
99104 //we need to ensure the application is connected to the IdP of the user - realtime
100- OidcUser oidcUser = (OidcUser ) authentication .getPrincipal ();
101- Map <String , Object > claims = oidcUser .getUserInfo ().getClaims ();
102- Institution institution = (Institution ) claims .get (InstitutionAdmin .INSTITUTION );
103- //We can't use any cache as this method is called right after automatic connection allowed
104- Map <String , Object > identityProvider = manage .providerById (EntityType .saml20_idp , institution .getManageIdentifier (), Environment .PROD );
105- Map <String , Object > data = getData (identityProvider );
106- boolean noneMatch = ((List <Map <String , String >>) data .getOrDefault ("allowedEntities" , List .of ()))
107- .stream ()
108- .noneMatch (allowedEntity -> allowedEntity .get ("name" ).equals (entityId ));
105+ if (!user .isSuperUser ()) {
106+ Map <String , Object > data = getIdentityProvider (authentication );
107+ boolean noneMatch = ((List <Map <String , String >>) data .getOrDefault ("allowedEntities" , List .of ()))
108+ .stream ()
109+ .noneMatch (allowedEntity -> allowedEntity .get ("name" ).equals (entityId ));
109110
110- if (noneMatch ) {
111- throw new UserRestrictionException (String .format ("User %s is not allowed to request policies for %s" ,
112- user .getEmail (), entityId ));
111+ if (noneMatch ) {
112+ throw new UserRestrictionException (String .format ("User %s is not allowed to request policies for %s" ,
113+ user .getEmail (), entityId ));
114+ }
113115 }
114- List <Map <String , Object >> policies = this .manage .policiesByServiceProvider (institution .getEntityID (), entityId );
116+ List <Map <String , Object >> policies = this .manage
117+ .policiesByServiceProvider (user .getAuthenticatingAuthority (), entityId );
115118 return ResponseEntity .ok (policies );
116119 }
117120
121+ @ SneakyThrows
122+ @ PostMapping ("/policies" )
123+ public ResponseEntity <Map <String , Object >> createPolicy (User user ,
124+ @ RequestBody Map <String , Object > policy ) {
125+ confirmInstitutionAdmin (user );
126+ //We don't want to use PolicyDefinition as @RequestBody, because the template from Manage is leading
127+ PolicyDefinition policyDefinition = this .objectMapper .convertValue (policy , PolicyDefinition .class );
128+ confirmPolicyAccess (user , policyDefinition , manage );
129+ return ResponseEntity .ok (policy );
130+ }
131+
118132 @ SneakyThrows
119133 @ PostMapping ("/unique-entity-id/{environment}" )
120134 public ResponseEntity <List <Map <String , Object >>> providersByEntityId (@ PathVariable ("environment" ) Environment environment ,
@@ -131,4 +145,16 @@ public ResponseEntity<Map<String, Object>> rejectChangeRequest(@RequestBody Chan
131145 manage .rejectChangeRequest (Environment .PROD , changeRequest );
132146 return Results .okResult ();
133147 }
148+
149+ private Map <String , Object > getIdentityProvider (Authentication authentication ) {
150+ OidcUser oidcUser = (OidcUser ) authentication .getPrincipal ();
151+ Map <String , Object > claims = oidcUser .getUserInfo ().getClaims ();
152+ Institution institution = (Institution ) claims .get (InstitutionAdmin .INSTITUTION );
153+ //We can't use any cache as this method is called right after automatic connection allowed
154+ Map <String , Object > identityProvider = manage .providerById (EntityType .saml20_idp , institution .getManageIdentifier (), Environment .PROD );
155+ Map <String , Object > data = getData (identityProvider );
156+ return data ;
157+ }
158+
159+
134160}
0 commit comments