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 |
/**
|
|
|
18 |
* Class for loading/storing oauth2 endpoints from the DB.
|
|
|
19 |
*
|
|
|
20 |
* @package core
|
|
|
21 |
* @copyright 2017 Damyon Wiese
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
namespace core\oauth2;
|
|
|
25 |
|
|
|
26 |
defined('MOODLE_INTERNAL') || die();
|
|
|
27 |
|
|
|
28 |
use core\persistent;
|
|
|
29 |
use lang_string;
|
|
|
30 |
/**
|
|
|
31 |
* Class for loading/storing oauth2 user field mappings from the DB
|
|
|
32 |
*
|
|
|
33 |
* @copyright 2017 Damyon Wiese
|
|
|
34 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
35 |
*/
|
|
|
36 |
class user_field_mapping extends persistent {
|
|
|
37 |
|
|
|
38 |
const TABLE = 'oauth2_user_field_mapping';
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* Return the list of valid internal user fields.
|
|
|
42 |
*
|
|
|
43 |
* @return array
|
|
|
44 |
*/
|
|
|
45 |
private static function get_user_fields() {
|
|
|
46 |
global $CFG;
|
|
|
47 |
require_once($CFG->dirroot . '/user/profile/lib.php');
|
|
|
48 |
|
|
|
49 |
return array_merge(\core_user::AUTHSYNCFIELDS, ['picture', 'username'], get_profile_field_names());
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
/**
|
|
|
53 |
* Return the definition of the properties of this model.
|
|
|
54 |
*
|
|
|
55 |
* @return array
|
|
|
56 |
*/
|
|
|
57 |
protected static function define_properties() {
|
|
|
58 |
return array(
|
|
|
59 |
'issuerid' => array(
|
|
|
60 |
'type' => PARAM_INT
|
|
|
61 |
),
|
|
|
62 |
'externalfield' => array(
|
|
|
63 |
'type' => PARAM_RAW_TRIMMED,
|
|
|
64 |
),
|
|
|
65 |
'internalfield' => array(
|
|
|
66 |
'type' => PARAM_ALPHANUMEXT,
|
|
|
67 |
'choices' => self::get_user_fields()
|
|
|
68 |
)
|
|
|
69 |
);
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
/**
|
|
|
73 |
* Return the list of internal fields
|
|
|
74 |
* in a format they can be used for choices in a select menu
|
|
|
75 |
* @return array
|
|
|
76 |
*/
|
|
|
77 |
public function get_internalfield_list() {
|
|
|
78 |
$userfields = array_merge(\core_user::AUTHSYNCFIELDS, ['picture', 'username']);
|
|
|
79 |
$internalfields = array_combine($userfields, $userfields);
|
|
|
80 |
return array_merge(['' => $internalfields], get_profile_field_list());
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
/**
|
|
|
84 |
* Return the list of internal fields with flat array
|
|
|
85 |
*
|
|
|
86 |
* Profile fields element has its array based on profile category.
|
|
|
87 |
* These elements need to be turned flat to make it easier to read.
|
|
|
88 |
*
|
|
|
89 |
* @return array
|
|
|
90 |
*/
|
|
|
91 |
public function get_internalfields() {
|
|
|
92 |
$userfieldlist = $this->get_internalfield_list();
|
|
|
93 |
$userfields = [];
|
|
|
94 |
array_walk_recursive($userfieldlist,
|
|
|
95 |
function($value, $key) use (&$userfields) {
|
|
|
96 |
$userfields[] = $key;
|
|
|
97 |
}
|
|
|
98 |
);
|
|
|
99 |
return $userfields;
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
/**
|
|
|
103 |
* Ensures that no HTML is saved to externalfield field
|
|
|
104 |
* but preserves all special characters that can be a part of the claim
|
|
|
105 |
* @return boolean true if validation is successful, string error if externalfield is not validated
|
|
|
106 |
*/
|
|
|
107 |
protected function validate_externalfield($value){
|
|
|
108 |
// This parameter type is set to PARAM_RAW_TRIMMED and HTML check is done here.
|
|
|
109 |
if (clean_param($value, PARAM_NOTAGS) !== $value){
|
|
|
110 |
return new lang_string('userfieldexternalfield_error', 'tool_oauth2');
|
|
|
111 |
}
|
|
|
112 |
return true;
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
}
|