AD UPN changed, users can't access Moodle Accounts

AD UPN changed, users can't access Moodle Accounts

by Grant Wright -
Number of replies: 2

Due to other organizational needs our users UPN has need to be changed.

With our current moodle setup this causes users not to be able access their account. I can manually set the new UPN in a users account to the new one. Is there a way to do this in bulk?

currently it is a k12.xx.us and I need to change it to  xx.org

Many thanks!


Average of ratings: -
In reply to Grant Wright

Re: AD UPN changed, users can't access Moodle Accounts

by Dave Perry -
Picture of Testers

We had to do this for student email addresses when we switched to O365. Write a script in PHP. Something like:


<?php
$db = MySQL_connect('dbserver','dbusername','dbpassword'); //check your siteroot/config.php for this info
$db = MySQL_select_db('dbname',$db); //dbname will also be in config.php

$users_sql = "SELECT id,username FROM mdl_user WHERE username LIKE '%@k12.xx.us'";
$users_query = MySQL_query($users_sql);
while( $user = MySQL_fetch_object($users_query) ) {
 $new_username = str_replace("k12.xx.us","xx.org",$user->username);
 $update_sql = "UPDATE mdl_user SET username='" . $new_username  . "' WHERE id=" . $user->id;
 echo '<p>Updating email address ' . $user->username . ' to ' . $new_username . ' for user ID ' . $user->id . '</p>';
 //uncomment the next line when you have run the script and checked it will do the correct thing for all users
 //MySQL_query($update_sql);
}
?>

This will look in the username field of all of your users (who have @k12.xx.us in the value), go through each one and amend the email address. And tell you what it's doing.

When you've put your database details in the 2nd and 3rd lines, and run it in a browser to check it'll work correctly, then uncomment the 3rd from last line (MySQL_query to run the update).

NOTE - the word MySQL in the above script is actually meant to be in lower case - the forum editor is autocorrecting it.

HTH


In reply to Dave Perry

Re: AD UPN changed, users can't access Moodle Accounts

by Grant Wright -

Thank you! Exactly what I was looking for.

Yes we are in the middle of an O365 transition.