5 Replies Latest reply on Jun 19, 2008 10:42 PM by shane.bryzak

    IdentityManager.createUser(String name, String password, String firstname, String lastname)

    nimo22

      Look at that:


      public class PersonEJBBean implements PersonEJBLocal {
      ...
      @In
      private EntityManager entityManager;
      ...
      public void createPerson(Person person) {
      ..
      Identity.setSecurityEnabled(false);
      ..
      identityManager.createUser(person.getLogin(), person.getPassword(), person.getFirstname(), person.getLastname());
      
      // MERGE additional infos to the person-instance
      person.setAge(users.getAge());
      entityManager.merge(person);
      
      }
      }
      
      



      When I run the method 'createPerson', then it will persist TWO Person-Instances. The first with person-instance A will be created after 'identityManager.createUser', the second person-instance B will created after invoking 'entityManager.merge(person);'.


      How can I avoid this double-persistence-behaviour?
      I use merge, but it does not merge it with instance A.


      Indeed, I can override the 'JpaIdentityStore', the 'IdentityStore' and the 'IdentityManager' to extend (or substitute) the method


      public boolean createUser(String name, String password, String firstname, String lastname)
      



      with an additional field 'String age':


      public boolean createUser(String name, String password, String firstname, String lastname, String age).


      Is there a better way, instead of overriding (extending) the three classes??

        • 1. Re: IdentityManager.createUser(String name, String password, String firstname, String lastname)
          nimo22

          I tried to extend the the createUser-Method in IdentityManager:



          I extended the JpaIdentityStore with 'String age' in createUser:



          ..
          @Name("org.jboss.seam.security.jpaIdentityStore")
          @Install(precedence = DEPLOYMENT)
          @Scope(APPLICATION)
          @BypassInterceptors
          @Startup
          public class MyJPAIdentityStore extends JpaIdentityStore
          {
          ...
          public boolean createUser(String username, String password, String firstname, String lastname, String age)
             {...}
          


          I also extended the IdentityManager




          public class MyIdentityManager extends IdentityManager
          {
          
           @Override
             public boolean createUser(String name, String password)
             {
                return createUser(name, password, null, null);
             }
          
             @Override
             public boolean createUser(String name, String password, String firstname, String lastname)
             {
                Identity.instance().checkPermission(USER_PERMISSION_NAME, PERMISSION_CREATE);
                return identityStore.createUser(name, password, firstname, lastname);
             }
          
             public boolean createUser(String name, String password, String firstname, String lastname, String age)
             {
                Identity.instance().checkPermission(USER_PERMISSION_NAME, PERMISSION_CREATE);
                return identityStore.createUser(name, password, firstname, lastname, idShift, idTeam, defaultlang);
             }
          
          



          and at last, I extended the IdentityStore




          public interface MyIdentityStore extends IdentityStore
          {
          ...
          boolean createUser(String username, String password);
          boolean createUser(String username, String password, String firstname, String lastname);
          boolean createUser(String username, String password, String firstname, String lastname, String age);
          



          Now, I want to use the the method.


          My Question: Is this all I have to do or do I more than I have to do? I want to keep it simple:-)




          • 2. Re: IdentityManager.createUser(String name, String password, String firstname, String lastname)
            shane.bryzak

            It isn't anywhere near as complicated as this.  Simply write an observer for JpaIdentityStore.EVENTUSERCREATED to get a reference to the persisted entity, then make whatever further changes to it that you want before merging.

            • 3. Re: IdentityManager.createUser(String name, String password, String firstname, String lastname)
              shane.bryzak

              Oops, that event should have been JpaIdentityStore.EVENT_USER_CREATED.

              • 4. Re: IdentityManager.createUser(String name, String password, String firstname, String lastname)
                nimo22

                ohh wow...thats fine..I saw the example of 'JpaIdentityStore.EVENTUSERCREATED' in the seamspace example..very nice.


                One little performance-drawback with this is, that I have to use the merge-method..so the EntityManager, at first, persist the new Person-Object. After persisting, the merge-method will UPDATE the referenced Person-Object.


                I can avoid the merge-method extending and overriding JPAIdentityStore:


                boolean createUser(String username, String password, String firstname, String lastname, String age);



                Besides, when I override the 'JPAIdentityStore', I have also override all dependencies (?). That means, I have also override the two classes 'IdentityStore' and 'IdentityManager'. Am I right?


                But I am eager to keep it simple, so I use the JpaIdentityStore.EVENTUSERCREATED.


                Thank u very much:-)




                • 5. Re: IdentityManager.createUser(String name, String password, String firstname, String lastname)
                  shane.bryzak

                  Alternatively you can write an observer for JpaIdentityStore.EVENT_PRE_PERSIST_USER, which will give you the instance of the entity before it is persisted.