Search in sources :

Example 86 with DirContextOperations

use of org.springframework.ldap.core.DirContextOperations in project perun by CESNET.

the class PerunVOImpl method getVoShortName.

public String getVoShortName(int voId) {
    DirContextOperations voEntry = findById(String.valueOf(voId));
    String[] voShortNameInformation = voEntry.getStringAttributes(PerunAttribute.PerunAttributeNames.ldapAttrOrganization);
    String voShortName = null;
    if (voShortNameInformation == null || voShortNameInformation[0] == null)
        throw new InternalErrorException("There is no shortName in ldap for vo with id=" + voId);
    if (voShortNameInformation.length != 1)
        throw new InternalErrorException("There is not exactly one short name of vo with id=" + voId + " in ldap. Count of shortnames is " + voShortNameInformation.length);
    voShortName = voShortNameInformation[0];
    return voShortName;
}
Also used : DirContextOperations(org.springframework.ldap.core.DirContextOperations) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException)

Example 87 with DirContextOperations

use of org.springframework.ldap.core.DirContextOperations in project perun by CESNET.

the class PerunVOImpl method synchronizeMembers.

@Override
public void synchronizeMembers(Vo vo, List<Member> members) {
    DirContextOperations voEntry = findByDN(buildDN(vo));
    doSynchronizeMembers(voEntry, members);
    ldapTemplate.modifyAttributes(voEntry);
// user attributes are set when synchronizing users
}
Also used : DirContextOperations(org.springframework.ldap.core.DirContextOperations)

Example 88 with DirContextOperations

use of org.springframework.ldap.core.DirContextOperations in project pentaho-platform by pentaho.

the class RolePreprocessingMapperTests method testMapUserFromContext.

@SuppressWarnings("unchecked")
@Test
public void testMapUserFromContext() throws Exception {
    RolePreprocessingMapper mapper = new RolePreprocessingMapper();
    // $NON-NLS-1$
    mapper.setTokenName("cn");
    // $NON-NLS-1$
    mapper.setRolePrefix("");
    // $NON-NLS-1$
    mapper.setRoleAttributes(new String[] { "uniqueMember" });
    // get the user record
    DirContextOperations ctx = // $NON-NLS-1$
    new SpringSecurityLdapTemplate(getContextSource()).retrieveEntry(// $NON-NLS-1$
    "uid=suzy,ou=users", null);
    // get any roles that aren't in the user record
    Set<String> extraRoles = new SpringSecurityLdapTemplate(getContextSource()).searchForSingleAttributeValues("ou=roles", "roleoccupant={0}", new String[] { "uid=suzy,ou=users,dc=pentaho,dc=org", "suzy" }, // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
    "cn");
    List<GrantedAuthority> authorities = new ArrayList<>();
    for (String extraRole : extraRoles) {
        authorities.add(new SimpleGrantedAuthority(extraRole));
    }
    // use the mapper to create a UserDetails instance
    // $NON-NLS-1$
    UserDetails userDetails = mapper.mapUserFromContext(ctx, "suzy", authorities);
    System.out.println(userDetails);
    // this asserts the ordering too; not strictly necessary
    Collection<? extends GrantedAuthority> expectedAuthorities = new ArrayList<GrantedAuthority>() {

        {
            add(new SimpleGrantedAuthority("A"));
            add(new SimpleGrantedAuthority("Authenticated"));
            add(new SimpleGrantedAuthority("is"));
            add(new SimpleGrantedAuthority("cto"));
        }
    };
    Collection<? extends GrantedAuthority> unexpectedAuthorities = userDetails.getAuthorities();
    assertEquals(expectedAuthorities, unexpectedAuthorities);
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) SpringSecurityLdapTemplate(org.springframework.security.ldap.SpringSecurityLdapTemplate) UserDetails(org.springframework.security.core.userdetails.UserDetails) DirContextOperations(org.springframework.ldap.core.DirContextOperations) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) ArrayList(java.util.ArrayList) RolePreprocessingMapper(org.pentaho.platform.plugin.services.security.userrole.ldap.RolePreprocessingMapper) Test(org.junit.Test)

Example 89 with DirContextOperations

use of org.springframework.ldap.core.DirContextOperations in project pentaho-platform by pentaho.

the class PentahoCachingLdapAuthenticator method performOperation.

private DirContextOperations performOperation(Authentication authentication, DelegateOperation operation) {
    DirContextOperations results = null;
    Object fromRegionCache = null;
    String cacheEntry = ROLES_BY_USER + hashUserAndPassword(authentication);
    if (logger.isTraceEnabled()) {
        logger.trace("cacheEntry:" + cacheEntry);
    }
    fromRegionCache = cacheManager.getFromRegionCache(cacheRegionName, cacheEntry);
    if (fromRegionCache instanceof DirContextOperations) {
        if (logger.isDebugEnabled()) {
            logger.debug("Cache Hit for " + authentication.getPrincipal());
        }
        results = (DirContextOperations) fromRegionCache;
    } else {
        if (logger.isDebugEnabled()) {
            logger.debug("Cache miss for " + authentication.getPrincipal());
        }
        results = operation.perform();
        cacheManager.putInRegionCache(cacheRegionName, cacheEntry, results);
    }
    return results;
}
Also used : DirContextOperations(org.springframework.ldap.core.DirContextOperations)

Aggregations

DirContextOperations (org.springframework.ldap.core.DirContextOperations)89 Name (javax.naming.Name)20 Test (org.junit.jupiter.api.Test)13 Test (org.junit.Test)9 UserDetails (org.springframework.security.core.userdetails.UserDetails)9 HashSet (java.util.HashSet)8 DirContextAdapter (org.springframework.ldap.core.DirContextAdapter)8 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)8 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)7 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)7 FilterBasedLdapUserSearch (org.springframework.security.ldap.search.FilterBasedLdapUserSearch)7 LdapConfig (com.thoughtworks.go.config.LdapConfig)6 BaseConfig (com.thoughtworks.go.config.server.security.ldap.BaseConfig)6 BasesConfig (com.thoughtworks.go.config.server.security.ldap.BasesConfig)6 ArrayList (java.util.ArrayList)6 Set (java.util.Set)6 NameNotFoundException (org.springframework.ldap.NameNotFoundException)6 GrantedAuthority (org.springframework.security.core.GrantedAuthority)5 PerunAttribute (cz.metacentrum.perun.ldapc.model.PerunAttribute)4 List (java.util.List)4