isMemberOf() trouble

oharel
Kilo Sage

Hi all,

I am trying to de-activate and lock out users from a certain OU (called MyOU), who are also not in a specific group (called MyGroup).

I am running an after business rule (for testing purposes I am just trying to change the title of the users now) :

deactivateAndLockout();

function deactivateAndLockout() {

 

  var user = new GlideRecord('sys_user');

  user.addQuery('source','CONTAINS','MyOU');

  user.query();

  while(user.next()) {

        var group = 'MyGroup';

        if(user.getUser().isMemberOf(group)){

                  user.title = 'grp';

                  user.update();

          } else {

                    user.title = 'not grp';

                    user.update();

          }

    }

}

I can see users in the group MyGroup, however, all users in MyOU get the title 'not grp', even those in the MyGroup.

What am I missing here?

harel

1 ACCEPTED SOLUTION

Your complete code will be:



deactivateAndLockout();



function deactivateAndLockout() {



  var gr = new GlideRecord('sys_user');


  gr.addQuery('source','CONTAINS','MyOU');


  gr.query();



  var group = 'MyGroup';



  while(gr.next()) {


      if(gs.getUser().getUserByID(gr.user_name).isMemberOf(group)) {


          gr.title = 'grp';


      }


      else {


          gr.title = 'not grp';


      }




      gr.update();


  }


}



Also unless you have other external dependencies, the name of the function "deactivateAndLockout" is a bit misleading, because this function neither deactivates nor locks a user out. That is, of course, at your own volition.


View solution in original post

10 REPLIES 10

Hi Daniel,



Yes, I tried working with the script you mention. The problem is that I don't have a user by ID - I can't provide either a sys_id or a UserID. I can only check the OU and the isMemeberOf().


Also, why can't I simple use:


var gr = new GlideRecord("sys_user");


gr.addQuery('source','CONTAINS','MyOU');


gr.query();




gr.next();


var group = "Hardware";




if(gs.getUser().isMemberOf(group)) {


  gs.title = 'grp';


}


else {


  gs.title = 'not grp';


}



'cause it's not working...



Thanks,


harel


If you are using Glide Query on users that have the source that contain 'MyOU' and you do the gr.next() then you have access to all fields with that user record (in the GlideRecord gr).



The key is gs.getUser().getUserByID(gr.user_name).isMemberOf(group);



I just tried the above in a background script and the log returned the correct results.







gs.getUser().isMemberOf(group)


Will not work if you do not have gs.getUser().getUserByID(gr.user_name).isMemberOf(group);


Hi Daniel,



So the whole thing should look like this:


deactivateAndLockout();


function deactivateAndLockout() {



  var gr = new GlideRecord('sys_user');


  gr.addQuery('source','CONTAINS','MyOU');


  gr.query();



  gr.next();



  var group = 'MyGroup';



  if(gs.getUser().getUserByID(gr.user_name).isMemberOf(group)) {


  gr.title = 'grp';


  gr.update();


  } else {


  gr.title = 'not grp';


  gr.update();


  }


}



I am asking because this does not update the title, so maybe I misundertood you.


Harel


Your complete code will be:



deactivateAndLockout();



function deactivateAndLockout() {



  var gr = new GlideRecord('sys_user');


  gr.addQuery('source','CONTAINS','MyOU');


  gr.query();



  var group = 'MyGroup';



  while(gr.next()) {


      if(gs.getUser().getUserByID(gr.user_name).isMemberOf(group)) {


          gr.title = 'grp';


      }


      else {


          gr.title = 'not grp';


      }




      gr.update();


  }


}



Also unless you have other external dependencies, the name of the function "deactivateAndLockout" is a bit misleading, because this function neither deactivates nor locks a user out. That is, of course, at your own volition.


Thank you so much!


The title thing was just to check that it's working. I changed it to gr.active and gr.locked_out once I saw the title is updated properly.


Thanks for your patience and help!


Harel