Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
namespace core\oauth2\service;
18
 
19
use core\oauth2\issuer;
20
use core\oauth2\user_field_mapping;
21
use core\oauth2\discovery\openidconnect;
22
 
23
/**
24
 * Class for Microsoft oAuth service, with the specific methods related to it.
25
 *
26
 * @package    core
27
 * @copyright  2021 Sara Arjona (sara@moodle.com)
28
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29
 */
30
class microsoft extends openidconnect implements issuer_interface {
31
 
32
    /**
33
     * Build an OAuth2 issuer, with all the default values for this service.
34
     *
35
     * @return issuer The issuer initialised with proper default values.
36
     */
37
    public static function init(): issuer {
38
        $record = (object) [
39
            'name' => 'Microsoft',
40
            'image' => 'https://www.microsoft.com/favicon.ico',
1441 ariadna 41
            'baseurl' => 'https://login.microsoftonline.com/common/v2.0',
1 efrain 42
            'loginscopes' => 'openid profile email user.read',
43
            'loginscopesoffline' => 'openid profile email user.read offline_access',
44
            'showonloginpage' => issuer::EVERYWHERE,
45
            'servicetype' => 'microsoft',
46
        ];
47
 
48
        $issuer = new issuer(0, $record);
49
        return $issuer;
50
    }
51
 
1441 ariadna 52
    #[\Override]
53
    protected static function create_field_mappings(issuer $issuer): void {
54
        // Remove existing user field mapping.
55
        foreach (user_field_mapping::get_records(['issuerid' => $issuer->get('id')]) as $userfieldmapping) {
56
            $userfieldmapping->delete();
1 efrain 57
        }
58
 
59
        // Create the field mappings.
60
        $mapping = [
1441 ariadna 61
            'sub' => 'idnumber',
62
            'givenname' => 'firstname',
63
            'familyname' => 'lastname',
64
            'email' => 'email',
1 efrain 65
            'displayName' => 'alternatename',
66
            'officeLocation' => 'address',
67
            'mobilePhone' => 'phone1',
1441 ariadna 68
            'locale' => 'lang',
1 efrain 69
        ];
1441 ariadna 70
 
1 efrain 71
        foreach ($mapping as $external => $internal) {
72
            $record = (object) [
73
                'issuerid' => $issuer->get('id'),
74
                'externalfield' => $external,
1441 ariadna 75
                'internalfield' => $internal,
1 efrain 76
            ];
77
            $userfieldmapping = new user_field_mapping(0, $record);
78
            $userfieldmapping->create();
79
        }
80
    }
81
}