diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2016-02-03 11:39:29 +0100 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2016-02-04 10:10:10 +0100 |
commit | a153a7058e2890d1730d5c368689a604b6d502b0 (patch) | |
tree | e57d172826d7151d2b0c0bea49acd43c4a1e5238 | |
parent | 52022312cc34f18744707f25bad36d3dc585b56e (diff) | |
download | sonarqube-a153a7058e2890d1730d5c368689a604b6d502b0.tar.gz sonarqube-a153a7058e2890d1730d5c368689a604b6d502b0.zip |
SONAR-7242 Identity Provider must define a display with icon url abd background color
8 files changed, 251 insertions, 33 deletions
diff --git a/it/it-plugins/base-auth-plugin/src/main/java/FakeBaseIdProvider.java b/it/it-plugins/base-auth-plugin/src/main/java/FakeBaseIdProvider.java index 9095523d58f..730bef971e1 100644 --- a/it/it-plugins/base-auth-plugin/src/main/java/FakeBaseIdProvider.java +++ b/it/it-plugins/base-auth-plugin/src/main/java/FakeBaseIdProvider.java @@ -20,6 +20,7 @@ import java.io.IOException; import org.sonar.api.config.Settings; import org.sonar.api.server.authentication.BaseIdentityProvider; +import org.sonar.api.server.authentication.Display; import org.sonar.api.server.authentication.UserIdentity; public class FakeBaseIdProvider implements BaseIdentityProvider { @@ -66,8 +67,11 @@ public class FakeBaseIdProvider implements BaseIdentityProvider { } @Override - public String getIconPath() { - return "/static/baseauthplugin/base.png"; + public Display getDisplay() { + return Display.builder() + .setIconPath("/static/baseauthplugin/base.png") + .setBackgroundColor("#205081") + .build(); } @Override diff --git a/it/it-plugins/oauth2-auth-plugin/src/main/java/FakeOAuth2IdProvider.java b/it/it-plugins/oauth2-auth-plugin/src/main/java/FakeOAuth2IdProvider.java index 907d375bf8c..5f3f73ec0a9 100644 --- a/it/it-plugins/oauth2-auth-plugin/src/main/java/FakeOAuth2IdProvider.java +++ b/it/it-plugins/oauth2-auth-plugin/src/main/java/FakeOAuth2IdProvider.java @@ -19,6 +19,7 @@ */ import org.sonar.api.config.Settings; +import org.sonar.api.server.authentication.Display; import org.sonar.api.server.authentication.OAuth2IdentityProvider; import org.sonar.api.server.authentication.UserIdentity; @@ -71,8 +72,11 @@ public class FakeOAuth2IdProvider implements OAuth2IdentityProvider { } @Override - public String getIconPath() { - return "/static/oauth2authplugin/oauth2.png"; + public Display getDisplay() { + return Display.builder() + .setIconPath("/static/oauth2authplugin/oauth2.png") + .setBackgroundColor("#444444") + .build(); } @Override diff --git a/server/sonar-server/src/test/java/org/sonar/server/authentication/InitFilterTest.java b/server/sonar-server/src/test/java/org/sonar/server/authentication/InitFilterTest.java index adc9173d071..65cb212bbeb 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/authentication/InitFilterTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/authentication/InitFilterTest.java @@ -27,6 +27,7 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.sonar.api.server.authentication.BaseIdentityProvider; +import org.sonar.api.server.authentication.Display; import org.sonar.api.server.authentication.IdentityProvider; import org.sonar.api.server.authentication.OAuth2IdentityProvider; import org.sonar.api.utils.log.LogTester; @@ -122,7 +123,7 @@ public class InitFilterTest { } @Override - public String getIconPath() { + public Display getDisplay() { return null; } diff --git a/server/sonar-server/src/test/java/org/sonar/server/authentication/TestIdentityProvider.java b/server/sonar-server/src/test/java/org/sonar/server/authentication/TestIdentityProvider.java index 60d4622f1fa..d162b5febca 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/authentication/TestIdentityProvider.java +++ b/server/sonar-server/src/test/java/org/sonar/server/authentication/TestIdentityProvider.java @@ -19,13 +19,14 @@ */ package org.sonar.server.authentication; +import org.sonar.api.server.authentication.Display; import org.sonar.api.server.authentication.IdentityProvider; public class TestIdentityProvider implements IdentityProvider { private String key; private String name; - private String iconPatch; + private Display display; private boolean enabled; private boolean allowsUsersToSignUp; @@ -50,12 +51,12 @@ public class TestIdentityProvider implements IdentityProvider { } @Override - public String getIconPath() { - return iconPatch; + public Display getDisplay() { + return display; } - public TestIdentityProvider setIconPatch(String iconPatch) { - this.iconPatch = iconPatch; + public TestIdentityProvider setDisplay(Display display) { + this.display = display; return this; } diff --git a/server/sonar-web/src/main/webapp/WEB-INF/app/views/sessions/_form.html.erb b/server/sonar-web/src/main/webapp/WEB-INF/app/views/sessions/_form.html.erb index d0b72146d04..17570f1ce12 100644 --- a/server/sonar-web/src/main/webapp/WEB-INF/app/views/sessions/_form.html.erb +++ b/server/sonar-web/src/main/webapp/WEB-INF/app/views/sessions/_form.html.erb @@ -6,10 +6,11 @@ <ul> <% auth_providers.each do |provider| %> <li> - <a href="<%= ApplicationController.root_context -%>/sessions/init/<%= provider.getKey().to_s %>" + <a href="/sessions/init/<%= provider.getKey().to_s %>" + style="background-color: <%= provider.getDisplay().getBackgroundColor().to_s %>" title="Log in with <%= provider.getName().to_s -%>"> <img alt="<%= provider.getName().to_s -%>" width="20" height="20" - src="<%= ApplicationController.root_context + provider.getIconPath().to_s -%>"> + src="<%= provider.getDisplay().getIconPath().to_s -%>"> <span>Log in with <%= provider.getName().to_s -%></span> </a> </li> @@ -52,21 +53,6 @@ </p> <div> - <div class="pull-left"> - <% auth_providers = Api::Utils.java_facade.getIdentityProviders().to_a %> - <ul class="list-inline"> - <% auth_providers.each do |provider| %> - <li> - <a class="oauth-link" - href="/sessions/init/<%= provider.getKey().to_s %>" - title="Login with <%= provider.getName().to_s -%>"> - <img alt="<%= provider.getName().to_s -%>" width="24" height="24" - src="<%= provider.getIconPath().to_s -%>"> - </a> - </li> - <% end %> - </ul> - </div> <div class="text-right overflow-hidden"> <button name="commit"><%= message('sessions.log_in') -%></button> <a class="spacer-left" href="<%= home_path -%>"><%= message('cancel') -%></a> diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/server/authentication/Display.java b/sonar-plugin-api/src/main/java/org/sonar/api/server/authentication/Display.java new file mode 100644 index 00000000000..74ffc57ad6a --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/server/authentication/Display.java @@ -0,0 +1,107 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.api.server.authentication; + +import javax.annotation.concurrent.Immutable; + +import static com.google.common.base.Preconditions.checkArgument; +import static org.apache.commons.lang.StringUtils.isNotBlank; + +/** + * Display information provided by the Identity Provider to be displayed into the login form. + * + * @since 5.4 + */ +@Immutable +public final class Display { + + private final String iconPath; + private final String backgroundColor; + + private Display(Builder builder) { + this.iconPath = builder.iconPath; + this.backgroundColor = builder.backgroundColor; + } + + /** + * URL path to the provider icon, as deployed at runtime, for example "/static/authgithub/github.svg" (in this + * case "authgithub" is the plugin key. Source file is "src/main/resources/static/github.svg"). + * It can also be an external URL, for example "http://www.mydomain/myincon.png". + * + * Must not be blank. + * <p/> + * The recommended format is SVG with a size of 24x24 pixels. + * Other supported format is PNG, with a size of 40x40 pixels. + */ + public String getIconPath() { + return iconPath; + } + + /** + * Background color for the provider button displayed in the login form. + * It's a Hexadecimal value, for instance #205081. + * <p/> + * If not provided, the default value is black (#000000) + */ + public String getBackgroundColor() { + return backgroundColor; + } + + public static Builder builder() { + return new Builder(); + } + + public static class Builder { + + private String iconPath; + private String backgroundColor = "#000000"; + + private Builder() { + } + + /** + * @see Display#getIconPath() + */ + public Builder setIconPath(String iconPath) { + this.iconPath = iconPath; + return this; + } + + /** + * @see Display#getBackgroundColor() + */ + public Builder setBackgroundColor(String backgroundColor) { + this.backgroundColor = backgroundColor; + return this; + } + + public Display build() { + checkArgument(isNotBlank(iconPath), "Icon path must not be blank"); + validateBackgroundColor(); + return new Display(this); + } + + private void validateBackgroundColor() { + checkArgument(isNotBlank(backgroundColor), "Background color must not be blank"); + checkArgument(backgroundColor.length() == 7 && backgroundColor.startsWith("#"), + "Background color must begin with a sharp followed by 6 characters"); + } + } +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/server/authentication/IdentityProvider.java b/sonar-plugin-api/src/main/java/org/sonar/api/server/authentication/IdentityProvider.java index 9944d2c51a7..f1b43ba1b2c 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/server/authentication/IdentityProvider.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/server/authentication/IdentityProvider.java @@ -47,13 +47,9 @@ public interface IdentityProvider { String getName(); /** - * URL path to the provider icon, as deployed at runtime, for example "/static/authgithub/github.svg" (in this - * case "authgithub" is the plugin key. Source file is "src/main/resources/static/github.svg"). Must not be blank. - * <p/> - * The recommended format is SVG with a size of 24x24 pixels. - * Other supported format is PNG, with a size of 48x48 pixels. + * Display information for the login form */ - String getIconPath(); + Display getDisplay(); /** * Is the provider fully configured and enabled ? If {@code true}, then @@ -67,4 +63,5 @@ public interface IdentityProvider { * registered users can login. */ boolean allowsUsersToSignUp(); + } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/server/authentication/DisplayTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/server/authentication/DisplayTest.java new file mode 100644 index 00000000000..2b756383238 --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/server/authentication/DisplayTest.java @@ -0,0 +1,118 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.api.server.authentication; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import static org.assertj.core.api.Assertions.assertThat; + +public class DisplayTest { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Test + public void create_display() throws Exception { + Display display = Display.builder() + .setIconPath("/static/authgithub/github.svg") + .setBackgroundColor("#123456") + .build(); + + assertThat(display.getIconPath()).isEqualTo("/static/authgithub/github.svg"); + assertThat(display.getBackgroundColor()).isEqualTo("#123456"); + } + + @Test + public void create_display_woth_default_background_color() throws Exception { + Display display = Display.builder() + .setIconPath("/static/authgithub/github.svg") + .build(); + + assertThat(display.getBackgroundColor()).isEqualTo("#000000"); + } + + @Test + public void fail_when_icon_path_is_null() throws Exception { + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("Icon path must not be blank"); + + Display.builder() + .setIconPath(null) + .setBackgroundColor("#123456") + .build(); + } + + @Test + public void fail_when_icon_path_is_blank() throws Exception { + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("Icon path must not be blank"); + + Display.builder() + .setIconPath("") + .setBackgroundColor("#123456") + .build(); + } + + @Test + public void fail_when_background_color_is_null() throws Exception { + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("Background color must not be blank"); + + Display.builder() + .setIconPath("/static/authgithub/github.svg") + .setBackgroundColor(null) + .build(); + } + + @Test + public void fail_when_background_color_is_blank() throws Exception { + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("Background color must not be blank"); + + Display.builder() + .setIconPath("/static/authgithub/github.svg") + .setBackgroundColor("") + .build(); + } + + @Test + public void fail_when_background_color_has_wrong_size() throws Exception { + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("Background color must begin with a sharp followed by 6 characters"); + + Display.builder() + .setIconPath("/static/authgithub/github.svg") + .setBackgroundColor("#1234") + .build(); + } + + @Test + public void fail_when_background_color_doesnt_begin_with_sharp() throws Exception { + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("Background color must begin with a sharp followed by 6 characters"); + + Display.builder() + .setIconPath("/static/authgithub/github.svg") + .setBackgroundColor("*123456") + .build(); + } +} |