oauthv1sregister.php 28.6 KB
Newer Older
1
2
3
4
<?php
/**
 *
 * @package    mahara
5
 * @subpackage auth-webservice
6
 * @author     Catalyst IT Ltd
7
8
 * @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.
9
10
11
12
13
 *
 */

define('INTERNAL', 1);
define('ADMIN', 1);
14
define('MENUITEM', 'webservices/oauthconfig');
15
16
define('SECTION_PAGE', 'oauth');
require(dirname(dirname(dirname(__FILE__))) . '/init.php');
Naomi Guyer's avatar
Naomi Guyer committed
17
18

define('TITLE', get_string('webservices_title', 'auth.webservice'));
19
define('SUBSECTIONHEADING', get_string('externalappsregister', 'auth.webservice'));
Naomi Guyer's avatar
Naomi Guyer committed
20

21
22
23
24
require_once(get_config('docroot') . 'webservice/libs/oauth-php/OAuthServer.php');
require_once(get_config('docroot') . 'webservice/libs/oauth-php/OAuthStore.php');
OAuthStore::instance('Mahara');

Matt Clarkson's avatar
Matt Clarkson committed
25
26
$server_id  = param_integer('edit', 0);
$config_server_id = param_integer('config', 0);
27
28
29
30
31
32
33
34
35
36
37
38
39
40
$dbserver = get_record('oauth_server_registry', 'id', $server_id);

$institutions = get_records_array('institution');
$iopts = array();
foreach ($institutions as $institution) {
    $iopts[trim($institution->name)] = $institution->displayname;
}

$services = get_records_array('external_services', 'restrictedusers', 0);
$sopts = array();
foreach ($services as $service) {
    $sopts[$service->id] = $service->name;
}

Matt Clarkson's avatar
Matt Clarkson committed
41
42
43
44
// we have a service config form
if ($config_server_id) {
    $form = webservice_server_config_form($config_server_id);
}
45
// we have an edit form
Matt Clarkson's avatar
Matt Clarkson committed
46
else if (!empty($dbserver)) {
47
48
49
50
51
52
53
54
    $disabled = array();
    list($moduletype, $module) = get_module_from_serverid($server_id);
    safe_require_plugin($moduletype, $module);
    $classname = generate_class_name($moduletype, $module);
    if (is_callable(array($classname, 'disable_webservice_fields'))) {
        $disabled = call_static_method($classname, 'disable_webservice_fields');
    }
    $form = webservice_server_edit_form($dbserver, $sopts, $iopts, $disabled);
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
}
// else we have just the standard list
else {
    $form = webservice_server_list_form($sopts, $iopts);
}

function webservices_add_application_submit(Pieform $form, $values) {
    global $SESSION, $USER;

    $dbuser = get_record('usr', 'id', $USER->get('id'));
    if (empty($dbuser)) {
        $SESSION->add_error_msg(get_string('erroruser', 'auth.webservice'));
        redirect('/webservice/admin/oauthv1sregister.php');
    }
    $store = OAuthStore::instance();

    $new_app = array(
                'application_title' => $values['application'],
                'application_uri'   => 'http://example.com',
                'requester_name'    => $dbuser->firstname . ' ' . $dbuser->lastname,
                'requester_email'   => $dbuser->email,
                'callback_uri'      => 'http://example.com',
                'institution'       => $values['institution'],
                'externalserviceid' => $values['service'],
      );
    $key = $store->updateConsumer($new_app, $dbuser->id, true);
    $c = (object) $store->getConsumer($key, $dbuser->id, true);
    if (empty($c)) {
        $SESSION->add_error_msg(get_string('errorregister', 'auth.webservice'));
        redirect('/webservice/admin/oauthv1sregister.php');
    }
    else {
87
88
89
90
91
92
93
94
95
96
97
        // New application added - now check that institution has 'webservices' auth
        // Get auth priority we need for the ensure_record_exists function
        $priorities  = get_record_sql("SELECT MAX(priority) AS maxpriority, (
                                           SELECT priority FROM {auth_instance}
                                           WHERE institution = ? AND authname = 'webservice') AS webservicepriority
                                      FROM {auth_instance} WHERE institution = ?", array($values['institution'], $values['institution']));
        $priority = is_null($priorities->webservicepriority) ? $priorities->maxpriority + 1 : $priorities->webservicepriority;
        if (!ensure_record_exists('auth_instance', (object) array('institution' => $values['institution'], 'authname' => 'webservice'),
                                                   (object) array('institution' => $values['institution'], 'authname' => 'webservice', 'active' => 1, 'priority' => $priority, 'instancename' => 'webservice'))) {
            $SESSION->add_error_msg(get_string('setauthinstancefailed', 'auth.webservice', institution_display_name($values['institution'])));
        }
98
99
100
101
102
103
104
105
106
107
108
109
        redirect('/webservice/admin/oauthv1sregister.php?edit=' . $c->id);
    }

}

function webservices_server_submit(Pieform $form, $values) {
    global $USER, $SESSION;
    $store = OAuthStore::instance();
    $is_admin = ($USER->get('admin') ||defined('ADMIN') || defined('INSTITUTIONALADMIN') || $USER->is_institutional_admin() ? true : false);
    $dbserver = get_record('oauth_server_registry', 'id', $values['token']);
    if ($dbserver) {
        if ($values['action'] == 'delete') {
Matt Clarkson's avatar
Matt Clarkson committed
110
111
112
113
114
115

            delete_records_sql('
                                DELETE FROM {oauth_server_config}
                                WHERE oauthserverregistryid = ?
                                ', array($dbserver->id));

116
117
118
119
120
121
122
123
124
125
            delete_records_sql('
                                DELETE FROM {oauth_server_token}
                                WHERE osr_id_ref = ?
                                ', array($dbserver->id));
            $store->deleteServer($dbserver->consumer_key, $dbserver->userid, $is_admin);
            $SESSION->add_ok_msg(get_string('oauthserverdeleted', 'auth.webservice'));
        }
        else if ($values['action'] == 'edit') {
            redirect('/webservice/admin/oauthv1sregister.php?edit=' . $values['token']);
        }
Matt Clarkson's avatar
Matt Clarkson committed
126
127
128
        else if ($values['action'] == 'config') {
            redirect('/webservice/admin/oauthv1sregister.php?config=' . $values['token']);
        }
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
    }
    redirect('/webservice/admin/oauthv1sregister.php');
}

function webservice_oauth_server_submit(Pieform $form, $values) {
    global $USER, $SESSION;

    $store = OAuthStore::instance();
    $dbserver = get_record('oauth_server_registry', 'id', $values['id']);
    if ($dbserver) {

       $app = array(
                    'application_title' => $values['application_title'],
                    'application_uri'   => $values['application_uri'],
                    'requester_name'    => $dbserver->requester_name,
                    'requester_email'   => $dbserver->requester_email,
                    'callback_uri'      => $values['callback_uri'],
                    'institution'       => $values['institution'],
                    'externalserviceid' => $values['service'],
                    'consumer_key'      => $dbserver->consumer_key,
                    'consumer_secret'   => $dbserver->consumer_secret,
                    'id'                => $values['id'],
       );
        $key = $store->updateConsumer($app, $USER->get('id'), true);
        $c = (object) $store->getConsumer($key, $USER->get('id'), true);
        if (empty($c)) {
            $SESSION->add_error_msg(get_string('errorregister', 'auth.webservice'));
            redirect('/webservice/admin/oauthv1sregister.php');
        }
        else {
            redirect('/webservice/admin/oauthv1sregister.php?edit=' . $c->id);
        }
    }

    $SESSION->add_error_msg(get_string('errorupdate', 'auth.webservice'));
    redirect('/webservice/admin/oauthv1sregister.php');
}

167
$pieform = pieform_instance($form);
168
$form = $pieform->build(false);
169

Naomi Guyer's avatar
Naomi Guyer committed
170
$smarty = smarty();
171
setpageicon($smarty, 'icon-puzzle-piece');
172
173
174
175
176
177
178
179
safe_require('auth', 'webservice');
PluginAuthWebservice::menu_items($smarty, 'webservice/oauthconfig');
$smarty->assign('form', $form);
$smarty->display('form.tpl');

function webservice_main_submit(Pieform $form, $values) {
}

180
function webservice_server_edit_form($dbserver, $sopts, $iopts, $disabled = array()) {
181
182
183
184
185

    $server_details =
        array(
            'name'             => 'webservice_oauth_server',
            'successcallback'  => 'webservice_oauth_server_submit',
186
            'jsform'           => false,
187
            'elements'   => array(
188
189
190
                    'id' => array(
                        'type'  => 'hidden',
                        'value' => $dbserver->id,
191
                    ),
192
193
194
195
196
197
198
199
200
                    'userid' => array(
                        'type'  => 'hidden',
                        'value' => $dbserver->userid,
                    ),
                    'consumer_key' => array(
                        'type'  => 'hidden',
                        'value' => $dbserver->consumer_key,
                    ),
                ),
201
            );
202
203
204
205
206
    $server_details['elements']['consumer_key_html'] = array(
        'title'        => get_string('consumer_key', 'auth.webservice'),
        'type'         => 'html',
        'value'        => $dbserver->consumer_key,
    );
207
208
    $server_details['elements']['consumer_secret'] = array(
        'title'        => get_string('consumer_secret', 'auth.webservice'),
209
210
        'type'         => 'html',
        'value'        => $dbserver->consumer_secret,
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
    );

    $server_details['elements']['application_title'] = array(
        'title'        => get_string('application_title', 'auth.webservice'),
        'defaultvalue' =>  $dbserver->application_title,
        'type'         => 'text',
    );

    $server_details['elements']['user'] = array(
        'title'        => get_string('serviceuser', 'auth.webservice'),
        'value'        =>  get_field('usr', 'username', 'id', $dbserver->userid),
        'type'         => 'html',
    );

    $server_details['elements']['application_uri'] = array(
        'title'        => get_string('application_uri', 'auth.webservice'),
        'defaultvalue' =>  $dbserver->application_uri,
        'type'         => 'text',
229
        'disabled'     => (isset($disabled['application_uri']) ? true : false),
230
231
232
233
234
235
    );

    $server_details['elements']['callback_uri'] = array(
        'title'        => get_string('callback', 'auth.webservice'),
        'defaultvalue' =>  $dbserver->callback_uri,
        'type'         => 'text',
236
        'disabled'     => (isset($disabled['callback_uri']) ? true : false),
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
    );

    $server_details['elements']['institution'] = array(
        'type'         => 'select',
        'title'        => get_string('institution'),
        'options'      => $iopts,
        'defaultvalue' => trim($dbserver->institution),
    );

    $server_details['elements']['service'] = array(
        'type'         => 'select',
        'title'        => get_string('servicename', 'auth.webservice'),
        'options'      => $sopts,
        'defaultvalue' => $dbserver->externalserviceid,
    );

    $server_details['elements']['enabled'] = array(
254
        'title'        => get_string('enabled'),
255
        'defaultvalue' => (($dbserver->enabled == 1) ? 'checked' : ''),
256
        'type'         => 'switchbox',
257
258
259
260
261
262
263
264
265
266
267
268
269
        'disabled'     => true,
    );

    $functions = get_records_array('external_services_functions', 'externalserviceid', $dbserver->externalserviceid);
    $function_list = array();
    if ($functions) {
        foreach ($functions as $function) {
            $dbfunction = get_record('external_functions', 'name', $function->functionname);
            $function_list[]= '<a href="' . get_config('wwwroot') . 'webservice/wsdoc.php?id=' . $dbfunction->id . '">' . $function->functionname . '</a>';
        }
    }
    $server_details['elements']['functions'] = array(
        'title'        => get_string('functions', 'auth.webservice'),
270
        'value'        => '<div class="align-with-input">' . implode(', ', $function_list) . '</div>',
271
272
273
274
275
276
277
        'type'         => 'html',
    );

    $server_details['elements']['submit'] = array(
        'type'  => 'submitcancel',
        'value' => array(get_string('save'), get_string('back')),
        'goto'  => get_config('wwwroot') . 'webservice/admin/oauthv1sregister.php',
278
        'class'        => 'btn-primary',
279
280
281
282
283
    );

    $elements = array(
            // fieldset for managing service function list
            'token_details' => array(
284
285
286
287
288
289
290
291
292
                    'type' => 'fieldset',
                    'class' => 'with-padding',
                    'elements' => array(
                        'sflist' => array(
                            'value' =>     pieform($server_details),
                        )
                    ),
                    'collapsible' => false,
                ),
293
294
295
        );

    $form = array(
Naomi Guyer's avatar
Naomi Guyer committed
296
        'renderer' => 'div',
297
298
        'id' => 'maintable',
        'name' => 'tokenconfig',
299
        'jsform' => false,
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
        'successcallback' => 'webservice_server_edit_submit',
        'elements' => $elements,
    );

    return $form;
}

function webservice_server_list_form($sopts, $iopts) {
    global $USER, $THEME;

    $dbconsumers = get_records_sql_assoc('
            SELECT  osr.id              as id,
                    userid              as userid,
                    institution         as institution,
                    externalserviceid   as externalserviceid,
                    u.username          as username,
                    u.email             as email,
                    consumer_key        as consumer_key,
                    consumer_secret     as consumer_secret,
Matt Clarkson's avatar
Matt Clarkson committed
319
                    osr.enabled         as enabled,
320
                    status              as status,
321
                    osr.ctime           as issue_date,
322
323
324
325
326
                    application_uri     as application_uri,
                    application_title   as application_title,
                    application_descr   as application_descr,
                    requester_name      as requester_name,
                    requester_email     as requester_email,
Matt Clarkson's avatar
Matt Clarkson committed
327
328
                    callback_uri        as callback_uri,
                    es.component        as component
329
330
            FROM {oauth_server_registry} osr
            JOIN {usr} u
Matt Clarkson's avatar
Matt Clarkson committed
331
332
333
                ON osr.userid = u.id
            JOIN {external_services} es
                ON es.id = osr.externalserviceid
334
335
336
337
338
339
340
341
342
343
344
345
            ORDER BY application_title, username
            ', array());
    $form = '';
    if (!empty($dbconsumers)) {
        $form = array(
            'name'            => 'webservices_tokens',
            'elementclasses'  => false,
            'successcallback' => 'webservices_tokens_submit',
            'renderer'   => 'multicolumntable',
            'elements'   => array(
                            'application' => array(
                                'title' => ' ',
346
                                'datatable' => true,
347
348
349
350
351
                                'type'  => 'html',
                                'value' => get_string('application', 'auth.webservice'),
                            ),
                            'username' => array(
                                'title' => ' ',
352
                                'datatable' => true,
353
354
355
356
357
                                'type'  => 'html',
                                'value' => get_string('owner', 'auth.webservice'),
                            ),
                            'consumer_key' => array(
                                'title' => ' ',
358
                                'datatable' => true,
359
360
361
362
363
                                'type'  => 'html',
                                'value' => get_string('consumer_key', 'auth.webservice'),
                            ),
                            'consumer_secret' => array(
                                'title' => ' ',
364
                                'datatable' => true,
365
366
367
368
369
                                'type'  => 'html',
                                'value' => get_string('consumer_secret', 'auth.webservice'),
                            ),
                            'enabled' => array(
                                'title' => ' ',
370
                                'datatable' => true,
371
                                'type'  => 'html',
372
                                'value' => get_string('enabled'),
373
                            ),
Matt Clarkson's avatar
Matt Clarkson committed
374
                            'institution' => array(
375
                                'title' => ' ',
376
                                'datatable' => true,
377
                                'type'  => 'html',
Matt Clarkson's avatar
Matt Clarkson committed
378
                                'value' => get_string('institution'),
379
                            ),
380
381
382
383
384
385
                            'actions' => array(
                                'title' => ' ',
                                'datatable' => true,
                                'type' => 'html',
                                'value' => '',
                            ),
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
                        ),
            );
        foreach ($dbconsumers as $consumer) {
            $form['elements']['id' . $consumer->id . '_application'] = array(
                'value'        =>  $consumer->application_title,
                'type'         => 'html',
                'key'        => $consumer->consumer_key,
            );

            if ($USER->is_admin_for_user($consumer->userid)) {
                $user_url = get_config('wwwroot') . 'admin/users/edit.php?id=' . $consumer->userid;
            }
            else {
                $user_url = get_config('wwwroot') . 'user/view.php?id=' . $consumer->userid;
            }
            $form['elements']['id' . $consumer->id . '_username'] = array(
                'value'        =>  '<a href="' . $user_url . '">' . $consumer->username . '</a>',
                'type'         => 'html',
                'key'        => $consumer->consumer_key,
            );
            $form['elements']['id' . $consumer->id . '_consumer_key'] = array(
                'value'        =>  $consumer->consumer_key,
                'type'         => 'html',
                'key'        => $consumer->consumer_key,
            );
            $form['elements']['id' . $consumer->id . '_consumer_secret'] = array(
                'value'        =>  $consumer->consumer_secret,
                'type'         => 'html',
                'key'        => $consumer->consumer_key,
            );
            $form['elements']['id' . $consumer->id . '_enabled'] = array(
417
418
419
420
421
                'value' => (
                            ($consumer->enabled == 1) ?
                             display_icon('enabledspecific', false, get_string('enabledspecific', 'mahara', $consumer->application_title)) :
                             display_icon('disabledspecific', false, get_string('disabledspecific', 'mahara', $consumer->application_title))
                           ),
422
                'type'         => 'html',
423
                'class'        => 'text-center',
424
425
                'key'        => $consumer->consumer_key,
            );
Matt Clarkson's avatar
Matt Clarkson committed
426
427
            $form['elements']['id' . $consumer->id . '_institution'] = array(
                'value'        => institution_display_name($consumer->institution),
428
429
430
431
432
433
                'type'         => 'html',
                'key'        => $consumer->consumer_key,
            );

            // edit and delete buttons
            $form['elements']['id' . $consumer->id . '_actions'] = array(
434
                'value' => pieform(array(
435
436
                        'name' => 'webservices_server_edit_' . $consumer->id,
                        'renderer' => 'div',
437
                        'class' => 'form-as-button pull-left',
438
439
440
441
442
443
444
                        'elementclasses' => false,
                        'successcallback' => 'webservices_server_submit',
                        'jsform' => false,
                        'elements' => array(
                            'token' => array('type' => 'hidden', 'value' => $consumer->id),
                            'action' => array('type' => 'hidden', 'value' => 'edit'),
                            'submit' => array(
445
446
                                'type' => 'button',
                                'usebuttontag' => true,
447
                                'class' => 'btn-default btn-xs',
448
449
                                'value' => '<span class="icon icon-pencil icon-lg" role="presentation" aria-hidden="true"></span><span class="sr-only">' . get_string('editspecific', 'mahara', $consumer->application_title) . '</span>',
                                'elementtitle' => get_string('editspecific', 'mahara', $consumer->application_title),
450
451
452
453
454
455
456
                            ),
                        ),
                    ))
                    .
                    pieform(array(
                        'name' => 'webservices_server_delete_' . $consumer->id,
                        'renderer' => 'div',
457
                        'class' => 'form-as-button pull-left',
458
459
460
461
462
463
464
                        'elementclasses' => false,
                        'successcallback' => 'webservices_server_submit',
                        'jsform' => false,
                        'elements' => array(
                            'token' => array('type' => 'hidden', 'value' => $consumer->id),
                            'action' => array('type' => 'hidden', 'value' => 'delete'),
                            'submit' => array(
465
466
                                'type' => 'button',
                                'usebuttontag' => true,
467
                                'class' => 'btn-default btn-xs',
468
469
                                'value' => '<span class="icon icon-trash icon-lg text-danger" role="presentation" aria-hidden="true"></span><span class="sr-only">'.get_string('deletespecific', 'mahara', $consumer->application_title).'</span>',
                                'elementtitle' => get_string('deletespecific', 'mahara', $consumer->application_title),
470
471
                            ),
                        ),
472
                    )),
473
474
                'type' => 'html',
                'key' => $consumer->consumer_key,
475
                'class' => 'webserviceconfigcontrols btn-group icon-cell',
476
            );
Matt Clarkson's avatar
Matt Clarkson committed
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506

            // Check if service has extra settings
            if ($consumer->component) {
                list($moduletype, $module) = explode("/", $consumer->component);

                $hasconfig = false;

                if (safe_require_plugin($moduletype, $module)) {
                    $classname = generate_class_name($moduletype, $module);
                    if (is_callable(array($classname, 'has_oauth_service_config'))) {
                        $hasconfig = call_static_method($classname, 'has_oauth_service_config');
                    }
                }

                if ($hasconfig) {
                    $form['elements']['id' . $consumer->id . '_actions']['value'] .=
                        pieform(array(
                            'name' => 'webservices_server_config_' . $consumer->id,
                            'renderer' => 'div',
                            'class' => 'form-as-button pull-left',
                            'elementclasses' => false,
                            'successcallback' => 'webservices_server_submit',
                            'jsform' => false,
                            'elements' => array(
                                'token' => array('type' => 'hidden', 'value' => $consumer->id),
                                'action' => array('type' => 'hidden', 'value' => 'config'),
                                'submit' => array(
                                    'type' => 'button',
                                    'usebuttontag' => true,
                                    'class' => 'btn-default btn-xs',
507
508
                                    'value' => '<span class="icon icon-cog icon-lg " role="presentation" aria-hidden="true"></span><span class="sr-only">'.get_string('managespecific', 'mahara', $consumer->application_title).'</span>',
                                    'elementtitle' => get_string('managespecific', 'mahara', $consumer->application_title),
Matt Clarkson's avatar
Matt Clarkson committed
509
510
511
512
513
514
                                ),
                            ),
                        ));
                }
            }

515
        }
Matt Clarkson's avatar
Matt Clarkson committed
516

517
        $pieform = pieform_instance($form);
518
        $form = $pieform->build(false);
519
520
    }

521
522
    $form = '<div class="table-responsive">' . $form . '</div><div>' .
        pieform(array(
523
524
525
526
            'name' => 'webservices_token_generate',
            'renderer' => 'div',
            'validatecallback' => 'webservices_add_application_validate',
            'successcallback' => 'webservices_add_application_submit',
527
            'class' => 'form-inline form-inline-align-bottom',
528
529
530
531
532
533
534
535
536
            'jsform' => false,
            'action' => get_config('wwwroot') . 'webservice/admin/oauthv1sregister.php',
            'elements' => array(
                'application' => array(
                    'type' => 'text',
                    'title' => get_string('application', 'auth.webservice') . ': ',
                ),
                'institution' => array(
                    'type' => 'select',
537
                    'class' => 'institution input-small',
538
539
540
541
542
543
544
545
546
                    'options' => $iopts,
                ),
                'service' => array(
                    'type' => 'select',
                    'options' => $sopts,
                ),
                'action' => array('type' => 'hidden', 'value' => 'add'),
                'submit' => array(
                    'type' => 'submit',
547
                    'class' => 'btn-primary',
548
549
550
                    'value' => get_string('add', 'auth.webservice'),
                ),
            ),
551
        )) . '</div>';
552
553

    $elements = array(
554
555
        // fieldset for managing service function list
        'register_server' => array(
556
557
558
            'type' => 'container',
            'title' => get_string('userapplications1', 'auth.webservice'),
            'isformgroup' => false,
559
560
561
562
563
564
565
566
            'elements' => array(
                'sflist' => array(
                    'type' => 'html',
                    'value' => $form,
                )
            ),
        ),
    );
567
568

    $form = array(
Naomi Guyer's avatar
Naomi Guyer committed
569
        'renderer' => 'div',
570
571
572
573
574
575
576
577
578
579
        'type' => 'div',
        'id' => 'maintable',
        'name' => 'maincontainer',
        'dieaftersubmit' => false,
        'successcallback' => 'webservice_main_submit',
        'elements' => $elements,
    );

    return $form;
}
Matt Clarkson's avatar
Matt Clarkson committed
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675

function webservice_server_config_form($serverid) {
    global $USER, $THEME;

    list($moduletype, $module) = get_module_from_serverid($serverid);

    if (safe_require_plugin($moduletype, $module)) {

        $elements = call_static_method(generate_class_name($moduletype, $module), 'get_oauth_service_config_options', $serverid);

        $elements['submit'] = array(
            'type'  => 'submitcancel',
            'value' => array(get_string('save'), get_string('back')),
            'goto'  => get_config('wwwroot') . 'webservice/admin/oauthv1sregister.php',
            'class' => 'btn-primary',
        );

        $elements['id'] = array(
            'type'  => 'hidden',
            'value' => $serverid,
        );

        $fieldset = array(
            // fieldset for managing service function list
            'token_details' => array(
                    'type' => 'fieldset',
                    'class' => 'with-padding',
                    'elements' => array(
                        'sflist' => array(
                            'value' =>  pieform(array(
                                'name' => 'oauthconfigoptions',
                                'successcallback'  => 'webservice_server_config_submit',
                                'elements' => $elements)),
                        )
                    ),
                    'collapsible' => false,
                ),
        );

        $form = array(
            'name' => 'maincontainer',
            'jsform' => false,
            'elements' => $fieldset,
        );

        return $form;
    }
}

function webservice_server_config_submit(Pieform $form, $values) {

    $serverid = $values['id'];

    list($moduletype, $module) = get_module_from_serverid($serverid);

    if (safe_require_plugin($moduletype, $module)) {

        call_static_method(generate_class_name($moduletype, $module), 'save_oauth_service_config_options', $serverid, $values);
        redirect(get_config('wwwroot') . 'webservice/admin/oauthv1sregister.php');
    }

    return false;
}


function update_oauth_server_config($serverid, $key, $value) {

    $dbvalue = get_field('oauth_server_config', 'value', 'oauthserverregistryid', $serverid, 'field', $key);
    if (false !== $dbvalue) {
        if (($dbvalue == $value)
            || set_field('oauth_server_config', 'value', $value, 'oauthserverregistryid', $serverid, 'field', $key)
        ) {
            return true;
        }
    }
    else {
        $config = new StdClass;
        $config->oauthserverregistryid = $serverid;
        $config->field = $key;
        $config->value = $value;
        $status = insert_record('oauth_server_config', $config);

        return true;
    }

    return false;
}

function get_module_from_serverid($serverid) {

    $consumer = get_record_sql('
            SELECT es.id, es.component
            FROM {oauth_server_registry} osr
            JOIN {external_services} es
                ON es.id = osr.externalserviceid
            WHERE osr.id = ? ', array($serverid));
676
677
678
679
    if (substr_count($consumer->component, '/') > 0) {
        return explode("/", $consumer->component);
    }
    return array('auth', 'webservice');
Matt Clarkson's avatar
Matt Clarkson committed
680
}