[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_privacy/src/Model/ -> RemoveModel.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\Model;
  12  
  13  use Joomla\CMS\Component\ComponentHelper;
  14  use Joomla\CMS\Factory;
  15  use Joomla\CMS\Language\Text;
  16  use Joomla\CMS\MVC\Model\BaseDatabaseModel;
  17  use Joomla\CMS\Plugin\PluginHelper;
  18  use Joomla\CMS\Table\Table;
  19  use Joomla\CMS\User\User;
  20  use Joomla\Component\Actionlogs\Administrator\Model\ActionlogModel;
  21  use Joomla\Component\Privacy\Administrator\Removal\Status;
  22  use Joomla\Component\Privacy\Administrator\Table\RequestTable;
  23  
  24  // phpcs:disable PSR1.Files.SideEffects
  25  \defined('_JEXEC') or die;
  26  // phpcs:enable PSR1.Files.SideEffects
  27  
  28  /**
  29   * Remove model class.
  30   *
  31   * @since  3.9.0
  32   */
  33  class RemoveModel extends BaseDatabaseModel
  34  {
  35      /**
  36       * Remove the user data.
  37       *
  38       * @param   integer  $id  The request ID to process
  39       *
  40       * @return  boolean
  41       *
  42       * @since   3.9.0
  43       */
  44      public function removeDataForRequest($id = null)
  45      {
  46          $id = !empty($id) ? $id : (int) $this->getState($this->getName() . '.request_id');
  47  
  48          if (!$id) {
  49              $this->setError(Text::_('COM_PRIVACY_ERROR_REQUEST_ID_REQUIRED_FOR_REMOVE'));
  50  
  51              return false;
  52          }
  53  
  54          /** @var RequestTable $table */
  55          $table = $this->getTable();
  56  
  57          if (!$table->load($id)) {
  58              $this->setError($table->getError());
  59  
  60              return false;
  61          }
  62  
  63          if ($table->request_type !== 'remove') {
  64              $this->setError(Text::_('COM_PRIVACY_ERROR_REQUEST_TYPE_NOT_REMOVE'));
  65  
  66              return false;
  67          }
  68  
  69          if ($table->status != 1) {
  70              $this->setError(Text::_('COM_PRIVACY_ERROR_CANNOT_REMOVE_UNCONFIRMED_REQUEST'));
  71  
  72              return false;
  73          }
  74  
  75          // If there is a user account associated with the email address, load it here for use in the plugins
  76          $db = $this->getDatabase();
  77  
  78          $userId = (int) $db->setQuery(
  79              $db->getQuery(true)
  80                  ->select($db->quoteName('id'))
  81                  ->from($db->quoteName('#__users'))
  82                  ->where('LOWER(' . $db->quoteName('email') . ') = LOWER(:email)')
  83                  ->bind(':email', $table->email)
  84                  ->setLimit(1)
  85          )->loadResult();
  86  
  87          $user = $userId ? User::getInstance($userId) : null;
  88  
  89          $canRemove = true;
  90  
  91          PluginHelper::importPlugin('privacy');
  92  
  93          /** @var Status[] $pluginResults */
  94          $pluginResults = Factory::getApplication()->triggerEvent('onPrivacyCanRemoveData', [$table, $user]);
  95  
  96          foreach ($pluginResults as $status) {
  97              if (!$status->canRemove) {
  98                  $this->setError($status->reason ?: Text::_('COM_PRIVACY_ERROR_CANNOT_REMOVE_DATA'));
  99  
 100                  $canRemove = false;
 101              }
 102          }
 103  
 104          if (!$canRemove) {
 105              $this->logRemoveBlocked($table, $this->getErrors());
 106  
 107              return false;
 108          }
 109  
 110          // Log the removal
 111          $this->logRemove($table);
 112  
 113          Factory::getApplication()->triggerEvent('onPrivacyRemoveData', [$table, $user]);
 114  
 115          return true;
 116      }
 117  
 118      /**
 119       * Method to get a table object, load it if necessary.
 120       *
 121       * @param   string  $name     The table name. Optional.
 122       * @param   string  $prefix   The class prefix. Optional.
 123       * @param   array   $options  Configuration array for model. Optional.
 124       *
 125       * @return  Table  A Table object
 126       *
 127       * @throws  \Exception
 128       * @since   3.9.0
 129       */
 130      public function getTable($name = 'Request', $prefix = 'Administrator', $options = [])
 131      {
 132          return parent::getTable($name, $prefix, $options);
 133      }
 134  
 135      /**
 136       * Log the data removal to the action log system.
 137       *
 138       * @param   RequestTable  $request  The request record being processed
 139       *
 140       * @return  void
 141       *
 142       * @since   3.9.0
 143       */
 144      public function logRemove(RequestTable $request)
 145      {
 146          $user = Factory::getUser();
 147  
 148          $message = [
 149              'action'      => 'remove',
 150              'id'          => $request->id,
 151              'itemlink'    => 'index.php?option=com_privacy&view=request&id=' . $request->id,
 152              'userid'      => $user->id,
 153              'username'    => $user->username,
 154              'accountlink' => 'index.php?option=com_users&task=user.edit&id=' . $user->id,
 155          ];
 156  
 157          $this->getActionlogModel()->addLog([$message], 'COM_PRIVACY_ACTION_LOG_REMOVE', 'com_privacy.request', $user->id);
 158      }
 159  
 160      /**
 161       * Log the data removal being blocked to the action log system.
 162       *
 163       * @param   RequestTable  $request  The request record being processed
 164       * @param   string[]      $reasons  The reasons given why the record could not be removed.
 165       *
 166       * @return  void
 167       *
 168       * @since   3.9.0
 169       */
 170      public function logRemoveBlocked(RequestTable $request, array $reasons)
 171      {
 172          $user = Factory::getUser();
 173  
 174          $message = [
 175              'action'      => 'remove-blocked',
 176              'id'          => $request->id,
 177              'itemlink'    => 'index.php?option=com_privacy&view=request&id=' . $request->id,
 178              'userid'      => $user->id,
 179              'username'    => $user->username,
 180              'accountlink' => 'index.php?option=com_users&task=user.edit&id=' . $user->id,
 181              'reasons'     => implode('; ', $reasons),
 182          ];
 183  
 184          $this->getActionlogModel()->addLog([$message], 'COM_PRIVACY_ACTION_LOG_REMOVE_BLOCKED', 'com_privacy.request', $user->id);
 185      }
 186  
 187      /**
 188       * Method to auto-populate the model state.
 189       *
 190       * @return  void
 191       *
 192       * @since   3.9.0
 193       */
 194      protected function populateState()
 195      {
 196          // Get the pk of the record from the request.
 197          $this->setState($this->getName() . '.request_id', Factory::getApplication()->input->getUint('id'));
 198  
 199          // Load the parameters.
 200          $this->setState('params', ComponentHelper::getParams('com_privacy'));
 201      }
 202  
 203      /**
 204       * Method to fetch an instance of the action log model.
 205       *
 206       * @return  ActionlogModel
 207       *
 208       * @since   4.0.0
 209       */
 210      private function getActionlogModel(): ActionlogModel
 211      {
 212          return Factory::getApplication()->bootComponent('com_actionlogs')
 213              ->getMVCFactory()->createModel('Actionlog', 'Administrator', ['ignore_request' => true]);
 214      }
 215  }


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