[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_privacy/src/Controller/ -> RequestController.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Administrator
   5   * @subpackage  com_privacy
   6   *
   7   * @copyright   (C) 2018 Open Source Matters, Inc. <https://www.joomla.org>
   8   * @license     GNU General Public License version 2 or later; see LICENSE.txt
   9   */
  10  
  11  namespace Joomla\Component\Privacy\Administrator\Controller;
  12  
  13  use Joomla\CMS\Language\Text;
  14  use Joomla\CMS\MVC\Controller\FormController;
  15  use Joomla\CMS\MVC\Model\BaseDatabaseModel;
  16  use Joomla\CMS\Router\Route;
  17  use Joomla\CMS\Uri\Uri;
  18  use Joomla\Component\Privacy\Administrator\Model\ExportModel;
  19  use Joomla\Component\Privacy\Administrator\Model\RemoveModel;
  20  use Joomla\Component\Privacy\Administrator\Model\RequestModel;
  21  use Joomla\Component\Privacy\Administrator\Table\RequestTable;
  22  
  23  // phpcs:disable PSR1.Files.SideEffects
  24  \defined('_JEXEC') or die;
  25  // phpcs:enable PSR1.Files.SideEffects
  26  
  27  /**
  28   * Request management controller class.
  29   *
  30   * @since  3.9.0
  31   */
  32  class RequestController extends FormController
  33  {
  34      /**
  35       * Method to complete a request.
  36       *
  37       * @param   string  $key     The name of the primary key of the URL variable.
  38       * @param   string  $urlVar  The name of the URL variable if different from the primary key (sometimes required to avoid router collisions).
  39       *
  40       * @return  boolean
  41       *
  42       * @since   3.9.0
  43       */
  44      public function complete($key = null, $urlVar = null)
  45      {
  46          // Check for request forgeries.
  47          $this->checkToken();
  48  
  49          /** @var RequestModel $model */
  50          $model = $this->getModel();
  51  
  52          /** @var RequestTable $table */
  53          $table = $model->getTable();
  54  
  55          // Determine the name of the primary key for the data.
  56          if (empty($key)) {
  57              $key = $table->getKeyName();
  58          }
  59  
  60          // To avoid data collisions the urlVar may be different from the primary key.
  61          if (empty($urlVar)) {
  62              $urlVar = $key;
  63          }
  64  
  65          $recordId = $this->input->getInt($urlVar);
  66  
  67          $item = $model->getItem($recordId);
  68  
  69          // Ensure this record can transition to the requested state
  70          if (!$this->canTransition($item, '2')) {
  71              $this->setMessage(Text::_('COM_PRIVACY_ERROR_COMPLETE_TRANSITION_NOT_PERMITTED'), 'error');
  72  
  73              $this->setRedirect(
  74                  Route::_(
  75                      'index.php?option=com_privacy&view=request&id=' . $recordId,
  76                      false
  77                  )
  78              );
  79  
  80              return false;
  81          }
  82  
  83          // Build the data array for the update
  84          $data = [
  85              $key     => $recordId,
  86              'status' => '2',
  87          ];
  88  
  89          // Access check.
  90          if (!$this->allowSave($data, $key)) {
  91              $this->setMessage(Text::_('JLIB_APPLICATION_ERROR_SAVE_NOT_PERMITTED'), 'error');
  92  
  93              $this->setRedirect(
  94                  Route::_(
  95                      'index.php?option=com_privacy&view=request&id=' . $recordId,
  96                      false
  97                  )
  98              );
  99  
 100              return false;
 101          }
 102  
 103          // Attempt to save the data.
 104          if (!$model->save($data)) {
 105              // Redirect back to the edit screen.
 106              $this->setMessage(Text::sprintf('JLIB_APPLICATION_ERROR_SAVE_FAILED', $model->getError()), 'error');
 107  
 108              $this->setRedirect(
 109                  Route::_(
 110                      'index.php?option=com_privacy&view=request&id=' . $recordId,
 111                      false
 112                  )
 113              );
 114  
 115              return false;
 116          }
 117  
 118          // Log the request completed
 119          $model->logRequestCompleted($recordId);
 120  
 121          $this->setMessage(Text::_('COM_PRIVACY_REQUEST_COMPLETED'));
 122  
 123          $url = 'index.php?option=com_privacy&view=requests';
 124  
 125          // Check if there is a return value
 126          $return = $this->input->get('return', null, 'base64');
 127  
 128          if (!is_null($return) && Uri::isInternal(base64_decode($return))) {
 129              $url = base64_decode($return);
 130          }
 131  
 132          // Redirect to the list screen.
 133          $this->setRedirect(Route::_($url, false));
 134  
 135          return true;
 136      }
 137  
 138      /**
 139       * Method to email the data export for a request.
 140       *
 141       * @return  boolean
 142       *
 143       * @since   3.9.0
 144       */
 145      public function emailexport()
 146      {
 147          // Check for request forgeries.
 148          $this->checkToken('get');
 149  
 150          /** @var ExportModel $model */
 151          $model = $this->getModel('Export');
 152  
 153          $recordId = $this->input->getUint('id');
 154  
 155          if (!$model->emailDataExport($recordId)) {
 156              // Redirect back to the edit screen.
 157              $this->setMessage(Text::sprintf('COM_PRIVACY_ERROR_EXPORT_EMAIL_FAILED', $model->getError()), 'error');
 158          } else {
 159              $this->setMessage(Text::_('COM_PRIVACY_EXPORT_EMAILED'));
 160          }
 161  
 162          $url = 'index.php?option=com_privacy&view=requests';
 163  
 164          // Check if there is a return value
 165          $return = $this->input->get('return', null, 'base64');
 166  
 167          if (!is_null($return) && Uri::isInternal(base64_decode($return))) {
 168              $url = base64_decode($return);
 169          }
 170  
 171          // Redirect to the list screen.
 172          $this->setRedirect(Route::_($url, false));
 173  
 174          return true;
 175      }
 176  
 177      /**
 178       * Method to export the data for a request.
 179       *
 180       * @return  $this
 181       *
 182       * @since   3.9.0
 183       */
 184      public function export()
 185      {
 186          $this->input->set('view', 'export');
 187  
 188          return $this->display();
 189      }
 190  
 191      /**
 192       * Method to invalidate a request.
 193       *
 194       * @param   string  $key     The name of the primary key of the URL variable.
 195       * @param   string  $urlVar  The name of the URL variable if different from the primary key (sometimes required to avoid router collisions).
 196       *
 197       * @return  boolean
 198       *
 199       * @since   3.9.0
 200       */
 201      public function invalidate($key = null, $urlVar = null)
 202      {
 203          // Check for request forgeries.
 204          $this->checkToken();
 205  
 206          /** @var RequestModel $model */
 207          $model = $this->getModel();
 208  
 209          /** @var RequestTable $table */
 210          $table = $model->getTable();
 211  
 212          // Determine the name of the primary key for the data.
 213          if (empty($key)) {
 214              $key = $table->getKeyName();
 215          }
 216  
 217          // To avoid data collisions the urlVar may be different from the primary key.
 218          if (empty($urlVar)) {
 219              $urlVar = $key;
 220          }
 221  
 222          $recordId = $this->input->getInt($urlVar);
 223  
 224          $item = $model->getItem($recordId);
 225  
 226          // Ensure this record can transition to the requested state
 227          if (!$this->canTransition($item, '-1')) {
 228              $this->setMessage(Text::_('COM_PRIVACY_ERROR_INVALID_TRANSITION_NOT_PERMITTED'), 'error');
 229  
 230              $this->setRedirect(
 231                  Route::_(
 232                      'index.php?option=com_privacy&view=request&id=' . $recordId,
 233                      false
 234                  )
 235              );
 236  
 237              return false;
 238          }
 239  
 240          // Build the data array for the update
 241          $data = [
 242              $key     => $recordId,
 243              'status' => '-1',
 244          ];
 245  
 246          // Access check.
 247          if (!$this->allowSave($data, $key)) {
 248              $this->setMessage(Text::_('JLIB_APPLICATION_ERROR_SAVE_NOT_PERMITTED'), 'error');
 249  
 250              $this->setRedirect(
 251                  Route::_(
 252                      'index.php?option=com_privacy&view=request&id=' . $recordId,
 253                      false
 254                  )
 255              );
 256  
 257              return false;
 258          }
 259  
 260          // Attempt to save the data.
 261          if (!$model->save($data)) {
 262              // Redirect back to the edit screen.
 263              $this->setMessage(Text::sprintf('JLIB_APPLICATION_ERROR_SAVE_FAILED', $model->getError()), 'error');
 264  
 265              $this->setRedirect(
 266                  Route::_(
 267                      'index.php?option=com_privacy&view=request&id=' . $recordId,
 268                      false
 269                  )
 270              );
 271  
 272              return false;
 273          }
 274  
 275          // Log the request invalidated
 276          $model->logRequestInvalidated($recordId);
 277  
 278          $this->setMessage(Text::_('COM_PRIVACY_REQUEST_INVALIDATED'));
 279  
 280          $url = 'index.php?option=com_privacy&view=requests';
 281  
 282          // Check if there is a return value
 283          $return = $this->input->get('return', null, 'base64');
 284  
 285          if (!is_null($return) && Uri::isInternal(base64_decode($return))) {
 286              $url = base64_decode($return);
 287          }
 288  
 289          // Redirect to the list screen.
 290          $this->setRedirect(Route::_($url, false));
 291  
 292          return true;
 293      }
 294  
 295      /**
 296       * Method to remove the user data for a privacy remove request.
 297       *
 298       * @return  boolean
 299       *
 300       * @since   3.9.0
 301       */
 302      public function remove()
 303      {
 304          // Check for request forgeries.
 305          $this->checkToken('request');
 306  
 307          /** @var RemoveModel $model */
 308          $model = $this->getModel('Remove');
 309  
 310          $recordId = $this->input->getUint('id');
 311  
 312          if (!$model->removeDataForRequest($recordId)) {
 313              // Redirect back to the edit screen.
 314              $this->setMessage(Text::sprintf('COM_PRIVACY_ERROR_REMOVE_DATA_FAILED', $model->getError()), 'error');
 315  
 316              $this->setRedirect(
 317                  Route::_(
 318                      'index.php?option=com_privacy&view=request&id=' . $recordId,
 319                      false
 320                  )
 321              );
 322  
 323              return false;
 324          }
 325  
 326          $this->setMessage(Text::_('COM_PRIVACY_DATA_REMOVED'));
 327  
 328          $url = 'index.php?option=com_privacy&view=requests';
 329  
 330          // Check if there is a return value
 331          $return = $this->input->get('return', null, 'base64');
 332  
 333          if (!is_null($return) && Uri::isInternal(base64_decode($return))) {
 334              $url = base64_decode($return);
 335          }
 336  
 337          // Redirect to the list screen.
 338          $this->setRedirect(Route::_($url, false));
 339  
 340          return true;
 341      }
 342  
 343      /**
 344       * Function that allows child controller access to model data after the data has been saved.
 345       *
 346       * @param   BaseDatabaseModel  $model      The data model object.
 347       * @param   array              $validData  The validated data.
 348       *
 349       * @return  void
 350       *
 351       * @since   3.9.0
 352       */
 353      protected function postSaveHook(BaseDatabaseModel $model, $validData = [])
 354      {
 355          // This hook only processes new items
 356          if (!$model->getState($model->getName() . '.new', false)) {
 357              return;
 358          }
 359  
 360          if (!$model->logRequestCreated($model->getState($model->getName() . '.id'))) {
 361              if ($error = $model->getError()) {
 362                  $this->app->enqueueMessage($error, 'warning');
 363              }
 364          }
 365  
 366          if (!$model->notifyUserAdminCreatedRequest($model->getState($model->getName() . '.id'))) {
 367              if ($error = $model->getError()) {
 368                  $this->app->enqueueMessage($error, 'warning');
 369              }
 370          } else {
 371              $this->app->enqueueMessage(Text::_('COM_PRIVACY_MSG_CONFIRM_EMAIL_SENT_TO_USER'));
 372          }
 373      }
 374  
 375      /**
 376       * Method to determine if an item can transition to the specified status.
 377       *
 378       * @param   object  $item       The item being updated.
 379       * @param   string  $newStatus  The new status of the item.
 380       *
 381       * @return  boolean
 382       *
 383       * @since   3.9.0
 384       */
 385      private function canTransition($item, $newStatus)
 386      {
 387          switch ($item->status) {
 388              case '0':
 389                  // A pending item can only move to invalid through this controller due to the requirement for a user to confirm the request
 390                  return $newStatus === '-1';
 391  
 392              case '1':
 393                  // A confirmed item can be marked completed or invalid
 394                  return in_array($newStatus, ['-1', '2'], true);
 395  
 396              // An item which is already in an invalid or complete state cannot transition, likewise if we don't know the state don't change anything
 397              case '-1':
 398              case '2':
 399              default:
 400                  return false;
 401          }
 402      }
 403  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer