Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 53... Línea 53...
53
     * List linked logins
53
     * List linked logins
54
     *
54
     *
55
     * Requires auth/oauth2:managelinkedlogins capability at the user context.
55
     * Requires auth/oauth2:managelinkedlogins capability at the user context.
56
     *
56
     *
57
     * @param int $userid (defaults to $USER->id)
57
     * @param int $userid (defaults to $USER->id)
58
     * @return boolean
58
     * @return linked_login[]
59
     */
59
     */
60
    public static function get_linked_logins($userid = false) {
60
    public static function get_linked_logins($userid = false) {
61
        global $USER;
61
        global $USER;
Línea 62... Línea 62...
62
 
62
 
Línea 77... Línea 77...
77
    /**
77
    /**
78
     * See if there is a match for this username and issuer in the linked_login table.
78
     * See if there is a match for this username and issuer in the linked_login table.
79
     *
79
     *
80
     * @param string $username as returned from an oauth client.
80
     * @param string $username as returned from an oauth client.
81
     * @param \core\oauth2\issuer $issuer
81
     * @param \core\oauth2\issuer $issuer
82
     * @return stdClass User record if found.
82
     * @return linked_login|false record if found and user exists, false otherwise.
83
     */
83
     */
84
    public static function match_username_to_user($username, $issuer) {
84
    public static function match_username_to_user($username, $issuer) {
85
        $params = [
85
        $params = [
86
            'issuerid' => $issuer->get('id'),
86
            'issuerid' => $issuer->get('id'),
87
            'username' => $username
87
            'username' => $username
Línea 104... Línea 104...
104
     *
104
     *
105
     * @param array $userinfo as returned from an oauth client.
105
     * @param array $userinfo as returned from an oauth client.
106
     * @param \core\oauth2\issuer $issuer
106
     * @param \core\oauth2\issuer $issuer
107
     * @param int $userid (defaults to $USER->id)
107
     * @param int $userid (defaults to $USER->id)
108
     * @param bool $skippermissions During signup we need to set this before the user is setup for capability checks.
108
     * @param bool $skippermissions During signup we need to set this before the user is setup for capability checks.
109
     * @return bool
109
     * @return linked_login
110
     */
110
     */
111
    public static function link_login($userinfo, $issuer, $userid = false, $skippermissions = false) {
111
    public static function link_login($userinfo, $issuer, $userid = false, $skippermissions = false) {
112
        global $USER;
112
        global $USER;
Línea 113... Línea 113...
113
 
113
 
Línea 174... Línea 174...
174
        $site = get_site();
174
        $site = get_site();
175
        $supportuser = \core_user::get_support_user();
175
        $supportuser = \core_user::get_support_user();
176
        $user = get_complete_user_data('id', $userid);
176
        $user = get_complete_user_data('id', $userid);
Línea 177... Línea 177...
177
 
177
 
-
 
178
        $data = new stdClass();
-
 
179
        $placeholders = \core_user::get_name_placeholders($user);
178
        $data = new stdClass();
180
        foreach ($placeholders as $field => $value) {
-
 
181
            $data->{$field} = $value;
179
        $data->fullname = fullname($user);
182
        }
180
        $data->sitename  = format_string($site->fullname);
183
        $data->sitename  = format_string($site->fullname);
181
        $data->admin     = generate_email_signoff();
184
        $data->admin     = generate_email_signoff();
182
        $data->issuername = format_string($issuer->get('name'));
185
        $data->issuername = format_string($issuer->get('name'));
Línea 229... Línea 232...
229
            return false;
232
            return false;
230
        }
233
        }
231
        $expires = $login->get('confirmtokenexpires');
234
        $expires = $login->get('confirmtokenexpires');
232
        if (time() > $expires) {
235
        if (time() > $expires) {
233
            $login->delete();
236
            $login->delete();
234
            return;
237
            return false;
235
        }
238
        }
236
        $login->set('confirmtokenexpires', 0);
239
        $login->set('confirmtokenexpires', 0);
237
        $login->set('confirmtoken', '');
240
        $login->set('confirmtoken', '');
238
        $login->update();
241
        $login->update();
239
        return true;
242
        return true;
Línea 242... Línea 245...
242
    /**
245
    /**
243
     * Create an account with a linked login that is already confirmed.
246
     * Create an account with a linked login that is already confirmed.
244
     *
247
     *
245
     * @param array $userinfo as returned from an oauth client.
248
     * @param array $userinfo as returned from an oauth client.
246
     * @param \core\oauth2\issuer $issuer
249
     * @param \core\oauth2\issuer $issuer
247
     * @return bool
250
     * @return stdClass
248
     */
251
     */
249
    public static function create_new_confirmed_account($userinfo, $issuer) {
252
    public static function create_new_confirmed_account($userinfo, $issuer) {
250
        global $CFG, $DB;
253
        global $CFG, $DB;
251
        require_once($CFG->dirroot.'/user/profile/lib.php');
254
        require_once($CFG->dirroot.'/user/profile/lib.php');
252
        require_once($CFG->dirroot.'/user/lib.php');
255
        require_once($CFG->dirroot.'/user/lib.php');
Línea 279... Línea 282...
279
     * Send an email with a link to confirm creating this account.
282
     * Send an email with a link to confirm creating this account.
280
     *
283
     *
281
     * @param array $userinfo as returned from an oauth client.
284
     * @param array $userinfo as returned from an oauth client.
282
     * @param \core\oauth2\issuer $issuer
285
     * @param \core\oauth2\issuer $issuer
283
     * @param int $userid (defaults to $USER->id)
286
     * @param int $userid (defaults to $USER->id)
284
     * @return bool
287
     * @return stdClass
285
     */
288
     */
286
    public static function send_confirm_account_email($userinfo, $issuer) {
289
    public static function send_confirm_account_email($userinfo, $issuer) {
287
        global $CFG, $DB;
290
        global $CFG, $DB;
288
        require_once($CFG->dirroot.'/user/profile/lib.php');
291
        require_once($CFG->dirroot.'/user/profile/lib.php');
289
        require_once($CFG->dirroot.'/user/lib.php');
292
        require_once($CFG->dirroot.'/user/lib.php');
Línea 317... Línea 320...
317
        $site = get_site();
320
        $site = get_site();
318
        $supportuser = \core_user::get_support_user();
321
        $supportuser = \core_user::get_support_user();
319
        $user = get_complete_user_data('id', $user->id);
322
        $user = get_complete_user_data('id', $user->id);
Línea 320... Línea 323...
320
 
323
 
-
 
324
        $data = new stdClass();
-
 
325
        $placeholders = \core_user::get_name_placeholders($user);
321
        $data = new stdClass();
326
        foreach ($placeholders as $field => $value) {
-
 
327
            $data->{$field} = $value;
322
        $data->fullname = fullname($user);
328
        }
323
        $data->sitename  = format_string($site->fullname);
329
        $data->sitename  = format_string($site->fullname);
Línea 324... Línea 330...
324
        $data->admin     = generate_email_signoff();
330
        $data->admin     = generate_email_signoff();
Línea 343... Línea 349...
343
        email_to_user($user, $supportuser, $subject, $message, $messagehtml);
349
        email_to_user($user, $supportuser, $subject, $message, $messagehtml);
344
        return $user;
350
        return $user;
345
    }
351
    }
Línea 346... Línea 352...
346
 
352
 
347
    /**
353
    /**
348
     * Delete linked login
354
     * Delete a users own linked login
349
     *
355
     *
350
     * Requires auth/oauth2:managelinkedlogins capability at the user context.
356
     * Requires auth/oauth2:managelinkedlogins capability at the user context.
351
     *
357
     *
352
     * @param int $linkedloginid
-
 
353
     * @return boolean
358
     * @param int $linkedloginid
354
     */
359
     */
355
    public static function delete_linked_login($linkedloginid) {
-
 
356
        $login = new linked_login($linkedloginid);
360
    public static function delete_linked_login($linkedloginid) {
Línea 357... Línea 361...
357
        $userid = $login->get('userid');
361
        global $USER;
358
 
362
 
359
        if (\core\session\manager::is_loggedinas()) {
363
        if (\core\session\manager::is_loggedinas()) {
Línea -... Línea 364...
-
 
364
            throw new moodle_exception('notwhileloggedinas', 'auth_oauth2');
-
 
365
        }
-
 
366
 
-
 
367
        $login = linked_login::get_record([
-
 
368
            'id' => $linkedloginid,
-
 
369
            'userid' => $USER->id,
360
            throw new moodle_exception('notwhileloggedinas', 'auth_oauth2');
370
            'confirmtoken' => '',
361
        }
371
        ], MUST_EXIST);
Línea 362... Línea 372...
362
 
372
 
363
        $context = context_user::instance($userid);
373
        $context = context_user::instance($login->get('userid'));
Línea 392... Línea 402...
392
    /**
402
    /**
393
     * Create a new user & update the profile fields
403
     * Create a new user & update the profile fields
394
     *
404
     *
395
     * @param array $userinfo
405
     * @param array $userinfo
396
     * @param object $user
406
     * @param object $user
397
     * @return object
407
     * @return stdClass
398
     */
408
     */
399
    private static function save_user(array $userinfo, object $user): object {
409
    private static function save_user(array $userinfo, object $user): object {
400
        // Map supplied issuer user info to Moodle user fields.
410
        // Map supplied issuer user info to Moodle user fields.
401
        $userfieldmapping = new \core\oauth2\user_field_mapping();
411
        $userfieldmapping = new \core\oauth2\user_field_mapping();
402
        $userfieldlist = $userfieldmapping->get_internalfields();
412
        $userfieldlist = $userfieldmapping->get_internalfields();