List of usage examples for javax.naming.directory DirContext REPLACE_ATTRIBUTE
int REPLACE_ATTRIBUTE
To view the source code for javax.naming.directory DirContext REPLACE_ATTRIBUTE.
Click Source Link
From source file:org.easy.ldap.AdminServiceImpl.java
@Override public void updateUser(LdapUser user, Map<RdnType, String> newData) { Preconditions.checkNotNull(newData); Preconditions.checkNotNull(user.getTenantId()); Preconditions.checkNotNull(user.getUserId()); Preconditions.checkArgument(user.getTenantId().trim().length() > 0); Preconditions.checkArgument(user.getUserId().trim().length() > 0); Preconditions.checkArgument(newData.size() > 0); try {/*from ww w . ja va 2 s .com*/ ModificationItem[] modifications = new ModificationItem[newData.size()]; int i = 0; for (RdnType attributeName : newData.keySet()) { /* if (attributeName.equals(RdnType.UID)) throw new RuntimeException("Cannot change uid.");*/ modifications[i] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute(attributeName.toString(), newData.get(attributeName))); i++; } LdapName rootDn = namingFactory.createUsersDn(user.getTenantId()); Rdn userRdn = NamingFactory.createRdn(RdnType.UID, user.getUserId()); LdapName subContextName = NamingFactory.createName(userRdn); ldapDao.updateSubContext(rootDn, subContextName, modifications); } catch (Exception e) { log.error(e); throw new java.lang.RuntimeException(e); } }
From source file:org.easy.ldap.LdapDao.java
public void updateRdn(LdapName rootDn, RdnType type, String rdnValue) { DirContext ctx = null;/*from ww w. ja v a 2 s . c om*/ try { ctx = contextFactory.createContext(rootDn.toString()); ModificationItem[] modifications = new ModificationItem[1]; Attribute attribute = new BasicAttribute(type.toString(), rdnValue); modifications[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, attribute); ctx.modifyAttributes("", modifications); } catch (NamingException e) { throw new RuntimeException(type.toString() + "=" + rdnValue + "," + rootDn.toString(), e); } finally { if (ctx != null) { try { ctx.close(); } catch (NamingException e) { log.debug(e); } } } }
From source file:org.fao.geonet.kernel.security.ldap.LdapUserDetailsManager.java
/** * Changes the password for the current user. The username is obtained from the security * context. <p> If the old password is supplied, the update will be made by rebinding as the * user, thus modifying the password using the user's permissions. If <code>oldPassword</code> * is null, the update will be attempted using a standard read/write context supplied by the * context source. </p>/*from ww w . j a v a2 s. co m*/ * * @param oldPassword the old password * @param newPassword the new value of the password. */ public void changePassword(final String oldPassword, final String newPassword) { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); Assert.notNull(authentication, "No authentication object found in security context. Can't change current user's password!"); String username = authentication.getName(); logger.debug("Changing password for user '" + username); final DistinguishedName dn = usernameMapper.buildDn(username); final ModificationItem[] passwordChange = new ModificationItem[] { new ModificationItem( DirContext.REPLACE_ATTRIBUTE, new BasicAttribute(passwordAttributeName, newPassword)) }; if (oldPassword == null) { template.modifyAttributes(dn, passwordChange); return; } template.executeReadWrite(new ContextExecutor() { public Object executeWithContext(DirContext dirCtx) throws NamingException { LdapContext ctx = (LdapContext) dirCtx; ctx.removeFromEnvironment("com.sun.jndi.ldap.connect.pool"); ctx.addToEnvironment(Context.SECURITY_PRINCIPAL, LdapUtils.getFullDn(dn, ctx).toString()); ctx.addToEnvironment(Context.SECURITY_CREDENTIALS, oldPassword); // TODO: reconnect doesn't appear to actually change the // credentials try { ctx.reconnect(null); } catch (javax.naming.AuthenticationException e) { throw new BadCredentialsException("Authentication for password change failed."); } ctx.modifyAttributes(dn, passwordChange); return null; } }); }
From source file:org.gbif.portal.registration.LDAPUtils.java
/** * Creates a user. String array contains: * 1) first name/* w ww .j a v a2s . co m*/ * 2) surname * 3) email * 4) username * 5) password * * @param userDetails * @return * @throws NamingException */ public boolean createNewUser(UserLogin userLogin) throws NamingException { DirContext ctx = getUserContext(); Attributes attributes = new BasicAttributes(); attributes.put(new BasicAttribute("sn", userLogin.getSurname())); attributes.put(new BasicAttribute("givenName", userLogin.getFirstName())); attributes.put(new BasicAttribute("cn", userLogin.getFirstName() + " " + userLogin.getSurname())); attributes.put(new BasicAttribute("mail", userLogin.getEmail())); if (userLogin.getTelephone() != null) { attributes.put(new BasicAttribute("telephoneNumber", userLogin.getTelephone())); } attributes.put(new BasicAttribute("userPassword", userLogin.getPassword())); attributes.put(new BasicAttribute("objectClass", "top")); attributes.put(new BasicAttribute("objectClass", "person")); attributes.put(new BasicAttribute("objectClass", "organizationalPerson")); attributes.put(new BasicAttribute("objectClass", "inetorgperson")); String contextName = "uid=" + userLogin.getUsername(); String fullContextName = contextName + "," + ctx.getNameInNamespace(); //add the user to ldap ctx.createSubcontext(contextName, attributes); //need to add user to group for (int i = 0; i < userGroups.length; i++) { DirContext groupContext = getGroupContext(); Attributes groupAttributes = groupContext.getAttributes(userGroups[i]); groupAttributes.get("uniqueMember").add(fullContextName); groupContext.modifyAttributes(userGroups[i], DirContext.REPLACE_ATTRIBUTE, groupAttributes); } return true; }
From source file:org.gbif.portal.registration.LDAPUtils.java
/** * Update the details of the supplied user in LDAP. * @param userLogin//from w w w . j av a2 s . co m * @return * @throws NamingException */ public boolean updateUser(UserLogin userLogin) throws NamingException { DirContext ctx = getUserContext(); Attributes attributes = new BasicAttributes(); attributes.put(new BasicAttribute("sn", userLogin.getSurname())); attributes.put(new BasicAttribute("givenName", userLogin.getFirstName())); attributes.put(new BasicAttribute("cn", userLogin.getFirstName() + " " + userLogin.getSurname())); attributes.put(new BasicAttribute("mail", userLogin.getEmail())); if (userLogin.getTelephone() != null) { attributes.put(new BasicAttribute("telephoneNumber", userLogin.getTelephone())); } attributes.put(new BasicAttribute("userPassword", userLogin.getPassword())); ctx.modifyAttributes("uid=" + userLogin.getUsername(), DirContext.REPLACE_ATTRIBUTE, attributes); return true; }
From source file:org.gbif.portal.registration.LDAPUtils.java
/** * Update the password for the supplied user. * @param username//from www . j av a2 s . c o m * @param newPassword * @throws NamingException */ public void updatePassword(String username, String newPassword) throws NamingException { DirContext ctx = getUserContext(); Attributes attributes = new BasicAttributes(); attributes.put(new BasicAttribute("userPassword", newPassword)); ctx.modifyAttributes("uid=" + username, DirContext.REPLACE_ATTRIBUTE, attributes); }
From source file:org.kitodo.production.services.data.LdapServerService.java
/** * Set next free uidNumber.// w w w . ja va 2 s . c o m */ private void setNextUidNumber(LdapServer ldapServer) { Hashtable<String, String> ldapEnvironment = initializeWithLdapConnectionSettings(ldapServer); DirContext ctx; try { ctx = new InitialDirContext(ldapEnvironment); Attributes attrs = ctx.getAttributes(ldapServer.getNextFreeUnixIdPattern()); Attribute la = attrs.get("uidNumber"); String oldValue = (String) la.get(0); int bla = Integer.parseInt(oldValue) + 1; BasicAttribute attrNeu = new BasicAttribute("uidNumber", String.valueOf(bla)); ModificationItem[] mods = new ModificationItem[1]; mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, attrNeu); ctx.modifyAttributes(ldapServer.getNextFreeUnixIdPattern(), mods); ctx.close(); } catch (NamingException e) { logger.error(e.getMessage(), e); } }
From source file:org.kitodo.production.services.data.LdapServerService.java
/** * change password of given user, needs old password for authentication. * * @param user/*from w w w . ja va 2s . co m*/ * User object * @param inNewPassword * String * @return boolean about result of change */ public boolean changeUserPassword(User user, String inNewPassword) throws NoSuchAlgorithmException { JDKMessageDigest.MD4 digester = new JDKMessageDigest.MD4(); PasswordEncryption passwordEncryption = user.getLdapGroup().getLdapServer().getPasswordEncryption(); Hashtable<String, String> env = initializeWithLdapConnectionSettings(user.getLdapGroup().getLdapServer()); if (!user.getLdapGroup().getLdapServer().isReadOnly()) { try { ModificationItem[] mods = new ModificationItem[4]; // encryption of password and Base64-Encoding MessageDigest md = MessageDigest.getInstance(passwordEncryption.getTitle()); md.update(inNewPassword.getBytes(StandardCharsets.UTF_8)); String encryptedPassword = new String(Base64.encodeBase64(md.digest()), StandardCharsets.UTF_8); // change attribute userPassword BasicAttribute userPassword = new BasicAttribute("userPassword", "{" + passwordEncryption + "}" + encryptedPassword); mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, userPassword); // change attribute lanmgrPassword BasicAttribute lanmgrPassword = proceedPassword("sambaLMPassword", inNewPassword, null); mods[1] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, lanmgrPassword); // change attribute ntlmPassword BasicAttribute ntlmPassword = proceedPassword("sambaNTPassword", inNewPassword, digester); mods[2] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, ntlmPassword); BasicAttribute sambaPwdLastSet = new BasicAttribute("sambaPwdLastSet", String.valueOf(System.currentTimeMillis() / 1000L)); mods[3] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, sambaPwdLastSet); DirContext ctx = new InitialDirContext(env); ctx.modifyAttributes(buildUserDN(user), mods); // Close the context when we're done ctx.close(); return true; } catch (NamingException e) { logger.debug("Benutzeranmeldung nicht korrekt oder Passwortnderung nicht mglich", e); return false; } } return false; }
From source file:org.kitodo.services.data.LdapServerService.java
/** * change password of given user, needs old password for authentication. * * @param user// w w w. j a v a2 s . c o m * User object * @param inNewPassword * String * @return boolean about result of change */ public boolean changeUserPassword(User user, String inNewPassword) throws NoSuchAlgorithmException { JDKMessageDigest.MD4 digester = new JDKMessageDigest.MD4(); PasswordEncryption passwordEncryption = user.getLdapGroup().getLdapServer().getPasswordEncryptionEnum(); Hashtable<String, String> env = initializeWithLdapConnectionSettings(user.getLdapGroup().getLdapServer()); if (!user.getLdapGroup().getLdapServer().isReadOnly()) { try { // encryption of password and Base64-Encoding MessageDigest md = MessageDigest.getInstance(passwordEncryption.getTitle()); md.update(inNewPassword.getBytes(StandardCharsets.UTF_8)); String encryptedPassword = new String(Base64.encodeBase64(md.digest()), StandardCharsets.UTF_8); // change attribute userPassword BasicAttribute userPassword = new BasicAttribute("userPassword", "{" + passwordEncryption + "}" + encryptedPassword); // change attribute lanmgrPassword BasicAttribute lanmgrPassword = proceedPassword("sambaLMPassword", inNewPassword, null); // change attribute ntlmPassword BasicAttribute ntlmPassword = proceedPassword("sambaNTPassword", inNewPassword, digester); BasicAttribute sambaPwdLastSet = new BasicAttribute("sambaPwdLastSet", String.valueOf(System.currentTimeMillis() / 1000L)); ModificationItem[] mods = new ModificationItem[4]; mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, userPassword); mods[1] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, lanmgrPassword); mods[2] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, ntlmPassword); mods[3] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, sambaPwdLastSet); DirContext ctx = new InitialDirContext(env); ctx.modifyAttributes(buildUserDN(user), mods); // Close the context when we're done ctx.close(); return true; } catch (NamingException e) { logger.debug("Benutzeranmeldung nicht korrekt oder Passwortnderung nicht mglich", e); return false; } } return false; }
From source file:org.ligoj.app.plugin.id.ldap.dao.UserLdapRepository.java
/** * Replace a value by another one without touching other values. * * @param dn/*from w ww . j a v a2 s .co m*/ * the DN of entry. * @param attribute * The attribute name, single value. * @param value * the new value. */ public void set(final Name dn, final String attribute, final String value) { final ModificationItem[] mods = new ModificationItem[1]; mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute(attribute, value)); template.modifyAttributes(org.springframework.ldap.support.LdapUtils.newLdapName(dn), mods); }