Ejemplo n.º 1
0
function truncate_user($userobj)
{
    $user_array = truncate_userinfo((array) $userobj);
    $obj = new stdClass();
    foreach ($user_array as $key => $value) {
        $obj->{$key} = $value;
    }
    return $obj;
}
Ejemplo n.º 2
0
 /**
  * will update a local user record from an external source.
  * is a lighter version of the one in moodlelib -- won't do
  * expensive ops such as enrolment
  *
  * If you don't pass $updatekeys, there is a performance hit and
  * values removed from DB won't be removed from moodle.
  *
  * @param string $username username (with system magic quotes)
  */
 function update_user_record($username, $updatekeys = false)
 {
     global $CFG;
     //just in case check text case
     $username = trim(moodle_strtolower($username));
     // get the current user record
     $user = get_record('user', 'username', $username, 'mnethostid', $CFG->mnet_localhost_id);
     if (empty($user)) {
         // trouble
         error_log("Cannot update non-existent user: {$username}");
         print_error('auth_dbusernotexist', 'auth', $username);
         die;
     }
     // Ensure userid is not overwritten
     $userid = $user->id;
     if ($newinfo = $this->get_userinfo($username)) {
         $newinfo = truncate_userinfo($newinfo);
         if (empty($updatekeys)) {
             // all keys? this does not support removing values
             $updatekeys = array_keys($newinfo);
         }
         foreach ($updatekeys as $key) {
             if (isset($newinfo[$key])) {
                 $value = $newinfo[$key];
             } else {
                 $value = '';
             }
             if (!empty($this->config->{'field_updatelocal_' . $key})) {
                 if ($user->{$key} != $value) {
                     // only update if it's changed
                     set_field('user', $key, addslashes($value), 'id', $userid);
                 }
             }
         }
     }
     return get_record_select('user', "id = {$userid} AND deleted = 0");
 }
Ejemplo n.º 3
0
/**
 * Will update a local user record from an external source (MNET users can not be updated using this method!).
 *
 * @param int $id user id
 * @return stdClass A complete user object
 */
function update_user_record_by_id($id)
{
    global $DB, $CFG;
    require_once $CFG->dirroot . "/user/profile/lib.php";
    require_once $CFG->dirroot . '/user/lib.php';
    $params = array('mnethostid' => $CFG->mnet_localhost_id, 'id' => $id, 'deleted' => 0);
    $oldinfo = $DB->get_record('user', $params, '*', MUST_EXIST);
    $newuser = array();
    $userauth = get_auth_plugin($oldinfo->auth);
    if ($newinfo = $userauth->get_userinfo($oldinfo->username)) {
        $newinfo = truncate_userinfo($newinfo);
        $customfields = $userauth->get_custom_user_profile_fields();
        foreach ($newinfo as $key => $value) {
            $iscustom = in_array($key, $customfields);
            if (!$iscustom) {
                $key = strtolower($key);
            }
            if (!property_exists($oldinfo, $key) && !$iscustom or $key === 'username' or $key === 'id' or $key === 'auth' or $key === 'mnethostid' or $key === 'deleted') {
                // Unknown or must not be changed.
                continue;
            }
            $confval = $userauth->config->{'field_updatelocal_' . $key};
            $lockval = $userauth->config->{'field_lock_' . $key};
            if (empty($confval) || empty($lockval)) {
                continue;
            }
            if ($confval === 'onlogin') {
                // MDL-4207 Don't overwrite modified user profile values with
                // empty LDAP values when 'unlocked if empty' is set. The purpose
                // of the setting 'unlocked if empty' is to allow the user to fill
                // in a value for the selected field _if LDAP is giving
                // nothing_ for this field. Thus it makes sense to let this value
                // stand in until LDAP is giving a value for this field.
                if (!(empty($value) && $lockval === 'unlockedifempty')) {
                    if ($iscustom || in_array($key, $userauth->userfields) && (string) $oldinfo->{$key} !== (string) $value) {
                        $newuser[$key] = (string) $value;
                    }
                }
            }
        }
        if ($newuser) {
            $newuser['id'] = $oldinfo->id;
            $newuser['timemodified'] = time();
            user_update_user((object) $newuser, false, false);
            // Save user profile data.
            profile_save_data((object) $newuser);
            // Trigger event.
            \core\event\user_updated::create_from_userid($newuser['id'])->trigger();
        }
    }
    return get_complete_user_data('id', $oldinfo->id);
}
Ejemplo n.º 4
0
/**
 * Will update a local user record from an external source
 *
 * @uses $CFG
 * @param string $username New user's username to add to record
 * @return user A {@link $USER} object
 */
function update_user_record($username, $authplugin)
{
    $username = trim(moodle_strtolower($username));
    /// just in case check text case
    $oldinfo = get_record('user', 'username', $username, '', '', '', '', 'username, auth');
    $userauth = get_auth_plugin($oldinfo->auth);
    if ($newinfo = $userauth->get_userinfo($username)) {
        $newinfo = truncate_userinfo($newinfo);
        foreach ($newinfo as $key => $value) {
            if ($key === 'username') {
                // 'username' is not a mapped updateable/lockable field, so skip it.
                continue;
            }
            $confval = $userauth->config->{'field_updatelocal_' . $key};
            $lockval = $userauth->config->{'field_lock_' . $key};
            if (empty($confval) || empty($lockval)) {
                continue;
            }
            if ($confval === 'onlogin') {
                $value = addslashes($value);
                // MDL-4207 Don't overwrite modified user profile values with
                // empty LDAP values when 'unlocked if empty' is set. The purpose
                // of the setting 'unlocked if empty' is to allow the user to fill
                // in a value for the selected field _if LDAP is giving
                // nothing_ for this field. Thus it makes sense to let this value
                // stand in until LDAP is giving a value for this field.
                if (!(empty($value) && $lockval === 'unlockedifempty')) {
                    set_field('user', $key, $value, 'username', $username) || error_log("Error updating {$key} for {$username}");
                }
            }
        }
    }
    return get_complete_user_data('username', $username);
}
Ejemplo n.º 5
0
 /**
  * Update a local user record from an external source.
  * This is a lighter version of the one in moodlelib -- won't do
  * expensive ops such as enrolment.
  *
  * If you don't pass $updatekeys, there is a performance hit and
  * values removed from LDAP won't be removed from moodle.
  *
  * @param string $username username
  * @param boolean $updatekeys true to update the local record with the external LDAP values.
  * @param bool $triggerevent set false if user_updated event should not be triggered.
  *             This will not affect user_password_updated event triggering.
  * @return stdClass|bool updated user record or false if there is no new info to update.
  */
 function update_user_record($username, $updatekeys = false, $triggerevent = false)
 {
     global $CFG, $DB;
     // Just in case check text case
     $username = trim(core_text::strtolower($username));
     // Get the current user record
     $user = $DB->get_record('user', array('username' => $username, 'mnethostid' => $CFG->mnet_localhost_id));
     if (empty($user)) {
         // trouble
         error_log($this->errorlogtag . get_string('auth_dbusernotexist', 'auth_db', '', $username));
         print_error('auth_dbusernotexist', 'auth_db', '', $username);
         die;
     }
     // Protect the userid from being overwritten
     $userid = $user->id;
     if ($newinfo = $this->get_userinfo($username)) {
         $newinfo = truncate_userinfo($newinfo);
         if (empty($updatekeys)) {
             // all keys? this does not support removing values
             $updatekeys = array_keys($newinfo);
         }
         if (!empty($updatekeys)) {
             $newuser = new stdClass();
             $newuser->id = $userid;
             foreach ($updatekeys as $key) {
                 if (isset($newinfo[$key])) {
                     $value = $newinfo[$key];
                 } else {
                     $value = '';
                 }
                 if (!empty($this->config->{'field_updatelocal_' . $key})) {
                     // Only update if it's changed.
                     if ($user->{$key} != $value) {
                         $newuser->{$key} = $value;
                     }
                 }
             }
             user_update_user($newuser, false, $triggerevent);
         }
     } else {
         return false;
     }
     return $DB->get_record('user', array('id' => $userid, 'deleted' => 0));
 }
Ejemplo n.º 6
0
 /**
  * will update a local user record from an external source.
  * is a lighter version of the one in moodlelib -- won't do
  * expensive ops such as enrolment
  *
  * If you don't pass $updatekeys, there is a performance hit and
  * values removed from DB won't be removed from moodle.
  *
  * @param string $username username
  * @param bool $updatekeys
  * @return stdClass
  */
 function update_user_record($username, $updatekeys = false)
 {
     global $CFG, $DB;
     //just in case check text case
     $username = trim(textlib::strtolower($username));
     // get the current user record
     $user = $DB->get_record('user', array('username' => $username, 'mnethostid' => $CFG->mnet_localhost_id));
     if (empty($user)) {
         // trouble
         error_log("Cannot update non-existent user: {$username}");
         print_error('auth_dbusernotexist', 'auth_db', $username);
         die;
     }
     // Ensure userid is not overwritten
     $userid = $user->id;
     $updated = false;
     if ($newinfo = $this->get_userinfo($username)) {
         $newinfo = truncate_userinfo($newinfo);
         if (empty($updatekeys)) {
             // all keys? this does not support removing values
             $updatekeys = array_keys($newinfo);
         }
         foreach ($updatekeys as $key) {
             if (isset($newinfo[$key])) {
                 $value = $newinfo[$key];
             } else {
                 $value = '';
             }
             if (!empty($this->config->{'field_updatelocal_' . $key})) {
                 if (isset($user->{$key}) and $user->{$key} != $value) {
                     // only update if it's changed
                     $DB->set_field('user', $key, $value, array('id' => $userid));
                     $updated = true;
                 }
             }
         }
     }
     if ($updated) {
         $DB->set_field('user', 'timemodified', time(), array('id' => $userid));
     }
     return $DB->get_record('user', array('id' => $userid, 'deleted' => 0));
 }
Ejemplo n.º 7
0
/**
 * Will update a local user record from an external source.
 * (MNET users can not be updated using this method!)
 *
 * @param string $username user's username to update the record
 * @return stdClass A complete user object
 */
function update_user_record($username)
{
    global $DB, $CFG;
    $username = trim(moodle_strtolower($username));
    /// just in case check text case
    $oldinfo = $DB->get_record('user', array('username' => $username, 'mnethostid' => $CFG->mnet_localhost_id), '*', MUST_EXIST);
    $newuser = array();
    $userauth = get_auth_plugin($oldinfo->auth);
    if ($newinfo = $userauth->get_userinfo($username)) {
        $newinfo = truncate_userinfo($newinfo);
        foreach ($newinfo as $key => $value) {
            $key = strtolower($key);
            if (!property_exists($oldinfo, $key) or $key === 'username' or $key === 'id' or $key === 'auth' or $key === 'mnethostid' or $key === 'deleted') {
                // unknown or must not be changed
                continue;
            }
            $confval = $userauth->config->{'field_updatelocal_' . $key};
            $lockval = $userauth->config->{'field_lock_' . $key};
            if (empty($confval) || empty($lockval)) {
                continue;
            }
            if ($confval === 'onlogin') {
                // MDL-4207 Don't overwrite modified user profile values with
                // empty LDAP values when 'unlocked if empty' is set. The purpose
                // of the setting 'unlocked if empty' is to allow the user to fill
                // in a value for the selected field _if LDAP is giving
                // nothing_ for this field. Thus it makes sense to let this value
                // stand in until LDAP is giving a value for this field.
                if (!(empty($value) && $lockval === 'unlockedifempty')) {
                    if ((string) $oldinfo->{$key} !== (string) $value) {
                        $newuser[$key] = (string) $value;
                    }
                }
            }
        }
        if ($newuser) {
            $newuser['id'] = $oldinfo->id;
            $DB->update_record('user', $newuser);
            // fetch full user record for the event, the complete user data contains too much info
            // and we want to be consistent with other places that trigger this event
            events_trigger('user_updated', $DB->get_record('user', array('id' => $oldinfo->id)));
        }
    }
    return get_complete_user_data('id', $oldinfo->id);
}
Ejemplo n.º 8
0
    /**
     * Create one or more users
     *
     * @param array $users  An array of users to create.
     * @return array An array of arrays
     */
    public static function create_users($users) {
        global $CFG, $DB;
        require_once($CFG->dirroot."/user/lib.php");
        require_once($CFG->dirroot."/user/profile/lib.php"); //required for customfields related function
                                                             //TODO: move the functions somewhere else as
                                                             //they are "user" related

        // Ensure the current user is allowed to run this function
        $context = get_context_instance(CONTEXT_SYSTEM);
        self::validate_context($context);
        require_capability('moodle/user:create', $context);

        // Do basic automatic PARAM checks on incoming data, using params description
        // If any problems are found then exceptions are thrown with helpful error messages
        $params = self::validate_parameters(self::create_users_parameters(), array('users'=>$users));

        $availableauths  = get_plugin_list('auth');
        unset($availableauths['mnet']);       // these would need mnethostid too
        unset($availableauths['webservice']); // we do not want new webservice users for now

        $availablethemes = get_plugin_list('theme');
        $availablelangs  = get_string_manager()->get_list_of_translations();

        $transaction = $DB->start_delegated_transaction();

        $userids = array();
        foreach ($params['users'] as $user) {
            // Make sure that the username doesn't already exist
            if ($DB->record_exists('user', array('username'=>$user['username'], 'mnethostid'=>$CFG->mnet_localhost_id))) {
                throw new invalid_parameter_exception('Username already exists: '.$user['username']);
            }

            // Make sure auth is valid
            if (empty($availableauths[$user['auth']])) {
                throw new invalid_parameter_exception('Invalid authentication type: '.$user['auth']);
            }

            // Make sure lang is valid
            if (empty($availablelangs[$user['lang']])) {
                throw new invalid_parameter_exception('Invalid language code: '.$user['lang']);
            }

            // Make sure lang is valid
            if (!empty($user['theme']) && empty($availablethemes[$user['theme']])) { //theme is VALUE_OPTIONAL,
                                                                                     // so no default value.
                                                                                     // We need to test if the client sent it
                                                                                     // => !empty($user['theme'])
                throw new invalid_parameter_exception('Invalid theme: '.$user['theme']);
            }

            // make sure there is no data loss during truncation
            $truncated = truncate_userinfo($user);
            foreach ($truncated as $key=>$value) {
                    if ($truncated[$key] !== $user[$key]) {
                        throw new invalid_parameter_exception('Property: '.$key.' is too long: '.$user[$key]);
                    }
            }

            $user['confirmed'] = true;
            $user['mnethostid'] = $CFG->mnet_localhost_id;
            $user['id'] = user_create_user($user);

            // custom fields
            if(!empty($user['customfields'])) {
                foreach($user['customfields'] as $customfield) {
                    $user["profile_field_".$customfield['type']] = $customfield['value']; //profile_save_data() saves profile file
                                                                                            //it's expecting a user with the correct id,
                                                                                            //and custom field to be named profile_field_"shortname"
                }
                profile_save_data((object) $user);
            }

            //preferences
            if (!empty($user['preferences'])) {
                foreach($user['preferences'] as $preference) {
                    set_user_preference($preference['type'], $preference['value'],$user['id']);
                }
            }

            $userids[] = array('id'=>$user['id'], 'username'=>$user['username']);
        }

        $transaction->allow_commit();

        return $userids;
    }
Ejemplo n.º 9
0
 /**
  * will update a local user record from an external source.
  * is a lighter version of the one in moodlelib -- won't do
  * expensive ops such as enrolment.
  *
  * If you don't pass $updatekeys, there is a performance hit and
  * values removed from DB won't be removed from moodle.
  *
  * @param string $username username
  * @param bool $updatekeys
  * @return stdClass
  */
 function update_user_record($username, $updatekeys = false)
 {
     global $CFG, $DB;
     //just in case check text case
     $username = trim(core_text::strtolower($username));
     // get the current user record
     $user = $DB->get_record('user', array('username' => $username, 'mnethostid' => $CFG->mnet_localhost_id));
     if (empty($user)) {
         // trouble
         error_log("Cannot update non-existent user: {$username}");
         print_error('auth_dbusernotexist', 'auth_db', $username);
         die;
     }
     // Ensure userid is not overwritten.
     $userid = $user->id;
     $needsupdate = false;
     $updateuser = new stdClass();
     $updateuser->id = $userid;
     if ($newinfo = $this->get_userinfo($username)) {
         $newinfo = truncate_userinfo($newinfo);
         if (empty($updatekeys)) {
             // All keys? This does not support removing values.
             $updatekeys = array_keys($newinfo);
         }
         foreach ($updatekeys as $key) {
             if (isset($newinfo[$key])) {
                 $value = $newinfo[$key];
             } else {
                 $value = '';
             }
             if (!empty($this->config->{'field_updatelocal_' . $key})) {
                 if (isset($user->{$key}) and $user->{$key} != $value) {
                     // Only update if it's changed.
                     $needsupdate = true;
                     $updateuser->{$key} = $value;
                 }
             }
         }
     }
     if ($needsupdate) {
         require_once $CFG->dirroot . '/user/lib.php';
         user_update_user($updateuser);
     }
     return $DB->get_record('user', array('id' => $userid, 'deleted' => 0));
 }
Ejemplo n.º 10
0
 /**
  * Create one or more users
  *
  * @param array $users  An array of users to create.
  * @return array An array of arrays
  */
 public static function create_users($users)
 {
     global $CFG, $DB;
     require_once $CFG->dirroot . "/lib/weblib.php";
     require_once $CFG->dirroot . "/user/lib.php";
     require_once $CFG->dirroot . "/user/profile/lib.php";
     //required for customfields related function
     //TODO: move the functions somewhere else as
     //they are "user" related
     // Ensure the current user is allowed to run this function
     $context = get_context_instance(CONTEXT_SYSTEM);
     self::validate_context($context);
     require_capability('moodle/user:create', $context);
     // Do basic automatic PARAM checks on incoming data, using params description
     // If any problems are found then exceptions are thrown with helpful error messages
     $params = self::validate_parameters(self::create_users_parameters(), array('users' => $users));
     $availableauths = get_plugin_list('auth');
     unset($availableauths['mnet']);
     // these would need mnethostid too
     unset($availableauths['webservice']);
     // we do not want new webservice users for now
     $availablethemes = get_plugin_list('theme');
     $availablelangs = get_string_manager()->get_list_of_translations();
     $transaction = $DB->start_delegated_transaction();
     $userids = array();
     foreach ($params['users'] as $user) {
         // Start of user info validation.
         //
         // Make sure that the username doesn't already exist
         if ($DB->record_exists('user', array('username' => $user['username'], 'mnethostid' => $CFG->mnet_localhost_id))) {
             $a = array('idnumber' => $user['idnumber'], 'username' => $user['username']);
             throw new moodle_exception('username_already_exists', 'local_sistemaaulaws', '', $a);
         }
         // Make sure auth is valid
         if (empty($availableauths[$user['auth']])) {
             $a = array('idnumber' => $user['idnumber'], 'username' => $user['username'], 'auth' => $user['auth']);
             throw new moodle_exception('invalid_authentication_type', 'local_sistemaaulaws', '', $a);
         }
         // Make sure lang is valid
         if (empty($availablelangs[$user['lang']])) {
             $a = new stdClass();
             $a->idnumber = $user['idnumber'];
             $a->name = $user['username'];
             $a->lang = $user['lang'];
             $a->object = get_string('object_name_user', 'local_sistemaaulaws');
             $a->object_text_name = get_string('object_text_name_user', 'local_sistemaaulaws');
             throw new modle('invalid_language_code', 'local_sistemaaulaws', '', $a);
         }
         // Make sure lang is valid
         if (!empty($user['theme']) && empty($availablethemes[$user['theme']])) {
             //theme is VALUE_OPTIONAL,
             // so no default value.
             // We need to test if the client sent it
             // => !empty($user['theme'])
             $a = new stdClass();
             $a->idnumber = $user['idnumber'];
             $a->name = $user['username'];
             $a->theme = $user['theme'];
             $a->object = get_string('object_name_user', 'local_sistemaaulaws');
             $a->object_text_name = get_string('object_text_name_user', 'local_sistemaaulaws');
             throw new modle_exception('invalid_theme', 'local_sistemaaulaws', '', $a);
         }
         if (!empty($user['email'])) {
             if (!validate_email($user['email'])) {
                 $a = new stdClass();
                 $a->idnumber = $user['idnumber'];
                 $a->name = $user['username'];
                 $a->email = $user['email'];
                 throw new moodle_exception('email_address_is_invalid', 'local_sistemaaulaws', $a);
             } else {
                 if ($DB->record_exists('user', array('email' => $user['email'], 'mnethostid' => $user['mnethostid']))) {
                     $a = new stdClass();
                     $a->idnumber = $user['idnumber'];
                     $a->name = $user['username'];
                     $a->email = $user['email'];
                     throw new moodle_exception('email_address_already_exists', 'local_sistemaaulaws', $a);
                 }
             }
         }
         //
         // End of user info validation.
         // CDCP: Achei válido deixar esta verificação em nossa função,
         // CDCP: é importante não perder nenhum dado.
         // CDCP: eles devem ser enviados no tamanho certo.
         //
         // make sure there is no data loss during truncation
         $truncated = truncate_userinfo($user);
         foreach ($truncated as $key => $value) {
             if ($truncated[$key] !== $user[$key]) {
                 $a = array('idnumber' => $user['idnumber'], 'username', 'key' => $key, 'value' => $user[$key]);
                 throw new moodle_exception('property_is_too_long', 'local_sistemaaulaws', '', $a);
             }
         }
         $user['confirmed'] = true;
         $user['mnethostid'] = $CFG->mnet_localhost_id;
         $user['id'] = user_create_user($user);
         // custom fields
         if (!empty($user['customfields'])) {
             foreach ($user['customfields'] as $customfield) {
                 $user["profile_field_" . $customfield['type']] = $customfield['value'];
                 //profile_save_data() saves profile file
                 //it's expecting a user with the correct id,
                 //and custom field to be named profile_field_"shortname"
             }
             profile_save_data((object) $user);
         }
         //preferences
         if (!empty($user['preferences'])) {
             foreach ($user['preferences'] as $preference) {
                 set_user_preference($preference['type'], $preference['value'], $user['id']);
             }
         }
         $userids[] = array('id' => $user['id'], 'idnumber' => $user['idnumber'], 'username' => $user['username']);
     }
     $transaction->allow_commit();
     return $userids;
 }
Ejemplo n.º 11
0
 function create_joomdle_user_record($username, $password, $auth = 'joomdle')
 {
     global $CFG, $DB;
     require_once $CFG->dirroot . '/user/profile/lib.php';
     require_once $CFG->dirroot . '/user/lib.php';
     // Just in case check text case.
     $username = trim(core_text::strtolower($username));
     $authplugin = get_auth_plugin($auth);
     $customfields = $authplugin->get_custom_user_profile_fields();
     $newuser = new stdClass();
     if ($newinfo = $authplugin->get_userinfo($username)) {
         $newinfo = truncate_userinfo($newinfo);
         foreach ($newinfo as $key => $value) {
             if (in_array($key, $authplugin->userfields) || in_array($key, $customfields)) {
                 $newuser->{$key} = $value;
             }
         }
     }
     if (!empty($newuser->email)) {
         if (email_is_not_allowed($newuser->email)) {
             unset($newuser->email);
         }
     }
     if (!isset($newuser->city)) {
         $newuser->city = '';
     }
     $newuser->auth = $auth;
     $newuser->username = $username;
     // Fix for MDL-8480
     // user CFG lang for user if $newuser->lang is empty
     // or $user->lang is not an installed language.
     if (empty($newuser->lang) || !get_string_manager()->translation_exists($newuser->lang)) {
         $newuser->lang = $CFG->lang;
     }
     $newuser->confirmed = 1;
     $newuser->lastip = getremoteaddr();
     $newuser->timecreated = time();
     $newuser->timemodified = $newuser->timecreated;
     $newuser->mnethostid = $CFG->mnet_localhost_id;
     $newuser->id = user_create_user($newuser, false, false);
     // Save user profile data.
     profile_save_data($newuser);
     /*
         $user = get_complete_user_data('id', $newuser->id);
         if (!empty($CFG->{'auth_'.$newuser->auth.'_forcechangepassword'})) {
             set_user_preference('auth_forcepasswordchange', 1, $user);
         }
         // Set the password.
         update_internal_user_password($user, $password);
     */
     // Trigger event.
     \core\event\user_created::create_from_userid($newuser->id)->trigger();
     return $newuser;
 }
Ejemplo n.º 12
0
/**
 * Will update a local user record from an external source
 *
 * @uses $CFG
 * @param string $username New user's username to add to record
 * @return user A {@link $USER} object
 */
function update_user_record($username, $authplugin)
{
    $username = trim(moodle_strtolower($username));
    /// just in case check text case
    $oldinfo = get_record('user', 'username', $username, '', '', '', '', 'username, auth');
    $userauth = get_auth_plugin($oldinfo->auth);
    if ($newinfo = $userauth->get_userinfo($username)) {
        $newinfo = truncate_userinfo($newinfo);
        foreach ($newinfo as $key => $value) {
            $confkey = 'field_updatelocal_' . $key;
            if (!empty($userauth->config->{$confkey}) and $userauth->config->{$confkey} === 'onlogin') {
                $value = addslashes(stripslashes($value));
                // Just in case
                set_field('user', $key, $value, 'username', $username) or error_log("Error updating {$key} for {$username}");
            }
        }
    }
    return get_complete_user_data('username', $username);
}
Ejemplo n.º 13
0
/**
 * Create one or more users
 *
 * @param array $users  An array of users to create.
 * @return array An array of arrays
 */
function create_users($users)
{
    global $CFG, $DB;
    require_once $CFG->dirroot . "/user/lib.php";
    require_once $CFG->dirroot . "/user/profile/lib.php";
    //required for customfields related function
    //TODO: move the functions somewhere else as
    //they are "user" related
    $availableauths = get_plugin_list('auth');
    $availablethemes = get_plugin_list('theme');
    $availablelangs = get_string_manager()->get_list_of_translations();
    $transaction = $DB->start_delegated_transaction();
    $userids = array();
    foreach ($users as $user) {
        // Make sure that the username doesn't already exist
        if ($DB->record_exists('user', array('username' => $user['username'], 'mnethostid' => $CFG->mnet_localhost_id))) {
            //            $userids[] = array('id' => $user['id'], 'username' => $user['username'], 'error'=>'Username already exists: ' . $user['username']);
            $user_rec = $DB->get_record('user', array('username' => $user['username']));
            $user['id'] = $user_rec->id;
            unset($user['password']);
            unset($user['auth']);
            user_update_user($user);
            $userids[] = array('id' => $user['id'], 'username' => $user['username'], 'error' => 'Updated');
            continue;
        }
        // Make sure auth is valid
        if (empty($availableauths[$user['auth']])) {
            $userids[] = array('id' => $user['id'], 'username' => $user['username'], 'error' => 'Invalid authentication type: ' . $user['auth']);
            continue;
        }
        // Make sure lang is valid
        if (empty($availablelangs[$user['lang']])) {
            $userids[] = array('id' => $user['id'], 'username' => $user['username'], 'error' => 'Invalid language code: ' . $user['lang']);
            continue;
        }
        // Make sure lang is valid
        if (!empty($user['theme']) && empty($availablethemes[$user['theme']])) {
            //theme is VALUE_OPTIONAL,
            // so no default value.
            // We need to test if the client sent it
            // => !empty($user['theme'])
            $userids[] = array('id' => $user['id'], 'username' => $user['username'], 'error' => 'Invalid theme: ' . $user['theme']);
            continue;
        }
        // make sure there is no data loss during truncation
        $truncated = truncate_userinfo($user);
        foreach ($truncated as $key => $value) {
            if ($truncated[$key] !== $user[$key]) {
                $userids[] = array('id' => $user['id'], 'username' => $user['username'], 'error' => 'Property: ' . $key . ' is too long: ' . $user[$key]);
                continue;
            }
        }
        $user['confirmed'] = true;
        $user['mnethostid'] = $CFG->mnet_localhost_id;
        $user['id'] = user_create_user($user);
        //        // custom fields
        //        if (!empty($user['customfields'])) {
        //            foreach ($user['customfields'] as $customfield) {
        //                $user["profile_field_" . $customfield['type']] = $customfield['value']; //profile_save_data() saves profile file
        //                //it's expecting a user with the correct id,
        //                //and custom field to be named profile_field_"shortname"
        //            }
        //            profile_save_data((object)$user);
        //        }
        //
        //        //preferences
        //        if (!empty($user['preferences'])) {
        //            foreach ($user['preferences'] as $preference) {
        //                set_user_preference($preference['type'], $preference['value'], $user['id']);
        //            }
        //        }
        $userids[] = array('id' => $user['id'], 'username' => $user['username'], 'error' => "");
    }
    $transaction->allow_commit();
    return $userids;
}
Ejemplo n.º 14
0
/**
 * Reads user information from ldap and returns it in an object
 *
 * @param object $ldapauth the ldap authentication instance
 * @param string $username username (with system magic quotes)
 * @param array $options an array with CLI input options
 * @return mixed object or false on error
 */
function local_ent_installer_get_userinfo_asobj($ldapauth, $username, $options = array())
{
    $user_array = local_ent_installer_get_userinfo($ldapauth, $username, $options);
    if ($user_array == false) {
        return false;
        //error or not found
    }
    $user_array = truncate_userinfo($user_array);
    $user = new stdClass();
    foreach ($user_array as $key => $value) {
        $user->{$key} = $value;
    }
    return $user;
}