Tribune

Home » Liferay Portal » English » 6. Portal Framework

Vista Combinata Vista Piatta Vista ad Albero
Discussioni [ Precedente | Successivo ]
toggle
Udaya Ramakrishnan
Getting Ldap Users using Query
8 novembre 2011 19.56
Risposta

Udaya Ramakrishnan

Punteggio: Junior Member

Messaggi: 67

Data di Iscrizione: 6 settembre 2011

Messaggi recenti

hi all,

i have to delete user from Liferay if the user gets deleted from LDAP.
As there is no sync for deletion between Liferay and LDAP, i found in many forums that i have to write a monthly scheduler to get users from Ldap and from Liferay and then compares this two list and delete users using Liferay API.
I got this from this forum
can anybody tell me how to get the Ldap users?

Thanks
Udaya.R
Amit Doshi
RE: Getting Ldap Users using Query
4 aprile 2012 2.08
Risposta

Amit Doshi

Punteggio: Liferay Master

Messaggi: 549

Data di Iscrizione: 29 dicembre 2010

Messaggi recenti

Hi Udaya,

I had done the same thing that you required with liferay 6.0 sp1.

Please check the below method.

 1
 2
 3public List<User> getAllLDAPPersons()
 4    {
 5        // check the cache first... if present return from cache
 6        final String searchPattern = "(&(objectclass=Person))"; // made dynamic search query according to your requirement to get users from the ldap
 7        final List<User> users = new ArrayList<User>();
 8        NamingEnumeration<SearchResult> results = null;
 9
10        try
11        {
12            final SearchControls controls = new SearchControls();
13            controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
14            // context was opened in constructor so we don't alway re-open
15            results = this.ctx.search("", searchPattern, controls);
16
17            while (results.hasMore())
18            {
19                final SearchResult searchResult = results.next();
20                final Attributes attributes = searchResult.getAttributes();
21                final Attribute cnAttr = attributes.get("cn");
22                User user = new User(); // It was my own pojo class to set and get user field from LDAP.
23                if (cnAttr != null)
24                {
25                    final String cn = (String) cnAttr.get();
26                    user.setCn(cn);
27                }
28                users.add(user);
29            }
30
31        } catch (final NameNotFoundException e)
32        {
33            log.error("name not found", e);
34        } catch (final NamingException e)
35        {
36            log.error("naming issue", e);
37        } finally
38        {
39            
40                    results.close();
41               
42        }
43        return users;
44
45    }


Hope it helps.

Thanks & Regards,
Amit Doshi