diff --git a/cas-mfa-java/src/main/java/net/unicon/cas/mfa/web/flow/view/MultifactorLoginViewPrincipalAttributeGreeter.java b/cas-mfa-java/src/main/java/net/unicon/cas/mfa/web/flow/view/MultifactorLoginViewPrincipalAttributeGreeter.java index 190a8c3..bc050bd 100644 --- a/cas-mfa-java/src/main/java/net/unicon/cas/mfa/web/flow/view/MultifactorLoginViewPrincipalAttributeGreeter.java +++ b/cas-mfa-java/src/main/java/net/unicon/cas/mfa/web/flow/view/MultifactorLoginViewPrincipalAttributeGreeter.java @@ -9,6 +9,8 @@ import org.springframework.binding.message.MessageContext; import org.springframework.binding.message.MessageResolver; +import java.util.Collection; + /** * Greets the principal by a configurable principal attribute and falls back * to the principal id, if none is found. @@ -36,7 +38,24 @@ public MultifactorLoginViewPrincipalAttributeGreeter(final String greetingAttrNa public String getPersonToGreet(final Principal p, final MessageContext messageContext) { String personId = p.getId(); - final String greetingPersonId = (String) p.getAttributes().get(this.greetingAttributeName); + final Object attrValue = p.getAttributes().get(this.greetingAttributeName); + + if (attrValue == null) { + LOGGER.warn("No attribute value could be found for [{}]", this.greetingAttributeName); + return p.getId(); + } + + String greetingPersonId = attrValue.toString(); + if (attrValue instanceof Collection) { + final Collection col =((Collection) attrValue); + if (!col.isEmpty()) { + greetingPersonId = col.iterator().next().toString(); + LOGGER.warn("Found multiple attribute values [{}] for [{}] to greet. Picked [{}]", + attrValue, this.greetingAttributeName, + greetingPersonId); + } + } + if (!StringUtils.isBlank(greetingPersonId)) { personId = greetingPersonId; } @@ -46,13 +65,12 @@ public String getPersonToGreet(final Principal p, final MessageContext messageCo final Message[] messages = messageContext.getMessagesBySource(CODE); if (messages == null || messages.length == 0) { - LOGGER.warn("The greeting message for pricipal [{}] could not be resolved by the " + LOGGER.warn("The greeting message for principal [{}] could not be resolved by the " + "code [{}] in any of the configured message resource bundles. Falling back to principal id [{}]", p, CODE, p.getId()); return p.getId(); } - final String msgToReturn = messages[0].getText(); - return msgToReturn; + return messages[0].getText(); } } diff --git a/cas-mfa-web/src/test/java/net/unicon/cas/mfa/web/flow/view/MultifactorLoginViewPrincipalAttributeGreeterTests.java b/cas-mfa-web/src/test/java/net/unicon/cas/mfa/web/flow/view/MultifactorLoginViewPrincipalAttributeGreeterTests.java index daa537c..15cd8f4 100644 --- a/cas-mfa-web/src/test/java/net/unicon/cas/mfa/web/flow/view/MultifactorLoginViewPrincipalAttributeGreeterTests.java +++ b/cas-mfa-web/src/test/java/net/unicon/cas/mfa/web/flow/view/MultifactorLoginViewPrincipalAttributeGreeterTests.java @@ -1,23 +1,22 @@ package net.unicon.cas.mfa.web.flow.view; -import static org.junit.Assert.*; -import static org.mockito.Mockito.*; - -import java.util.HashMap; -import java.util.Map; - import org.jasig.cas.authentication.principal.Principal; import org.jasig.cas.authentication.principal.SimplePrincipal; import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.springframework.binding.message.Message; import org.springframework.binding.message.MessageContext; import org.springframework.binding.message.Severity; -@RunWith(JUnit4.class) +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.when; + public class MultifactorLoginViewPrincipalAttributeGreeterTests { @Mock @@ -37,6 +36,22 @@ public MultifactorLoginViewPrincipalAttributeGreeterTests() { when(messageContext.getMessagesBySource(any(Object.class))).thenReturn(values); } + @Test + public void testValidPrincipalMultivaluedAttributeToGreet() { + final Map map = new HashMap(); + map.put("firstName", Arrays.asList("cas", "sso")); + map.put("lastName", "user"); + + final Principal p = new SimplePrincipal("userid", map); + + final MultifactorLoginViewPrincipalAttributeGreeter greeter = new MultifactorLoginViewPrincipalAttributeGreeter( + "firstName"); + + configureMessageContextForPrincipal("cas"); + final String value = greeter.getPersonToGreet(p, this.messageContext); + assertTrue(value.contains("cas")); + } + @Test public void testValidPrincipalAttributeToGreet() { final Map map = new HashMap();