Skip to content

Instantly share code, notes, and snippets.

@katzueno
Last active August 6, 2018 04:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save katzueno/cbcdef051c2c5882d07bf1c0c9687661 to your computer and use it in GitHub Desktop.
Save katzueno/cbcdef051c2c5882d07bf1c0c9687661 to your computer and use it in GitHub Desktop.
Batch Modify Users

Batch Modify Users job for concrete5

(c) 2018. concrete5 Japan, Inc. (Katz Ueno) MIT License.

About

This is concrete5 Job to batch edit all existing users to make anonymouse or edit.

For example, you have the production site. You may want to copy the data to test environment. When you copy the production data, you may want to erase all of exisiting user information to protect their privacy upon test.

Production & Default environment protection

concrete5 can set environment. If this concrete5 environment is set as production or default, this job WILL NOT RUN for safety measurement.

Install

  1. Upload the file to /application/jobs/batch_modify_users.php
  2. Edit around line 55~68, due to the concrete5 restriction, you may not be able to apply multiple filters (checked upto 8.4.1).
  3. Edit around line 114-115 if you have additional user attributes that needs to be clean.
  4. Vsit: Dashboard - System and Settings - Optimization - Automated Job
  5. Install Batch Modify Users Job
  6. Move to non-production or non-default environment concrete5 site, then run a automated job.
<?php
namespace Application\Job;
use Concrete\Core\User\UserInfo;
use Core;
use Config;
use View;
class BatchModifyUsers extends \Concrete\Core\Job\QueueableJob
{
/**
* @return mixed
*/
public function getJobName()
{
return t('Batch Modify Users');
}
/**
* @return mixed
*/
public function getJobDescription()
{
return t('An automated job to modify the existing user data for non-production environment');
}
/**
* @param \ZendQueue\Queue $q
* @return mixed
*/
public function start(\ZendQueue\Queue $q)
{
/*
* Production Safety Check
* Enable it if you want additional human error prevention.
*
*/
$request = \Concrete\Core\Http\Request::getInstance();
$app = \Concrete\Core\Support\Facade\Application::getFacadeApplication();
$environment = ($app->environment()) ? $app->environment() : 'default';
if (
($environment == 'production') ||
($environment == 'default')
) {
die;
}
// Get all users
$list = new \Concrete\Core\User\UserList();
/*
* Filter the users
* Comment out lines and apply the settings
*/
// Exclude users whose uID is less than 1 (exclude superadmin)
$list->filter('u.uID', 1,'>');
// Exclude users who has never logged in.
// $list->filter('u.uNumLogins', 1, '<');
// Exclude users who is active
// $list->filterByIsActive(false);
// Exclude users who is validated
// $list->filterByIsValidated(false);
// Exclude users who was added at certain date
// $list->filterByDateAdded($date, '<');
// Exclude Administrators group users
// $list->filterByGroup('Administrators', false);
// Include users who are in any of the $groups (array of \Concrete\Core\User\Group\Group instance)
// $list->filterByInAnyGroup($groups, true)
/*
* Sending user IDs to job queue to execute
*
*/
$results = $list->executeGetResults();
foreach ($results as $queryRow) {
$q->send($queryRow['uID']);
}
}
/**
* @param \ZendQueue\Queue $q
* @return mixed
*/
public function finish(\ZendQueue\Queue $q)
{
return t('All tasks processed');
}
/**
* @param \ZendQueue\Message $msg
* @return mixed
*/
public function processQueueItem(\ZendQueue\Message $msg)
{
$error = Core::make('helper/validation/error');
$vs = Core::make('helper/validation/strings');
$ui = UserInfo::getByID($msg->body);
$em = $ui->getUserEmail();
if ($em) {
try {
if (!$vs->email($em)) {
throw new \Exception(t('Invalid user who\'s email address is ') . " | uid: " . $msg->body);
}
$oUser = UserInfo::getByEmail($em);
if ($oUser) {
$userName = '';
$data = [];
$userName = $oUser->getUserID();
// Add additional process if you have extra user attrivutes
//$oUser->setAttribute('name', 'Last_'.$userName);
$data = [
'uName' => $userName,
'uEmail' => $userName . '@example.com',
];
$oUser->update($data);
}
} catch (\Exception $e) {
$error->add($e);
}
//@TODO: Error handling
// if ($error->has()) {
// $this->set('error', $error);
// }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment