Merge branch 'wip-MDL-31175-m21' of git://github.com/samhemelryk/moodle into MOODLE_2...
[moodle.git] / user / managetoken.php
blob1c62bd901f97d01cc54c7ae68fffd2989d21d64f
1 <?php
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.
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/>.
18 /**
19 * Web service test client.
21 * @package webservice
22 * @copyright 2009 Moodle Pty Ltd (http://moodle.com)
23 * @author Jerome Mouneyrac
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 require('../config.php');
29 require_login();
30 require_sesskey();
32 $usercontext = get_context_instance(CONTEXT_USER, $USER->id);
34 $PAGE->set_context($usercontext);
35 $PAGE->set_url('/user/managetoken.php');
36 $PAGE->set_title(get_string('securitykeys', 'webservice'));
37 $PAGE->set_heading(get_string('securitykeys', 'webservice'));
38 $PAGE->set_pagelayout('standard');
40 $rsstokenboxhtml = $webservicetokenboxhtml = '';
41 /// Manage user web service tokens
42 if ( !is_siteadmin($USER->id)
43 && !empty($CFG->enablewebservices)
44 && has_capability('moodle/webservice:createtoken', $usercontext )) {
45 require($CFG->dirroot.'/webservice/lib.php');
47 $action = optional_param('action', '', PARAM_ACTION);
48 $tokenid = optional_param('tokenid', '', PARAM_SAFEDIR);
49 $confirm = optional_param('confirm', 0, PARAM_BOOL);
51 $webservice = new webservice(); //load the webservice library
52 $wsrenderer = $PAGE->get_renderer('core', 'webservice');
54 if ($action == 'resetwstoken') {
55 $token = $webservice->get_created_by_user_ws_token($USER->id, $tokenid);
56 /// Display confirmation page to Reset the token
57 if (!$confirm) {
58 $resetconfirmation = $wsrenderer->user_reset_token_confirmation($token);
59 } else {
60 /// Delete the token that need to be regenerated
61 $webservice->delete_user_ws_token($tokenid);
65 //no point creating the table is we're just displaying a confirmation screen
66 if (empty($resetconfirmation)) {
67 $webservice->generate_user_ws_tokens($USER->id); //generate all token that need to be generated
68 $tokens = $webservice->get_user_ws_tokens($USER->id);
69 foreach ($tokens as $token) {
70 if ($token->restrictedusers) {
71 $authlist = $webservice->get_ws_authorised_user($token->wsid, $USER->id);
72 if (empty($authlist)) {
73 $token->enabled = false;
77 $webservicetokenboxhtml = $wsrenderer->user_webservice_tokens_box($tokens, $USER->id,
78 $CFG->enablewsdocumentation); //display the box for web service token
82 //RSS keys
83 if (!empty($CFG->enablerssfeeds)) {
84 require_once($CFG->dirroot.'/lib/rsslib.php');
86 $action = optional_param('action', '', PARAM_ACTION);
87 $confirm = optional_param('confirm', 0, PARAM_BOOL);
89 $rssrenderer = $PAGE->get_renderer('core', 'rss');
91 if ($action=='resetrsstoken') {
92 /// Display confirmation page to Reset the token
93 if (!$confirm) {
94 $resetconfirmation = $rssrenderer->user_reset_rss_token_confirmation();
95 } else {
96 rss_delete_token($USER->id);
99 if (empty($resetconfirmation)) {
100 $token = rss_get_token($USER->id);
101 $rsstokenboxhtml = $rssrenderer->user_rss_token_box($token); //display the box for the user's RSS token
105 // PAGE OUTPUT
106 echo $OUTPUT->header();
107 if (!empty($resetconfirmation)) {
108 echo $resetconfirmation;
109 } else {
110 echo $webservicetokenboxhtml;
111 echo $rsstokenboxhtml;
113 echo $OUTPUT->footer();