8 Replies Latest reply on Nov 11, 2008 8:32 AM by accless

    RunAsOperation issue?

    zergspirit

      Hi, I'm having an issue using the RunAsOperation class.
      It's actually quite simple.
      In the docs, it says :




        new RunAsOperation() {
      
             @Override
      
             public String[] getRoles() {
      
                return new String[] { "admin" };
      
             }
      
             public void execute() {
      
                executePrivilegedOperation();
      
             }         
      
          }.run();



      ... but there's no method getRoles to override, and I didn't find a nice and easy way to do it otherwise (without having to actually override RunAsOperation).
      Thanks!



        • 1. Re: RunAsOperation issue?
          shane.bryzak

          This was an oversight, I've updated the docs now.  Instead of overriding the getRoles() method, you simply need to call addRole() before calling run(), like this:



          new RunAsOperation() {       
            public void execute() {
              // do stuff here
            }         
          }.addRole("admin")
           .run()



          • 2. Re: RunAsOperation issue?
            zergspirit

            Works perfectly, thanks!

            • 3. Re: RunAsOperation issue?
              accless

              I have the scenario where i need to add several roles temporarily and the amount of those roles is also dynamically. Is there a convinient way of adding several roles?
              (until 2.0 the getRoles-Method was perfect for me)



              greetings

              • 4. Re: RunAsOperation issue?
                shane.bryzak

                RunAsOperation op = new RunAsOperation() {       
                  public void execute() {
                    // do stuff here
                  }         
                };
                
                for (String role : tempRoles) {
                  op.addRole(role);
                }
                
                op.run();


                • 5. Re: RunAsOperation issue?
                  accless

                  That was my idea, too. But the RunAsOperation is ABSTRACT, thats why i cannot store the instanze in a variable!


                  Greetings

                  • 6. Re: RunAsOperation issue?
                    shane.bryzak

                    Er, you're right of course.  Ok, as a workaround you can try the following code (it's a little messy, sorry) and I'll do something to improve this for the next release.


                          new RunAsOperation() {
                             public void execute() {
                                for (String role : roles)
                                {
                                   for ( Group sg : getSubject().getPrincipals(Group.class) )      
                                   {
                                      if ( Identity.ROLES_GROUP.equals( sg.getName() ) )
                                      {
                                         sg.addMember(new SimplePrincipal(role));
                                         break;
                                      }
                                   }
                                            
                                   SimpleGroup roleGroup = new SimpleGroup(Identity.ROLES_GROUP);
                                   roleGroup.addMember(new SimplePrincipal(role));
                                   getSubject().getPrincipals().add(roleGroup);
                                }             
                                
                                // execute privileged operations here           
                             }         
                          }.run();



                    • 7. Re: RunAsOperation issue?
                      shane.bryzak

                      I've just committed a change to SVN, now the addRole() method will add the role directly to the subject.  That means, you can simply call addRole() in the execute() method and the role will be properly added to that operation.

                      • 8. Re: RunAsOperation issue?
                        accless

                        thx!


                        greetings