Skip to content

Commit

Permalink
Merge pull request #3758 from Johaney-s/branding
Browse files Browse the repository at this point in the history
feat(core): support branding between new and old GUI
  • Loading branch information
Johaney-s authored Nov 4, 2022
2 parents 74b1ced + f947e7a commit eeed432
Show file tree
Hide file tree
Showing 15 changed files with 236 additions and 36 deletions.
18 changes: 10 additions & 8 deletions perun-base/src/test/resources/perun-apps-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ brands:
pwd_reset: "pwd-reset"
publications: "publications"
old_gui_domain: "perun-dev"
old_gui_alert: New GUI is <b><a href="http://www.google.com">here</a></b>
- name: other
new_apps:
api: ""
admin: ""
consolidator: ""
linker: ""
profile: ""
pwd_reset: ""
publications: ""
old_gui_domain: ""
api: "other-api"
admin: "other-admin"
consolidator: "other-consolidator"
linker: "other-linker"
profile: "other-profile"
pwd_reset: "other-pwd-reset"
publications: "other-publication"
old_gui_domain: "other-old_gui_domain"
vos: ["test_vo", "other_vo"]
...
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonSetter;

import java.util.ArrayList;
import java.util.List;

/**
Expand Down Expand Up @@ -52,16 +53,32 @@ public static Brand getBrandContainingDomain(String domain) {
return null;
}

/**
* Iterates brands and searches for such that contains vo's shortname.
* If none found, returns default branding.
*/
public static Brand getBrandContainingVo(String voShortname) {
Brand defaultBrand = instance.getBrands()
.stream()
.filter(brand -> brand.getName().equals("default"))
.findFirst()
.orElse(null);
return instance.getBrands().stream()
.filter(brand -> brand.getVoShortnames().contains(voShortname))
.findFirst()
.orElse(defaultBrand);
}

/**
* Class holding data for a single branding.
*/
public static class Brand {

private String name;

private String oldGuiDomain;

private NewApps newApps;
private List<String> voShortnames = new ArrayList<>();
private String oldGuiAlert;

@JsonGetter("name")
public String getName() {
Expand All @@ -83,6 +100,26 @@ public void setNewApps(NewApps newApps) {
this.newApps = newApps;
}

@JsonGetter("voShortnames")
public List<String> getVoShortnames() {
return voShortnames;
}

@JsonSetter("vos")
public void setVoShortnames(List<String> voShortnames) {
this.voShortnames = voShortnames;
}

@JsonGetter("oldGuiAlert")
public String getOldGuiAlert() {
return oldGuiAlert;
}

@JsonSetter("old_gui_alert")
public void setOldGuiAlert(String oldGuiAlert) {
this.oldGuiAlert = oldGuiAlert;
}

@JsonGetter("oldGuiDomain")
public String getOldGuiDomain() {
return oldGuiDomain;
Expand All @@ -98,7 +135,9 @@ public String toString() {
return "Brand{" +
"name='" + name + '\'' +
", oldGuiDomain='" + oldGuiDomain + '\'' +
", oldGuiAlert='" + oldGuiAlert + '\'' +
", newApps=" + newApps +
", vos=" + voShortnames +
'}';
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1161,8 +1161,11 @@ public static void sendPasswordResetEmail(User user, String email, String namesp
}
} else {
// if no brand contains specified domain, set default old gui domain
brand = getInstance().getBrands().get(0);
url = brand.getOldGuiDomain();
PerunAppsConfig.Brand defaultBrand = getInstance().getBrands().stream()
.filter(b -> b.getName().equals("default"))
.findFirst()
.orElseThrow(() -> new InternalErrorException("Default GUI branding configuration is missing"));
url = defaultBrand.getOldGuiDomain();
}
} catch (MalformedURLException ex) {
throw new InternalErrorException("Not valid URL of running Perun instance.", ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import org.junit.Before;
import org.junit.Test;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

/**
Expand All @@ -18,6 +20,7 @@ public void setUp() {
expectedDefaultBrand = new PerunAppsConfig.Brand();
expectedDefaultBrand.setOldGuiDomain("perun-dev");
expectedDefaultBrand.setName("default");
expectedDefaultBrand.setOldGuiAlert("New GUI is <b><a href=\"http://www.google.com\">here</a></b>");

var newApps = new PerunAppsConfig.NewApps();
newApps.setApi("api");
Expand All @@ -42,5 +45,20 @@ public void init() {
assertThat(config.getBrands().get(0))
.usingRecursiveComparison()
.isEqualTo(expectedDefaultBrand);
assertThat(config.getBrands().get(1).getOldGuiAlert())
.isNull();
}

@Test
public void vos() {
PerunAppsConfig config = PerunAppsConfig.getInstance();

assertThat(config)
.isNotNull();
assertThat(config.getBrands())
.hasSize(2);
assertThat(config.getBrands().get(1).getVoShortnames())
.containsAll(List.of("test_vo", "other_vo"));
assertThat(config.getBrands().get(0).getVoShortnames()).isNullOrEmpty();
}
}
15 changes: 15 additions & 0 deletions perun-openapi/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1431,6 +1431,8 @@ components:
name: { type: string }
oldGuiDomain: { type: string }
newApps: { $ref: '#/components/schemas/NewApps' }
vos: { type: array, items: { type: string } }
oldGuiAlert: { type: string }

PerunAppsConfig:
type: object
Expand Down Expand Up @@ -3537,6 +3539,19 @@ paths:
default:
$ref: '#/components/responses/ExceptionResponse'

/json/utils/getNewGuiAlert:
get:
tags:
- Utils
operationId: getNewGuiAlert
summary: Gets new GUI alert (banner html content) for the brand of incoming request.
description: Returns new GUI alert
responses:
'200':
$ref: '#/components/responses/StringResponse'
default:
$ref: '#/components/responses/ExceptionResponse'

#################################################
# #
# AuthzResolver #
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import cz.metacentrum.perun.core.bl.MembersManagerBl;
import cz.metacentrum.perun.core.bl.UsersManagerBl;
import cz.metacentrum.perun.core.blImpl.AuthzResolverBlImpl;
import cz.metacentrum.perun.core.impl.PerunAppsConfig;
import cz.metacentrum.perun.registrar.exceptions.ApplicationMailAlreadyRemovedException;
import cz.metacentrum.perun.registrar.exceptions.ApplicationMailExistsException;
import cz.metacentrum.perun.registrar.exceptions.ApplicationMailNotExistsException;
Expand Down Expand Up @@ -1525,8 +1526,8 @@ private String substituteCommonStrings(Application app, List<ApplicationFormItem

/**
* Return base URL of Perun instance taken from VO/Group attribute. If not set,
* value of config property "perunUrl" is used. If can't determine, then empty
* string is returned.
* value of branded, old gui domain is used. Otherwise, value of config property "perunUrl" is used.
* If can't determine, then empty string is returned.
*
* e.g. https://perun.cesnet.cz
*
Expand All @@ -1536,6 +1537,11 @@ private String substituteCommonStrings(Application app, List<ApplicationFormItem
*/
private String getPerunUrl(Vo vo, Group group) {
String result = getPropertyFromConfiguration("perunUrl");

PerunAppsConfig.Brand voBrand = PerunAppsConfig.getBrandContainingVo(vo.getShortName());
if (voBrand != null && !voBrand.getName().equals("default")) {
result = voBrand.getOldGuiDomain();
}
try {
if (group != null) {
Attribute a = attrManager.getAttribute(registrarSession, group, URN_GROUP_REGISTRAR_URL);
Expand Down Expand Up @@ -1595,10 +1601,14 @@ private String replacePerunGuiUrl(String mailText, Vo vo, Group group) {
// only namespace "fed", "cert",...
String namespace = m2.group(1);

newValue = getPerunUrl(vo, group);
if (newValue != null && !newValue.isEmpty()) {
if (!newValue.endsWith("/")) newValue += "/";
newValue += namespace + "/gui/";
if (namespace.equals("newGUI")) {
newValue = PerunAppsConfig.getBrandContainingVo(vo.getShortName()).getNewApps().getAdmin();
} else {
newValue = getPerunUrl(vo, group);
if (newValue != null && !newValue.isEmpty()) {
if (!newValue.endsWith("/")) newValue += "/";
newValue += namespace + "/gui/";
}
}
}
// substitute {appGuiUrl-authz} with actual value or empty string
Expand Down Expand Up @@ -1699,14 +1709,23 @@ private String replaceAppDetailUrl(String mailText, int appId, Vo vo, Group grou
// only namespace "fed", "cert",...
String namespace = m2.group(1);

newValue = getPerunUrl(vo, group);
if (newValue != null && !newValue.isEmpty()) {
if (!newValue.endsWith("/")) newValue += "/";
newValue += namespace + "/gui/";
newValue += "?vo/appdetail?id="+appId;
//newValue += getFedAuthz().contains(namespace) ? "?vo/appdetail?id="+appId : "#vo/appdetail?id="+appId;
if (namespace.equals("newGUI")) {
newValue = PerunAppsConfig.getBrandContainingVo(vo.getShortName()).getNewApps().getAdmin();
if (newValue != null && !newValue.isEmpty()) {
if (!newValue.endsWith("/")) newValue += "/";
newValue += "organizations/" + vo.getId();
newValue += group == null ? "" : "/groups/" + group.getId();
newValue += "/applications/" + appId;
}
} else {
newValue = getPerunUrl(vo, group);
if (newValue != null && !newValue.isEmpty()) {
if (!newValue.endsWith("/")) newValue += "/";
newValue += namespace + "/gui/";
newValue += "?vo/appdetail?id="+appId;
//newValue += getFedAuthz().contains(namespace) ? "?vo/appdetail?id="+appId : "#vo/appdetail?id="+appId;
}
}

}

// substitute {appDetailUrl-authz} with actual value or empty string
Expand Down
12 changes: 12 additions & 0 deletions perun-rpc/src/main/java/cz/metacentrum/perun/rpc/Api.java
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,18 @@ private void serve(HttpServletRequest req, HttpServletResponse resp, boolean isG
out.close();
return;

} else if ("utils".equals(manager) && "getNewGuiAlert".equals(method)) {

String requestDomain = req.getScheme() + "://" + req.getServerName();
if (PerunAppsConfig.getBrandContainingDomain(requestDomain) == null) {
ser.write(null);
} else {
ser.write(PerunAppsConfig.getBrandContainingDomain(requestDomain).getOldGuiAlert());
}
// closes the request
out.close();
return;

} else if ("utils".equals(manager) && PERUNSTATUS.equals(method)) {
Date date = new Date();
Timestamp timestamp = new Timestamp(date.getTime());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public class PerunWebSession {
private ArrayList<GeneralObject> entitiesHistoryList = new ArrayList<GeneralObject>();

private BasicOverlayType configuration;
private String newGuiAlert;

// RPC URL
private String rpcUrl = "";
Expand Down Expand Up @@ -886,4 +887,12 @@ public BasicOverlayType getConfiguration() {
public void setConfiguration(BasicOverlayType configuration) {
this.configuration = configuration;
}

public String getNewGuiAlert() {
return newGuiAlert;
}

public void setNewGuiAlert(String newGuiAlert) {
this.newGuiAlert = newGuiAlert;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import cz.metacentrum.perun.webgui.client.resources.LargeIcons;
import cz.metacentrum.perun.webgui.client.resources.SmallIcons;
import cz.metacentrum.perun.webgui.json.GetGuiConfiguration;
import cz.metacentrum.perun.webgui.json.GetNewGuiAlert;
import cz.metacentrum.perun.webgui.json.JsonCallbackEvents;
import cz.metacentrum.perun.webgui.json.JsonUtils;
import cz.metacentrum.perun.webgui.json.attributesManager.GetListOfAttributes;
Expand All @@ -44,10 +45,14 @@
import cz.metacentrum.perun.webgui.widgets.CantLogAsServiceUserWidget;
import cz.metacentrum.perun.webgui.widgets.Confirm;
import cz.metacentrum.perun.webgui.widgets.NotUserOfPerunWidget;
import org.eclipse.jetty.util.ajax.JSON;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* The main web GUI class. It's GWT Entry point.
Expand Down Expand Up @@ -168,17 +173,27 @@ public void onFinished(JavaScriptObject jso) {
final PerunPrincipal pp = (PerunPrincipal)jso;
session.setPerunPrincipal(pp);

GetGuiConfiguration getConfig = new GetGuiConfiguration(new JsonCallbackEvents(){
GetNewGuiAlert getNewGuiAlert = new GetNewGuiAlert(new JsonCallbackEvents() {
@Override
public void onFinished(JavaScriptObject jso) {
BasicOverlayType basic = (jso == null) ? null : jso.cast();
String alert = (basic == null) ? null : basic.getString();
session.setNewGuiAlert(alert);

session.setConfiguration((BasicOverlayType)jso.cast());

String newGuiAlertContent = session.getConfiguration().getCustomProperty("newAdminGuiAlert");
String newGuiAlertContent = session.getNewGuiAlert();
if (newGuiAlertContent != null && !newGuiAlertContent.isEmpty()) {
DOM.getElementById("perun-new-gui-alert").setInnerHTML(newGuiAlertContent);
DOM.getElementById("perun-new-gui-alert").setInnerHTML(session.getNewGuiAlert());
DOM.getElementById("perun-new-gui-alert").setClassName("newGuiAlertActive");
}
}
});
getNewGuiAlert.retrieveData();

GetGuiConfiguration getConfig = new GetGuiConfiguration(new JsonCallbackEvents(){
@Override
public void onFinished(JavaScriptObject jso) {

session.setConfiguration((BasicOverlayType)jso.cast());

// check if user exists
if (session.getUser() == null && !pp.getRoles().hasAnyRole()) {
Expand Down
Loading

0 comments on commit eeed432

Please sign in to comment.