Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Batch-creating users in MODX
<?php
// If running from an external php file instead of as a snippet on a MODX resource, first fetch the MODX object with the code from http://rtfm.modx.com/display/revolution20/Loading+MODx+Externally
// Prepare the data in this format:
$accounts = array(
array(
'username' => 'someone',
'password' => 'unencrypted', // Will be encrypted in the below process
'usergroups' => 'Administrator:2', // will set the user with a role of super user (= role 2) in the Administrator user group
'fullname' => 'John Doe',
'email' => 'someone@gmail.com',
// Can use any modUser or modUserProfile fields here. See core/model/schema/modx.mysql.schema.php (or something like that) for all fields you could set.
),
array(
'username' => 'anotherone5',
'password' => 'unencrypted1eG$$%', // Will be encrypted on save
'usergroups' => 'Administrator:2', // will set the user with a role of super user (= role 2) in the Administrator user group
'fullname' => 'My Employee',
'email' => 'someemployee@company.com',
// Can use any modUser or modUserProfile fields here. See core/model/schema/modx.mysql.schema.php (or something like that) for all fields you could set.
),
);
// Now let's start processing, based on the Login addons Register processor and UNTESTED. Don't try this on a live install right away, please. Not responsible for anything that may happen.
foreach ($accounts as $fields) {
/* create user and profile */
$user = $modx->newObject('modUser');
$profile = $modx->newObject('modUserProfile');
/* allow overriding of class key */
if (empty($fields['class_key'])) $fields['class_key'] = 'modUser';
/* set user and profile */
$user->fromArray($fields);
$user->set('username',$fields['username']);
$user->set('active',1); // Change to 1 to set user as active
$version = $modx->getVersionData();
$user->set('password',$fields['password']);
$profile->fromArray($fields);
$profile->set('internalKey',0);
$user->addOne($profile,'Profile');
/* if usergroups set */
$usergroups = $fields['usergroups'];
if (!empty($usergroups)) {
$usergroups = explode(',',$usergroups);
foreach ($usergroups as $usergroupMeta) {
$usergroupMeta = explode(':',$usergroupMeta);
if (empty($usergroupMeta[0])) continue;
/* get usergroup */
$pk = array();
$pk[intval($usergroupMeta[0]) > 0 ? 'id' : 'name'] = $usergroupMeta[0];
$usergroup = $modx->getObject('modUserGroup',$pk);
if (!$usergroup) continue;
/* get role */
$rolePk = !empty($usergroupMeta[1]) ? $usergroupMeta[1] : 'Member';
$role = $modx->getObject('modUserGroupRole',array('name' => $rolePk));
/* create membership */
$member = $modx->newObject('modUserGroupMember');
$member->set('member',0);
$member->set('user_group',$usergroup->get('id'));
if (!empty($role)) {
$member->set('role',$role->get('id'));
} else {
$member->set('role',1);
}
$user->addMany($member,'UserGroupMembers');
}
}
/* save user */
if (!$user->save()) {
$modx->log(modX::LOG_LEVEL_ERROR,'[Login] Could not save newly registered user: '.$user->get('id').' with username: '.$user->get('username'));
return 'Error saving user '.print_r($scriptProperties,true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.