Skip to content

Commit 3c5c2d6

Browse files
committed
Fix for fine-grained access control with resource authorizations
1 parent 1895eb0 commit 3c5c2d6

File tree

1 file changed

+35
-38
lines changed

1 file changed

+35
-38
lines changed

src/main/java/org/computate/frFR/java/EcrireApiClasse.java

Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1993,7 +1993,6 @@ public void ecrireGenApiServiceImpl2(String classeLangueNom) throws Exception {
19931993
tl(5, "HttpResponse<Buffer> authorizationDecision = authorizationDecisionResponse.result();");
19941994
tl(5, "JsonArray scopes = authorizationDecisionResponse.failed() ? new JsonArray() : authorizationDecision.bodyAsJsonArray().stream().findFirst().map(decision -> ((JsonObject)decision).getJsonArray(\"scopes\")).orElse(new JsonArray());");
19951995
if(StringUtils.equals(classeApiMethodeMethode, "GET")) {
1996-
tl(5, "{");
19971996
} else {
19981997
if(classeRoleUtilisateur) {
19991998
tl(5, "scopes.add(\"GET\");");
@@ -2011,39 +2010,41 @@ public void ecrireGenApiServiceImpl2(String classeLangueNom) throws Exception {
20112010
tl(9, "), MultiMap.caseInsensitiveMultiMap()");
20122011
tl(7, ")");
20132012
tl(6, "));");
2014-
tl(5, "} else {");
2013+
tl(5, "}");
20152014
}
20162015
if(classeRessourcesAutorisation.size() > 0) {
2017-
l();
2018-
tl(6, "if(!scopes.contains(\"", classeApiMethodeMethode, "\")) {");
2019-
tl(7, "//");
2020-
tl(7, "List<String> fqs = new ArrayList<>();");
2021-
tl(7, "List<String> groups = Optional.ofNullable(", i18nGlobale.getString(I18n.var_requeteSite), ".getGroups()).orElse(new ArrayList<>());");
2016+
tl(5, "if(!scopes.contains(\"", classeApiMethodeMethode, "\")) {");
2017+
tl(6, "//");
2018+
tl(6, "List<String> fqs = new ArrayList<>();");
2019+
tl(6, "List<String> groups = Optional.ofNullable(", i18nGlobale.getString(I18n.var_requeteSite), ".getGroups()).orElse(new ArrayList<>());");
20222020
for(String classeRessourceAutorisation : classeRessourcesAutorisation) {
20232021

2024-
tl(7, "groups.stream().map(group -> {");
2025-
tl(10, "Matcher mPermission = Pattern.compile(\"^/", StringUtils.substringBefore(classeRessourceAutorisation, "-"), "-(.*)-", classeApiMethodeMethode, "$\").matcher(group);");
2026-
tl(10, "return mPermission.find() ? mPermission.group(1) : null;");
2027-
tl(9, "}).filter(v -> v != null).forEach(", i18nGlobale.getString(I18n.var_valeur), " -> {");
2028-
tl(10, "fqs.add(String.format(\"%s:%s\", \"", StringUtils.substringAfter(classeRessourceAutorisation, "-"), "\", ", i18nGlobale.getString(I18n.var_valeur), "));");
2029-
tl(9, "});");
2022+
tl(6, "groups.stream().map(group -> {");
2023+
tl(9, "Matcher mPermission = Pattern.compile(\"^/", StringUtils.substringBefore(classeRessourceAutorisation, "-"), "-(.*)-", classeApiMethodeMethode, "$\").matcher(group);");
2024+
tl(9, "return mPermission.find() ? mPermission.group(1) : null;");
2025+
tl(8, "}).filter(v -> v != null).forEach(", i18nGlobale.getString(I18n.var_valeur), " -> {");
2026+
tl(9, "fqs.add(String.format(\"%s:%s\", \"", StringUtils.substringAfter(classeRessourceAutorisation, "-"), "\", ", i18nGlobale.getString(I18n.var_valeur), "));");
2027+
tl(8, "});");
20302028
}
2031-
tl(7, "JsonObject authParams = ", i18nGlobale.getString(I18n.var_requeteSite), ".get", i18nGlobale.getString(I18n.var_RequeteService), "().getParams();");
2032-
tl(7, "JsonObject authQuery = authParams.getJsonObject(\"query\");");
2033-
tl(7, "if(authQuery == null) {");
2034-
tl(8, "authQuery = new JsonObject();");
2035-
tl(8, "authParams.put(\"query\", authQuery);");
2036-
tl(7, "}");
2037-
tl(7, "JsonArray fq = authQuery.getJsonArray(\"fq\");");
2038-
tl(7, "if(fq == null) {");
2039-
tl(8, "fq = new JsonArray();");
2040-
tl(8, "authQuery.put(\"fq\", fq);");
2041-
tl(7, "}");
2042-
tl(7, "if(fqs.size() > 0) {");
2043-
tl(8, "fq.add(fqs.stream().collect(Collectors.joining(\" OR \")));");
2044-
tl(8, "scopes.add(\"", classeApiMethodeMethode, "\");");
2045-
tl(7, "}");
2029+
tl(6, "JsonObject authParams = ", i18nGlobale.getString(I18n.var_requeteSite), ".get", i18nGlobale.getString(I18n.var_RequeteService), "().getParams();");
2030+
tl(6, "JsonObject authQuery = authParams.getJsonObject(\"query\");");
2031+
tl(6, "if(authQuery == null) {");
2032+
tl(7, "authQuery = new JsonObject();");
2033+
tl(7, "authParams.put(\"query\", authQuery);");
2034+
tl(6, "}");
2035+
tl(6, "JsonArray fq = authQuery.getJsonArray(\"fq\");");
2036+
tl(6, "if(fq == null) {");
2037+
tl(7, "fq = new JsonArray();");
2038+
tl(7, "authQuery.put(\"fq\", fq);");
20462039
tl(6, "}");
2040+
tl(6, "if(fqs.size() > 0) {");
2041+
tl(7, "fq.add(fqs.stream().collect(Collectors.joining(\" OR \")));");
2042+
tl(7, "scopes.add(\"", classeApiMethodeMethode, "\");");
2043+
tl(6, "}");
2044+
tl(5, "}");
2045+
tl(5, "{");
2046+
} else {
2047+
tl(5, "} else {");
20472048
}
20482049
tl(6, i18nGlobale.getString(I18n.var_requeteSite), ".setScopes(scopes.stream().map(o -> o.toString()).collect(Collectors.toList()));");
20492050
tl(6, "List<String> scopes2 = ", i18nGlobale.getString(I18n.var_requeteSite), ".getScopes();");
@@ -2212,7 +2213,6 @@ public void ecrireGenApiServiceImpl2(String classeLangueNom) throws Exception {
22122213
tl(5, "HttpResponse<Buffer> authorizationDecision = authorizationDecisionResponse.result();");
22132214
tl(5, "JsonArray scopes = authorizationDecisionResponse.failed() ? new JsonArray() : authorizationDecision.bodyAsJsonArray().stream().findFirst().map(decision -> ((JsonObject)decision).getJsonArray(\"scopes\")).orElse(new JsonArray());");
22142215
if(StringUtils.equals(classeApiMethodeMethode, "GET")) {
2215-
tl(5, "{");
22162216
} else {
22172217
if(classeRoleUtilisateur) {
22182218
tl(5, "scopes.add(\"GET\");");
@@ -2230,10 +2230,10 @@ public void ecrireGenApiServiceImpl2(String classeLangueNom) throws Exception {
22302230
tl(9, "), MultiMap.caseInsensitiveMultiMap()");
22312231
tl(7, ")");
22322232
tl(6, "));");
2233-
tl(5, "} else {");
2233+
tl(5, "}");
22342234
}
22352235
if(classeRessourcesAutorisation.size() > 0) {
2236-
l();
2236+
tl(5, "if(!scopes.contains(\"", classeApiMethodeMethode, "\")) {");
22372237
tl(6, "//");
22382238
tl(6, "List<String> fqs = new ArrayList<>();");
22392239
tl(6, "List<String> groups = Optional.ofNullable(", i18nGlobale.getString(I18n.var_requeteSite), ".getGroups()).orElse(new ArrayList<>());");
@@ -2261,16 +2261,13 @@ public void ecrireGenApiServiceImpl2(String classeLangueNom) throws Exception {
22612261
tl(7, "fq.add(fqs.stream().collect(Collectors.joining(\" OR \")));");
22622262
tl(7, "scopes.add(\"", classeApiMethodeMethode, "\");");
22632263
tl(6, "}");
2264-
l();
2264+
tl(5, "}");
2265+
tl(5, "{");
2266+
} else {
2267+
tl(5, "} else {");
22652268
}
22662269
tl(6, i18nGlobale.getString(I18n.var_requeteSite), ".setScopes(scopes.stream().map(o -> o.toString()).collect(Collectors.toList()));");
22672270
tl(6, "List<String> scopes2 = ", i18nGlobale.getString(I18n.var_requeteSite), ".getScopes();");
2268-
// if(classeRoleSession || classeRoleUtilisateur || classeRoleChacun) {
2269-
// tl(6, "if(!scopes2.contains(\"POST\"))");
2270-
// tl(7, "scopes2.add(\"POST\");");
2271-
// tl(6, "if(!scopes2.contains(\"PATCH\"))");
2272-
// tl(7, "scopes2.add(\"PATCH\");");
2273-
// }
22742271
} else {
22752272
tl(3, "authorizationProvider.getAuthorizations(", i18nGlobale.getString(I18n.var_requeteSite), ".get", i18nGlobale.getString(I18n.var_Utilisateur), "()).onFailure(ex -> {");
22762273
tl(4, "String msg = String.format(\"403 FORBIDDEN user %s to %s %s\", siteRequest.getUser().attributes().getJsonObject(\"accessToken\").getString(\"preferred_username\"), serviceRequest.getExtra().getString(\"method\"), serviceRequest.getExtra().getString(\"uri\"));");

0 commit comments

Comments
 (0)