Skip to content

Instantly share code, notes, and snippets.

@adamfranco
Created November 18, 2019 18:37
Show Gist options
  • Save adamfranco/b299378d26bd62cc2cab223664524123 to your computer and use it in GitHub Desktop.
Save adamfranco/b299378d26bd62cc2cab223664524123 to your computer and use it in GitHub Desktop.
Custom callback functions to execute on user login.
// this function gets executed
// if the CAS username doesn't match a username in WordPress
function wpcas_nowpuser( $user_name ) {
midd_wpcas_nowpuser($user_name);
// Authenticate again now that the user exists
$wpCAS = new wpCAS;
$wpCAS->authenticate();
}
function midd_wpcas_nowpuser($username) {
require_once( ABSPATH . WPINC . '/registration.php' );
$email = phpCAS::getAttribute('EMail');
if (!preg_match('/^.+@.+$/', $email)) {
var_dump(phpCAS::getAttributes());
die('No valid email, could not provision account.');
}
$user_id = wpmu_create_user($username, md5(rand().$username), $email);
$firstName = phpCAS::getAttribute('FirstName');
$lastName = phpCAS::getAttribute('LastName');
preg_match('/^(.+)@(.+)$/', $email, $matches);
$emailUser = $matches[1];
$emailDomain = $matches[2];
if (!$firstName && !$lastName) {
$lastName = $emailUser;
}
// $displayName = trim($firstName.' '.$lastName.' ('.$emailDomain.')');
$displayName = trim($firstName.' '.$lastName);
if ($login = phpCAS::getAttribute('Login'))
$nicename = $login;
else
$nicename = $emailUser;
$data = array();
$data['ID'] = $user_id;
$data['user_login'] = $username;
$data['user_email'] = $email;
$data['user_nicename'] = $nicename;
$data['nickname'] = $nicename;
$data['display_name'] = $displayName;
$data['first_name'] = $firstName;
$data['last_name'] = $lastName;
wp_update_user($data);
// If we wish to provision a site for the user, now would be the time to do it.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment