Sommaire :


Nous ne détaillons dans ce document que la manipulation en écriture des utilisateurs LDAP, qui est la seule manipulation en écriture implémenté actuellement dans esup-commons.

Un exemple de manipulation en écriture des utilisateur LDAP est présent dans le code de l'application esup-activaccount.

Utilisation basique

L'implémentation utilisée pour manipuler seulement les utilisateurs LDAP est WriteableLdapUserServiceImpl.

La déclaration d'un bean writeableUserService de cette classe ressemblera à :

<bean id="writeableLdapUserService"
	class="org.esupportail.activ.services.ldap.WriteableLdapUserServiceImpl" >
    <description>
	This bean provides LDAP write facilities to all the other beans.
	It must implement interface
	org.esupportail.commons.services.ldap.WriteableLdapUserService.
	This service is used to write in LDAP people branch dn.
    </description>
    <property name="ldapTemplate" ref="writeableLdapTemplate" >
	<description>
	    The LDAP template used to access the LDAP directory.
	    May not be set if you want to bind LDAP directory with user account.
	</description>
    </property>
    <property name="contextSource" ref="writeableContextSource">
	<description>
	    The data source used by the LDAP template. Must be set if ldapTemplate
	    is empty
	</description>
    </property>
    <property name="dnAuth" value="ou=people,dc=domain,dc=edu">
	<description>The DN path to use to connect user</description>
    </property>
    <property name="idAuth" value="uid">
	<description>The LDAP attribute that stores the unique identifier of
	    users binding to the LDAP directory</description>
    </property>
    <property name="dnSubPath" value="ou=people">
	<description>The DN sub path. Used to create or delete entries</description>
    </property>
    <property name="idAttribute" value="uid">
	<description>
	    The LDAP attribute that stores the unique identifier of
	    users in the LDAP directory (optional, "uid" by
	    default).
	</description>
    </property>
    <property name="attributes">
	<description>
	    The LDAP attributes to update.
	</description>
	<list>
	    <value>cn</value>
	    <value>password</value>
	    <value>shadowLastChange</value>
	</list>
    </property>
</bean>

Utilisation du service LDAP depuis du code Java

Exceptions

Les exceptions lancées par les appels aux méthodes de WriteableLdapUserService peuvent lancer les exceptions suivantes :

Manipulation en écriture d'un utilisateur

Pour modifier un utilisateur LDAP à l'aide d'un compte LDAP , il faut appeler le service LDAP de la manière suivante :

writeableLdapUserService.defineAuthenticatedContext("adminDN", "adminPassword");

Ainsi on va se connecter à l'annuaire avec les identifiants : adminDN/adminPassword. Ces identifiants peuvent provenir d'un formulaire de l'applcation créé avec JSF.

/* On recherche le compte à modifier dans l'annuaire LDAP */
LdapUser ldapUser = ldapUserService.getLdapUser("fjammes");

/* On efface les attributs que l'on ne souhaite pas réécrire */
ldapUser.getAttributes().clear();

List<String> listPasswordAttr = new ArrayList<String>();
listPasswordAttr.add(account.getPassword());
ldapUser.getAttributes().put(LdapSchema.getPassword(), listPasswordAttr);

/* Writing of shadowLastChange in LDAP */
List<String> listShadowLastChangeAttr = new ArrayList<String>();
Calendar cal = Calendar.getInstance();
String shadowLastChange = Integer.toString(
	(int) Math.floor(cal.getTimeInMillis()
	/ (1000 * 3600 * 24)));
if (logger.isDebugEnabled()) {
	logger.debug("Writing shadowLastChange in LDAP: " + shadowLastChange);
}

listShadowLastChangeAttr.add(shadowLastChange);
ldapUser.getAttributes().put(LdapSchema.getShadowLastChange(), listShadowLastChangeAttr);

/* Writing of displayName in LDAP */
List<String> listDisplayNameAttr = new ArrayList<String>();
listDisplayNameAttr.add(account.getDisplayName());
ldapUser.getAttributes().put(LdapSchema.getDisplayName(), listDisplayNameAttr);
this.writeableLdapUserService.updateLdapUser(ldapUser);

ldapUser.getAttributes().clear();

/* For security reasons, all passwords are erased */
account.setPassword(null);

logger.info("Activated account [" + account.getId() + "]");

this.writeableLdapUserService.defineAnonymousContext();