List of usage examples for javax.naming.directory BasicAttribute add
public boolean add(Object attrVal)
From source file:org.springframework.ldap.support.LdapUtilsTest.java
@Test public void testIterateAttributeValues() { String expectedAttributeName = "someAttribute"; BasicAttribute expectedAttribute = new BasicAttribute(expectedAttributeName); expectedAttribute.add("value1"); expectedAttribute.add("value2"); LdapUtils.iterateAttributeValues(expectedAttribute, handlerMock); verify(handlerMock).handleAttributeValue(expectedAttributeName, "value1", 0); verify(handlerMock).handleAttributeValue(expectedAttributeName, "value2", 1); }
From source file:com.surevine.ldap2alfresco.ProfileFieldTextConverter.java
/** * Encode some attributes as Attributes. * @param json The JSON object to convert * @param attributes Collection of attributes to insert into *//* w w w .j av a 2 s . c o m*/ public void toAttributes(final Attributes attributes, final JSONObject json) { BasicAttribute attr = new BasicAttribute(attributeLabel); try { if (allowMultiples) { JSONArray jsonValues = json.getJSONArray(jsonLabel); for (int x = 0; x < jsonValues.length(); x++) { String jsonValue = jsonValues.getString(x); if (jsonValue != null && jsonValue.length() > 0) { attr.add(jsonValue); } } } else { String jsonValue = json.getString(jsonLabel); if (jsonValue != null && jsonValue.length() > 0) { attr.add(jsonValue); } } } catch (JSONException e) { logException(Level.ERROR, e); return; } attributes.put(attr); }
From source file:org.springframework.ldap.support.LdapUtilsTest.java
@Test public void testCollectAttributeValues() { String expectedAttributeName = "someAttribute"; BasicAttribute expectedAttribute = new BasicAttribute(expectedAttributeName); expectedAttribute.add("value1"); expectedAttribute.add("value2"); BasicAttributes attributes = new BasicAttributes(); attributes.put(expectedAttribute);//from w w w. j a va2 s. c om LinkedList list = new LinkedList(); LdapUtils.collectAttributeValues(attributes, expectedAttributeName, list); assertThat(list).hasSize(2); assertThat(list.get(0)).isEqualTo("value1"); assertThat(list.get(1)).isEqualTo("value2"); }
From source file:org.archone.ad.domain.LdapActions.java
@RPCAction(name = "group.add", required = { "groupId" }) @SecuredMethod(constraints = "administrator.by_domain") public HashMap<String, Object> addGroup(OperationContext opContext) throws NamingException { String groupId = (String) opContext.getParams().get("groupId"); GroupDn groupDn = nameHelper.newGroupDnFromId(groupId); DomainDn domainDn = nameHelper.newDomainDnFromDomain(groupDn.getDomain()); DirContextAdapter userDirContext = (DirContextAdapter) SecurityUtils.getSubject().getPrincipal(); Attributes attrs = new BasicAttributes(); BasicAttribute ocattr = new BasicAttribute("objectclass"); for (String objectClassName : ldapConfiguration.getGroupObjectClassList()) { ocattr.add(objectClassName); }// www . ja v a 2s.c o m attrs.put(ocattr); String description = (String) opContext.getParams().get("description"); if (description != null && !description.isEmpty()) { BasicAttribute descattr = new BasicAttribute("description"); descattr.add(description); attrs.put(descattr); } userDirContext.bind(groupDn, null, attrs); HashMap<String, Object> response = new HashMap<String, Object>(); response.put("success", true); return response; }
From source file:org.apache.archiva.redback.common.ldap.role.DefaultLdapRoleMapper.java
public boolean saveRole(String roleName, DirContext context) throws MappingException { if (hasRole(context, roleName)) { return true; }//from www . java 2 s . c o m String groupName = findGroupName(roleName); if (groupName == null) { if (this.useDefaultRoleName) { groupName = roleName; } else { log.warn("skip group creation as no mapping for roleName:'{}'", roleName); return false; } } List<String> allGroups = getAllGroups(context); if (allGroups.contains(groupName)) { log.info("group {} already exists for role.", groupName, roleName); return false; } Attributes attributes = new BasicAttributes(true); BasicAttribute objectClass = new BasicAttribute("objectClass"); objectClass.add("top"); objectClass.add("groupOfUniqueNames"); attributes.put(objectClass); attributes.put("cn", groupName); // attribute mandatory when created a group so add admin as default member BasicAttribute basicAttribute = new BasicAttribute(getLdapGroupMember()); basicAttribute.add(this.userIdAttribute + "=admin," + getBaseDn()); attributes.put(basicAttribute); try { String dn = "cn=" + groupName + "," + this.groupsDn; context.createSubcontext(dn, attributes); log.info("created group with dn:'{}", dn); return true; } catch (NameAlreadyBoundException e) { log.info("skip group '{}' creation as already exists", groupName); return true; } catch (LdapException e) { throw new MappingException(e.getMessage(), e); } catch (NamingException e) { throw new MappingException(e.getMessage(), e); } }
From source file:com.surevine.ldap2alfresco.ProfileFieldTelephoneConverter.java
/** * Encode some attributes as Attributes. * @param json The JSON object to convert * @param attributes Collection of attributes to insert into *///from ww w . ja v a2 s.co m public void toAttributes(final Attributes attributes, final JSONObject json) { BasicAttribute attr = new BasicAttribute(attributeLabel); try { if (allowMultiples) { JSONArray jsonNumbers = json.getJSONArray(jsonLabel); for (int x = 0; x < jsonNumbers.length(); x++) { JSONObject jsonNumber = jsonNumbers.getJSONObject(x); String number = encodePhoneNumber(jsonNumber); if (number != null && number.length() > 0) { attr.add(number); } } } else { JSONObject jsonNumber = json.getJSONObject(jsonLabel); String number = encodePhoneNumber(jsonNumber); if (number != null && number.length() > 0) { attr.add(number); } } } catch (JSONException e) { logException(Level.ERROR, e); return; } attributes.put(attr); }
From source file:org.archone.ad.domain.LdapActions.java
@RPCAction(name = "user.add", required = { "userId" }) @SecuredMethod(constraints = "administrator.by_domain") public HashMap<String, Object> addUser(OperationContext opContext) throws NamingException { String userId = (String) opContext.getParams().get("userId"); UserDn userDn = nameHelper.newUserDnFromId(userId); DomainDn domainDn = nameHelper.newDomainDnFromDomain(userDn.getDomain()); DirContextAdapter userDirContext = (DirContextAdapter) SecurityUtils.getSubject().getPrincipal(); Attributes attrs = new BasicAttributes(); BasicAttribute ocattr = new BasicAttribute("objectclass"); for (String objectClassName : ldapConfiguration.getUserObjectClassList()) { ocattr.add(objectClassName); }/*from w ww. j a v a 2 s . co m*/ attrs.put(ocattr); for (DisplayAttribute displayAttribute : displayAttributeHelper.getApiNameIndexedAttrDef().values()) { Object attrValue = opContext.getParams().get(displayAttribute.getApiName()); if (attrValue != null) { BasicAttribute attr = new BasicAttribute(displayAttribute.getLdapName()); if (attrValue instanceof List) { for (Object attrOneValue : (List) attrValue) { attr.add(attrOneValue); } } else { attr.add(attrValue); } attrs.put(attr); } else if (displayAttribute.isMustHave()) { throw new RuntimeException(displayAttribute.getApiName() + " is required!"); } } userDirContext.bind(userDn, null, attrs); HashMap<String, Object> response = new HashMap<String, Object>(); response.put("success", true); return response; }
From source file:org.apache.archiva.redback.common.ldap.role.DefaultLdapRoleMapper.java
public boolean removeUserRole(String roleName, String username, DirContext context) throws MappingException { String groupName = findGroupName(roleName); if (groupName == null) { log.warn("no group found for role '{}", roleName); return false; }//from w w w .ja v a 2 s . c o m NamingEnumeration<SearchResult> namingEnumeration = null; try { SearchControls searchControls = new SearchControls(); searchControls.setDerefLinkFlag(true); searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE); String filter = "objectClass=" + getLdapGroupClass(); namingEnumeration = context.search("cn=" + groupName + "," + getGroupsDn(), filter, searchControls); while (namingEnumeration.hasMore()) { SearchResult searchResult = namingEnumeration.next(); Attribute attribute = searchResult.getAttributes().get(getLdapGroupMember()); if (attribute != null) { BasicAttribute basicAttribute = new BasicAttribute(getLdapGroupMember()); basicAttribute.add(this.userIdAttribute + "=" + username + "," + getGroupsDn()); context.modifyAttributes("cn=" + groupName + "," + getGroupsDn(), new ModificationItem[] { new ModificationItem(DirContext.REMOVE_ATTRIBUTE, basicAttribute) }); } return true; } return false; } catch (LdapException e) { throw new MappingException(e.getMessage(), e); } catch (NamingException e) { throw new MappingException(e.getMessage(), e); } finally { if (namingEnumeration != null) { try { namingEnumeration.close(); } catch (NamingException e) { log.warn("failed to close search results", e); } } } }
From source file:org.apache.archiva.redback.common.ldap.role.DefaultLdapRoleMapper.java
public boolean saveUserRole(String roleName, String username, DirContext context) throws MappingException { String groupName = findGroupName(roleName); if (groupName == null) { log.warn("no group found for role '{}", roleName); groupName = roleName;/*from w w w . java 2s .co m*/ } NamingEnumeration<SearchResult> namingEnumeration = null; try { SearchControls searchControls = new SearchControls(); searchControls.setDerefLinkFlag(true); searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE); String filter = "objectClass=" + getLdapGroupClass(); namingEnumeration = context.search("cn=" + groupName + "," + getGroupsDn(), filter, searchControls); while (namingEnumeration.hasMore()) { SearchResult searchResult = namingEnumeration.next(); Attribute attribute = searchResult.getAttributes().get(getLdapGroupMember()); if (attribute == null) { BasicAttribute basicAttribute = new BasicAttribute(getLdapGroupMember()); basicAttribute.add(this.userIdAttribute + "=" + username + "," + getBaseDn()); context.modifyAttributes("cn=" + groupName + "," + getGroupsDn(), new ModificationItem[] { new ModificationItem(DirContext.ADD_ATTRIBUTE, basicAttribute) }); } else { attribute.add(this.userIdAttribute + "=" + username + "," + getBaseDn()); context.modifyAttributes("cn=" + groupName + "," + getGroupsDn(), new ModificationItem[] { new ModificationItem(DirContext.REPLACE_ATTRIBUTE, attribute) }); } return true; } return false; } catch (LdapException e) { throw new MappingException(e.getMessage(), e); } catch (NamingException e) { throw new MappingException(e.getMessage(), e); } finally { if (namingEnumeration != null) { try { namingEnumeration.close(); } catch (NamingException e) { log.warn("failed to close search results", e); } } } }
From source file:org.wso2.carbon.directory.server.manager.internal.LDAPServerStoreManager.java
private void constructBasicAttributes(BasicAttributes basicAttributes, String id, String principleName, Object credential, String commonName, String surName) throws DirectoryServerManagerException { // set the objectClass type for schema BasicAttribute objectClass = new BasicAttribute(LDAPServerManagerConstants.LDAP_OBJECT_CLASS); objectClass.add(LDAPServerManagerConstants.LDAP_INTET_ORG_PERSON); objectClass.add(LDAPServerManagerConstants.LDAP_ORG_PERSON); objectClass.add(LDAPServerManagerConstants.LDAP_PERSON); objectClass.add(LDAPServerManagerConstants.LDAP_TOP); // Add Kerberos specific object classes objectClass.add(LDAPServerManagerConstants.LDAP_KRB5_PRINCIPLE); objectClass.add(LDAPServerManagerConstants.LDAP_KRB5_KDC); objectClass.add(LDAPServerManagerConstants.LDAP_SUB_SCHEMA); basicAttributes.put(objectClass);//from w ww. j av a2s.c om BasicAttribute uid = new BasicAttribute(LDAPServerManagerConstants.LDAP_UID); uid.add(id); basicAttributes.put(uid); String principal = getFullyQualifiedPrincipalName(principleName); BasicAttribute principalAttribute = new BasicAttribute( LDAPServerManagerConstants.KRB5_PRINCIPAL_NAME_ATTRIBUTE); principalAttribute.add(principal); basicAttributes.put(principalAttribute); BasicAttribute versionNumberAttribute = new BasicAttribute( LDAPServerManagerConstants.KRB5_KEY_VERSION_NUMBER_ATTRIBUTE); versionNumberAttribute.add("0"); basicAttributes.put(versionNumberAttribute); BasicAttribute userPassword = new BasicAttribute(LDAPServerManagerConstants.LDAP_PASSWORD); //Since we are using the KDC, we will always use plain text password. //KDC does not support other types of passwords String password = getPasswordToStore((String) credential, LDAPServerManagerConstants.PASSWORD_HASH_METHOD_PLAIN_TEXT); userPassword.add(password.getBytes()); basicAttributes.put(userPassword); if (commonName == null || commonName.isEmpty()) { commonName = principleName + " Service"; } BasicAttribute cn = new BasicAttribute(LDAPServerManagerConstants.LDAP_COMMON_NAME); cn.add(commonName); basicAttributes.put(cn); BasicAttribute sn = new BasicAttribute(LDAPServerManagerConstants.SERVER_PRINCIPAL_ATTRIBUTE_NAME); sn.add(surName); basicAttributes.put(sn); }