Commit a05043e6 authored by Richard Mansfield's avatar Richard Mansfield
Browse files

Add create_user() function; use it in 5 places users get created

parent de4f11e8
......@@ -214,43 +214,19 @@ function adduser_submit(Pieform $form, $values) {
}
$authinstance = get_record('auth_instance', 'id', $values['authinstance']);
$institution = new Institution($authinstance->institution);
db_begin();
$id = insert_record('usr', $user, 'id', true);
$user->id = $id;
$user->id = create_user($user, array(), $authinstance->institution, $authinstance, $values['remoteusername']);
if (isset($user->admin) && $user->admin) {
activity_add_admin_defaults(array($user->id));
}
if ($institution->name != 'mahara') {
$institution->addUserAsMember($user);
if ($values['institutionadmin']) {
set_field('usr_institution', 'admin', 1, 'usr', $user->id);
}
}
if ($authinstance->authname != 'internal') {
if (isset($values['remoteusername']) && strlen($values['remoteusername']) > 0) {
$un = $values['remoteusername'];
}
else {
$un = $user->username;
}
insert_record('auth_remote_user', (object) array(
'authinstance' => $authinstance->id,
'remoteusername' => $un,
'localusr' => $user->id,
));
if ($values['institutionadmin']) {
set_field('usr_institution', 'admin', 1, 'usr', $user->id, 'institution', $authinstance->institution);
}
// Set profile fields
foreach (array('firstname', 'lastname', 'email') as $field) {
set_profile_field($id, $field, $user->{$field});
}
handle_event('createuser', $user);
db_commit();
try {
......@@ -263,7 +239,7 @@ function adduser_submit(Pieform $form, $values) {
$SESSION->add_error_msg(get_string('newuseremailnotsent', 'admin'));
}
redirect('/admin/users/edit.php?id='.$id);
redirect('/admin/users/edit.php?id='.$user->id);
}
$smarty = smarty();
......
......@@ -301,6 +301,7 @@ function uploadcsv_submit(Pieform $form, $values) {
log_info('Inserting users from the CSV file');
db_begin();
$authobj = get_record('auth_instance', 'id', $authinstance);
$addedusers = array();
foreach ($CSVDATA as $record) {
log_debug('adding user ' . $record[$formatkeylookup['username']]);
......@@ -319,29 +320,17 @@ function uploadcsv_submit(Pieform $form, $values) {
$user->preferredname = $record[$formatkeylookup['preferredname']];
}
$user->passwordchange = (int)$values['forcepasswordchange'];
$id = insert_record('usr', $user, 'id', true);
$user->id = $id;
if ($institution->name != 'mahara') {
$institution->addUserAsMember($user);
}
if (get_field('auth_instance', 'authname', 'id', $authinstance) != 'internal') {
// Assume the admin knows what they're doing when they choose the external auth instance.
delete_records('auth_remote_user', 'authinstance', $authinstance, 'remoteusername', $user->username);
insert_record('auth_remote_user', (object) array(
'authinstance' => $authinstance,
'remoteusername' => $user->username,
'localusr' => $id,
));
}
$profilefields = new StdClass;
foreach ($FORMAT as $field) {
if ($field == 'username' || $field == 'password') {
continue;
}
set_profile_field($id, $field, $record[$formatkeylookup[$field]]);
$profilefields->{$field} = $record[$formatkeylookup[$field]];
}
handle_event('createuser', $user);
$user->id = create_user($user, $profilefields, $institution, $authobj);
if ($values['emailusers']) {
$addedusers[] = $user;
}
......
......@@ -1236,23 +1236,7 @@ function login_submit(Pieform $form, $values) {
$USER->email = null;
}
try {
db_begin();
$USER->commit();
if (isset($userdata->firstname)) {
set_profile_field($USER->id, 'firstname', $USER->firstname);
}
if (isset($userdata->lastname)) {
set_profile_field($USER->id, 'lastname', $USER->lastname);
}
if (isset($userdata->email)) {
set_profile_field($USER->id, 'email', $USER->email);
}
if ($authinstance->institution !== 'mahara') {
$USER->join_institution($authinstance->institution);
}
handle_event('createuser', $USER->to_stdclass());
db_commit();
create_user($USER, array(), $institution);
$USER->reanimate($USER->id, $authinstance->id);
}
catch (Exception $e) {
......
......@@ -214,26 +214,8 @@ class AuthXmlrpc extends Auth {
db_begin();
$user->username = get_new_username($remoteuser->username);
$user->commit();
// Make sure that there's no remote user record for this user. This
// can happen when a user has SSOed in, and then been deleted from
// Mahara.
//
// This makes undeleting the old user record "interesting", because
// now we don't have the remoteuser record for them...
delete_records('auth_remote_user', 'authinstance', $user->authinstance, 'remoteusername', $remoteuser->username);
insert_record('auth_remote_user', (object) array(
'authinstance' => $user->authinstance,
'remoteusername' => $remoteuser->username,
'localusr' => $user->id,
));
$user->join_institution($peer->institution);
set_profile_field($user->id, 'firstname', $user->firstname);
set_profile_field($user->id, 'lastname', $user->lastname);
set_profile_field($user->id, 'email', $user->email);
$user->id = create_user($user, array(), $peer->institution, $this, $remoteuser->username);
$this->import_user_settings($user, $remoteuser);
......@@ -245,7 +227,6 @@ class AuthXmlrpc extends Auth {
*/
$userobj = $user->to_stdclass();
$userarray = (array)$userobj;
handle_event('createuser', $userarray);
db_commit();
// Now we have fired the create event, we need to re-get the data
......
......@@ -1080,4 +1080,55 @@ function addfriend_submit(Pieform $form, $values) {
redirect('/user/view.php?id=' . $values['id']);
}
function create_user($user, $profile=array(), $institution=null, $remoteauth=null, $remotename=null) {
db_begin();
if ($user instanceof User) {
$user->commit();
$user = $user->to_stdclass();
}
else {
$user->id = insert_record('usr', $user, 'id', true);
}
set_profile_field($user->id, 'email', $user->email);
set_profile_field($user->id, 'firstname', $user->firstname);
set_profile_field($user->id, 'lastname', $user->lastname);
foreach ($profile as $k => $v) {
if (in_array($k, array('firstname', 'lastname', 'email'))) {
continue;
}
set_profile_field($user->id, $k, $v);
}
if (!empty($institution) && $institution != 'mahara') {
if (is_string($institution)) {
$institution = new Institution($institution);
}
if ($institution->name != 'mahara') {
$institution->addUserAsMember($user);
}
}
if (!empty($remoteauth) && $remoteauth->authname != 'internal') {
if (isset($remotename) && strlen($remotename) > 0) {
$un = $remotename;
}
else {
$un = $user->username;
}
delete_records('auth_remote_user', 'authinstance', $user->authinstance, 'remoteusername', $un);
insert_record('auth_remote_user', (object) array(
'authinstance' => $user->authinstance,
'remoteusername' => $un,
'localusr' => $user->id,
));
}
handle_event('createuser', $user);
db_commit();
return $user->id;
}
?>
......@@ -80,6 +80,7 @@ if (isset($key)) {
function create_registered_user($profilefields=array()) {
global $registration, $SESSION, $USER;
require_once(get_config('libroot') . 'user.php');
db_begin();
......@@ -108,16 +109,11 @@ if (isset($key)) {
$user->firstname = $registration->firstname;
$user->lastname = $registration->lastname;
$user->email = $registration->email;
$user->commit();
$user->add_institution_request($registration->institution);
create_user($user, $profilefields);
$registration->id = $user->id;
$user->add_institution_request($registration->institution);
// Insert standard stuff as artefacts
set_profile_field($user->id, 'email', $registration->email);
set_profile_field($user->id, 'firstname', $registration->firstname);
set_profile_field($user->id, 'lastname', $registration->lastname);
if (!empty($registration->lang) && $registration->lang != 'default') {
set_account_preference($user->id, 'lang', $registration->lang);
}
......@@ -125,17 +121,7 @@ if (isset($key)) {
// Delete the old registration record
delete_records('usr_registration', 'id', $registrationid);
// Set mandatory profile fields
foreach(ArtefactTypeProfile::get_mandatory_fields() as $field => $type) {
// @todo here and above, use the method for getting "always mandatory" fields
if (in_array($field, array('firstname', 'lastname', 'email'))) {
continue;
}
set_profile_field($user->id, $field, $profilefields[$field]);
}
db_commit();
handle_event('createuser', $registration);
// Log the user in and send them to the homepage
$USER = new LiveUser();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment