6 Replies Latest reply on Dec 13, 2011 9:37 AM by Bruno Felix

    Problem when try save a new user or try update the root user.

    Agnaldo Oliveira Newbie

      Hello all,

       

      I want create or update a user on gatein 3.1.0-GA portal using the code below.

       

      OBS: I try to login on gatein using the commented code, but this no have effect

       

      PortalContainer container = PortalContainer.getInstance();

      //                    ExoContainer container = ExoContainerContext.getCurrentContainer();

       

      //                    UsernameCredential usernameCredential = new UsernameCredential("root");

      //                    PasswordCredential passwordCredential = new PasswordCredential("gtn");

      //

      //                    Authenticator authenticator = (Authenticator)container.getComponentInstanceOfType(Authenticator.class);

      // authenticator.validateUser(new Credential[]{usernameCredential, passwordCredential});

       

       

                          OrganizationService orgService = (OrganizationService) container.getComponentInstanceOfType(OrganizationService.class);

                          RequestLifeCycle.begin((ComponentRequestLifecycle)orgService);

       

                          UserHandler userHandler = orgService.getUserHandler();

      //                    userHandler.authenticate("root", "gtn");

       

                          org.exoplatform.services.organization.User user = userHandler.findUserByName("root");

                          System.out.println(user.getUserName());

                          System.out.println(user.getFirstName());

                          System.out.println(user.getLastName());

                          System.out.println(user.getEmail());

                          System.out.println(user.getPassword());

       

                          user.setEmail(usuarioEmpresa.getUsuario().getLogin());

                          user.setFirstName(usuarioEmpresa.getUsuario().getName());

                          user.setPassword(usuarioEmpresa.getUsuario().getPassword());

                          user.setUserName(usuarioEmpresa.getNomeECM());

      //                    userHandler.createUser(user, false);

                          userHandler.saveUser(user, true);

                          RequestLifeCycle.end();

       

      The sysouts print all informations of user, but user's toString show @null why?

       

      But this throws a Exception:

       

      java.lang.IllegalArgumentException: User cannot be null

                at org.picketlink.idm.impl.api.session.managers.AbstractManager.checkNotNullArgument(AbstractManager.java:271)

                at org.picketlink.idm.impl.api.session.managers.AttributesManagerImpl.updatePassword(AttributesManagerImpl.java:558)

                at org.exoplatform.services.organization.idm.UserDAOImpl.persistUserInfo(UserDAOImpl.java:449)

                at org.exoplatform.services.organization.idm.UserDAOImpl.saveUser(UserDAOImpl.java:158)

                at com.softsimples.ecm.admin.ECMAdminActivatorImpl.configureAdminDefaultPortal(ECMAdminActivatorImpl.java:124)

                at com.softsimples.ecm.admin.ECMAdminActivatorImpl.startModule(ECMAdminActivatorImpl.java:63)

                at com.softsimples.servlet.SoftSimplesHandler.iniciarModulos(SoftSimplesHandler.java:91)

                at com.softsimples.servlet.SoftSimplesHandler.carregarModulos(SoftSimplesHandler.java:82)

                at com.softsimples.servlet.SoftSimplesHandler.init(SoftSimplesHandler.java:71)

                at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1173)

                at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:993)

                at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4149)

                at org.apache.catalina.core.StandardContext.start(StandardContext.java:4458)

                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:526)

                at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:987)

                at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:909)

                at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:495)

                at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1206)

                at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:314)

                at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)

                at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)

                at org.apache.catalina.core.StandardHost.start(StandardHost.java:722)

                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:583)

                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)

       

      Thank you all

        • 1. Problem when try save a new user or try update the root user.
          Agnaldo Oliveira Newbie

          Hello all

           

          I find the solution, this code works fine.

           

          PortalContainer container = PortalContainer.getInstance();

            

                              OrganizationService orgService = (OrganizationService) container.getComponentInstanceOfType(OrganizationService.class);

                              RequestLifeCycle.begin((ComponentRequestLifecycle)orgService);

           

                              UserHandler userHandler = orgService.getUserHandler();

                              MembershipHandler membershipHandler = orgService.getMembershipHandler();

                              MembershipTypeHandler membershipTypeHandler = orgService.getMembershipTypeHandler();

                              GroupHandler groupHandler = orgService.getGroupHandler();

           

                              MembershipType membershipTypeManager =  membershipTypeHandler.findMembershipType("manager");

                              MembershipType membershipTypeMember =  membershipTypeHandler.findMembershipType("member");

           

                              Group groupAdministrators = groupHandler.findGroupById("/platform/administrators");

                              Group groupExecutive = groupHandler.findGroupById("/organization/management/executive-board");

                              Group groupUsers = groupHandler.findGroupById("/platform/users");

           

                              org.exoplatform.services.organization.User user = userHandler.createUserInstance(usuarioEmpresa.getNomeECM());

                              user.setOrganizationId(usuarioEmpresa.getEmpresa().getId());

                              user.setEmail(usuarioEmpresa.getUsuario().getLogin());

                              user.setFirstName(usuarioEmpresa.getUsuario().getName());

                              user.setLastName("");

                              user.setPassword(usuarioEmpresa.getUsuario().getPassword());

                              userHandler.createUser(user, false);

           

           

                              membershipHandler.linkMembership(user, groupAdministrators, membershipTypeManager, false);

                              membershipHandler.linkMembership(user, groupExecutive, membershipTypeMember, false);

                              membershipHandler.linkMembership(user, groupUsers, membershipTypeMember, false);

           

                              RequestLifeCycle.end();

           

          my problem was a OrganizationId i don't have seted and the membership is to relationship the groups with user.

           

          Tank you all

          • 2. Re: Problem when try save a new user or try update the root user.
            Bruno Felix Newbie

            Hello,

             

            I'm trying to use your code to create a user if it doesn't exist in GateIn after authentication via my own login module.

            Sadly, it doesn't work: the user seems to be correctly created, but I've got an abort by sharedstateloginmodule when trying to connect with the freshly created user.  It fails in UserDAOImpl class, in authenticate method at line 267:

             

            authenticated = session.getAttributesManager().validatePassword(idmUser, password);

             

            There, authenticated is false and then the login fails.

             

            Here comes the method (same as yours!):

             

            public void testUserCreation() throws Exception{
                     PortalContainer container = PortalContainer.getInstance();
                     OrganizationService orgService = (OrganizationService) container.getComponentInstanceOfType(OrganizationService.class);
                    RequestLifeCycle.begin((ComponentRequestLifecycle)orgService);
                    UserHandler userHandler = orgService.getUserHandler();
                    MembershipHandler membershipHandler = orgService.getMembershipHandler();
                    MembershipTypeHandler membershipTypeHandler = orgService.getMembershipTypeHandler();
                    GroupHandler groupHandler = orgService.getGroupHandler();
              
             MembershipType membershipTypeManager = membershipTypeHandler.findMembershipType("manager");
             MembershipType membershipTypeMember = membershipTypeHandler.findMembershipType("member");
             Group groupAdministrators = groupHandler.findGroupById("/platform/administrators");
             Group groupExecutive = groupHandler.findGroupById("/organization/management/executive-board");
             Group groupUsers = groupHandler.findGroupById("/platform/users");
            
             org.exoplatform.services.organization.User user = userHandler.createUserInstance(((NameCallback)callbacks[0]).getName());
             user.setOrganizationId("default");
                    user.setEmail(((NameCallback)callbacks[0]).getName() + "@" + user.getOrganizationId() + ".com");
                    user.setFirstName(((NameCallback)callbacks[0]).getName());
                    user.setLastName(((NameCallback)callbacks[0]).getName());
                    user.setPassword((((PasswordCallback)callbacks[1]).getPassword()).toString());
                    try{
                         userHandler.createUser(user, true);
                    }catch (Exception e) {
             log.error("Problem creating user in GateIn DB: " + e.getMessage());
             log.error("Cause: " + e.getCause());
             }
            
                    membershipHandler.linkMembership(user, groupAdministrators, membershipTypeManager, false);
                    membershipHandler.linkMembership(user, groupExecutive, membershipTypeMember, false);
                    membershipHandler.linkMembership(user, groupUsers, membershipTypeMember, false);
              
                    RequestLifeCycle.end();
                }
            

            I'm a little bit lost...

            Thanks for your help!

            • 3. Re: Problem when try save a new user or try update the root user.
              Bruno Felix Newbie

              My problem seems to be linked with picketlink (which I absolutly don't know...)

              After having my user created, I try to check if my user is correctly created with following code:

               

              public boolean IsUserExisting(String userName){
                       PortalContainer container = PortalContainer.getInstance();
                       OrganizationService orgService = (OrganizationService) container.getComponentInstanceOfType(OrganizationService.class);
                
                       UserHandler userHandler = orgService.getUserHandler();
                       org.exoplatform.services.organization.User user = null;
                       try {
                                   user = userHandler.findUserByName(userName);
                            } catch (Exception e) {
                                   log.error("Cannot search for user into GateIn DB: " + e.getMessage());
                                   return false;
                            }
                         if (user==null)
                              return false;
                         else
                              return true;
                  }
              

               

              And I can see my user being found without problem.

              But, when I try to log into GateIn with this user, the SharedStateLoginModule reports following exception:

               

              INFO  [org.exoplatform.services.organization.idm.UserDAOImpl] (http-0.0.0.0-8085-1) Cannot obtain user: toto;
              org.picketlink.idm.common.exception.IdentityException: IdentityObjectType[USER] not present in the store.
                      at org.picketlink.idm.impl.store.hibernate.HibernateIdentityStoreImpl.getHibernateIdentityObjectType(HibernateIdentityStoreImpl.java:2558)
                      at org.picketlink.idm.impl.store.hibernate.HibernateIdentityStoreImpl.findIdentityObject(HibernateIdentityStoreImpl.java:648)
                      at org.picketlink.idm.impl.repository.WrapperIdentityStoreRepository.findIdentityObject(WrapperIdentityStoreRepository.java:155)
                      at org.picketlink.idm.impl.api.session.managers.PersistenceManagerImpl.findUser(PersistenceManagerImpl.java:426)
                      at org.exoplatform.services.organization.idm.UserDAOImpl.getPopulatedUser(UserDAOImpl.java:480)
                      at org.exoplatform.services.organization.idm.UserDAOImpl.findUserByName(UserDAOImpl.java:228)
                      at alu.osp.web.portal.loginmodule.JaasLoginModule.IsUserExisting(JaasLoginModule.java:241)
                      at alu.osp.web.portal.loginmodule.JaasLoginModule.login(JaasLoginModule.java:126)
                      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 javax.security.auth.login.LoginContext.invoke(LoginContext.java:769)
                      at javax.security.auth.login.LoginContext.access$000(LoginContext.java:186)
                      at javax.security.auth.login.LoginContext$4.run(LoginContext.java:683)
                      at java.security.AccessController.doPrivileged(Native Method)
                      at javax.security.auth.login.LoginContext.invokePriv(LoginContext.java:680)
                      at javax.security.auth.login.LoginContext.login(LoginContext.java:579)
                      at org.jboss.security.plugins.auth.JaasSecurityManagerBase.defaultLogin(JaasSecurityManagerBase.java:553)
                      at org.jboss.security.plugins.auth.JaasSecurityManagerBase.authenticate(JaasSecurityManagerBase.java:487)
                      at org.jboss.security.plugins.auth.JaasSecurityManagerBase.isValid(JaasSecurityManagerBase.java:365)
                      at org.jboss.security.plugins.JaasSecurityManager.isValid(JaasSecurityManager.java:160)
                      at org.jboss.web.tomcat.security.JBossWebRealm.authenticate(JBossWebRealm.java:384)
                      at org.apache.catalina.authenticator.FormAuthenticator.authenticate(FormAuthenticator.java:258)
                      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:417)
                      at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92)
                      at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126)
                      at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)
                      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
                      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
                      at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)
                      at alu.mcas.web.loginmodule.TestFormAuthValve.invoke(TestFormAuthValve.java:42)
                      at org.apache.catalina.authenticator.SingleSignOn.invoke(SingleSignOn.java:402)
                      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
                      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)
                      at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829)
                      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:598)
                      at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
                      at java.lang.Thread.run(Thread.java:619)
              Caused by: org.hibernate.HibernateException: createCriteria is not valid without active transaction
                      at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:338)
                      at $Proxy386.createCriteria(Unknown Source)
                      at org.picketlink.idm.impl.store.hibernate.HibernateIdentityStoreImpl.getHibernateIdentityObjectType(HibernateIdentityStoreImpl.java:2532)
                      ... 38 more
              

               

              I didn't change anything to picketlink configuration from GateIn package (I'm using 3.1.0-GA version).

              I had a rapid look to picketlink doc but didn't find how to fix my problem...

               

              Any help is welcome!

              • 4. Re: Problem when try save a new user or try update the root user.
                Khoi Nguyen Novice

                I guess that OrganizationService is not started in lifecycle.

                 

                Please try it

                {code}

                public boolean IsUserExisting(String userName) {

                      PortalContainer container = PortalContainer.getInstance();

                      OrganizationService orgService = (OrganizationService) container.getComponentInstanceOfType(OrganizationService.class);

                 

                      UserHandler userHandler = orgService.getUserHandler();

                      org.exoplatform.services.organization.User user = null;

                      try {

                         RequestLifeCycle.begin((ComponentRequestLifecycle) orgService);

                         user = userHandler.findUserByName(userName);

                      }

                      catch (Exception e) {

                         log.error("Cannot search for user into GateIn DB: " + e.getMessage());

                         return false;

                      } finally {

                         RequestLifeCycle.end();

                      }

                      if (user == null)

                         return false;

                      else

                         return true;

                   }


                {code}


                • 5. Re: Problem when try save a new user or try update the root user.
                  Bruno Felix Newbie

                  I'm sorry, but I think I was not clear: I can find my user without problem with the method "IsExistingUser", even without starting cycle life of OrganizationService.

                   

                  But, I can't login to GateIn because the SharedStateLoginModule aborts with the exception given at the previous post.

                   

                  Here comes the my login modules config:

                   

                  <deployment xmlns="urn:jboss:bean-deployer:2.0">
                  
                    <application-policy xmlns="urn:jboss:security-beans:1.0" name="gatein-domain">
                      <authentication>
                        <login-module code="org.exoplatform.web.security.PortalLoginModule" flag="optional">
                          <module-option name="portalContainerName">portal</module-option>
                          <module-option name="realmName">gatein-domain</module-option>
                        </login-module>
                        <login-module code="org.exoplatform.services.security.jaas.SharedStateLoginModule" flag="required">
                          <module-option name="portalContainerName">portal</module-option>
                          <module-option name="realmName">gatein-domain</module-option>
                        </login-module>
                        <!-- My custom login module -->
                        <login-module code="org.toto.MyJaasLoginModule" flag="required">
                          <module-option name="javax.security.auth.login.host">sol006p</module-option>
                        </login-module>
                        <login-module code="org.exoplatform.services.security.j2ee.JbossLoginModule" flag="required">
                          <module-option name="portalContainerName">portal</module-option>
                          <module-option name="realmName">gatein-domain</module-option>
                        </login-module>
                      </authentication>
                    </application-policy>
                  
                  </deployment>
                  

                   

                  When tracing, I can see that in org.picketlink.idm.impl.store.hibernate.HibernateIdentityStoreImpl.java (line 2344 in validateCredential method):

                   

                   

                  if (value instanceof String && hibernateCredential.getTextValue() != null)
                           {
                              return value.toString().equals(hibernateCredential.getTextValue());
                           }
                  

                   

                  this returns false.

                  Which causes validateUser method of org.exoplatform.services.organization.auth.OrganizationAuthenticatorImpl to throw an exception:

                   

                  if (!success)
                           throw new LoginException("Login failed for " + user);
                  
                        return user;
                  

                   

                  And this is the beginning of my problem

                  • 6. Re: Problem when try save a new user or try update the root user.
                    Bruno Felix Newbie

                    I forgot to inform you I solved my problem.

                    Shame on me: I was extending the wrong login module...

                    Now with org.exoplatform.services.security.jaas.AbstractLoginModule, it's working