webservice MDL-24372 manage token doesn't appear twice in the admin menu when create...
[moodle.git] / admin / webservice / tokens.php
CommitLineData
5ae9f539 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 * Web services tokens admin UI
20 *
21 * @package webservice
22 * @author Jerome Mouneyrac
23 * @copyright 2009 Moodle Pty Ltd (http://moodle.com)
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
5ae9f539 26require_once('../../config.php');
9c954e88 27require_once($CFG->libdir . '/adminlib.php');
de55d469 28require_once($CFG->dirroot . '/' . $CFG->admin . '/webservice/forms.php');
9c954e88 29require_once($CFG->libdir . '/externallib.php');
5ae9f539 30
9c954e88 31$action = optional_param('action', '', PARAM_ACTION);
a3103a10 32$tokenid = optional_param('tokenid', '', PARAM_SAFEDIR);
33$confirm = optional_param('confirm', 0, PARAM_BOOL);
34
15e417fe 35admin_externalpage_setup('addwebservicetoken');
36
fc0c3d6f 37//Deactivate the second 'Manage token' navigation node, and use the main 'Manage token' navigation node
38$node = $PAGE->settingsnav->find('addwebservicetoken', navigation_node::TYPE_SETTING);
39$newnode = $PAGE->settingsnav->find('webservicetokens', navigation_node::TYPE_SETTING);
40if ($node && $newnode) {
41 $node->display = false;
42 $newnode->make_active();
43}
44
5ae9f539 45require_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM));
46
de55d469 47$tokenlisturl = new moodle_url("/" . $CFG->admin . "/settings.php", array('section' => 'webservicetokens'));
5ae9f539 48
a44460b5 49require_once($CFG->dirroot . "/webservice/lib.php");
50$webservicemanager = new webservice();
51
5ae9f539 52switch ($action) {
9c954e88 53
5ae9f539 54 case 'create':
15e417fe 55 $mform = new web_service_token_form(null, array('action' => 'create'));
9c954e88 56 $data = $mform->get_data();
15e417fe 57 if ($mform->is_cancelled()) {
9c954e88 58 redirect($tokenlisturl);
59 } else if ($data and confirm_sesskey()) {
60 ignore_user_abort(true);
a44460b5 61
62 //check the the user is allowed for the service
63 $selectedservice = $webservicemanager->get_external_service_by_id($data->service);
64 if ($selectedservice->restrictedusers) {
65 $restricteduser = $webservicemanager->get_ws_authorised_user($data->service, $data->user);
66 if (empty($restricteduser)) {
a089b8d1 67 $allowuserurl = new moodle_url('/' . $CFG->admin . '/webservice/service_users.php',
a44460b5 68 array('id' => $selectedservice->id));
69 $allowuserlink = html_writer::tag('a', $selectedservice->name , array('href' => $allowuserurl));
70 $errormsg = $OUTPUT->notification(get_string('usernotallowed', 'webservice', $allowuserlink));
71 }
72 }
73
74 //process the creation
75 if (empty($errormsg)) {
76 //TODO improvement: either move this function from externallib.php to webservice/lib.php
77 // either move most of webservicelib.php functions into externallib.php
78 // (create externalmanager class) MDL-23523
79 external_generate_token(EXTERNAL_TOKEN_PERMANENT, $data->service,
80 $data->user, get_context_instance(CONTEXT_SYSTEM),
81 $data->validuntil, $data->iprestriction);
82 redirect($tokenlisturl);
83 }
15e417fe 84 }
85
9c954e88 86 //OUTPUT: create token form
61ef8f9f 87 echo $OUTPUT->header();
15e417fe 88 echo $OUTPUT->heading(get_string('createtoken', 'webservice'));
a44460b5 89 if (!empty($errormsg)) {
90 echo $errormsg;
91 }
15e417fe 92 $mform->display();
93 echo $OUTPUT->footer();
94 die;
5ae9f539 95 break;
96
a44460b5 97 case 'delete':
9c954e88 98 $token = $webservicemanager->get_created_by_user_ws_token($USER->id, $tokenid);
99
100 //Delete the token
101 if ($confirm and confirm_sesskey()) {
102 $webservicemanager->delete_user_ws_token($token->id);
103 redirect($tokenlisturl);
15e417fe 104 }
9c954e88 105
106 ////OUTPUT: display delete token confirmation box
107 echo $OUTPUT->header();
108 $renderer = $PAGE->get_renderer('core', 'webservice');
109 echo $renderer->admin_delete_token_confirmation($token);
110 echo $OUTPUT->footer();
111 die;
5ae9f539 112 break;
113
114 default:
9c954e88 115 //wrong url access
116 redirect($tokenlisturl);
5ae9f539 117 break;
118}