Merge branch 'MDL-51969-master' of https://github.com/dmitriim/moodle
[moodle.git] / user / managetoken.php
blob390f70cfb63fb0f4da89941212c0a80835b74f4d
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.
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/>.
17 /**
18 * Web service test client.
20 * @package core_webservice
21 * @copyright 2009 Moodle Pty Ltd (http://moodle.com)
22 * @author Jerome Mouneyrac
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 require('../config.php');
28 require_login();
29 require_sesskey();
31 $usercontext = context_user::instance($USER->id);
33 $PAGE->set_context($usercontext);
34 $PAGE->set_url('/user/managetoken.php');
35 $PAGE->set_title(get_string('securitykeys', 'webservice'));
36 $PAGE->set_heading(get_string('securitykeys', 'webservice'));
37 $PAGE->set_pagelayout('admin');
39 $rsstokenboxhtml = $webservicetokenboxhtml = '';
40 // Manage user web service tokens.
41 if ( !is_siteadmin($USER->id)
42 && !empty($CFG->enablewebservices)
43 && has_capability('moodle/webservice:createtoken', $usercontext )) {
44 require($CFG->dirroot.'/webservice/lib.php');
46 $action = optional_param('action', '', PARAM_ALPHANUMEXT);
47 $tokenid = optional_param('tokenid', '', PARAM_SAFEDIR);
48 $confirm = optional_param('confirm', 0, PARAM_BOOL);
50 $webservice = new webservice(); // Load the webservice library.
51 $wsrenderer = $PAGE->get_renderer('core', 'webservice');
53 if ($action == 'resetwstoken') {
54 $token = $webservice->get_created_by_user_ws_token($USER->id, $tokenid);
55 // Display confirmation page to Reset the token.
56 if (!$confirm) {
57 $resetconfirmation = $wsrenderer->user_reset_token_confirmation($token);
58 } else {
59 // Delete the token that need to be regenerated.
60 $webservice->delete_user_ws_token($tokenid);
64 // No point creating the table is we're just displaying a confirmation screen.
65 if (empty($resetconfirmation)) {
66 $webservice->generate_user_ws_tokens($USER->id); // Generate all token that need to be generated.
67 $tokens = $webservice->get_user_ws_tokens($USER->id);
68 foreach ($tokens as $token) {
69 if ($token->restrictedusers) {
70 $authlist = $webservice->get_ws_authorised_user($token->wsid, $USER->id);
71 if (empty($authlist)) {
72 $token->enabled = false;
76 $webservicetokenboxhtml = $wsrenderer->user_webservice_tokens_box($tokens, $USER->id,
77 $CFG->enablewsdocumentation); // Display the box for web service token.
81 // RSS keys.
82 if (!empty($CFG->enablerssfeeds)) {
83 require_once($CFG->dirroot.'/lib/rsslib.php');
85 $action = optional_param('action', '', PARAM_ALPHANUMEXT);
86 $confirm = optional_param('confirm', 0, PARAM_BOOL);
88 $rssrenderer = $PAGE->get_renderer('core', 'rss');
90 if ($action == 'resetrsstoken') {
91 // Display confirmation page to Reset the token.
92 if (!$confirm) {
93 $resetconfirmation = $rssrenderer->user_reset_rss_token_confirmation();
94 } else {
95 rss_delete_token($USER->id);
98 if (empty($resetconfirmation)) {
99 $token = rss_get_token($USER->id);
100 $rsstokenboxhtml = $rssrenderer->user_rss_token_box($token); // Display the box for the user's RSS token.
104 // PAGE OUTPUT.
105 echo $OUTPUT->header();
106 if (!empty($resetconfirmation)) {
107 echo $resetconfirmation;
108 } else {
109 echo $webservicetokenboxhtml;
110 echo $rsstokenboxhtml;
112 echo $OUTPUT->footer();