4 Replies Latest reply on Aug 7, 2009 11:16 AM by Marco Röösli

    Getting a list of roles from Identity

    lholmquist Newbie

      I have a custom authentication method that gets the roles for a specific user from a db.  the roles are added using the identity.addRole('role') method.   is there a way to then list these roles that i have added.  i know i can use the hasRole method to restrict things.  but it would be nice also show the roles for that user.

        • 1. Re: Getting a list of roles from Identity
          Daniel Wachter Newbie

          Yes you can gather the roles of a user like this:


          #{identityManager.getImpliedRoles(USERNAME)}


          • 2. Re: Getting a list of roles from Identity
            lholmquist Newbie

            Thanks for the quick reply.   what version of seam is this in.  I am using jboss developer studio and chose the Seam 2.0 project wizard.  I was unable to find the IdentityManager class. I would also like to point out that i am a Seam noob


            thanks

            • 3. Re: Getting a list of roles from Identity
              lholmquist Newbie

              If i use the 2.1 project wizard, its there.



              Another Question.    I'm new to seam and have been using the Seam Framework second edition book.   I see that using the IdentityManager u can do all the CRUD operations with it.  I don't normally mapped my pojo's to a db.  i like to write the querys myself.  How do u extend the Identity Manager functionallity to write those custom query's.


              i'm sorry if this is not a seam question

              • 4. Re: Getting a list of roles from Identity
                Marco Röösli Newbie

                if you just wan't to know the roles hold in the identity object you can try this:



                public List<String> getIdentityRoles(Identity identity) {
                      List<String> roles = new ArrayList<String>();
                      Subject subject = identity.getSubject();
                
                      for (Group sg : subject.getPrincipals(Group.class)) {
                         if (Identity.ROLES_GROUP.equals(sg.getName())) {
                            Enumeration<Principal> e = (Enumeration<Principal>) sg.members();
                            while (e.hasMoreElements()) {
                               Principal member = (Principal) e.nextElement();
                               roles.add(member.getName());
                            }
                         }
                      }
                      return roles;
                   }



                Greetz Marco