KEYCLOAK-3370 Add option to override theme in client template and client

This commit is contained in:
stianst 2018-01-17 12:47:57 +01:00 committed by Marek Posolda
parent 35ada9d636
commit f762173eb0
4 changed files with 124 additions and 7 deletions

View file

@ -2,10 +2,14 @@ package org.keycloak.theme;
import org.keycloak.Config;
import org.keycloak.common.Version;
import org.keycloak.models.ClientModel;
import org.keycloak.models.ClientTemplateModel;
import org.keycloak.models.KeycloakSession;
public class DefaultThemeSelectorProvider implements ThemeSelectorProvider {
public static final String LOGIN_THEME_KEY = "login_theme";
private final KeycloakSession session;
public DefaultThemeSelectorProvider(KeycloakSession session) {
@ -17,11 +21,29 @@ public class DefaultThemeSelectorProvider implements ThemeSelectorProvider {
String name = null;
switch (type) {
case ACCOUNT:
name = session.getContext().getRealm().getAccountTheme();
case WELCOME:
name = Config.scope("theme").get("welcomeTheme");
break;
case LOGIN:
name = session.getContext().getRealm().getLoginTheme();
ClientModel client = session.getContext().getClient();
if (client != null) {
name = client.getAttribute(LOGIN_THEME_KEY);
if (name == null || name.isEmpty()) {
ClientTemplateModel clientTemplate = client.getClientTemplate();
if (clientTemplate != null) {
name = clientTemplate.getAttribute(LOGIN_THEME_KEY);
}
}
}
if (name == null) {
name = session.getContext().getRealm().getLoginTheme();
}
break;
case ACCOUNT:
name = session.getContext().getRealm().getAccountTheme();
break;
case EMAIL:
name = session.getContext().getRealm().getEmailTheme();
@ -29,12 +51,9 @@ public class DefaultThemeSelectorProvider implements ThemeSelectorProvider {
case ADMIN:
name = session.getContext().getRealm().getAdminTheme();
break;
case WELCOME:
name = Config.scope("theme").get("welcomeTheme");
break;
}
if (name == null) {
if (name == null || name.isEmpty()) {
name = Config.scope("theme").get("default", Version.NAME.toLowerCase());
}
@ -44,4 +63,6 @@ public class DefaultThemeSelectorProvider implements ThemeSelectorProvider {
@Override
public void close() {
}
}

View file

@ -0,0 +1,74 @@
package org.keycloak.testsuite.forms;
import org.jboss.arquillian.graphene.page.Page;
import org.junit.Test;
import org.keycloak.representations.idm.ClientRepresentation;
import org.keycloak.representations.idm.ClientTemplateRepresentation;
import org.keycloak.representations.idm.RealmRepresentation;
import org.keycloak.testsuite.AbstractTestRealmKeycloakTest;
import org.keycloak.testsuite.admin.ApiUtil;
import org.keycloak.testsuite.pages.LoginPage;
import java.util.HashMap;
import static org.junit.Assert.assertEquals;
public class ThemeSelectorTest extends AbstractTestRealmKeycloakTest {
@Page
protected LoginPage loginPage;
@Override
public void configureTestRealm(RealmRepresentation testRealm) {
}
@Test
public void clientOverride() {
loginPage.open();
assertEquals("keycloak", detectTheme());
ClientRepresentation rep = testRealm().clients().findByClientId("test-app").get(0);
rep.getAttributes().put("login_theme", "base");
testRealm().clients().get(rep.getId()).update(rep);
loginPage.open();
assertEquals("base", detectTheme());
rep.getAttributes().put("login_theme", "");
testRealm().clients().get(rep.getId()).update(rep);
}
@Test
public void clientTemplateOverride() {
ClientTemplateRepresentation templateRep = new ClientTemplateRepresentation();
templateRep.setName("loginTheme");
templateRep.setAttributes(new HashMap<>());
templateRep.getAttributes().put("login_theme", "base");
String templateId = ApiUtil.getCreatedId(testRealm().clientTemplates().create(templateRep));
loginPage.open();
assertEquals("keycloak", detectTheme());
ClientRepresentation rep = testRealm().clients().findByClientId("test-app").get(0);
rep.setClientTemplate("loginTheme");
testRealm().clients().get(rep.getId()).update(rep);
loginPage.open();
assertEquals("base", detectTheme());
rep.setClientTemplate("NONE");
testRealm().clients().get(rep.getId()).update(rep);
testRealm().clientTemplates().get(templateId).remove();
}
private String detectTheme() {
if(driver.getPageSource().contains("/login/keycloak/css/login.css")) {
return "keycloak";
} else {
return "base";
}
}
}

View file

@ -44,6 +44,17 @@
</div>
<kc-tooltip>{{:: 'consent-required.tooltip' | translate}}</kc-tooltip>
</div>
<div class="form-group">
<label class="col-md-2 control-label" for="loginTheme">{{:: 'login-theme' | translate}}</label>
<div class="col-sm-6">
<select class="form-control" id="loginTheme"
ng-model="clientEdit.attributes['login_theme']"
ng-options="o.name as o.name for o in serverInfo.themes.login">
<option value="" selected></option>
</select>
</div>
<kc-tooltip>{{:: 'login-theme.tooltip' | translate}}</kc-tooltip>
</div>
<div class="form-group">
<label class="col-md-2 control-label" for="protocol">{{:: 'client-protocol' | translate}}</label>
<div class="col-sm-6">

View file

@ -24,6 +24,17 @@
</div>
<kc-tooltip>{{:: 'client-template.description.tooltip' | translate}}</kc-tooltip>
</div>
<div class="form-group">
<label class="col-md-2 control-label" for="loginTheme">{{:: 'login-theme' | translate}}</label>
<div class="col-sm-6">
<select class="form-control" id="loginTheme"
ng-model="template.attributes['login_theme']"
ng-options="o.name as o.name for o in serverInfo.themes.login">
<option value="" selected></option>
</select>
</div>
<kc-tooltip>{{:: 'login-theme.tooltip' | translate}}</kc-tooltip>
</div>
<div class="form-group">
<label class="col-md-2 control-label" for="protocol">{{:: 'protocol' | translate}}</label>
<div class="col-sm-6">