Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 39 and 311]

   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   * Output rendering for the plugin.
  19   *
  20   * @package     tool_oauth2
  21   * @copyright   2017 Damyon Wiese
  22   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace tool_oauth2\output;
  25  
  26  use plugin_renderer_base;
  27  use html_table;
  28  use html_table_cell;
  29  use html_table_row;
  30  use html_writer;
  31  use core\oauth2\issuer;
  32  use core\oauth2\api;
  33  use moodle_url;
  34  
  35  defined('MOODLE_INTERNAL') || die();
  36  
  37  /**
  38   * Implements the plugin renderer
  39   *
  40   * @copyright 2017 Damyon Wiese
  41   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  42   */
  43  class renderer extends plugin_renderer_base {
  44      /**
  45       * This function will render one beautiful table with all the issuers.
  46       *
  47       * @param \core\oauth2\issuer[] $issuers - list of all issuers.
  48       * @return string HTML to output.
  49       */
  50      public function issuers_table($issuers) {
  51          global $CFG;
  52  
  53          $table = new html_table();
  54          $table->head  = [
  55              get_string('name'),
  56              get_string('issuerusedforlogin', 'tool_oauth2'),
  57              get_string('logindisplay', 'tool_oauth2'),
  58              get_string('issuerusedforinternal', 'tool_oauth2'),
  59              get_string('discoverystatus', 'tool_oauth2') . ' ' . $this->help_icon('discovered', 'tool_oauth2'),
  60              get_string('systemauthstatus', 'tool_oauth2') . ' ' . $this->help_icon('systemaccountconnected', 'tool_oauth2'),
  61              get_string('edit'),
  62          ];
  63          $table->attributes['class'] = 'admintable generaltable';
  64          $data = [];
  65  
  66          $index = 0;
  67  
  68          foreach ($issuers as $issuer) {
  69              // We need to handle the first and last ones specially.
  70              $first = false;
  71              if ($index == 0) {
  72                  $first = true;
  73              }
  74              $last = false;
  75              if ($index == count($issuers) - 1) {
  76                  $last = true;
  77              }
  78  
  79              // Name.
  80              $name = $issuer->get('name');
  81              $image = $issuer->get('image');
  82              if ($image) {
  83                  $name = '<img width="24" height="24" alt="" src="' . s($image) . '"> ' . s($name);
  84              }
  85              $namecell = new html_table_cell($name);
  86              $namecell->header = true;
  87  
  88              // Login issuer.
  89              if ((int)$issuer->get('showonloginpage') == issuer::SERVICEONLY) {
  90                  $loginissuer = $this->pix_icon('no', get_string('notloginissuer', 'tool_oauth2'), 'tool_oauth2');
  91                  $logindisplayas = '';
  92              } else {
  93                  $logindisplayas = s($issuer->get_display_name());
  94                  if ($issuer->get('id') && $issuer->is_configured() && !empty($issuer->get_endpoint_url('userinfo'))) {
  95                      $loginissuer = $this->pix_icon('yes', get_string('loginissuer', 'tool_oauth2'), 'tool_oauth2');
  96                  } else {
  97                      $loginissuer = $this->pix_icon('notconfigured', get_string('notconfigured', 'tool_oauth2'), 'tool_oauth2');
  98                  }
  99              }
 100              $loginissuerstatuscell = new html_table_cell($loginissuer);
 101  
 102              // Internal services issuer.
 103              if ((int)$issuer->get('showonloginpage') == issuer::LOGINONLY) {
 104                  $serviceissuer = $this->pix_icon('no', get_string('issuersservicesnotallow', 'tool_oauth2'), 'tool_oauth2');
 105              } else if ($issuer->get('id') && $issuer->is_configured()) {
 106                  $serviceissuer = $this->pix_icon('yes', get_string('issuersservicesallow', 'tool_oauth2'), 'tool_oauth2');
 107              } else {
 108                  $serviceissuer = $this->pix_icon('notconfigured', get_string('notconfigured', 'tool_oauth2'), 'tool_oauth2');
 109              }
 110              $internalissuerstatuscell = new html_table_cell($serviceissuer);
 111  
 112              // Discovered.
 113              if (!empty($issuer->get('scopessupported'))) {
 114                  $discovered = $this->pix_icon('yes', get_string('discovered', 'tool_oauth2'), 'tool_oauth2');
 115              } else {
 116                  if (!empty($issuer->get_endpoint_url('discovery'))) {
 117                      $discovered = $this->pix_icon('no', get_string('notdiscovered', 'tool_oauth2'), 'tool_oauth2');
 118                  } else {
 119                      $discovered = '-';
 120                  }
 121              }
 122  
 123              $discoverystatuscell = new html_table_cell($discovered);
 124  
 125              // Connected.
 126              if ($issuer->is_system_account_connected()) {
 127                  $systemaccount = \core\oauth2\api::get_system_account($issuer);
 128                  $systemauth = s($systemaccount->get('email')) . ' (' . s($systemaccount->get('username')). ') ';
 129                  $systemauth .= $this->pix_icon('yes', get_string('systemaccountconnected', 'tool_oauth2'), 'tool_oauth2');
 130              } else {
 131                  $systemauth = $this->pix_icon('no', get_string('systemaccountnotconnected', 'tool_oauth2'), 'tool_oauth2');
 132              }
 133  
 134              $params = ['id' => $issuer->get('id'), 'action' => 'auth'];
 135              $authurl = new moodle_url('/admin/tool/oauth2/issuers.php', $params);
 136              $icon = $this->pix_icon('auth', get_string('connectsystemaccount', 'tool_oauth2'), 'tool_oauth2');
 137              $authlink = html_writer::link($authurl, $icon);
 138              $systemauth .= ' ' . $authlink;
 139  
 140              $systemauthstatuscell = new html_table_cell($systemauth);
 141  
 142              $links = '';
 143              // Action links.
 144              $editurl = new moodle_url('/admin/tool/oauth2/issuers.php', ['id' => $issuer->get('id'), 'action' => 'edit']);
 145              $editlink = html_writer::link($editurl, $this->pix_icon('t/edit', get_string('edit')));
 146              $links .= ' ' . $editlink;
 147  
 148              // Endpoints.
 149              $editendpointsurl = new moodle_url('/admin/tool/oauth2/endpoints.php', ['issuerid' => $issuer->get('id')]);
 150              $str = get_string('editendpoints', 'tool_oauth2');
 151              $editendpointlink = html_writer::link($editendpointsurl, $this->pix_icon('t/viewdetails', $str));
 152              $links .= ' ' . $editendpointlink;
 153  
 154              // User field mapping.
 155              $params = ['issuerid' => $issuer->get('id')];
 156              $edituserfieldmappingsurl = new moodle_url('/admin/tool/oauth2/userfieldmappings.php', $params);
 157              $str = get_string('edituserfieldmappings', 'tool_oauth2');
 158              $edituserfieldmappinglink = html_writer::link($edituserfieldmappingsurl, $this->pix_icon('t/user', $str));
 159              $links .= ' ' . $edituserfieldmappinglink;
 160  
 161              // Delete.
 162              $deleteurl = new moodle_url('/admin/tool/oauth2/issuers.php', ['id' => $issuer->get('id'), 'action' => 'delete']);
 163              $deletelink = html_writer::link($deleteurl, $this->pix_icon('t/delete', get_string('delete')));
 164              $links .= ' ' . $deletelink;
 165              // Enable / Disable.
 166              if ($issuer->get('enabled')) {
 167                  // Disable.
 168                  $disableparams = ['id' => $issuer->get('id'), 'sesskey' => sesskey(), 'action' => 'disable'];
 169                  $disableurl = new moodle_url('/admin/tool/oauth2/issuers.php', $disableparams);
 170                  $disablelink = html_writer::link($disableurl, $this->pix_icon('t/hide', get_string('disable')));
 171                  $links .= ' ' . $disablelink;
 172              } else {
 173                  // Enable.
 174                  $enableparams = ['id' => $issuer->get('id'), 'sesskey' => sesskey(), 'action' => 'enable'];
 175                  $enableurl = new moodle_url('/admin/tool/oauth2/issuers.php', $enableparams);
 176                  $enablelink = html_writer::link($enableurl, $this->pix_icon('t/show', get_string('enable')));
 177                  $links .= ' ' . $enablelink;
 178              }
 179              if (!$last) {
 180                  // Move down.
 181                  $params = ['id' => $issuer->get('id'), 'action' => 'movedown', 'sesskey' => sesskey()];
 182                  $movedownurl = new moodle_url('/admin/tool/oauth2/issuers.php', $params);
 183                  $movedownlink = html_writer::link($movedownurl, $this->pix_icon('t/down', get_string('movedown')));
 184                  $links .= ' ' . $movedownlink;
 185              }
 186              if (!$first) {
 187                  // Move up.
 188                  $params = ['id' => $issuer->get('id'), 'action' => 'moveup', 'sesskey' => sesskey()];
 189                  $moveupurl = new moodle_url('/admin/tool/oauth2/issuers.php', $params);
 190                  $moveuplink = html_writer::link($moveupurl, $this->pix_icon('t/up', get_string('moveup')));
 191                  $links .= ' ' . $moveuplink;
 192              }
 193  
 194              $editcell = new html_table_cell($links);
 195  
 196              $row = new html_table_row([
 197                  $namecell,
 198                  $loginissuerstatuscell,
 199                  $logindisplayas,
 200                  $internalissuerstatuscell,
 201                  $discoverystatuscell,
 202                  $systemauthstatuscell,
 203                  $editcell,
 204              ]);
 205  
 206              if (!$issuer->get('enabled')) {
 207                  $row->attributes['class'] = 'dimmed_text';
 208              }
 209  
 210              $data[] = $row;
 211              $index++;
 212          }
 213          $table->data = $data;
 214          return html_writer::table($table);
 215      }
 216  
 217      /**
 218       * This function will render one beautiful table with all the endpoints.
 219       *
 220       * @param \core\oauth2\endpoint[] $endpoints - list of all endpoints.
 221       * @param int $issuerid
 222       * @return string HTML to output.
 223       */
 224      public function endpoints_table($endpoints, $issuerid) {
 225          global $CFG;
 226  
 227          $table = new html_table();
 228          $table->head  = [
 229              get_string('name'),
 230              get_string('url'),
 231              get_string('edit'),
 232          ];
 233          $table->attributes['class'] = 'admintable generaltable';
 234          $data = [];
 235  
 236          $index = 0;
 237  
 238          foreach ($endpoints as $endpoint) {
 239              // Name.
 240              $name = $endpoint->get('name');
 241              $namecell = new html_table_cell(s($name));
 242              $namecell->header = true;
 243  
 244              // Url.
 245              $url = $endpoint->get('url');
 246              $urlcell = new html_table_cell(s($url));
 247  
 248              $links = '';
 249              // Action links.
 250              $editparams = ['issuerid' => $issuerid, 'endpointid' => $endpoint->get('id'), 'action' => 'edit'];
 251              $editurl = new moodle_url('/admin/tool/oauth2/endpoints.php', $editparams);
 252              $editlink = html_writer::link($editurl, $this->pix_icon('t/edit', get_string('edit')));
 253              $links .= ' ' . $editlink;
 254  
 255              // Delete.
 256              $deleteparams = ['issuerid' => $issuerid, 'endpointid' => $endpoint->get('id'), 'action' => 'delete'];
 257              $deleteurl = new moodle_url('/admin/tool/oauth2/endpoints.php', $deleteparams);
 258              $deletelink = html_writer::link($deleteurl, $this->pix_icon('t/delete', get_string('delete')));
 259              $links .= ' ' . $deletelink;
 260  
 261              $editcell = new html_table_cell($links);
 262  
 263              $row = new html_table_row([
 264                  $namecell,
 265                  $urlcell,
 266                  $editcell,
 267              ]);
 268  
 269              $data[] = $row;
 270              $index++;
 271          }
 272          $table->data = $data;
 273          return html_writer::table($table);
 274      }
 275  
 276      /**
 277       * This function will render one beautiful table with all the user_field_mappings.
 278       *
 279       * @param \core\oauth2\user_field_mapping[] $userfieldmappings - list of all user_field_mappings.
 280       * @param int $issuerid
 281       * @return string HTML to output.
 282       */
 283      public function user_field_mappings_table($userfieldmappings, $issuerid) {
 284          global $CFG;
 285  
 286          $table = new html_table();
 287          $table->head  = [
 288              get_string('userfieldexternalfield', 'tool_oauth2'),
 289              get_string('userfieldinternalfield', 'tool_oauth2'),
 290              get_string('edit'),
 291          ];
 292          $table->attributes['class'] = 'admintable generaltable';
 293          $data = [];
 294  
 295          $index = 0;
 296  
 297          foreach ($userfieldmappings as $userfieldmapping) {
 298              // External field.
 299              $externalfield = $userfieldmapping->get('externalfield');
 300              $externalfieldcell = new html_table_cell(s($externalfield));
 301  
 302              // Internal field.
 303              $internalfield = $userfieldmapping->get('internalfield');
 304              $internalfieldcell = new html_table_cell(s($internalfield));
 305  
 306              $links = '';
 307              // Action links.
 308              $editparams = ['issuerid' => $issuerid, 'userfieldmappingid' => $userfieldmapping->get('id'), 'action' => 'edit'];
 309              $editurl = new moodle_url('/admin/tool/oauth2/userfieldmappings.php', $editparams);
 310              $editlink = html_writer::link($editurl, $this->pix_icon('t/edit', get_string('edit')));
 311              $links .= ' ' . $editlink;
 312  
 313              // Delete.
 314              $deleteparams = ['issuerid' => $issuerid, 'userfieldmappingid' => $userfieldmapping->get('id'), 'action' => 'delete'];
 315              $deleteurl = new moodle_url('/admin/tool/oauth2/userfieldmappings.php', $deleteparams);
 316              $deletelink = html_writer::link($deleteurl, $this->pix_icon('t/delete', get_string('delete')));
 317              $links .= ' ' . $deletelink;
 318  
 319              $editcell = new html_table_cell($links);
 320  
 321              $row = new html_table_row([
 322                  $externalfieldcell,
 323                  $internalfieldcell,
 324                  $editcell,
 325              ]);
 326  
 327              $data[] = $row;
 328              $index++;
 329          }
 330          $table->data = $data;
 331          return html_writer::table($table);
 332      }
 333  }