Skip to content

Instantly share code, notes, and snippets.

@millipedia
Created November 5, 2020 10:31
Show Gist options
  • Save millipedia/27721928b6f9eaaaa83c37bda829ccf9 to your computer and use it in GitHub Desktop.
Save millipedia/27721928b6f9eaaaa83c37bda829ccf9 to your computer and use it in GitHub Desktop.
ProcessWire user import
<?php namespace ProcessWire;
/**
* 201105
*
* Quick script to read in a CSV of users and create
* users in ProcessWire's LoginRegisterPro module.
* Place this in the root of your PW install and then open /user_import.php
* Yeah - sure it could be done in a shell script or in Tracy but this is
* pretty simple.
*
* s.
*
*/
require("index.php");
?>
<html>
<head></head>
<body>
<ul>
<?php
$loginRegister = $modules->get('LoginRegisterPro');
// get whatever custom role you want to assign
$role = $roles->get('login-register');
// In case you need to delete any users you imported badly before ... ahem.
// $users = $pages->find("template=user");
// foreach($users as $u) { // don't use $user
// if($u->hasRole('login-register') && !$u->hasRole('superuser')){
// if($u->name !='millipedia_gmail.com' && $u->name !='api_user'){
// echo "<li>Deleting: $u->name</li>";
// $u->delete();
// }
// }
// }
// read the data from a csv file - no linebreaks allowed
$userData = array_map('str_getcsv', file('userdata.csv'));
foreach($userData as $data) {
// our csv is email, password, display_name
// make sure you match this up.
// display_name is an extra field
// we added (fieldname user_name) - you may not want it or have it.
$email=$data[0];
$pass=$data[1];
$display_name=$data[2]; // take out if not needed
if(!$loginRegister->allowUserEmail($email)) {
// this user cannot be imported because email
// is invalid, in blacklist, or collides with another
echo "<li>Cannot add user: $email</li>";
continue;
}else{
$u = new User();
$u->name = $loginRegister->emailToName($email);
$u->email = $email;
$u->pass = $pass;
$u->roles->add($role);
$u->user_name = $display_name; // take out if not needed
$u->save();
echo "<li>Added new user: $email</li>";
}
}
?>
</ul>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment