2 Replies Latest reply on Aug 17, 2007 2:35 AM by J-C jc

    How do I get user roles in 2.6?

    Jon Whitmore Newbie

      Hello. I'm using AS 4.2.0 and portal 2.6.


      Under 2.4 there was a method roleModule#getRoles(User user) for obtaining roles for a specific user. Now that I'm using 2.6 that method is no longer in the interface and I'm wondering how I can accomplish the same thing.

      How can I determine if the current user has a certain role under 2.6? I am currently using the LdapExtLoginModule.

      nollie

        • 1. Re: How do I get user roles in 2.6?
          Jon Whitmore Newbie

          Right now I'm doing this, but I'd rather have a cleaner solution with the RoleModule ...

          public static boolean hasRole(String roleName) {
           Subject caller = SecurityAssociation.getSubject();
           if ( caller == null ) {
           return false;
          
           }
           Set princes = caller.getPrincipals();
           Iterator i = princes.iterator();
           while (i.hasNext() ) {
           String roles = i.next().toString();
           // e.g. Roles(members:User,Admin,Administrators,Authenticated)
           // make sure to check for commas, colons, and close paren
           // so "User" isn't mistaken for "SuperUser" and the like
           if ( roles.contains(":" + roleName + ",") ||
           roles.contains("," + roleName + ",") ||
           roles.contains("," + roleName + ")")) {
           return true;
          
           }
          
           }
           return false;
          
          }
          


          Any suggestions?

          • 2. Re: How do I get user roles in 2.6?
            J-C jc Apprentice

            You can also delegate that to a session bean:

            SessionContext ctx = ...
            ...
            ctx.isCallerInRole("myRole");