Search in sources :

Example 26 with BasicAttribute

use of javax.naming.directory.BasicAttribute in project hadoop by apache.

the class TestLdapGroupsMappingBase method setupMocksBase.

@Before
public void setupMocksBase() throws NamingException {
    MockitoAnnotations.initMocks(this);
    DirContext ctx = getContext();
    doReturn(ctx).when(groupsMapping).getDirContext();
    when(ctx.search(Mockito.anyString(), Mockito.anyString(), Mockito.any(Object[].class), Mockito.any(SearchControls.class))).thenReturn(userNames);
    // We only ever call hasMoreElements once for the user NamingEnum, so
    // we can just have one return value
    when(userNames.hasMoreElements()).thenReturn(true);
    SearchResult groupSearchResult = mock(SearchResult.class);
    // We're going to have to define the loop here. We want two iterations,
    // to get both the groups
    when(groupNames.hasMoreElements()).thenReturn(true, true, false);
    when(groupNames.nextElement()).thenReturn(groupSearchResult);
    // Define the attribute for the name of the first group
    Attribute group1Attr = new BasicAttribute("cn");
    group1Attr.add(testGroups[0]);
    Attributes group1Attrs = new BasicAttributes();
    group1Attrs.put(group1Attr);
    // Define the attribute for the name of the second group
    Attribute group2Attr = new BasicAttribute("cn");
    group2Attr.add(testGroups[1]);
    Attributes group2Attrs = new BasicAttributes();
    group2Attrs.put(group2Attr);
    // This search result gets reused, so return group1, then group2
    when(groupSearchResult.getAttributes()).thenReturn(group1Attrs, group2Attrs);
    when(getUserNames().nextElement()).thenReturn(getUserSearchResult());
    when(getUserSearchResult().getAttributes()).thenReturn(getAttributes());
    // Define results for groups 1 level up
    SearchResult parentGroupResult = mock(SearchResult.class);
    // only one parent group
    when(parentGroupNames.hasMoreElements()).thenReturn(true, false);
    when(parentGroupNames.nextElement()).thenReturn(parentGroupResult);
    // Define the attribute for the parent group
    Attribute parentGroup1Attr = new BasicAttribute("cn");
    parentGroup1Attr.add(testParentGroups[2]);
    Attributes parentGroup1Attrs = new BasicAttributes();
    parentGroup1Attrs.put(parentGroup1Attr);
    // attach the attributes to the result
    when(parentGroupResult.getAttributes()).thenReturn(parentGroup1Attrs);
    when(parentGroupResult.getNameInNamespace()).thenReturn("CN=some_group,DC=test,DC=com");
}
Also used : BasicAttribute(javax.naming.directory.BasicAttribute) BasicAttributes(javax.naming.directory.BasicAttributes) BasicAttribute(javax.naming.directory.BasicAttribute) Attribute(javax.naming.directory.Attribute) BasicAttributes(javax.naming.directory.BasicAttributes) Attributes(javax.naming.directory.Attributes) SearchControls(javax.naming.directory.SearchControls) SearchResult(javax.naming.directory.SearchResult) DirContext(javax.naming.directory.DirContext) Before(org.junit.Before)

Example 27 with BasicAttribute

use of javax.naming.directory.BasicAttribute in project spring-security by spring-projects.

the class LdapUserDetailsMapperTests method testNonRetrievedRoleAttributeIsIgnored.

/**
	 * SEC-303. Non-retrieved role attribute causes NullPointerException
	 */
@Test
public void testNonRetrievedRoleAttributeIsIgnored() throws Exception {
    LdapUserDetailsMapper mapper = new LdapUserDetailsMapper();
    mapper.setRoleAttributes(new String[] { "userRole", "nonRetrievedAttribute" });
    BasicAttributes attrs = new BasicAttributes();
    attrs.put(new BasicAttribute("userRole", "x"));
    DirContextAdapter ctx = new DirContextAdapter(attrs, new DistinguishedName("cn=someName"));
    ctx.setAttributeValue("uid", "ani");
    LdapUserDetailsImpl user = (LdapUserDetailsImpl) mapper.mapUserFromContext(ctx, "ani", AuthorityUtils.NO_AUTHORITIES);
    assertThat(user.getAuthorities()).hasSize(1);
    assertThat(AuthorityUtils.authorityListToSet(user.getAuthorities())).contains("ROLE_X");
}
Also used : BasicAttribute(javax.naming.directory.BasicAttribute) BasicAttributes(javax.naming.directory.BasicAttributes) DistinguishedName(org.springframework.ldap.core.DistinguishedName) DirContextAdapter(org.springframework.ldap.core.DirContextAdapter) Test(org.junit.Test)

Example 28 with BasicAttribute

use of javax.naming.directory.BasicAttribute in project spring-security by spring-projects.

the class LdapUserDetailsMapperTests method testPasswordAttributeIsMappedCorrectly.

@Test
public void testPasswordAttributeIsMappedCorrectly() throws Exception {
    LdapUserDetailsMapper mapper = new LdapUserDetailsMapper();
    mapper.setPasswordAttributeName("myappsPassword");
    BasicAttributes attrs = new BasicAttributes();
    attrs.put(new BasicAttribute("myappsPassword", "mypassword".getBytes()));
    DirContextAdapter ctx = new DirContextAdapter(attrs, new DistinguishedName("cn=someName"));
    ctx.setAttributeValue("uid", "ani");
    LdapUserDetails user = (LdapUserDetailsImpl) mapper.mapUserFromContext(ctx, "ani", AuthorityUtils.NO_AUTHORITIES);
    assertThat(user.getPassword()).isEqualTo("mypassword");
}
Also used : BasicAttribute(javax.naming.directory.BasicAttribute) BasicAttributes(javax.naming.directory.BasicAttributes) DistinguishedName(org.springframework.ldap.core.DistinguishedName) DirContextAdapter(org.springframework.ldap.core.DirContextAdapter) Test(org.junit.Test)

Example 29 with BasicAttribute

use of javax.naming.directory.BasicAttribute in project perun by CESNET.

the class LdapConnectorImpl method createUser.

//-----------------------USER MODIFICATION METHODS----------------------------
public void createUser(User user) throws InternalErrorException {
    // Create a set of attributes
    Attributes attributes = new BasicAttributes();
    // Create the objectclass to add
    Attribute objClasses = new BasicAttribute("objectClass");
    objClasses.add("top");
    objClasses.add("person");
    objClasses.add("organizationalPerson");
    objClasses.add("inetOrgPerson");
    objClasses.add("perunUser");
    objClasses.add("tenOperEntry");
    objClasses.add("inetUser");
    String firstName = user.getFirstName();
    String lastName = user.getLastName();
    if (firstName == null)
        firstName = "";
    if (lastName == null || lastName.isEmpty())
        lastName = "N/A";
    // Add attributes
    attributes.put(objClasses);
    attributes.put("entryStatus", "active");
    attributes.put("sn", lastName);
    attributes.put("cn", firstName + " " + lastName);
    if (!firstName.isEmpty())
        attributes.put("givenName", firstName);
    attributes.put("perunUserId", String.valueOf(user.getId()));
    if (user.isServiceUser())
        attributes.put("isServiceUser", "1");
    else
        attributes.put("isServiceUser", "0");
    if (user.isSponsoredUser())
        attributes.put("isSponsoredUser", "1");
    else
        attributes.put("isSponsoredUser", "0");
    // Create the entry
    try {
        ldapTemplate.bind(getUserDN(String.valueOf(user.getId())), null, attributes);
        log.debug("New entry created in LDAP: User {} in Group with Id=" + user.getId() + ".", user);
    } catch (NameNotFoundException e) {
        throw new InternalErrorException(e);
    }
}
Also used : BasicAttribute(javax.naming.directory.BasicAttribute) BasicAttributes(javax.naming.directory.BasicAttributes) BasicAttribute(javax.naming.directory.BasicAttribute) Attribute(javax.naming.directory.Attribute) NameNotFoundException(org.springframework.ldap.NameNotFoundException) BasicAttributes(javax.naming.directory.BasicAttributes) Attributes(javax.naming.directory.Attributes) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException)

Example 30 with BasicAttribute

use of javax.naming.directory.BasicAttribute in project perun by CESNET.

the class LdapConnectorImpl method removeGroup.

public void removeGroup(Group group) throws InternalErrorException {
    List<String> uniqueUsersIds = new ArrayList<String>();
    uniqueUsersIds = this.getAllUniqueMembersInGroup(group.getId(), group.getVoId());
    for (String s : uniqueUsersIds) {
        Attribute memberOf = new BasicAttribute("memberOf", "perunGroupId=" + group.getId() + ",perunVoId=" + group.getVoId() + "," + ldapProperties.getLdapBase());
        ModificationItem memberOfItem = new ModificationItem(DirContext.REMOVE_ATTRIBUTE, memberOf);
        this.updateUserWithUserId(s, new ModificationItem[] { memberOfItem });
    }
    try {
        ldapTemplate.unbind(getGroupDN(String.valueOf(group.getVoId()), String.valueOf(group.getId())));
        log.debug("Entry deleted from LDAP: Group {} from Vo with ID=" + group.getVoId() + ".", group);
    } catch (NameNotFoundException e) {
        throw new InternalErrorException(e);
    }
}
Also used : BasicAttribute(javax.naming.directory.BasicAttribute) ModificationItem(javax.naming.directory.ModificationItem) BasicAttribute(javax.naming.directory.BasicAttribute) Attribute(javax.naming.directory.Attribute) NameNotFoundException(org.springframework.ldap.NameNotFoundException) ArrayList(java.util.ArrayList) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException)

Aggregations

BasicAttribute (javax.naming.directory.BasicAttribute)44 Attribute (javax.naming.directory.Attribute)28 BasicAttributes (javax.naming.directory.BasicAttributes)25 Attributes (javax.naming.directory.Attributes)17 ModificationItem (javax.naming.directory.ModificationItem)17 HashSet (java.util.HashSet)14 File (java.io.File)7 Set (java.util.Set)7 MutablePartitionConfiguration (org.apache.directory.server.core.configuration.MutablePartitionConfiguration)7 AbstractBootstrapSchema (org.apache.directory.server.core.schema.bootstrap.AbstractBootstrapSchema)7 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)5 IOException (java.io.IOException)5 InputStream (java.io.InputStream)5 ArrayList (java.util.ArrayList)5 DirContext (javax.naming.directory.DirContext)5 Test (org.junit.Test)5 PrivkeySchema (org.nhindirect.ldap.PrivkeySchema)5 Collections (java.util.Collections)4 Date (java.util.Date)3 Enumeration (java.util.Enumeration)3