Skip to content

Commit

Permalink
Convert handlebars to Jinja, and Fine-grained auth permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
computate committed Jul 10, 2024
1 parent 43fbdb9 commit 7eec33e
Show file tree
Hide file tree
Showing 6 changed files with 1,581 additions and 1,723 deletions.
7 changes: 6 additions & 1 deletion src/main/java/org/computate/frFR/java/ConfigCles.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,12 @@ public class ConfigCles {
public static final String var_attribuer = "var_attribuer";
public static final String var_Attribuer = "var_Attribuer";
public static final String var_AucunNomTrouve = "var_AucunNomTrouve";
public static final String var_authPorteeAdmin = "var_authPorteeAdmin";
public static final String var_authPorteeSuperAdmin = "var_authPorteeSuperAdmin";
public static final String var_authRoleAdmin = "var_authRoleAdmin";
public static final String var_authRoleSuperAdmin = "var_authRoleSuperAdmin";
public static final String var_AUTH_ROLE_ADMIN = "var_AUTH_ROLE_ADMIN";
public static final String var_AUTH_PORTEE_ADMIN = "var_AUTH_PORTEE_ADMIN";
public static final String var_AUTH_PORTEE_SUPER_ADMIN = "var_AUTH_PORTEE_SUPER_ADMIN";
public static final String var_AUTH_ROLE_LIRE_REQUIS = "var_AUTH_ROLE_LIRE_REQUIS";
public static final String var_AUTH_ROLE_REQUIS = "var_AUTH_ROLE_REQUIS";
public static final String var_AUTH_POLITIQUE_GRANULEE = "var_AUTH_POLITIQUE_GRANULEE";
Expand Down Expand Up @@ -370,6 +373,7 @@ public class ConfigCles {
public static final String var_Peupler = "var_Peupler";
public static final String var_pivot = "var_pivot";
public static final String var_Pivot = "var_Pivot";
public static final String var_portees = "var_portees";
public static final String var_PourClasse = "var_PourClasse";
public static final String var_Pour = "var_Pour";
public static final String var_Precedent = "var_Precedent";
Expand Down Expand Up @@ -447,6 +451,7 @@ public class ConfigCles {
public static final String var_SITE_JAVA_ENSEMBLE = "var_SITE_JAVA_ENSEMBLE";
public static final String var_siteNom = "var_siteNom";
public static final String var_SITE_NOM = "var_SITE_NOM";
public static final String var_SITE_THEME = "var_SITE_THEME";
public static final String var_siteUrlBase = "var_siteUrlBase";
public static final String var_SiteUrlBase = "var_SiteUrlBase";
public static final String var_SITE_URL_BASE = "var_SITE_URL_BASE";
Expand Down
7 changes: 0 additions & 7 deletions src/main/java/org/computate/frFR/java/ConfigSite.java
Original file line number Diff line number Diff line change
Expand Up @@ -761,12 +761,6 @@ protected void _siteEcrireMethodes() throws Exception {
siteEcrireMethodes.addAll(o);
}

public String authRoleAdmin = null;

protected void _authRoleAdmin() throws Exception {
authRoleAdmin = langueConfigGlobale.getString(ConfigCles.var_AUTH_ROLE_ADMIN);
}

public Boolean authPolitiqueGranulee;
/**
**/
Expand Down Expand Up @@ -928,7 +922,6 @@ public void initConfigSite() throws Exception {
_customerProfileId9();
_customerProfileId10();
_siteEcrireMethodes();
_authRoleAdmin();
_authPolitiqueGranulee();
_ecrireApi();
_ecrireCommentaire();
Expand Down
51 changes: 33 additions & 18 deletions src/main/java/org/computate/frFR/java/EcrireApiClasse.java
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ public void ecrireGenApiService(String classeLangueNom) throws Exception {
auteurGenApiService.l("import io.vertx.ext.web.api.service.WebApiServiceGen;");
auteurGenApiService.l("import io.vertx.ext.web.api.service.ServiceRequest;");
auteurGenApiService.l("import io.vertx.ext.web.api.service.ServiceResponse;");
auteurGenApiService.l("import io.vertx.ext.web.templ.handlebars.HandlebarsTemplateEngine;");
auteurGenApiService.l("import com.hubspot.jinjava.Jinjava;");
auteurGenApiService.l("import io.vertx.core.WorkerExecutor;");
auteurGenApiService.l("import io.vertx.pgclient.PgPool;");
auteurGenApiService.l("import io.vertx.kafka.client.producer.KafkaProducer;");
Expand Down Expand Up @@ -373,7 +373,7 @@ public void ecrireApiServiceImpl(String classeLangueNom) throws Exception {
auteurApiServiceImpl.l("import io.vertx.pgclient.PgPool;");
auteurApiServiceImpl.l("import io.vertx.kafka.client.producer.KafkaProducer;");
if(classePage)
auteurApiServiceImpl.l("import io.vertx.ext.web.templ.handlebars.HandlebarsTemplateEngine;");
auteurApiServiceImpl.l("import com.hubspot.jinjava.Jinjava;");
// auteurGenApiService.l("import ", classeNomEnsemble, ".", classeNomSimple, "ApiServiceVertxEBProxy;");
auteurApiServiceImpl.l();
auteurApiServiceImpl.l("/**");
Expand Down Expand Up @@ -1751,6 +1751,7 @@ public void ecrireGenApiServiceImpl2(String classeLangueNom) throws Exception {
String classePageNomSimpleMethode = classeDoc.getString("classePageNomSimple" + classeApiMethode + "_" + classeLangueNom + "_stored_string");
String classeApiOperationIdMethode = classeDoc.getString("classeApiOperationId" + classeApiMethode + "_" + classeLangueNom + "_stored_string");
String classeApiUriMethode = classeDoc.getString("classeApiUri" + classeApiMethode + "_" + classeLangueNom + "_stored_string");
String classeApiMethodeMethode = classeDoc.getString("classeApiMethode" + classeApiMethode + "_" + classeLangueNom + "_stored_string");
String classeApiTypeMedia200Methode = classeDoc.getString("classeApiTypeMedia200" + classeApiMethode + "_" + classeLangueNom + "_stored_string");
String classeApiTypeMediaRequeteMethode = classeDoc.getString("classeApiTypeMediaRequete" + classeApiMethode + "_" + classeLangueNom + "_stored_string");
String classePageLangueNom = classeDoc.getString("classePageLangueNom" + classeApiMethode + "_" + classeLangueNom + "_stored_string");
Expand Down Expand Up @@ -1808,8 +1809,16 @@ public void ecrireGenApiServiceImpl2(String classeLangueNom) throws Exception {
tl(5, ".sendForm(MultiMap.caseInsensitiveMultiMap()");
tl(7, ".add(\"grant_type\", \"urn:ietf:params:oauth:grant-type:uma-ticket\")");
tl(7, ".add(\"audience\", config.getString(ComputateConfigKeys.AUTH_CLIENT))");
tl(7, ".add(\"response_mode\", \"decision\")");
tl(7, ".add(\"permission\", String.format(\"%s#%s\", ", classeNomSimple, ".CLASS_SIMPLE_NAME, \"", classeApiMethode, "\"))");
tl(7, ".add(\"response_mode\", \"permissions\")");
if(classeApiMethode.equals(classeApiMethodeMethode)) {
tl(7, ".add(\"permission\", String.format(\"%s#%s\", ", classeNomSimple, ".CLASS_SIMPLE_NAME, \"", classeApiMethode, "\"))");
} else {
tl(7, ".add(\"permission\", String.format(\"%s#%s\", ", classeNomSimple, ".CLASS_SIMPLE_NAME, config.getString(ComputateConfigKeys.", classeLangueConfig.getString(ConfigCles.var_AUTH_PORTEE_ADMIN), ")))");
tl(7, ".add(\"permission\", String.format(\"%s#%s\", ", classeNomSimple, ".CLASS_SIMPLE_NAME, config.getString(ComputateConfigKeys.", classeLangueConfig.getString(ConfigCles.var_AUTH_PORTEE_SUPER_ADMIN), ")))");
tl(7, ".add(\"permission\", String.format(\"%s#%s\", ", classeNomSimple, ".CLASS_SIMPLE_NAME, \"GET\"))");
tl(7, ".add(\"permission\", String.format(\"%s#%s\", ", classeNomSimple, ".CLASS_SIMPLE_NAME, \"POST\"))");
tl(7, ".add(\"permission\", String.format(\"%s#%s\", ", classeNomSimple, ".CLASS_SIMPLE_NAME, \"PATCH\"))");
}
tl(3, ").onFailure(ex -> {");
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\"));");
tl(4, "eventHandler.handle(Future.succeededFuture(");
Expand All @@ -1824,7 +1833,8 @@ public void ecrireGenApiServiceImpl2(String classeLangueNom) throws Exception {
tl(4, "));");
tl(3, "}).onSuccess(authorizationDecision -> {");
tl(4, "try {");
tl(5, "if(!authorizationDecision.bodyAsJsonObject().getBoolean(\"result\")) {");
tl(5, "JsonArray scopes = authorizationDecision.bodyAsJsonArray().stream().findFirst().map(decision -> ((JsonObject)decision).getJsonArray(\"scopes\")).orElse(new JsonArray());");
tl(5, "if(!scopes.contains(\"", classeApiMethodeMethode, "\")) {");
tl(6, "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\"));");
tl(6, "eventHandler.handle(Future.succeededFuture(");
tl(7, "new ServiceResponse(403, \"FORBIDDEN\",");
Expand All @@ -1837,6 +1847,7 @@ public void ecrireGenApiServiceImpl2(String classeLangueNom) throws Exception {
tl(7, ")");
tl(6, "));");
tl(5, "} else {");
tl(6, classeLangueConfig.getString(ConfigCles.var_requeteSite), ".setScopes(scopes.stream().map(o -> o.toString()).collect(Collectors.toList()));");
} else {
tl(3, "authorizationProvider.getAuthorizations(", classeLangueConfig.getString(ConfigCles.var_requeteSite), ".get", classeLangueConfig.getString(ConfigCles.var_Utilisateur), "()).onFailure(ex -> {");
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\"));");
Expand Down Expand Up @@ -1883,8 +1894,16 @@ public void ecrireGenApiServiceImpl2(String classeLangueNom) throws Exception {
tl(5, ".sendForm(MultiMap.caseInsensitiveMultiMap()");
tl(7, ".add(\"grant_type\", \"urn:ietf:params:oauth:grant-type:uma-ticket\")");
tl(7, ".add(\"audience\", config.getString(ComputateConfigKeys.AUTH_CLIENT))");
tl(7, ".add(\"response_mode\", \"decision\")");
tl(7, ".add(\"permission\", String.format(\"%s#%s\", ", classeNomSimple, ".CLASS_SIMPLE_NAME, \"", classeApiMethode, "\"))");
tl(7, ".add(\"response_mode\", \"permissions\")");
if(classeApiMethode.equals(classeApiMethodeMethode)) {
tl(7, ".add(\"permission\", String.format(\"%s#%s\", ", classeNomSimple, ".CLASS_SIMPLE_NAME, \"", classeApiMethode, "\"))");
} else {
tl(7, ".add(\"permission\", String.format(\"%s#%s\", ", classeNomSimple, ".CLASS_SIMPLE_NAME, config.getString(ComputateConfigKeys.", classeLangueConfig.getString(ConfigCles.var_AUTH_PORTEE_ADMIN), ")))");
tl(7, ".add(\"permission\", String.format(\"%s#%s\", ", classeNomSimple, ".CLASS_SIMPLE_NAME, config.getString(ComputateConfigKeys.", classeLangueConfig.getString(ConfigCles.var_AUTH_PORTEE_SUPER_ADMIN), ")))");
tl(7, ".add(\"permission\", String.format(\"%s#%s\", ", classeNomSimple, ".CLASS_SIMPLE_NAME, \"GET\"))");
tl(7, ".add(\"permission\", String.format(\"%s#%s\", ", classeNomSimple, ".CLASS_SIMPLE_NAME, \"POST\"))");
tl(7, ".add(\"permission\", String.format(\"%s#%s\", ", classeNomSimple, ".CLASS_SIMPLE_NAME, \"PATCH\"))");
}
tl(3, ").onFailure(ex -> {");
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\"));");
tl(4, "eventHandler.handle(Future.succeededFuture(");
Expand All @@ -1899,7 +1918,8 @@ public void ecrireGenApiServiceImpl2(String classeLangueNom) throws Exception {
tl(4, "));");
tl(3, "}).onSuccess(authorizationDecision -> {");
tl(4, "try {");
tl(5, "if(!authorizationDecision.bodyAsJsonObject().getBoolean(\"result\")) {");
tl(5, "JsonArray scopes = authorizationDecision.bodyAsJsonArray().stream().findFirst().map(decision -> ((JsonObject)decision).getJsonArray(\"scopes\")).orElse(new JsonArray());");
tl(5, "if(!scopes.contains(\"", classeApiMethodeMethode, "\")) {");
tl(6, "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\"));");
tl(6, "eventHandler.handle(Future.succeededFuture(");
tl(7, "new ServiceResponse(403, \"FORBIDDEN\",");
Expand All @@ -1912,6 +1932,7 @@ public void ecrireGenApiServiceImpl2(String classeLangueNom) throws Exception {
tl(7, ")");
tl(6, "));");
tl(5, "} else {");
tl(6, classeLangueConfig.getString(ConfigCles.var_requeteSite), ".setScopes(scopes.stream().map(o -> o.toString()).collect(Collectors.toList()));");
} else {
tl(4, "{");
tl(5, "try {");
Expand Down Expand Up @@ -3123,7 +3144,7 @@ else if(classeApiMethode.equals(classeLangueConfig.getString(ConfigCles.var_PUTC
if(classePageNomCanoniqueMethode != null) {
l();
tl(1, "public String ", classeLangueConfig.getString(ConfigCles.var_template), classeApiMethode, classeNomSimple, "() {");
tl(2, "return Optional.ofNullable(config.getString(", classePartsConfigCles.nomSimple(classeLangueNom), ".TEMPLATE_PATH)).orElse(\"templates\") + \"/", classeLangueNom, "/", classePageNomSimpleMethode, "\";");
tl(2, "return \"/", classeLangueNom, "/", classePageNomSimpleMethode, ".htm\";");
t(1, "}");
}
l();
Expand Down Expand Up @@ -3167,7 +3188,7 @@ else if(classeApiMethode.contains(classeLangueConfig.getString(ConfigCles.var_Re
tl(3, "String pageTemplateUri = ", classeLangueConfig.getString(ConfigCles.var_template), classeApiMethode, classeNomSimple, "();");
tl(3, "String siteTemplatePath = config.getString(ComputateConfigKeys.TEMPLATE_PATH);");
tl(3, "Path resourceTemplatePath = Path.of(siteTemplatePath, pageTemplateUri);");
tl(3, "String template = siteTemplatePath == null ? Files.readString(resourceTemplatePath, Charset.forName(\"UTF-8\")) : Resources.toString(Resources.getResource(resourceTemplatePath.toString()), StandardCharsets.UTF_8);");
tl(3, "String template = siteTemplatePath == null ? Resources.toString(Resources.getResource(resourceTemplatePath.toString()), StandardCharsets.UTF_8) : Files.readString(resourceTemplatePath, Charset.forName(\"UTF-8\"));");
tl(3, classePageNomSimpleMethode, " page = new ", classePageNomSimpleMethode, "();");
tl(3, "MultiMap ", classeLangueConfig.getString(ConfigCles.var_requeteEnTetes), " = MultiMap.caseInsensitiveMultiMap();");
tl(3, classeLangueConfig.getString(ConfigCles.var_requeteSite), ".set", classeLangueConfig.getString(ConfigCles.var_RequeteEnTetes), "(", classeLangueConfig.getString(ConfigCles.var_requeteEnTetes), ");");
Expand All @@ -3180,14 +3201,8 @@ else if(classeApiMethode.contains(classeLangueConfig.getString(ConfigCles.var_Re
tl(3, "page.set", classeLangueConfig.getString(ConfigCles.var_ListeRecherche), classeApiClasseNomSimple, "_(", classeLangueConfig.getString(ConfigCles.var_liste), classeApiClasseNomSimple, ");");
tl(3, "page.set", classeLangueConfig.getString(ConfigCles.var_RequeteSite), "_(", classeLangueConfig.getString(ConfigCles.var_requeteSite), ");");
tl(3, "page.", classeLangueConfig.getString(ConfigCles.var_promesseLoin), classePageNomSimpleMethode, "(", classeLangueConfig.getString(ConfigCles.var_requeteSite), ").onSuccess(a -> {");
tl(4, "JsonObject ctx = JsonObject.mapFrom(page);");
tl(4, "ctx.put(", classePartsConfigCles.nomSimple(langueNom), ".STATIC_BASE_URL, config.getString(", classePartsConfigCles.nomSimple(langueNom), ".STATIC_BASE_URL));");
tl(4, "ctx.put(", classePartsConfigCles.nomSimple(langueNom), ".GITHUB_ORG, config.getString(", classePartsConfigCles.nomSimple(langueNom), ".GITHUB_ORG));");
tl(4, "ctx.put(", classePartsConfigCles.nomSimple(langueNom), ".SITE_NAME, config.getString(", classePartsConfigCles.nomSimple(langueNom), ".SITE_NAME));");
tl(4, "ctx.put(", classePartsConfigCles.nomSimple(langueNom), ".SITE_DISPLAY_NAME, config.getString(", classePartsConfigCles.nomSimple(langueNom), ".SITE_DISPLAY_NAME));");
tl(4, "ctx.put(", classePartsConfigCles.nomSimple(langueNom), ".SITE_POWERED_BY_URL, config.getString(", classePartsConfigCles.nomSimple(langueNom), ".SITE_POWERED_BY_URL));");
tl(4, "ctx.put(", classePartsConfigCles.nomSimple(langueNom), ".SITE_POWERED_BY_NAME, config.getString(", classePartsConfigCles.nomSimple(langueNom), ".SITE_POWERED_BY_NAME));");
tl(4, "ctx.put(", classePartsConfigCles.nomSimple(langueNom), ".SITE_POWERED_BY_IMAGE_URI, config.getString(", classePartsConfigCles.nomSimple(langueNom), ".SITE_POWERED_BY_IMAGE_URI));");
tl(4, "JsonObject ctx = ComputateConfigKeys.getPageContext(config);");
tl(4, "ctx.mergeIn(JsonObject.mapFrom(page));");
tl(4, "String renderedTemplate = jinjava.render(template, ctx.getMap());");
tl(4, "Buffer buffer = Buffer.buffer(renderedTemplate);");
}
Expand Down
Loading

0 comments on commit 7eec33e

Please sign in to comment.