cancel
Showing results for 
Search instead for 
Did you mean: 

get user groups

luisg
Champ in-the-making
Champ in-the-making
Hi there.

I'm developing an smartgwt (Java) application that communicates with Alfresco to store information.
I'm using Alfresco 3.2 SDK, and now I'm trying to get all the groups that a users belongs to (the user that login in my application)
Is possible to get those groups?
Can any of you help me?

Thanks

Luis
6 REPLIES 6

mrogers
Star Contributor
Star Contributor
Yes the Java Authority service makes this information available.

You can also get it from Java Script via.
person.getContainerGroups(ScriptNode person)

luisg
Champ in-the-making
Champ in-the-making
Hi, and thanks for your reply.

I'm trying to use Java Authority Service with no success. I'm a little lost…

I have some code right know… I'm testing if the user exists, and that code works fine:


protected static void groups(String username) throws Exception{

      administrationService = WebServiceFactory.getAdministrationService();
      
      int batchSize = 5;
      QueryConfiguration queryCfg = new QueryConfiguration();
      queryCfg.setFetchSize(batchSize);
      WebServiceFactory.getAdministrationService().setHeader(
            new RepositoryServiceLocator().getServiceName().getNamespaceURI(),
            "QueryHeader",
            queryCfg);

      // check the user
      boolean existUser=false;
      try {
         // test before the user
         UserDetails userDetails =  administrationService.getUser(username);
         existUser=true;
      } catch (Exception e){
         existUser=false;
         e.printStackTrace();
      }

      if (existUser) {System.out.println("the user "+username+" exists");}
      else{System.out.println("the user "+username+" DO NOT exists");}

               /*TODO: get all the groups which the user belongs to*/

}

In 'TODO' part I need to write the appropriate code to get all the groups which the user belongs to… I have had no success.

I saw, in the forum, that we need to do something like:


AuthorityService authorityService=serviceRegistry.getAuthorityService();
Set<String> auths = authorityService.getAuthoritiesForUser(username);

The thing I dont understand is how to get an instance of 'serviceRegistry'.
I'm a little lost here… Can someone help?

Thanks

Luis

luisg
Champ in-the-making
Champ in-the-making
Hi again…

I had take a look at http://wiki.alfresco.com/wiki/Java_Foundation_API. After that I write the following code:


ApplicationContext appContext = new ClassPathXmlApplicationContext("alfresco/application-context.xml");
ServiceRegistry serviceRegistry = (ServiceRegistry)appContext.getBean(ServiceRegistry.SERVICE_REGISTRY);

AuthorityService authorityService = serviceRegistry.getAuthorityService();
Set<String> auths = authorityService.getAuthoritiesForUser(username);
Iterator<String> authIt = auths.iterator();
while (authIt.hasNext()){
           System.out.println("- " + authIt.next());
}

But when I run this code (as a java application in Eclipse) I get the following error:


Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'avmRemoteService' defined in class path resource [alfresco/remote-services-context.xml]: Invocation of init method failed; nested exception is java.rmi.server.ExportException: Port already in use: 50501; nested exception is:
   java.net.BindException: Address already in use
Caused by: java.rmi.server.ExportException: Port already in use: 50501; nested exception is:
   java.net.BindException: Address already in use
   at sun.rmi.transport.tcp.TCPTransport.listen(TCPTransport.java:328)
   at sun.rmi.transport.tcp.TCPTransport.exportObject(TCPTransport.java:236)
   at sun.rmi.transport.tcp.TCPEndpoint.exportObject(TCPEndpoint.java:411)
   at sun.rmi.transport.LiveRef.exportObject(LiveRef.java:147)
   at sun.rmi.server.UnicastServerRef.exportObject(UnicastServerRef.java:207)
   at java.rmi.server.UnicastRemoteObject.exportObject(UnicastRemoteObject.java:310)
   at java.rmi.server.UnicastRemoteObject.exportObject(UnicastRemoteObject.java:252)
   at org.springframework.remoting.rmi.RmiServiceExporter.prepare(RmiServiceExporter.java:280)
   at org.springframework.remoting.rmi.RmiServiceExporter.afterPropertiesSet(RmiServiceExporter.java:227)
   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1203)
   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1172)
   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:427)
   at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:249)
   at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:155)
   at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:246)
   at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:160)
   at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:291)
   at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:352)
   at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:122)
   at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:66)
   at pt.moredata.Grupos.groups(Grupos.java:100)
   at pt.moredata.Grupos.main(Grupos.java:41)
Caused by: java.net.BindException: Address already in use
   at java.net.PlainSocketImpl.socketBind(Native Method)
   at java.net.AbstractPlainSocketImpl.bind(AbstractPlainSocketImpl.java:336)
   at java.net.ServerSocket.bind(ServerSocket.java:336)
   at java.net.ServerSocket.<init>(ServerSocket.java:202)
   at java.net.ServerSocket.<init>(ServerSocket.java:114)
   at sun.rmi.transport.proxy.RMIDirectSocketFactory.createServerSocket(RMIDirectSocketFactory.java:45)
   at sun.rmi.transport.proxy.RMIMasterSocketFactory.createServerSocket(RMIMasterSocketFactory.java:351)
   at sun.rmi.transport.tcp.TCPEndpoint.newServerSocket(TCPEndpoint.java:667)
   at sun.rmi.transport.tcp.TCPTransport.listen(TCPTransport.java:317)

This is caused because Alfresco is running and, as you know, uses the ports 50500 to 50507.
This is something I can't understand, cause to test my code I need my Alfresco running.

Can any of you can tell me what I'm doing wrong?

Thanks

Luis

mrogers
Star Contributor
Star Contributor
You need to think through the environment where your code is running.

Your code is starting Alfresco as local java application and this is the way that the unit tests and some of the SDK examples run. But if you are running Alfresco as a local java app then you don't want another instance hogging the same resources.   So shut down your already running instance of alfresco to run your test.

Or you can have an application which connects to a remote instance of Alfresco.    I think this is the pattern you want.
The recommended approach is to write a web script that implements your buisness logic and returns your results   (in your case gets the user's groups).   You then call your web script from your remote application.    Since your web script runs on the alfresco repository you can simply inject those services that you need without resorting to the service registry.

luisg
Champ in-the-making
Champ in-the-making
Hi there, and thanks for you explanation.

Yes, I have a Java application and I was trying to connect to an running instance of Alfresco.

I thought it was a way to get the groups from a user just using the Java API without the use of do new web scripts. I was trying to avoid web scripts, but with your explanation, I think I must use them…

I never created a web script… Do you have an example how to do this kind of operations?

Thanks

Luis

luisg
Champ in-the-making
Champ in-the-making
Hi again…

I was looking into alfresco web scripts and I think I can do my own… Then I just need to find a way to call the web script and treat the response (Json file) in my Java code, right?

Thanks

Luis