2 Replies Latest reply on May 2, 2009 2:21 PM by nitzan tomer

    user registeration and roles

    nitzan tomer Newbie

      hi everyone,


      i can't seem to understand the relationship between the user roles i have stored in the database and the user roles managed by the IdentityManager.


      for example, let's say i have two roles in the database: member and admin.
      when a new user registers to the system i need to assign a role for him (that's part of the data a user holds), but how do i assign that role?  should i write that in the code? what if i'll want to change the default role for new users?  i'll have to change the code and that's not a nice way of doing things..


      the same goes for the method grantRole of the IdentityManager, it gets a string for the role name, where should i get that from?  should it be hardcoded ?


      this issue really puzzles me and i can't seem to find any example on the manner (on the net or in the seam examples).


      thanks a lot, nitza;

        • 1. Re: user registeration and roles
          Radu B Newbie

          How about adding a boolean field to your Role entity isDefaultRole?


          In your registration bean, you get a list of roles with isDefaultRole = true.
          You just have to iterate through results and call



          identityManager.grantRole(usernameYouAreTryingToSave, iteratedRole.getName())




          You map your entity classes with identity user and roles in components.xml



               <security:jpa-identity-store  
                    user-class="your.package.UserAccount"
                    role-class="your.package.Role"
               />




          Check also the Security chapter from Seam documentation which I think is very well written and you can easily perform what you need after going through it.

          • 2. Re: user registeration and roles
            nitzan tomer Newbie

            thanks for your reply.


            adding a boolean to the role entity is a good idea, but i still don't understand how i iterate over the roles..
            the IdentityManager has the listRoles method which returns a list of roles but it returns only the names of the roles and not the objects so i can not check if it's the default role.


            i can query the db (or make an entity-query) for the roles but i thought that maybe there's already a way to do that, plus it sounds like a waste to do that every time a user registers.


            thanks, nitzan;