Search in sources :

Example 1 with NamedPersonImpl

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;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) AttributeNamedPersonImpl(org.jasig.services.persondir.support.AttributeNamedPersonImpl) LinkedHashMap(java.util.LinkedHashMap) IPersonAttributes(org.jasig.services.persondir.IPersonAttributes) AttributeNamedPersonImpl(org.jasig.services.persondir.support.AttributeNamedPersonImpl) NamedPersonImpl(org.jasig.services.persondir.support.NamedPersonImpl) ArrayList(java.util.ArrayList) List(java.util.List) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 2 with NamedPersonImpl

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"));
}
Also used : IPortletWindowRegistry(org.apereo.portal.portlet.registry.IPortletWindowRegistry) PortletAppType(org.apache.pluto.container.om.portlet.impl.PortletAppType) HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ArrayList(java.util.ArrayList) UserAttributeType(org.apache.pluto.container.om.portlet.impl.UserAttributeType) IPortletWindow(org.apereo.portal.portlet.om.IPortletWindow) IPortletDefinitionId(org.apereo.portal.portlet.om.IPortletDefinitionId) IPortletDefinitionRegistry(org.apereo.portal.portlet.registry.IPortletDefinitionRegistry) NamedPersonImpl(org.jasig.services.persondir.support.NamedPersonImpl) IPortletEntity(org.apereo.portal.portlet.om.IPortletEntity) IPersonAttributeDao(org.jasig.services.persondir.IPersonAttributeDao) ArrayList(java.util.ArrayList) List(java.util.List) PortletWindow(org.apache.pluto.container.PortletWindow) IPortletWindow(org.apereo.portal.portlet.om.IPortletWindow) HashMap(java.util.HashMap) Map(java.util.Map) IPortletDefinition(org.apereo.portal.portlet.om.IPortletDefinition) Test(org.junit.Test)

Example 3 with NamedPersonImpl

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;
    }
}
Also used : IPersonAttributes(org.jasig.services.persondir.IPersonAttributes) NamedPersonImpl(org.jasig.services.persondir.support.NamedPersonImpl) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

ArrayList (java.util.ArrayList)3 List (java.util.List)3 NamedPersonImpl (org.jasig.services.persondir.support.NamedPersonImpl)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 IPersonAttributes (org.jasig.services.persondir.IPersonAttributes)2 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 Set (java.util.Set)1 PortletWindow (org.apache.pluto.container.PortletWindow)1 PortletAppType (org.apache.pluto.container.om.portlet.impl.PortletAppType)1 UserAttributeType (org.apache.pluto.container.om.portlet.impl.UserAttributeType)1 IPortletDefinition (org.apereo.portal.portlet.om.IPortletDefinition)1 IPortletDefinitionId (org.apereo.portal.portlet.om.IPortletDefinitionId)1 IPortletEntity (org.apereo.portal.portlet.om.IPortletEntity)1 IPortletWindow (org.apereo.portal.portlet.om.IPortletWindow)1 IPortletDefinitionRegistry (org.apereo.portal.portlet.registry.IPortletDefinitionRegistry)1 IPortletWindowRegistry (org.apereo.portal.portlet.registry.IPortletWindowRegistry)1 IPersonAttributeDao (org.jasig.services.persondir.IPersonAttributeDao)1