mahara_institution_external.php 21.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
/**
 *
 * @package    mahara
 * @subpackage auth-webservice
 * @author     Catalyst IT Ltd
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
 * @copyright  For copyright information on Mahara, please see the README file distributed with this software.
 *
 */

/**
 * External user API
 *
 * @package    auth
 * @subpackage webservice
 * @copyright  2009 Moodle Pty Ltd (http://moodle.com)
 * @copyright  Copyright (C) 2011 Catalyst IT Ltd (http://www.catalyst.net.nz)
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 * @author     Piers Harding
 */

require_once(get_config('docroot') . 'webservice/lib.php');
require_once(get_config('docroot') . 'webservice/rest/locallib.php');
require_once(get_config('docroot') . 'lib/user.php');
require_once(get_config('docroot') . 'lib/group.php');
require_once(get_config('docroot') . 'lib/institution.php');
require_once(get_config('docroot') . 'lib/searchlib.php');

global $WEBSERVICE_OAUTH_USER;

/**
 * Class container for core Mahara institution related API calls
 */
class mahara_institution_external extends external_api {

    /**
     * Check that a user exists
     *
     * @param array $user array('id' => .., 'username' => ..)
     * @return array() of user
     */
    private static function checkuser($user) {
        if (isset($user['id'])) {
            $id = $user['id'];
        }
        else if (isset($user['userid'])) {
            $id = $user['userid'];
        }
        else if (isset($user['username'])) {
            $dbuser = get_record('usr', 'username', $user['username']);
            if (empty($dbuser)) {
                throw new WebserviceInvalidParameterException(get_string('invalidusername', 'auth.webservice', $user['username']));
            }
            $id = $dbuser->id;
        }
        else {
            throw new WebserviceInvalidParameterException(get_string('musthaveid', 'auth.webservice'));
        }
        // now get the user
        if ($user = get_user($id)) {
            return $user;
        }
        else {
            throw new WebserviceInvalidParameterException(get_string('invaliduserid', 'auth.webservice', $id));
        }
    }

    /**
     * parameter definition for input of add_members method
     *
     * Returns description of method parameters
     * @return external_function_parameters
     */
    public static function add_members_parameters() {
        return new external_function_parameters(
                    array(
                            'institution'     => new external_value(PARAM_TEXT, 'Mahara institution'),
                            'users'           => new external_multiple_structure(
                                                    new external_single_structure(
                                                        array(
                                                                'id'              => new external_value(PARAM_NUMBER, 'ID of the favourites owner', VALUE_OPTIONAL),
                                                                'username'        => new external_value(PARAM_RAW, 'Username of the favourites owner', VALUE_OPTIONAL),
                                                                )
                                                            )
                                                        )
                                                    )
                                                );
    }

    /**
     * Add one or more members to an institution
     *
94
     * @param string $institution
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
     * @param array $users
     */
    public static function add_members($institution, $users) {
        global $USER, $WEBSERVICE_INSTITUTION;
        self::check_oauth();

        $params = array('institution' => $institution, 'users' => $users);
        $params = self::validate_parameters(self::add_members_parameters(), $params);

        if (!$USER->get('admin') && !$USER->is_institutional_admin()) {
            throw new AccessDeniedException("Institution::add_members | " . get_string('accessdenied', 'auth.webservice'));
        }
        // check the institution is allowed
        if (!$USER->can_edit_institution($params['institution'])) {
            throw new WebserviceInvalidParameterException('add_members | ' . get_string('accessdeniedforinst', 'auth.webservice', $params['institution']));
        }
        db_begin();
        $userids = array();
        foreach ($params['users'] as $user) {
            $dbuser = self::checkuser($user);
            // Make sure auth is valid
            if (!$authinstance = get_record('auth_instance', 'id', $dbuser->authinstance)) {
                throw new WebserviceInvalidParameterException(get_string('invalidauthtype', 'auth.webservice', $dbuser->authinstance));
            }
            // check the institution is allowed
            // basic check authorisation to edit for the current institution
            if (!$USER->can_edit_institution($authinstance->institution)) {
                throw new WebserviceInvalidParameterException('add_members | ' . get_string('accessdeniedforinstuser', 'auth.webservice', $authinstance->institution, $dbuser->id));
            }
            $userids[]= $dbuser->id;
        }
        $institution = new Institution($params['institution']);
        $maxusers = $institution->maxuseraccounts;
        if (!empty($maxusers)) {
            $members = $institution->countMembers();
            if ($members + count($userids) > $maxusers) {
                throw new AccessDeniedException("Institution::add_members | " . get_string('institutionuserserrortoomanyinvites', 'admin'));
            }
        }
        $institution->add_members($userids);
        db_commit();

        return null;
    }

    /**
     * parameter definition for output of add_members method
     */
    public static function add_members_returns() {
        return null;
    }

    /**
     * parameter definition for input of invite_members method
     *
     * Returns description of method parameters
     * @return external_function_parameters
     */
    public static function invite_members_parameters() {
        return new external_function_parameters(
                array(
                        'institution'     => new external_value(PARAM_TEXT, 'Mahara institution'),
                        'users'           => new external_multiple_structure(
                                                new external_single_structure(
                                                    array(
                                                            'id'              => new external_value(PARAM_NUMBER, 'ID of the favourites owner', VALUE_OPTIONAL),
                                                            'username'        => new external_value(PARAM_RAW, 'Username of the favourites owner', VALUE_OPTIONAL),
                                                        )
                                                    )
                                                )
                                            )
                                        );
    }

    /**
     * Invite one or more users to an institution
     *
172
     * @param string $institution
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
     * @param array $users
     */
    public static function invite_members($institution, $users) {
        global $USER, $WEBSERVICE_INSTITUTION;
        self::check_oauth();

        $params = array('institution' => $institution, 'users' => $users);
        $params = self::validate_parameters(self::invite_members_parameters(), $params);

        if (!$USER->get('admin') && !$USER->is_institutional_admin()) {
            throw new AccessDeniedException("Institution::invite_members | " . get_string('accessdenied', 'auth.webservice'));
        }
        // check the institution is allowed
        if (!$USER->can_edit_institution($params['institution'])) {
            throw new WebserviceInvalidParameterException('invite_members | ' . get_string('accessdeniedforinst', 'auth.webservice', $params['institution']));
        }
        db_begin();
        $userids = array();
        foreach ($params['users'] as $user) {
            $dbuser = self::checkuser($user);

            // Make sure auth is valid
            if (!$authinstance = get_record('auth_instance', 'id', $dbuser->authinstance)) {
                throw new WebserviceInvalidParameterException('invite_members | ' . get_string('invalidauthtype', 'auth.webservice', $dbuser->authinstance));
            }
            // check the institution is allowed
            // basic check authorisation to edit for the current institution
            if (!$USER->can_edit_institution($authinstance->institution)) {
                throw new WebserviceInvalidParameterException('invite_members | ' . get_string('accessdeniedforinstuser', 'auth.webservice', $authinstance->institution, $dbuser->id));
            }
            $userids[]= $dbuser->id;
        }
        $institution = new Institution($params['institution']);
        $maxusers = $institution->maxuseraccounts;
        if (!empty($maxusers)) {
            if ($members + $institution->countInvites() + count($userids) > $maxusers) {
                throw new AccessDeniedException("Institution::invite_members | " . get_string('institutionuserserrortoomanyinvites', 'admin'));
            }
        }

        $institution->invite_users($userids);
        db_commit();

        return null;
    }

    /**
     * parameter definition for output of invite_members method
     */
    public static function invite_members_returns() {
        return null;
    }

    /**
     * parameter definition for input of remove_members method
     *
     * Returns description of method parameters
     * @return external_function_parameters
     */
    public static function remove_members_parameters() {
        return new external_function_parameters(
                    array(
                            'institution'     => new external_value(PARAM_TEXT, 'Mahara institution'),
                            'users'           => new external_multiple_structure(
                                                    new external_single_structure(
                                                        array(
                                                                'id'              => new external_value(PARAM_NUMBER, 'ID of the favourites owner', VALUE_OPTIONAL),
                                                                'username'        => new external_value(PARAM_RAW, 'Username of the favourites owner', VALUE_OPTIONAL),
                                                            )
                                                        )
                                                    )
                                                )
                                            );
    }

    /**
     * remove one or more users from an institution
     *
251
     * @param string $institution
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
     * @param array $users
     */
    public static function remove_members($institution, $users) {
        global $USER, $WEBSERVICE_INSTITUTION;
        self::check_oauth();

        $params = array('institution' => $institution, 'users' => $users);
        $params = self::validate_parameters(self::remove_members_parameters(), $params);

        if (!$USER->get('admin') && !$USER->is_institutional_admin()) {
            throw new AccessDeniedException("Institution::remove_members | " . get_string('accessdenied', 'auth.webservice'));
        }
        // check the institution is allowed
        if (!$USER->can_edit_institution($params['institution'])) {
            throw new WebserviceInvalidParameterException('remove_members | ' . get_string('accessdeniedforinst', 'auth.webservice', $params['institution']));
        }
        db_begin();
        $userids = array();
        foreach ($params['users'] as $user) {
            $dbuser = self::checkuser($user);

            // Make sure auth is valid
            if (!$authinstance = get_record('auth_instance', 'id', $dbuser->authinstance)) {
                throw new WebserviceInvalidParameterException('remove_members | ' . get_string('invalidauthtype', 'auth.webservice', $dbuser->authinstance));
            }

            // check the institution is allowed
            // basic check authorisation to edit for the current institution
            if (!$USER->can_edit_institution($authinstance->institution)) {
                throw new WebserviceInvalidParameterException('remove_members | ' . get_string('accessdeniedforinstuser', 'auth.webservice', $authinstance->institution, $dbuser->id));
            }
            $userids[]= $dbuser->id;
        }
        $institution = new Institution($params['institution']);
        $institution->removeMembers($userids);
        db_commit();

        return null;
    }

    /**
     * parameter definition for output of remove_members method
     */
    public static function remove_members_returns() {
        return null;
    }

    /**
     * parameter definition for input of decline_members method
     *
     * Returns description of method parameters
     * @return external_function_parameters
     */
    public static function decline_members_parameters() {
        return new external_function_parameters(
                    array(
                            'institution'     => new external_value(PARAM_TEXT, 'Mahara institution'),
                            'users'           => new external_multiple_structure(
                                                    new external_single_structure(
                                                        array(
                                                                'id'              => new external_value(PARAM_NUMBER, 'ID of the favourites owner', VALUE_OPTIONAL),
                                                                'username'        => new external_value(PARAM_RAW, 'Username of the favourites owner', VALUE_OPTIONAL),
                                                            )
                                                        )
                                                    )
                                                )
                                            );
    }

    /**
     * decline one or more users request for membership to an institution
     *
324
     * @param string $institution
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
     * @param array $users
     */
    public static function decline_members($institution, $users) {
        global $USER, $WEBSERVICE_INSTITUTION;
        self::check_oauth();

        $params = array('institution' => $institution, 'users' => $users);
        $params = self::validate_parameters(self::decline_members_parameters(), $params);

        if (!$USER->get('admin') && !$USER->is_institutional_admin()) {
            throw new AccessDeniedException("Institution::decline_members | " . get_string('accessdenied', 'auth.webservice'));
        }
        // check the institution is allowed
        if (!$USER->can_edit_institution($params['institution'])) {
            throw new WebserviceInvalidParameterException('decline_members | ' . get_string('accessdeniedforinst', 'auth.webservice', $params['institution']));
        }
        db_begin();
        $userids = array();
        foreach ($params['users'] as $user) {
            $dbuser = self::checkuser($user);

            // Make sure auth is valid
            if (!$authinstance = get_record('auth_instance', 'id', $dbuser->authinstance)) {
                throw new WebserviceInvalidParameterException('decline_members | ' . get_string('invalidauthtype', 'auth.webservice', $dbuser->authinstance));
            }

            // check the institution is allowed
            // basic check authorisation to edit for the current institution
            if (!$USER->can_edit_institution($authinstance->institution)) {
                throw new WebserviceInvalidParameterException('decline_members | ' . get_string('accessdeniedforinstuser', 'auth.webservice', $authinstance->institution, $dbuser->id));
            }
            $userids[]= $dbuser->id;
        }
        $institution = new Institution($params['institution']);
        $institution->decline_requests($userids);
        db_commit();

        return null;
    }

    /**
     * parameter definition for output of decline_members method
     */
    public static function decline_members_returns() {
        return null;
    }

    /**
     * parameter definition for input of get_members method
     *
     * Returns description of method parameters
     * @return external_function_parameters
     */
    public static function get_members_parameters() {

        return new external_function_parameters(
                    array(
                            'institution'     => new external_value(PARAM_TEXT, 'Mahara institution'),
                    )
                );
    }

    /**
     * Get institution members
     *
390
391
     * @param string $institution
     * @return array An array of arrays describing users
392
393
394
395
396
397
     */
    public static function get_members($institution) {
        global $USER, $WEBSERVICE_INSTITUTION;
        self::check_oauth();

        // Do basic automatic PARAM checks on incoming data, using params description
398
        $params = self::validate_parameters(self::get_members_parameters(), array('institution' => $institution));
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
        if (!$USER->get('admin') && !$USER->is_institutional_admin()) {
            throw new AccessDeniedException("Institution::get_members | " . get_string('accessdenied', 'auth.webservice'));
        }
        // check the institution is allowed
        if (!$USER->can_edit_institution($params['institution'])) {
            throw new WebserviceInvalidParameterException('get_members | ' . get_string('accessdeniedforinst', 'auth.webservice', $params['institution']));
        }
        $institution = new Institution($params['institution']);
        $institution->member = true; // Only fetch the users belonging to the institution indicated in $params['institution']
        $data = institutional_admin_user_search('', $institution, 0);
        $users = array();

        if (!empty($data['data'])) {
            foreach ($data['data'] as $user) {
                $users[] = array('id'=> $user['id'], 'username'=>$user['username']);
            }
        }
        return $users;
    }

    /**
     * parameter definition for output of get_members method
     *
     * Returns description of method result value
423
     * @return external_multiple_structure
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
     */
    public static function get_members_returns() {
        return new external_multiple_structure(
                    new external_single_structure(
                    array(
                            'id'              => new external_value(PARAM_NUMBER, 'ID of the user'),
                            'username'        => new external_value(PARAM_RAW, 'Username policy is defined in Mahara security config'),
                        )
                    )
                );
    }

    /**
     * parameter definition for input of get_requests method
     *
     * Returns description of method parameters
     * @return external_function_parameters
     */
    public static function get_requests_parameters() {

        return new external_function_parameters(
445
446
447
448
            array(
                  'institution'     => new external_value(PARAM_TEXT, 'Mahara institution'),
            )
        );
449
450
451
452
453
    }

    /**
     * Get institution requests
     *
454
455
     * @param string $institution
     * @return array An array of arrays describing users
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
     */
    public static function get_requests($institution) {
        global $USER, $WEBSERVICE_INSTITUTION;
        self::check_oauth();

        // Do basic automatic PARAM checks on incoming data, using params description
        $params = self::validate_parameters(self::get_members_parameters(), array('institution'=>$institution));
        if (!$USER->get('admin') && !$USER->is_institutional_admin()) {
            throw new AccessDeniedException("Institution::get_requests | " . get_string('accessdenied', 'auth.webservice'));
        }
        // check the institution is allowed
        if (!$USER->can_edit_institution($params['institution'])) {
            throw new WebserviceInvalidParameterException('get_requests | ' . get_string('accessdeniedforinst', 'auth.webservice', $params['institution']));
        }

        $users = array();
        $dbrequests = get_records_array('usr_institution_request', 'institution', $params['institution']);

        if (!empty($dbrequests)) {
            foreach ($dbrequests as $user) {
                $dbuser = get_record('usr', 'id', $user->usr);
                $users[] = array('id'=> $user->usr, 'username'=>$dbuser->username);
            }
        }
        return $users;
    }

    /**
     * Check if OAuth is enabled and reject
     * @throws Exception
     */
    private static function check_oauth() {
        global $WEBSERVICE_OAUTH_USER;
        if ($WEBSERVICE_OAUTH_USER) {
            throw new MaharaException(get_string('nooauth', 'auth.webservice'));
        }
    }

    /**
     * parameter definition for output of get_requests method
     *
     * Returns description of method result value
498
     * @return external_multiple_structure
499
500
501
502
503
504
505
506
507
508
509
510
     */
    public static function get_requests_returns() {
        return new external_multiple_structure(
                    new external_single_structure(
                        array(
                                'id'              => new external_value(PARAM_NUMBER, 'ID of the user'),
                                'username'        => new external_value(PARAM_RAW, 'Username policy is defined in Mahara security config'),
                            )
                        )
                    );
    }
}