3 Replies Latest reply on May 14, 2007 10:00 AM by anescu

    identity.hasRole problem

      Hi,

      I'm trying to use the "identity.hasRole" method in the authenticator class but the result is always false.

      So, for example, if I put this code in the authenticate method:

      identity.addRole( new String( "account_admin" ) );


      and then I test (in the same method)

      System.out.println( "### account_admin: " + identity.hasRole( new String( "account_admin" ) ) );


      The result is always "false". Why? How do you test then the roles a user has? I would need to add on the session context some data, that are strictly related to the roles the user has. The roles are loaded from the DB, and placed in the Identity object. But then when I need to create dynamic content based on the roles I can't find any role.

        • 1. Re: identity.hasRole problem

          Ok, now I'm really confused!

          So, now i've tried the following:
          In the authenticator class

          public boolean authenticate()
          {
          ...
           Identity.instance().addRole( new String( "account_admin" ) );
          ...
           System.out.println( "### account_admin: " + Identity.instance().hasRole( new String( "account_admin" ) ) );
          ...
          }


          and in another class:

          @In( required = false )
          Identity identity;
          ...
          
          public Collection getValues()
          {
           System.out.println( "### again account_admin: " + identity.hasRole( new String( "account_admin" ) ) );
          }

          The method is called to populate a select box on the interface.

          So, in the first class, the answer is false, and in the second the answer is true. How can this be possible? I need to be able to load some objects based on the roles of the user, I suppose the best way to do this is from the authentication method, but apparently I can't, because of the result I get. Anyone knows if I'm missing something, or if this is a known bug?

          • 2. Re: identity.hasRole problem
            sradford

            Until the authenticate() method completes and the login process itself completes the user's roles are not assigned. Any that you add by calling Identity.addRole() are added to a temporary store and only added to the user's actual list of roles when the login process is 'committed'.

            As an aside. why do you have 'new String("some string")' in all your code?

            • 3. Re: identity.hasRole problem

              That was just a temp debug code, because I was not sure why the code was not working (I tried several versions, because I had no idea why it was not working). The roles are actually loaded from the DB, and later on I was testing for the roles and perform some actions based on specific roles.

              But, for example, I load from the DB a user, with the roles it has, and based on the roles, I also load some object lists in the session context. The easiest/cleanest way to do that is to call a method from the authenticate() method and based on the roles to init some objects. Where then would I do that?

              I could work around and test when loading the roles someting like this:

              for( Role role : user.getUserRoles() )
              {
               identity.addRole( role.getRoleName() );
               if( HUB_ADMIN.equals( role.getRoleName() ) )
               doHubAdminLoading()
               if( HUB_USER.equals( role.getRoleName() ) )
               doHubUserLoading()
              }