Skip to content
This repository has been archived by the owner on Nov 3, 2017. It is now read-only.

Commit

Permalink
fixed issue with multivalued attributes on greetings
Browse files Browse the repository at this point in the history
  • Loading branch information
SavvasMisaghMoayyed committed Jun 17, 2015
1 parent a5be22b commit 3c2cbef
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
Expand All @@ -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();
}

}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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();
Expand Down

0 comments on commit 3c2cbef

Please sign in to comment.