Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]
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. 13 // 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/>. 16 17 /** 18 * Contains the class used for the displaying the tokens table. 19 * 20 * @package core_webservice 21 * @copyright 2017 John Okely <john@moodle.com> 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25 namespace webservice; 26 27 defined('MOODLE_INTERNAL') || die; 28 29 require_once($CFG->libdir . '/tablelib.php'); 30 require_once($CFG->dirroot . '/webservice/lib.php'); 31 require_once($CFG->dirroot . '/user/lib.php'); 32 33 /** 34 * Class for the displaying the participants table. 35 * 36 * @package core_webservice 37 * @copyright 2017 John Okely <john@moodle.com> 38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 39 */ 40 class token_table extends \table_sql { 41 42 /** 43 * @var bool $showalltokens Whether or not the user is able to see all tokens. 44 */ 45 protected $showalltokens; 46 47 /** 48 * Sets up the table. 49 * @param int $id The id of the table 50 */ 51 public function __construct($id) { 52 parent::__construct($id); 53 54 // Get the context. 55 $context = \context_system::instance(); 56 57 // Can we see tokens created by all users? 58 $this->showalltokens = has_capability('moodle/webservice:managealltokens', $context); 59 60 // Define the headers and columns. 61 $headers = []; 62 $columns = []; 63 64 $headers[] = get_string('token', 'webservice'); 65 $columns[] = 'token'; 66 $headers[] = get_string('user'); 67 $columns[] = 'fullname'; 68 $headers[] = get_string('service', 'webservice'); 69 $columns[] = 'name'; 70 $headers[] = get_string('iprestriction', 'webservice'); 71 $columns[] = 'iprestriction'; 72 $headers[] = get_string('validuntil', 'webservice'); 73 $columns[] = 'validuntil'; 74 if ($this->showalltokens) { 75 // Only need to show creator if you can see tokens created by other people. 76 $headers[] = get_string('tokencreator', 'webservice'); 77 $columns[] = 'creatorlastname'; // So we can have semi-useful sorting. Table SQL doesn't two fullname collumns. 78 } 79 $headers[] = get_string('operation', 'webservice'); 80 $columns[] = 'operation'; 81 82 $this->define_columns($columns); 83 $this->define_headers($headers); 84 85 $this->no_sorting('operation'); 86 $this->no_sorting('token'); 87 $this->no_sorting('iprestriction'); 88 89 $this->set_attribute('id', $id); 90 } 91 92 /** 93 * Generate the operation column. 94 * 95 * @param \stdClass $data Data for the current row 96 * @return string Content for the column 97 */ 98 public function col_operation($data) { 99 $tokenpageurl = new \moodle_url( 100 "/admin/webservice/tokens.php", 101 [ 102 "sesskey" => sesskey(), 103 "action" => "delete", 104 "tokenid" => $data->id 105 ] 106 ); 107 return \html_writer::link($tokenpageurl, get_string("delete")); 108 } 109 110 /** 111 * Generate the validuntil column. 112 * 113 * @param \stdClass $data Data for the current row 114 * @return string Content for the column 115 */ 116 public function col_validuntil($data) { 117 if (empty($data->validuntil)) { 118 return ''; 119 } else { 120 return userdate($data->validuntil, get_string('strftimedatetime', 'langconfig')); 121 } 122 } 123 124 /** 125 * Generate the fullname column. Also includes capabilities the user is missing for the webservice (if any) 126 * 127 * @param \stdClass $data Data for the current row 128 * @return string Content for the column 129 */ 130 public function col_fullname($data) { 131 global $OUTPUT; 132 133 $userprofilurl = new \moodle_url('/user/profile.php', ['id' => $data->userid]); 134 $content = \html_writer::link($userprofilurl, fullname($data)); 135 136 // Make up list of capabilities that the user is missing for the given webservice. 137 $webservicemanager = new \webservice(); 138 $usermissingcaps = $webservicemanager->get_missing_capabilities_by_users([['id' => $data->userid]], $data->serviceid); 139 140 if (!is_siteadmin($data->userid) && array_key_exists($data->userid, $usermissingcaps)) { 141 $missingcapabilities = implode(', ', $usermissingcaps[$data->userid]); 142 if (!empty($missingcapabilities)) { 143 $capabilitiesstring = get_string('usermissingcaps', 'webservice', $missingcapabilities) . ' ' . 144 $OUTPUT->help_icon('missingcaps', 'webservice'); 145 $content .= \html_writer::div($capabilitiesstring, 'missingcaps'); 146 } 147 } 148 149 return $content; 150 } 151 152 /** 153 * Generate the token column. 154 * 155 * @param \stdClass $data Data for the current row 156 * @return string Content for the column 157 */ 158 public function col_token($data) { 159 global $USER; 160 // Hide the token if it wasn't created by the current user. 161 if ($data->creatorid != $USER->id) { 162 return '-'; 163 } 164 165 return $data->token; 166 } 167 168 /** 169 * Generate the creator column. 170 * 171 * @param \stdClass $data 172 * @return string 173 */ 174 public function col_creatorlastname($data) { 175 // We have loaded all the name fields for the creator, with the 'creator' prefix. 176 // So just remove the prefix and make up a user object. 177 $user = []; 178 foreach ($data as $key => $value) { 179 if (strpos($key, 'creator') !== false) { 180 $newkey = str_replace('creator', '', $key); 181 $user[$newkey] = $value; 182 } 183 } 184 185 $creatorprofileurl = new \moodle_url('/user/profile.php', ['id' => $data->creatorid]); 186 return \html_writer::link($creatorprofileurl, fullname((object)$user)); 187 } 188 189 /** 190 * This function is used for the extra user fields. 191 * 192 * These are being dynamically added to the table so there are no functions 'col_<userfieldname>' as 193 * the list has the potential to increase in the future and we don't want to have to remember to add 194 * a new method to this class. We also don't want to pollute this class with unnecessary methods. 195 * 196 * @param string $colname The column name 197 * @param \stdClass $data 198 * @return string 199 */ 200 public function other_cols($colname, $data) { 201 return s($data->{$colname}); 202 } 203 204 /** 205 * Query the database for results to display in the table. 206 * 207 * Note: Initial bars are not implemented for this table because it includes user details twice and the initial bars do not work 208 * when the user table is included more than once. 209 * 210 * @param int $pagesize size of page for paginated displayed table. 211 * @param bool $useinitialsbar Not implemented. Please pass false. 212 */ 213 public function query_db($pagesize, $useinitialsbar = false) { 214 global $DB, $USER; 215 216 if ($useinitialsbar) { 217 debugging('Initial bar not implemented yet. Call out($pagesize, false)'); 218 } 219 220 $usernamefields = get_all_user_name_fields(true, 'u'); 221 $creatorfields = get_all_user_name_fields(true, 'c', null, 'creator'); 222 223 $params = ["tokenmode" => EXTERNAL_TOKEN_PERMANENT]; 224 225 // TODO: in order to let the administrator delete obsolete token, split the request in multiple request or use LEFT JOIN. 226 227 if ($this->showalltokens) { 228 // Show all tokens. 229 $sql = "SELECT t.id, t.token, u.id AS userid, $usernamefields, s.name, t.iprestriction, t.validuntil, s.id AS serviceid, 230 t.creatorid, $creatorfields 231 FROM {external_tokens} t, {user} u, {external_services} s, {user} c 232 WHERE t.tokentype = :tokenmode AND s.id = t.externalserviceid AND t.userid = u.id AND c.id = t.creatorid"; 233 $countsql = "SELECT COUNT(t.id) 234 FROM {external_tokens} t, {user} u, {external_services} s, {user} c 235 WHERE t.tokentype = :tokenmode AND s.id = t.externalserviceid AND t.userid = u.id AND c.id = t.creatorid"; 236 } else { 237 // Only show tokens created by the current user. 238 $sql = "SELECT t.id, t.token, u.id AS userid, $usernamefields, s.name, t.iprestriction, t.validuntil, s.id AS serviceid, 239 t.creatorid, $creatorfields 240 FROM {external_tokens} t, {user} u, {external_services} s, {user} c 241 WHERE t.creatorid=:userid AND t.tokentype = :tokenmode AND s.id = t.externalserviceid AND t.userid = u.id AND 242 c.id = t.creatorid"; 243 $countsql = "SELECT COUNT(t.id) 244 FROM {external_tokens} t, {user} u, {external_services} s, {user} c 245 WHERE t.creatorid=:userid AND t.tokentype = :tokenmode AND s.id = t.externalserviceid AND 246 t.userid = u.id AND c.id = t.creatorid"; 247 $params["userid"] = $USER->id; 248 } 249 250 $sort = $this->get_sql_sort(); 251 if ($sort) { 252 $sql = $sql . ' ORDER BY ' . $sort; 253 } 254 255 $total = $DB->count_records_sql($countsql, $params); 256 $this->pagesize($pagesize, $total); 257 258 $this->rawdata = $DB->get_recordset_sql($sql, $params, $this->get_page_start(), $this->get_page_size()); 259 } 260 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body