[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_users/src/Model/ -> MethodModel.php (source)

   1  <?php
   2  
   3  /**
   4   * @package    Joomla.Administrator
   5   * @subpackage com_users
   6   *
   7   * @copyright  (C) 2022 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\Users\Administrator\Model;
  12  
  13  use Exception;
  14  use Joomla\CMS\Event\MultiFactor\GetSetup;
  15  use Joomla\CMS\Factory;
  16  use Joomla\CMS\Language\Text;
  17  use Joomla\CMS\MVC\Model\BaseDatabaseModel;
  18  use Joomla\CMS\User\User;
  19  use Joomla\CMS\User\UserFactoryInterface;
  20  use Joomla\Component\Users\Administrator\DataShape\SetupRenderOptions;
  21  use Joomla\Component\Users\Administrator\Helper\Mfa as MfaHelper;
  22  use Joomla\Component\Users\Administrator\Table\MfaTable;
  23  
  24  // phpcs:disable PSR1.Files.SideEffects
  25  \defined('_JEXEC') or die;
  26  // phpcs:enable PSR1.Files.SideEffects
  27  
  28  /**
  29   * Multi-factor Authentication management model
  30   *
  31   * @since 4.2.0
  32   */
  33  class MethodModel extends BaseDatabaseModel
  34  {
  35      /**
  36       * List of MFA Methods
  37       *
  38       * @var   array
  39       * @since 4.2.0
  40       */
  41      protected $mfaMethods = null;
  42  
  43      /**
  44       * Get the specified MFA Method's record
  45       *
  46       * @param   string  $method  The Method to retrieve.
  47       *
  48       * @return  array
  49       * @since 4.2.0
  50       */
  51      public function getMethod(string $method): array
  52      {
  53          if (!$this->methodExists($method)) {
  54              return [
  55                  'name'          => $method,
  56                  'display'       => '',
  57                  'shortinfo'     => '',
  58                  'image'         => '',
  59                  'canDisable'    => true,
  60                  'allowMultiple' => true,
  61              ];
  62          }
  63  
  64          return $this->mfaMethods[$method];
  65      }
  66  
  67      /**
  68       * Is the specified MFA Method available?
  69       *
  70       * @param   string  $method  The Method to check.
  71       *
  72       * @return  boolean
  73       * @since 4.2.0
  74       */
  75      public function methodExists(string $method): bool
  76      {
  77          if (!is_array($this->mfaMethods)) {
  78              $this->populateMfaMethods();
  79          }
  80  
  81          return isset($this->mfaMethods[$method]);
  82      }
  83  
  84      /**
  85       * @param   User|null  $user  The user record. Null to use the currently logged in user.
  86       *
  87       * @return  array
  88       * @throws  Exception
  89       *
  90       * @since 4.2.0
  91       */
  92      public function getRenderOptions(?User $user = null): SetupRenderOptions
  93      {
  94          if (is_null($user)) {
  95              $user = Factory::getApplication()->getIdentity() ?: Factory::getUser();
  96          }
  97  
  98          $renderOptions = new SetupRenderOptions();
  99  
 100          $event    = new GetSetup($this->getRecord($user));
 101          $results = Factory::getApplication()
 102              ->getDispatcher()
 103              ->dispatch($event->getName(), $event)
 104              ->getArgument('result', []);
 105  
 106          if (empty($results)) {
 107              return $renderOptions;
 108          }
 109  
 110          foreach ($results as $result) {
 111              if (empty($result)) {
 112                  continue;
 113              }
 114  
 115              return $renderOptions->merge($result);
 116          }
 117  
 118          return $renderOptions;
 119      }
 120  
 121      /**
 122       * Get the specified MFA record. It will return a fake default record when no record ID is specified.
 123       *
 124       * @param   User|null  $user  The user record. Null to use the currently logged in user.
 125       *
 126       * @return  MfaTable
 127       * @throws  Exception
 128       *
 129       * @since 4.2.0
 130       */
 131      public function getRecord(User $user = null): MfaTable
 132      {
 133          if (is_null($user)) {
 134              $user = Factory::getApplication()->getIdentity()
 135                  ?: Factory::getContainer()->get(UserFactoryInterface::class)->loadUserById(0);
 136          }
 137  
 138          $defaultRecord = $this->getDefaultRecord($user);
 139          $id            = (int) $this->getState('id', 0);
 140  
 141          if ($id <= 0) {
 142              return $defaultRecord;
 143          }
 144  
 145          /** @var MfaTable $record */
 146          $record = $this->getTable('Mfa', 'Administrator');
 147          $loaded = $record->load(
 148              [
 149                  'user_id' => $user->id,
 150                  'id'      => $id,
 151              ]
 152          );
 153  
 154          if (!$loaded) {
 155              return $defaultRecord;
 156          }
 157  
 158          if (!$this->methodExists($record->method)) {
 159              return $defaultRecord;
 160          }
 161  
 162          return $record;
 163      }
 164  
 165      /**
 166       * Return the title to use for the page
 167       *
 168       * @return  string
 169       *
 170       * @since 4.2.0
 171       */
 172      public function getPageTitle(): string
 173      {
 174          $task = $this->getState('task', 'edit');
 175  
 176          switch ($task) {
 177              case 'mfa':
 178                  $key = 'COM_USERS_USER_MULTIFACTOR_AUTH';
 179                  break;
 180  
 181              default:
 182                  $key = sprintf('COM_USERS_MFA_%s_PAGE_HEAD', $task);
 183                  break;
 184          }
 185  
 186          return Text::_($key);
 187      }
 188  
 189      /**
 190       * @param   User|null  $user  The user record. Null to use the current user.
 191       *
 192       * @return  MfaTable
 193       * @throws  Exception
 194       *
 195       * @since 4.2.0
 196       */
 197      protected function getDefaultRecord(?User $user = null): MfaTable
 198      {
 199          if (is_null($user)) {
 200              $user = Factory::getApplication()->getIdentity()
 201                  ?: Factory::getContainer()->get(UserFactoryInterface::class)->loadUserById(0);
 202          }
 203  
 204          $method = $this->getState('method');
 205          $title  = '';
 206  
 207          if (is_null($this->mfaMethods)) {
 208              $this->populateMfaMethods();
 209          }
 210  
 211          if ($method && isset($this->mfaMethods[$method])) {
 212              $title = $this->mfaMethods[$method]['display'];
 213          }
 214  
 215          /** @var MfaTable $record */
 216          $record = $this->getTable('Mfa', 'Administrator');
 217  
 218          $record->bind(
 219              [
 220                  'id'      => null,
 221                  'user_id' => $user->id,
 222                  'title'   => $title,
 223                  'method'  => $method,
 224                  'default' => 0,
 225                  'options' => [],
 226              ]
 227          );
 228  
 229          return $record;
 230      }
 231  
 232      /**
 233       * Populate the list of MFA Methods
 234       *
 235       * @return void
 236       * @since 4.2.0
 237       */
 238      private function populateMfaMethods(): void
 239      {
 240          $this->mfaMethods = [];
 241          $mfaMethods       = MfaHelper::getMfaMethods();
 242  
 243          if (empty($mfaMethods)) {
 244              return;
 245          }
 246  
 247          foreach ($mfaMethods as $method) {
 248              $this->mfaMethods[$method['name']] = $method;
 249          }
 250  
 251          // We also need to add the backup codes Method
 252          $this->mfaMethods['backupcodes'] = [
 253              'name'          => 'backupcodes',
 254              'display'       => Text::_('COM_USERS_USER_BACKUPCODES'),
 255              'shortinfo'     => Text::_('COM_USERS_USER_BACKUPCODES_DESC'),
 256              'image'         => 'media/com_users/images/emergency.svg',
 257              'canDisable'    => false,
 258              'allowMultiple' => false,
 259          ];
 260      }
 261  }


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