Groups

Assigning a Large Number of Users to a Group

gourmet fare

Batch Update User Groups

What would be the best way to assign a large number of users to a specified group?

glyn asked...

I currently have a site with 52 users, all of which I need to update with either of two new User Groups. Is there some sort of Batch tool that I could use for this?

The Solution

BobRay says...

The easiest way for me is a custom snippet like this (assuming that you want to add every user on the site to the two groups):

[[!AddUsersToGroups]]

The snippet code:

<?php
/* AddUsersToGroups snippet */
$userObjs = $modx->getCollection('modUser');
$count = count($userObjs);
foreach($userObject as $userObj) {
    $userObj->joinGroup('GroupOne');
    $userObj->joinGroup('GroupTwo');
}
 
return '<p>' . $count . " Users Updated";

If you had a lot more users, I'd recommend using getIterator() instead of getCollection().