Skip to content

Instantly share code, notes, and snippets.

@jpdevries
Last active November 23, 2020 11:39
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpdevries/7c17dce93df93ca932e2 to your computer and use it in GitHub Desktop.
Save jpdevries/7c17dce93df93ca932e2 to your computer and use it in GitHub Desktop.
Duplicate MODX User Group from Command Line
<?php
/**
* Duplicates a MODX User Group
* USAGE:
* php duplicateusergroup.php 1 New\ Group
* php duplicateusergroup.php 1 New\ Group newuser you@you.com password
*/
$tstart = microtime(true);
set_time_limit(0);
require '/path/to/your/MODX/root/config.core.php';
require MODX_CORE_PATH . 'model/modx/modx.class.php';
$modx = modX::getInstance();
$modx->initialize('mgr');
$modx->getService('error','error.modError', '', '');
$modx->setLogLevel(modX::LOG_LEVEL_INFO);
$modx->setLogTarget('ECHO');
$clone = $argv[1];
$groupName = $argv[2];
$userName = $argv[3]; //optional. must set userEmail and password if setting userName
$userEmail = $argv[4];
$password = $argv[5];
// create a new user group and name it
$userGroup = $modx->newObject('modUserGroup');
$userGroup->set('name',$groupName);
// get the group we will be cloning
$adminGroup = $modx->getObject('modUserGroup',$clone);
$adminUsers = $adminGroup->getUsersIn();
if($userGroup->save()) { // make sure we can save the user group
foreach($adminUsers as $adminUser) { // loop through all it's users
//$profile = $adminUser->getOne('Profile'); // just because you might want this
$role = 0;
$userGroupMember = $modx->getObject('modUserGroupMember', array('user_group'=>$adminGroup->get('id'), 'member'=>$adminUser->get('id')));
$role = $userGroupMember->get('role');
$adminUser->joinGroup($userGroup->get('id'), $role); // put each user iin the new group
$modx->log(modX::LOG_LEVEL_INFO, "{$adminUser->get('username')} joined group {$userGroup->get('name')}");
}
}
// allows you to add a user from the command line as well
if (isset($userName) && isset($userEmail) && isset($password)) {
$user = $modx->newObject('modUser', array('username'=>$userName, 'password'=>$password));
$profile = $modx->newObject('modUserProfile', array(
'fullname' => $groupName,
'email' => $userEmail
));
$user->addOne($profile);
if ($user->save()) {
$role = 0; //http://forums.modx.com/thread/?thread=79466
$user->joinGroup($userGroup->get('id'),$role);
$modx->log(modX::LOG_LEVEL_INFO, "$userName joined group $groupName");
} else {
$modx->log(modX::LOG_LEVEL_ERROR, "Unable to create $userName user.");
}
}
$tend = microtime(true);
$totalTime= sprintf("%2.4f seconds", ($tend - $tstart));
$modx->log(modX::LOG_LEVEL_INFO, "DuplicateUserGroup executed in {$totalTime}");
@jpdevries
Copy link
Author

The duplicateusergroup script requires 2 parameters and accepts an additional three

  • id of user group to duplicate
  • name of new user group
  • name of a new user to create and add to group (optional)
  • email address of new user (required if creating new user)
  • password of new user (required if creating new user)

Usage

php duplicateusergroup.php 1 New\ Group newuser you@you.com password

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment