/** * Set stuff that needs to be initialised once before a user record is created. */ public function create() { $this->set('ctime', time()); if (get_config('cleanurls') && is_null($this->urlid)) { $desiredurlid = generate_urlid($this->username, get_config('cleanurluserdefault'), 3, 30); $this->set('urlid', get_new_profile_urlid($desiredurlid)); } }
/** * Create user * * @param object $user stdclass or User object for the usr table * @param array $profile profile field/values to set * @param string|object $institution Institution the user should joined to (name or Institution object) * @param bool $remoteauth authinstance record for a remote authinstance * @param string $remotename username on the remote site * @param array $accountprefs user account preferences to set * @return integer id of the new user */ function create_user($user, $profile = array(), $institution = null, $remoteauth = null, $remotename = null, $accountprefs = array(), $quickhash = false) { db_begin(); if ($user instanceof User) { $user->create(); $user->quota_init(); $user->commit(); $user = $user->to_stdclass(); } else { $user->ctime = db_format_timestamp(time()); // Ensure this user has a profile urlid if (get_config('cleanurls') && (!isset($user->urlid) || is_null($user->urlid))) { $user->urlid = generate_urlid($user->username, get_config('cleanurluserdefault'), 3, 30); $user->urlid = get_new_profile_urlid($user->urlid); } if (empty($user->quota)) { $user->quota = get_config_plugin('artefact', 'file', 'defaultquota'); } if (get_config('defaultaccountlifetime')) { // we need to set the user expiry to the site default one $user->expiry = date('Y-m-d', mktime(0, 0, 0, date('m'), date('d'), date('Y')) + (int) get_config('defaultaccountlifetime')); } $user->id = insert_record('usr', $user, 'id', true); } if (isset($user->email) && $user->email != '') { set_profile_field($user->id, 'email', $user->email, TRUE); } if (isset($user->firstname) && $user->firstname != '') { set_profile_field($user->id, 'firstname', $user->firstname, TRUE); } if (isset($user->lastname) && $user->lastname != '') { set_profile_field($user->id, 'lastname', $user->lastname, TRUE); } foreach ($profile as $k => $v) { if (in_array($k, array('firstname', 'lastname', 'email'))) { continue; } set_profile_field($user->id, $k, $v, TRUE); } if (!empty($institution)) { if (is_string($institution)) { $institution = new Institution($institution); } if ($institution->name != 'mahara') { $institution->addUserAsMember($user); // uses $user->newuser if (empty($accountprefs['licensedefault'])) { $accountprefs['licensedefault'] = LICENSE_INSTITUTION_DEFAULT; } } } $authobj = get_record('auth_instance', 'id', $user->authinstance); $authinstance = AuthFactory::create($authobj->id); // For legacy compatibility purposes, we'll also put the remote auth on there if it has been // specifically requested. if ($authinstance->needs_remote_username() || !empty($remoteauth)) { if (isset($remotename) && strlen($remotename) > 0) { $un = $remotename; } else { $un = $user->username; } // remote username must not already exist if (record_exists('auth_remote_user', 'remoteusername', $un, 'authinstance', $user->authinstance)) { throw new InvalidArgumentException("user_create: remoteusername already exists: ({$un}, {$user->authinstance})"); } insert_record('auth_remote_user', (object) array('authinstance' => $user->authinstance, 'remoteusername' => $un, 'localusr' => $user->id)); } // Set account preferences if (!empty($accountprefs)) { $expectedprefs = expected_account_preferences(); foreach ($expectedprefs as $eprefkey => $epref) { if (isset($accountprefs[$eprefkey]) && $accountprefs[$eprefkey] != $epref) { set_account_preference($user->id, $eprefkey, $accountprefs[$eprefkey]); } } } // Copy site views and collections to the new user's profile $userobj = new User(); $userobj->find_by_id($user->id); $userobj->copy_site_views_collections_to_new_user(); reset_password($user, false, $quickhash); handle_event('createuser', $user); db_commit(); return $user->id; }