AnsweredAssumed Answered

Creating Users and Groups Programmatically

Question asked by joaotpd on Jul 15, 2009
Latest reply on Oct 14, 2009 by ferbru60
Hi all!!

I'm trying to create users and groups programmatically… For this I'm using the Alfresco Developer Guide Example:

BootstrapAuthorityCreator.java

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import javax.transaction.UserTransaction;
import org.alfresco.model.ContentModel;
import org.alfresco.service.cmr.security.AuthenticationService;
import org.alfresco.service.cmr.security.AuthorityService;
import org.alfresco.service.cmr.security.AuthorityType;
import org.alfresco.service.cmr.security.PersonService;
import org.alfresco.service.namespace.QName;
import org.alfresco.service.transaction.TransactionService;
import org.apache.log4j.Logger;

public class BootstrapAuthorityCreator {
   
    private AuthorityService authorityService;
    private PersonService personService;
    private TransactionService transactionService;
    private AuthenticationService authenticationService;
    private UserTransaction tx;
    private static Logger logger = Logger.getLogger(BootstrapAuthorityCreator.class);

    public void init() throws Exception {

       String salesGroup;
       String marketingGroup;
      
       tx = transactionService.getUserTransaction();
        tx.begin();
       
       // create tuser1, tuser2, tuser3, tuser4
        if(!authenticationService.authenticationExists("tuser1")) {
          authenticationService.createAuthentication("tuser1", "password".toCharArray());
          if (logger.isDebugEnabled()) logger.debug("Created tuser1 auth");
        }

        if (!personService.personExists("tuser1")) {
          personService.createPerson(createDefaultProperties("tuser1", "Test", "User1", "tuser1@localhost", "password"));
          if (logger.isDebugEnabled()) logger.debug("Created tuser1 person");
       }

        if(!authenticationService.authenticationExists("tuser2")) {
          authenticationService.createAuthentication("tuser2", "password".toCharArray());
          if (logger.isDebugEnabled()) logger.debug("Created tuser2 auth");
        }

       if (!personService.personExists("tuser2")) {
          personService.createPerson(createDefaultProperties("tuser2", "Test", "User2", "tuser2@localhost", "password"));
          if (logger.isDebugEnabled()) logger.debug("Created tuser2 person");
       }

        if(!authenticationService.authenticationExists("tuser3")) {
          authenticationService.createAuthentication("tuser3", "password".toCharArray());
          if (logger.isDebugEnabled()) logger.debug("Created tuser3 auth");
        }

       if (!personService.personExists("tuser3")) {
          personService.createPerson(createDefaultProperties("tuser3", "Test", "User3", "tuser3@localhost", "password"));
          if (logger.isDebugEnabled()) logger.debug("Created tuser3 person");
       }

        if(!authenticationService.authenticationExists("tuser4")) {
          authenticationService.createAuthentication("tuser4", "password".toCharArray());
          if (logger.isDebugEnabled()) logger.debug("Created tuser4 auth");
        }

       if (!personService.personExists("tuser4")) {
          personService.createPerson(createDefaultProperties("tuser4", "Test", "User4", "tuser4@localhost", "password"));
          if (logger.isDebugEnabled()) logger.debug("Created tuser4 person");
       }
       
       if (authorityService.authorityExists(authorityService.getName(AuthorityType.GROUP, "sales"))) {
          salesGroup = authorityService.getName(AuthorityType.GROUP, "sales");
       } else {
          // create the sales group
          salesGroup = authorityService.createAuthority(AuthorityType.GROUP, null, "sales");
       }

       //add tuser1 and tuser2 to the sales group
       authorityService.addAuthority(salesGroup, "tuser1");
       authorityService.addAuthority(salesGroup, "tuser2");
       
       if (authorityService.authorityExists(authorityService.getName(AuthorityType.GROUP, "marketing"))) {
          marketingGroup = authorityService.getName(AuthorityType.GROUP, "marketing");
       } else {
          // create the marketing group
          marketingGroup = authorityService.createAuthority(AuthorityType.GROUP, null, "marketing");
       }

       //add tuser3 and tuser4 to the marketing group
       authorityService.addAuthority(marketingGroup, "tuser3");
       authorityService.addAuthority(marketingGroup, "tuser4");
       
       tx.commit();
    }
   
    private Map<QName, Serializable> createDefaultProperties(String userName, String firstName, String lastName,
            String email, String password) {
        HashMap<QName, Serializable> properties = new HashMap<QName, Serializable>();
        properties.put(ContentModel.PROP_USERNAME, userName);
        properties.put(ContentModel.PROP_FIRSTNAME, firstName);
        properties.put(ContentModel.PROP_LASTNAME, lastName);
        properties.put(ContentModel.PROP_EMAIL, email);
        properties.put(ContentModel.PROP_PASSWORD, password);
        return properties;
    }

   public void setAuthorityService(AuthorityService authorityService) {
      this.authorityService = authorityService;
   }

   public void setPersonService(PersonService personService) {
      this.personService = personService;
   }

   public void setTransactionService(TransactionService transactionService) {
      this.transactionService = transactionService;
   }

   public void setAuthenticationService(AuthenticationService authenticationService) {
      this.authenticationService = authenticationService;
   }
}

module-context.xml

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans.dtd'>

<beans>

    <!– A simple class that is initialized by Spring –>
    <bean id="sdk.createUsers.createUsersBean" class="org.alfresco.module.sdkcreateusers.BootstrapAuthorityCreator" init-method="init">
        <property name="personService">
            <ref bean="personService" />
        </property>
        <property name="authorityService">
            <ref bean="authorityService" />
        </property>
        <property name="transactionService">
            <ref bean="transactionService" />
        </property>       
        <property name="authenticationService">
            <ref bean="authenticationService" />
        </property>       
    </bean>
</beans>

but this doesn't seem to work… I'm getting this error:

(…)
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sdk.createUsers.createUsersBean' defined in file [/opt/alfresco/tomcat/webapps/alfresco/WEB-INF/classes/alfresco/module/sdkCreateUsers/module-context.xml]: Invocation of init method failed; nested exception is java.lang.NullPointerException
Caused by: java.lang.NullPointerException
        at org.alfresco.repo.search.IndexerComponent.createNode(IndexerComponent.java:72)
        at org.alfresco.repo.node.index.NodeIndexer.indexCreateNode(NodeIndexer.java:91)
        at org.alfresco.repo.node.db.DbNodeServiceImpl.createNode(DbNodeServiceImpl.java:345)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:304)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
        at org.alfresco.repo.transaction.SingleEntryTransactionResourceInterceptor.invokeInternal(SingleEntryTransactionResourceInterceptor.java:163)
        at org.alfresco.repo.transaction.SingleEntryTransactionResourceInterceptor.invoke(SingleEntryTransactionResourceInterceptor.java:138)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
        at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
        at $Proxy9.createNode(Unknown Source)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:304)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
        at org.alfresco.repo.tenant.MultiTNodeServiceInterceptor.invoke(MultiTNodeServiceInterceptor.java:110)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
        at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
        at $Proxy9.createNode(Unknown Source)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.alfresco.repo.service.StoreRedirectorProxyFactory$RedirectorInvocationHandler.invoke(StoreRedirectorProxyFactory.java:221)
        at $Proxy10.createNode(Unknown Source)
        at org.alfresco.repo.node.MLPropertyInterceptor.invoke(MLPropertyInterceptor.java:279)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
        at org.alfresco.repo.node.NodeRefPropertyMethodInterceptor.invoke(NodeRefPropertyMethodInterceptor.java:188)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
        at org.alfresco.repo.node.NodeRefPropertyMethodInterceptor.invoke(NodeRefPropertyMethodInterceptor.java:188)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
        at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
        at $Proxy9.createNode(Unknown Source)
        at org.alfresco.repo.security.authentication.RepositoryAuthenticationDao.createUser(RepositoryAuthenticationDao.java:254)
        at org.alfresco.repo.security.authentication.AuthenticationServiceImpl.createAuthentication(AuthenticationServiceImpl.java:82)
        at org.alfresco.repo.security.authentication.AbstractChainingAuthenticationService.createAuthentication(AbstractChainingAuthenticationService.java:76)
        at org.alfresco.module.sdkcreateusers.BootstrapAuthorityCreator.init(BootstrapAuthorityCreator.java:35)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod(AbstractAutowireCapableBeanFactory.java:1242)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1208)
        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.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:246)
        at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:189)
        at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:49)
        at org.alfresco.web.app.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:69)
        at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3843)
        at org.apache.catalina.core.StandardContext.start(StandardContext.java:4342)
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791)
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771)
        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:525)
        at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:627)
        at org.apache.catalina.startup.HostConfig.deployDescriptors(HostConfig.java:553)
        at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:488)
        at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1149)
        at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:311)
        at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
        at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
        at org.apache.catalina.core.StandardHost.start(StandardHost.java:719)
        at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
        at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
        at org.apache.catalina.core.StandardService.start(StandardService.java:516)
        at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
        at org.apache.catalina.startup.Catalina.start(Catalina.java:578)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
        at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
15:50:19,065 INFO  [org.alfresco.web.site.FrameworkHelper] Successfully Initialized Web Framework
(…)

Can someone please tell me what I'm doing wrong??
I'm deploying the .amp in alfresco-community-tomcat-3.2…
Thanks!!

João Duarte

Outcomes