use of org.jasig.services.persondir.support.NamedPersonImpl in project uPortal by Jasig.
the class LocalAccountPersonAttributeDao method mapPersonAttributes.
/**
* This implementation uses the result attribute mapping to supplement, rather than replace, the
* attributes returned from the database.
*/
protected IPersonAttributes mapPersonAttributes(final ILocalAccountPerson person) {
final Map<String, List<Object>> mappedAttributes = new LinkedHashMap<String, List<Object>>();
mappedAttributes.putAll(person.getAttributes());
// map the user's username to the portal's username attribute in the
// attribute map
mappedAttributes.put(this.getUsernameAttributeProvider().getUsernameAttribute(), Collections.<Object>singletonList(person.getName()));
// to build one from the first and last name attributes
if (!mappedAttributes.containsKey(displayNameAttribute) || mappedAttributes.get(displayNameAttribute).size() == 0 || StringUtils.isBlank((String) mappedAttributes.get(displayNameAttribute).get(0))) {
final List<Object> firstNames = mappedAttributes.get("givenName");
final List<Object> lastNames = mappedAttributes.get("sn");
final StringBuilder displayName = new StringBuilder();
if (firstNames != null && firstNames.size() > 0) {
displayName.append(firstNames.get(0)).append(" ");
}
if (lastNames != null && lastNames.size() > 0) {
displayName.append(lastNames.get(0));
}
mappedAttributes.put(displayNameAttribute, Collections.<Object>singletonList(displayName.toString()));
}
for (final Map.Entry<String, Set<String>> resultAttrEntry : this.getResultAttributeMapping().entrySet()) {
final String dataKey = resultAttrEntry.getKey();
//Only map found data attributes
if (mappedAttributes.containsKey(dataKey)) {
Set<String> resultKeys = resultAttrEntry.getValue();
//If dataKey has no mapped resultKeys just use the dataKey
if (resultKeys == null) {
resultKeys = Collections.singleton(dataKey);
}
//Add the value to the mapped attributes for each mapped key
final List<Object> value = mappedAttributes.get(dataKey);
for (final String resultKey : resultKeys) {
if (resultKey == null) {
//TODO is this possible?
if (!mappedAttributes.containsKey(dataKey)) {
mappedAttributes.put(dataKey, value);
}
} else if (!mappedAttributes.containsKey(resultKey)) {
mappedAttributes.put(resultKey, value);
}
}
}
}
final IPersonAttributes newPerson;
final String name = person.getName();
if (name != null) {
newPerson = new NamedPersonImpl(name, mappedAttributes);
} else {
final String userNameAttribute = this.getConfiguredUserNameAttribute();
newPerson = new AttributeNamedPersonImpl(userNameAttribute, mappedAttributes);
}
return newPerson;
}
use of org.jasig.services.persondir.support.NamedPersonImpl in project uPortal by Jasig.
the class RequestAttributeServiceImplTest method testControl.
/**
* Default test for function, returns the multivalued attribute map with one multi-valued
* attribute.
*/
@Test
public void testControl() {
MockHttpServletRequest httpServletRequest = new MockHttpServletRequest();
httpServletRequest.setRemoteUser("username");
Map<String, List<Object>> attributes = new HashMap<String, List<Object>>();
attributes.put("attribute1", Arrays.asList(new Object[] { "value1", "value2", "value3" }));
NamedPersonImpl personAttributes = new NamedPersonImpl("username", attributes);
PortletWindow plutoPortletWindow = mock(PortletWindow.class);
IPortletWindow portletWindow = mock(IPortletWindow.class);
IPortletEntity portletEntity = mock(IPortletEntity.class);
when(portletWindow.getPortletEntity()).thenReturn(portletEntity);
IPortletDefinition portletDefinition = mock(IPortletDefinition.class);
when(portletEntity.getPortletDefinition()).thenReturn(portletDefinition);
IPortletDefinitionId portletDefinitionId = mock(IPortletDefinitionId.class);
when(portletDefinition.getPortletDefinitionId()).thenReturn(portletDefinitionId);
IPersonAttributeDao personAttributeDao = mock(IPersonAttributeDao.class);
when(personAttributeDao.getPerson("username")).thenReturn(personAttributes);
IPortletWindowRegistry portletWindowRegistry = mock(IPortletWindowRegistry.class);
when(portletWindowRegistry.convertPortletWindow(httpServletRequest, plutoPortletWindow)).thenReturn(portletWindow);
List<UserAttributeType> userAttributesList = new ArrayList<UserAttributeType>();
UserAttributeType userAttribute = new UserAttributeType();
userAttribute.setName("attribute1");
userAttributesList.add(userAttribute);
PortletAppType portletApplicationDefinition = new PortletAppType();
portletApplicationDefinition.addUserAttribute("attribute1");
IPortletDefinitionRegistry portletDefinitionRegistry = mock(IPortletDefinitionRegistry.class);
when(portletDefinitionRegistry.getParentPortletApplicationDescriptor(portletDefinitionId)).thenReturn(portletApplicationDefinition);
RequestAttributeServiceImpl service = new RequestAttributeServiceImpl();
service.setPersonAttributeDao(personAttributeDao);
service.setPortletDefinitionRegistry(portletDefinitionRegistry);
service.setPortletWindowRegistry(portletWindowRegistry);
Object attribute = service.getAttribute(httpServletRequest, plutoPortletWindow, IPortletRenderer.MULTIVALUED_USERINFO_MAP_ATTRIBUTE);
Assert.assertNotNull(attribute);
Assert.assertTrue(attribute instanceof Map);
@SuppressWarnings("unchecked") Map<String, List<Object>> attributeMap = (Map<String, List<Object>>) attribute;
List<Object> values = attributeMap.get("attribute1");
Assert.assertEquals(3, values.size());
Assert.assertTrue(values.contains("value1"));
Assert.assertTrue(values.contains("value2"));
Assert.assertTrue(values.contains("value3"));
}
use of org.jasig.services.persondir.support.NamedPersonImpl in project uPortal by Jasig.
the class PersonLookupHelperImpl method getVisiblePerson.
/**
* Filter an IPersonAttributes for a specified viewing principal. The returned person will
* contain only the attributes provided in the permitted attributes list. <code>null</code> if
* the principal does not have permission to view the user.
*
* @param principal
* @param person
* @param generallyPermittedAttributes
* @return
*/
protected IPersonAttributes getVisiblePerson(final IAuthorizationPrincipal principal, final IPersonAttributes person, final Set<String> generallyPermittedAttributes) {
// has permission or deny through one of the contained groups.
if (person.getName() != null && principal.hasPermission(IPermission.PORTAL_USERS, IPermission.VIEW_USER_ACTIVITY, person.getName())) {
// If the user has permission, filter the person attributes according
// to the specified permitted attributes; the collection of permitted
// attributes can be different based on whether the user is trying to
// access information about him/herself.
final Set<String> permittedAttributes = person.getName().equals(principal.getKey()) ? getPermittedOwnAttributes(principal, generallyPermittedAttributes) : generallyPermittedAttributes;
final Map<String, List<Object>> visibleAttributes = new HashMap<>();
for (String attr : person.getAttributes().keySet()) {
if (permittedAttributes.contains(attr)) {
visibleAttributes.put(attr, person.getAttributeValues(attr));
}
}
// use the filtered attribute list to create and return a new
// person object
final IPersonAttributes visiblePerson = new NamedPersonImpl(person.getName(), visibleAttributes);
return visiblePerson;
} else {
logger.debug("Principal " + principal.getKey() + " does not have permissions to view user " + person.getName());
return null;
}
}
Aggregations