ef22c1b6 |
1 | <?php |
2 | |
3 | // This file is part of Moodle - http://moodle.org/ |
4 | // |
5 | // Moodle is free software: you can redistribute it and/or modify |
6 | // it under the terms of the GNU General Public License as published by |
7 | // the Free Software Foundation, either version 3 of the License, or |
8 | // (at your option) any later version. |
9 | // |
10 | // Moodle is distributed in the hope that it will be useful, |
11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | // GNU General Public License for more details. |
14 | // |
15 | // You should have received a copy of the GNU General Public License |
16 | // along with Moodle. If not, see <http://www.gnu.org/licenses/>. |
17 | |
18 | /** |
19 | * External user API |
20 | * |
21 | * @package moodlecore |
22 | * @subpackage webservice |
23 | * @copyright 2009 Petr Skoda (http://skodak.org) |
24 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
25 | */ |
26 | |
27 | require_once("$CFG->libdir/externallib.php"); |
28 | |
29 | class moodle_user_external extends external_api { |
30 | |
31 | public static function get_users($params) { |
32 | $context = get_context_instance(CONTEXT_SYSTEM); |
33 | requier_capability('moodle/user:viewdetails', $context); |
34 | self::validate_context($context); |
35 | |
36 | $search = validate_param($params['search'], PARAM_RAW); |
37 | |
38 | //TODO: this search is probably useless for external systems because it is not exact |
39 | // 1/ we should specify multiple search parameters including the mnet host id |
40 | // 2/ custom profile fileds not inlcuded |
41 | |
42 | return get_users(true, $search, false, null, 'firstname ASC','', '', '', 1000, 'id, mnethostid, auth, confirmed, username, idnumber, firstname, lastname, email, emailstop, lang, theme, timezone, mailformat, city, description, country'); |
43 | } |
44 | |
45 | public static function create_users($params) { |
46 | global $CFG, $DB; |
47 | |
48 | $context = get_context_instance(CONTEXT_SYSTEM); |
49 | requier_capability('moodle/user:create', $context); |
50 | self::validate_context($context); |
51 | |
52 | //TODO: this list is incomplete - we have preferences and custom fields too |
53 | $accepted = array( |
54 | 'password' => PARAM_RAW, |
55 | 'auth' => PARAM_SAFEDIR, |
56 | 'username' => PARAM_RAW, |
57 | 'idnumber' => PARAM_RAW, |
58 | 'firstname' => PARAM_CLEAN, |
59 | 'lastname' => PARAM_CLEAN, |
60 | 'email' => PARAM_EMAIL, |
61 | 'emailstop' => PARAM_BOOL, |
62 | 'lang' => PARAM_SAFEDIR, // validate using list of available langs - ignored if wrong |
63 | 'theme' => PARAM_SAFEDIR, |
64 | 'timezone' => PARAM_ALPHANUMEXT, |
65 | 'mailformat' => PARAM_ALPHA, |
66 | 'description' => PARAM_RAW, |
67 | 'city' => PARAM_CLEAN, |
68 | 'country' => PARAM_ALPHANUMEXT, |
69 | ); |
70 | |
71 | $required = array('username', 'firstname', 'lastname', 'email', 'password'); //TODO: password may not be required in some cases |
72 | $langs = get_list_of_languages(); |
73 | |
74 | // verify data first, only then start creating records |
75 | $users = array(); |
76 | foreach ($params as $data) { |
77 | $user = array(); |
78 | foreach ($accepted as $key=>$type) { |
79 | if (array_key_exists($key, $data)) { |
80 | $user[$key] = validate_param($data[$key], $type); |
81 | unset($data[$key]); |
82 | } |
83 | } |
84 | if (!empty($data)) { |
85 | throw new invalid_parameter_exception('Unsupported parameters in user array'); |
86 | } |
87 | foreach ($required as $req) { |
88 | if (!array_key_exists($req, $user) or empty($user[$req])) { |
89 | throw new invalid_parameter_exception("$req is required in user array"); |
90 | } |
91 | } |
92 | if (!isset($user['auth'])) { |
93 | $user['auth'] = 'manual'; |
94 | } |
95 | if (!exists_auth_plugin($user['auth'])) { |
96 | throw new invalid_parameter_exception($user['auth']." is not valid authentication plugin"); |
97 | } |
98 | |
99 | if (isset($user['lang']) and !isset($langs[$user['lang']])) { |
100 | unset($user['lang']); |
101 | } |
102 | |
103 | //TODO: add more param validations here: username, etc. |
104 | |
105 | if ($DB->get_record('user', array('username'=>$user['username'], 'mnethostid'=>$CFG->mnet_localhost_id))) { |
106 | throw new invalid_parameter_exception($user['username']." username is already taken, sorry"); |
107 | } |
108 | |
109 | if (isset($users[$user['username']])) { |
110 | throw new invalid_parameter_exception("multiple users with the same username requested"); |
111 | } |
112 | $users[$user['username']] = $user; |
113 | } |
114 | |
115 | $result = array(); |
116 | |
117 | foreach ($users as $user) { |
118 | $record = create_user_record($user['username'], $user['password'], $user['auth']); |
119 | unset($user['username']); |
120 | unset($user['password']); |
121 | unset($user['auth']); |
122 | |
123 | // now override the default (or external) values |
124 | foreach ($user as $key=>$value) { |
125 | $record->$key = $value; |
126 | } |
127 | $DB->update_record('user', $record); |
128 | |
129 | unset($record->password); // lets keep this as a secret ;-) |
130 | $result[$record->id] = $record; |
131 | } |
132 | |
133 | return $result; |
134 | } |
135 | |
136 | |
137 | public static function delete_users($params) { |
138 | //TODO |
139 | } |
140 | |
141 | |
142 | public static function update_users($params) { |
143 | //TODO |
144 | } |
145 | } |