Skip navigation
Currently Being Moderated

Access Users in CRX

Sep 8, 2011 9:22 AM

How can I get the list of users in /home/users in CRX? Is this possible from within the Flex application?

 
Replies
  • Currently Being Moderated
    Sep 8, 2011 12:11 PM   in reply to JPatSoco

    Unfortunately, there aren't any Flex APIs available to get at that info. But it's not hard to create yourself an OSGI Service that does get at it, and expose it using Data Services.

     

    Here's some code I happened to have lying around that has the bulk of the necessary bits. I used declarative services with the following manifest and XML config files. I keep meaning to convert this to use the Maven SCR plugin, but haven't gotten around to it yet.

     

    META-INF/MANIFEST.MF

    Manifest-Version: 1.0
    Bundle-ManifestVersion: 2
    Bundle-Name: User Manager Service
    Bundle-SymbolicName: matt.usermanagerservice.impl
    Bundle-Version: 1.0.0
    Import-Package: org.osgi.framework;version="1.3.0",
    org.apache.sling.jcr.api,
    javax.jcr,
    matt.usermanagerservice;version="0.0.1",
    org.apache.jackrabbit.api;version="2.2.8",
    org.apache.jackrabbit.api.security;version="2.2.8",
    org.apache.jackrabbit.api.security.principal;version="2.2.8",
    org.apache.jackrabbit.api.security.user;version="2.2.8"
    Export-Package: matt.usermanagerservice.impl;version="0.0.1"
    Bundle-ClassPath: .
    Service-Component: OSGI-INF/usermanagerservice.xml

     

    OSGI-INF/usermanagerservice.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <components
      xmlns:scr="http://www.osgi.org/xmlns/scr/v1.0.0">
      <scr:component enabled="true" immediate="true"
        name="matt.usermanagerservice">
        <scr:implementation class="matt.usermanagerservice.impl.UserManagerService"/>
        <scr:service servicefactory="false">
          <scr:provide interface="matt.usermanagerservice.UserManagerService"/>
        </scr:service>
        <scr:property name="service.description" value="User Manager Service"/>
        <scr:property name="service.vendor"      value="Matt Butler"/>
        <scr:property name="service.pid"         value="matt.usermanagerservice.impl"/>
        <scr:property name="connectors.remoting.id" value="UserManagerService"/>
        <scr:reference name="repository"
          interface="org.apache.sling.jcr.api.SlingRepository"
          cardinality="1..1" policy="static"
          bind="setRepository"
          unbind="unsetRepository"/>
      </scr:component>
    </components>

     


    UserManagerService.java

    package matt.usermanagerservice.impl;

     

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;

     

    import javax.jcr.AccessDeniedException;
    import javax.jcr.PropertyType;
    import javax.jcr.RepositoryException;
    import javax.jcr.Session;
    import javax.jcr.UnsupportedRepositoryOperationException;
    import javax.jcr.Value;
    import javax.jcr.ValueFormatException;

     

    import org.apache.jackrabbit.api.JackrabbitSession;
    import org.apache.jackrabbit.api.security.user.Authorizable;
    import org.apache.jackrabbit.api.security.user.Group;
    import org.apache.jackrabbit.api.security.user.UserManager;
    import org.apache.sling.jcr.api.SlingRepository;

     


    import matt.usermanagerservice.User;  // A Java bean used to hold User data.. Set it up however you want.

     

    public class UserManagerService  implements matt.usermanagerservice.UserManagerService
    {
        private SlingRepository repository = null;

     

        public UserManagerService()
        {

     

        }

     

        protected void setRepository(SlingRepository repository) {
            this.repository = repository;
        }
       
       
        protected void unsetRepository(SlingRepository repository) {
            this.repository = null;
        }
        // TODO: Look into user org.apache.jackrabbit.api.security.principal.PrincipalManager since it has better searching APIs and an explicit getEveryone() method. Can be got via JRSession.getPrincipleManager()
        public List<User> findUsers(String propertyNameToFind, String propertyValueToFind)  {
            List<User> users = new ArrayList<User>();
            JackrabbitSession session = null;
            try {
                session = getSession();
                Iterator<Authorizable> userIter = session.getUserManager().findAuthorizables(propertyNameToFind, propertyValueToFind, UserManager.SEARCH_TYPE_USER);
                while (userIter.hasNext()) {
                    org.apache.jackrabbit.api.security.user.User jackRabbitUser = (org.apache.jackrabbit.api.security.user.User) userIter.next();
                    matt.usermanagerservice.impl.User user = new matt.usermanagerservice.impl.User();
                    user.setUserID(jackRabbitUser.getID());
                   
                    Map<String, String> properties = new HashMap<String, String>();
                    properties.put("principalName", jackRabbitUser.getPrincipal().getName());
                   
                    populateProperties(jackRabbitUser, ".", properties);
                    populateProperties(jackRabbitUser, "profile", properties);
                   
                    user.setProperties(properties);

     

                   
                    Iterator<Group> memberOfIter = jackRabbitUser.memberOf();
                    List<String> groups = new ArrayList<String>();
                    while (memberOfIter.hasNext()) {
                        Group group = memberOfIter.next();
                        groups.add(group.getID());
                    }
                    user.setGroups(groups);
                    users.add(user);
                }

     

                return users;
               
            } catch (RepositoryException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return null;
            } catch (CantFindSession e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return null;
            } finally {
                if (session != null)
                    session.logout();
            }

     

        }

     


        private void populateProperties(
                org.apache.jackrabbit.api.security.user.User jackRabbitUser,
                String relativePath,
                Map<String,
                String> properties)
                        throws RepositoryException {
           
            Iterator<String> propertyNameIter = jackRabbitUser.getPropertyNames(relativePath);
            while (propertyNameIter.hasNext()) {
                String propertyName = propertyNameIter.next();
                Value propertyValue = jackRabbitUser.getProperty(relativePath+"/"+propertyName)[0];
                if (propertyValue.getType() != PropertyType.BINARY) { // tends to screw things up.. check it out later.
                    try {
                        properties.put(propertyName, propertyValue.getString());
                    } catch (ValueFormatException vfe) {
                        properties.put(propertyName, "Cannot convert to String");
                    }
                }
            }
           
        }
       
        private JackrabbitSession getSession() throws CantFindSession {
            String whatAmIDoing = "";
               if (repository == null) {
                throw new CantFindSession("No SlingRepository bound");
            }
            try {
                Session jcrSession = repository.loginAdministrative(repository.getDefaultWorkspace());
                if ( jcrSession instanceof JackrabbitSession)  {
                    org.apache.jackrabbit.api.JackrabbitSession jackrabbitSession = (JackrabbitSession) jcrSession;
                    return jackrabbitSession;
                } else {
                    throw new CantFindSession("Didn't get a JackrabbitSession from repository.loginAdministrative");
                }
            } catch (RepositoryException e) {
                throw new CantFindSession("Failed calling repository.loginAdministrative", e);
            }
        }
     
    }

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 12, 2011 6:56 AM   in reply to JPatSoco

    Hi

     

    Inside of the Experience Server, you can create Data Services destinations by deploying your code as OSGi bundles. These bundles can export Java Packages - these will then become available for use by the Data Services Message Broker.

     

    For more information on this, see this Re: Access Users in CRX sample for a step by step overview on creating and deploying bundles to CRX which setup Data Services destinations. Once you have done that, you can try out this sample Re: Access Users in CRX which shows how to retrieve data from the Sample DB via a POJO. (The first sample is a pre-requisite, since it shows the overall approach to getting your code into the Experience Server as bundles)

     

    You can see the "Creating Data Services applications for the Experience Server" on this page Re: Access Users in CRX for a full set of samples. Also, when you start the Experience Server, you should see a Getting Started link Re: Access Users in CRX which provides more information about developing for ADEP.

     

    I don't think Matt's sample code would work remotely outside of the CRX container, but can you provide some more information on how you are planning on deploying this?

     

    Re: Access Users in CRX http://help.adobe.com/en_US/enterpriseplatform/10.0/AEPDeveloperGuide/ WSe2a127949a92fa1061747727130427d726e-8000.html

    Re: Access Users in CRX http://help.adobe.com/en_US/enterpriseplatform/10.0/AEPDeveloperGuide/ WSe2a127949a92fa10-322b2f813003fea16b-8000.html

    Re: Access Users in CRX http://help.adobe.com/en_US/enterpriseplatform/10.0/AEPDeveloperGuide/ index.html

    Re: Access Users in CRX http://blogs.adobe.com/eptech/?p=46

     

    Regards,

     

    Ashish Vashisht

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points