Java Code Examples for org.springframework.ldap.core.DirContextOperations#getObjectAttribute()

The following examples show how to use org.springframework.ldap.core.DirContextOperations#getObjectAttribute() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CustomUserDetailsContextMapper.java    From airsonic-advanced with GNU General Public License v3.0 4 votes vote down vote up
@Override
public UserDetails mapUserFromContext(DirContextOperations ctx, String username,
                                      Collection<? extends GrantedAuthority> authorities) {
    String dn = ctx.getNameInNamespace();

    LOG.debug("Mapping user details from context with DN: " + dn);

    // User must be defined in Airsonic, unless auto-shadowing is enabled.
    User user = securityService.getUserByName(username, false);
    if (user == null && !settingsService.isLdapAutoShadowing()) {
        throw new BadCredentialsException("User does not exist.");
    }

    if (user == null) {
        User newUser = new User(username, null, true, 0L, 0L, 0L, Set.of(Role.STREAM, Role.SETTINGS));
        securityService.createUser(newUser, "", "Autogenerated for new LDAP user");
        LOG.info("Created local user '{}' for DN {}", username, dn);
        user = securityService.getUserByName(username, false);
    }

    // LDAP authentication must be enabled for the given user.
    if (!user.isLdapAuthenticated()) {
        throw new BadCredentialsException("LDAP authentication disabled for user.");
    }

    LdapUserDetailsImpl.Essence essence = new LdapUserDetailsImpl.Essence();
    essence.setDn(dn);

    Object passwordValue = ctx.getObjectAttribute(passwordAttributeName);

    if (passwordValue != null) {
        essence.setPassword(mapPassword(passwordValue));
    }

    essence.setUsername(user.getUsername());

    // Add the supplied authorities
    for (GrantedAuthority authority : securityService.getGrantedAuthorities(user)) {
        essence.addAuthority(authority);
    }

    // Check for PPolicy data

    PasswordPolicyResponseControl ppolicy = (PasswordPolicyResponseControl) ctx
            .getObjectAttribute(PasswordPolicyControl.OID);

    if (ppolicy != null) {
        essence.setTimeBeforeExpiration(ppolicy.getTimeBeforeExpiration());
        essence.setGraceLoginsRemaining(ppolicy.getGraceLoginsRemaining());
    }

    return essence.createUserDetails();

}
 
Example 2
Source File: CustomUserDetailsContextMapper.java    From airsonic with GNU General Public License v3.0 4 votes vote down vote up
public UserDetails mapUserFromContext(DirContextOperations ctx, String username,
                                      Collection<? extends GrantedAuthority> authorities) {
    String dn = ctx.getNameInNamespace();

    LOG.debug("Mapping user details from context with DN: " + dn);

    // User must be defined in Airsonic, unless auto-shadowing is enabled.
    User user = securityService.getUserByName(username, false);
    if (user == null && !settingsService.isLdapAutoShadowing()) {
        throw new BadCredentialsException("User does not exist.");
    }

    if (user == null) {
        User newUser = new User(username, "", null, true, 0L, 0L, 0L);
        newUser.setStreamRole(true);
        newUser.setSettingsRole(true);
        securityService.createUser(newUser);
        LOG.info("Created local user '" + username + "' for DN " + dn);
        user = securityService.getUserByName(username, false);
    }

    // LDAP authentication must be enabled for the given user.
    if (!user.isLdapAuthenticated()) {
        throw new BadCredentialsException("LDAP authentication disabled for user.");
    }

    LdapUserDetailsImpl.Essence essence = new LdapUserDetailsImpl.Essence();
    essence.setDn(dn);

    Object passwordValue = ctx.getObjectAttribute(passwordAttributeName);

    if (passwordValue != null) {
        essence.setPassword(mapPassword(passwordValue));
    }

    essence.setUsername(user.getUsername());

    // Add the supplied authorities
    for (GrantedAuthority authority : securityService.getGrantedAuthorities(user.getUsername())) {
        essence.addAuthority(authority);
    }

    // Check for PPolicy data

    PasswordPolicyResponseControl ppolicy = (PasswordPolicyResponseControl) ctx
            .getObjectAttribute(PasswordPolicyControl.OID);

    if (ppolicy != null) {
        essence.setTimeBeforeExpiration(ppolicy.getTimeBeforeExpiration());
        essence.setGraceLoginsRemaining(ppolicy.getGraceLoginsRemaining());
    }

    return essence.createUserDetails();

}