Ignore:
Timestamp:
May 2, 2016, 12:09:23 PM (3 years ago)
Author:
jrpelegrina
Message:

Updated to moodle 3.0.3

File:
1 edited

Legend:

Unmodified
Added
Removed
  • moodle/trunk/fuentes/lib/classes/user.php

    r136 r1331  
    5050    public static $supportuser = false;
    5151
     52    /** @var array store user fields properties cache. */
     53    protected static $propertiescache = null;
     54
    5255    /**
    5356     * Return user object from db or create noreply or support user,
     
    7073        switch ($userid) {
    7174            case self::NOREPLY_USER:
    72                 return self::get_noreply_user($strictness);
     75                return self::get_noreply_user();
    7376                break;
    7477            case self::SUPPORT_USER:
    75                 return self::get_support_user($strictness);
     78                return self::get_support_user();
    7679                break;
    7780            default:
     
    239242        }
    240243    }
     244
     245    /**
     246     * Check if the given user is an active user in the site.
     247     *
     248     * @param  stdClass  $user         user object
     249     * @param  boolean $checksuspended whether to check if the user has the account suspended
     250     * @param  boolean $checknologin   whether to check if the user uses the nologin auth method
     251     * @throws moodle_exception
     252     * @since  Moodle 3.0
     253     */
     254    public static function require_active_user($user, $checksuspended = false, $checknologin = false) {
     255
     256        if (!self::is_real_user($user->id)) {
     257            throw new moodle_exception('invaliduser', 'error');
     258        }
     259
     260        if ($user->deleted) {
     261            throw new moodle_exception('userdeleted');
     262        }
     263
     264        if (empty($user->confirmed)) {
     265            throw new moodle_exception('usernotconfirmed', 'moodle', '', $user->username);
     266        }
     267
     268        if (isguestuser($user)) {
     269            throw new moodle_exception('guestsarenotallowed', 'error');
     270        }
     271
     272        if ($checksuspended and $user->suspended) {
     273            throw new moodle_exception('suspended', 'auth');
     274        }
     275
     276        if ($checknologin and $user->auth == 'nologin') {
     277            throw new moodle_exception('suspended', 'auth');
     278        }
     279    }
     280
     281    /**
     282     * Definition of user profile fields and the expected parameter type for data validation.
     283     *
     284     * @return void
     285     */
     286    protected static function fill_properties_cache() {
     287
     288        if (self::$propertiescache !== null) {
     289            return;
     290        }
     291
     292        // Array of user fields properties and expected parameters.
     293        // Every new field on the user table should be added here otherwise it won't be validated.
     294        $fields = array();
     295        $fields['id'] = array('type' => PARAM_INT);
     296        $fields['auth'] = array('type' => PARAM_NOTAGS);
     297        $fields['confirmed'] = array('type' => PARAM_BOOL);
     298        $fields['policyagreed'] = array('type' => PARAM_BOOL);
     299        $fields['deleted'] = array('type' => PARAM_BOOL);
     300        $fields['suspended'] = array('type' => PARAM_BOOL);
     301        $fields['mnethostid'] = array('type' => PARAM_BOOL);
     302        $fields['username'] = array('type' => PARAM_USERNAME);
     303        $fields['password'] = array('type' => PARAM_NOTAGS);
     304        $fields['idnumber'] = array('type' => PARAM_NOTAGS);
     305        $fields['firstname'] = array('type' => PARAM_NOTAGS);
     306        $fields['lastname'] = array('type' => PARAM_NOTAGS);
     307        $fields['surname'] = array('type' => PARAM_NOTAGS);
     308        $fields['email'] = array('type' => PARAM_RAW_TRIMMED);
     309        $fields['emailstop'] = array('type' => PARAM_INT);
     310        $fields['icq'] = array('type' => PARAM_NOTAGS);
     311        $fields['skype'] = array('type' => PARAM_NOTAGS);
     312        $fields['aim'] = array('type' => PARAM_NOTAGS);
     313        $fields['yahoo'] = array('type' => PARAM_NOTAGS);
     314        $fields['msn'] = array('type' => PARAM_NOTAGS);
     315        $fields['phone1'] = array('type' => PARAM_NOTAGS);
     316        $fields['phone2'] = array('type' => PARAM_NOTAGS);
     317        $fields['institution'] = array('type' => PARAM_TEXT);
     318        $fields['department'] = array('type' => PARAM_TEXT);
     319        $fields['address'] = array('type' => PARAM_TEXT);
     320        $fields['city'] = array('type' => PARAM_TEXT);
     321        $fields['country'] = array('type' => PARAM_TEXT);
     322        $fields['lang'] = array('type' => PARAM_TEXT);
     323        $fields['calendartype'] = array('type' => PARAM_NOTAGS);
     324        $fields['theme'] = array('type' => PARAM_NOTAGS);
     325        $fields['timezones'] = array('type' => PARAM_TEXT);
     326        $fields['firstaccess'] = array('type' => PARAM_INT);
     327        $fields['lastaccess'] = array('type' => PARAM_INT);
     328        $fields['lastlogin'] = array('type' => PARAM_INT);
     329        $fields['currentlogin'] = array('type' => PARAM_INT);
     330        $fields['lastip'] = array('type' => PARAM_NOTAGS);
     331        $fields['secret'] = array('type' => PARAM_TEXT);
     332        $fields['picture'] = array('type' => PARAM_INT);
     333        $fields['url'] = array('type' => PARAM_URL);
     334        $fields['description'] = array('type' => PARAM_CLEANHTML);
     335        $fields['descriptionformat'] = array('type' => PARAM_INT);
     336        $fields['mailformat'] = array('type' => PARAM_INT);
     337        $fields['maildigest'] = array('type' => PARAM_INT);
     338        $fields['maildisplay'] = array('type' => PARAM_INT);
     339        $fields['autosubscribe'] = array('type' => PARAM_INT);
     340        $fields['trackforums'] = array('type' => PARAM_INT);
     341        $fields['timecreated'] = array('type' => PARAM_INT);
     342        $fields['timemodified'] = array('type' => PARAM_INT);
     343        $fields['trustbitmask'] = array('type' => PARAM_INT);
     344        $fields['imagealt'] = array('type' => PARAM_TEXT);
     345        $fields['lastnamephonetic'] = array('type' => PARAM_NOTAGS);
     346        $fields['firstnamephonetic'] = array('type' => PARAM_NOTAGS);
     347        $fields['middlename'] = array('type' => PARAM_NOTAGS);
     348        $fields['alternatename'] = array('type' => PARAM_NOTAGS);
     349
     350        self::$propertiescache = $fields;
     351    }
     352
     353    /**
     354     * Get properties of a user field.
     355     *
     356     * @param string $property property name to be retrieved.
     357     * @throws coding_exception if the requested property name is invalid.
     358     * @return array the property definition.
     359     */
     360    public static function get_property_definition($property) {
     361
     362        self::fill_properties_cache();
     363
     364        if (!array_key_exists($property, self::$propertiescache)) {
     365            throw new coding_exception('Invalid property requested.');
     366        }
     367
     368        return self::$propertiescache[$property];
     369    }
     370
     371    /**
     372     * Clean the properties cache.
     373     *
     374     * During unit tests we need to be able to reset all caches so that each new test starts in a known state.
     375     * Intended for use only for testing, phpunit calls this before every test.
     376     */
     377    public static function reset_caches() {
     378        self::$propertiescache = null;
     379    }
    241380}
Note: See TracChangeset for help on using the changeset viewer.