[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Site
   5   * @subpackage  com_users
   6   *
   7   * @copyright   (C) 2009 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\Site\Model;
  12  
  13  use Joomla\CMS\Access\Access;
  14  use Joomla\CMS\Component\ComponentHelper;
  15  use Joomla\CMS\Factory;
  16  use Joomla\CMS\Form\Form;
  17  use Joomla\CMS\Form\FormFactoryInterface;
  18  use Joomla\CMS\Language\Multilanguage;
  19  use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
  20  use Joomla\CMS\MVC\Model\FormModel;
  21  use Joomla\CMS\Plugin\PluginHelper;
  22  use Joomla\CMS\String\PunycodeHelper;
  23  use Joomla\CMS\User\User;
  24  use Joomla\CMS\User\UserHelper;
  25  use Joomla\Component\Users\Administrator\Model\UserModel;
  26  use Joomla\Registry\Registry;
  27  
  28  // phpcs:disable PSR1.Files.SideEffects
  29  \defined('_JEXEC') or die;
  30  // phpcs:enable PSR1.Files.SideEffects
  31  
  32  /**
  33   * Profile model class for Users.
  34   *
  35   * @since  1.6
  36   */
  37  class ProfileModel extends FormModel
  38  {
  39      /**
  40       * @var     object  The user profile data.
  41       * @since   1.6
  42       */
  43      protected $data;
  44  
  45      /**
  46       * Constructor.
  47       *
  48       * @param   array                 $config       An array of configuration options (name, state, dbo, table_path, ignore_request).
  49       * @param   MVCFactoryInterface   $factory      The factory.
  50       * @param   FormFactoryInterface  $formFactory  The form factory.
  51       *
  52       * @see     \Joomla\CMS\MVC\Model\BaseDatabaseModel
  53       * @since   3.2
  54       */
  55      public function __construct($config = array(), MVCFactoryInterface $factory = null, FormFactoryInterface $formFactory = null)
  56      {
  57          $config = array_merge(
  58              array(
  59                  'events_map' => array('validate' => 'user')
  60              ),
  61              $config
  62          );
  63  
  64          parent::__construct($config, $factory, $formFactory);
  65      }
  66  
  67      /**
  68       * Method to get the profile form data.
  69       *
  70       * The base form data is loaded and then an event is fired
  71       * for users plugins to extend the data.
  72       *
  73       * @return  User
  74       *
  75       * @since   1.6
  76       * @throws  \Exception
  77       */
  78      public function getData()
  79      {
  80          if ($this->data === null) {
  81              $userId = $this->getState('user.id');
  82  
  83              // Initialise the table with Joomla\CMS\User\User.
  84              $this->data = new User($userId);
  85  
  86              // Set the base user data.
  87              $this->data->email1 = $this->data->get('email');
  88  
  89              // Override the base user data with any data in the session.
  90              $temp = (array) Factory::getApplication()->getUserState('com_users.edit.profile.data', array());
  91  
  92              foreach ($temp as $k => $v) {
  93                  $this->data->$k = $v;
  94              }
  95  
  96              // Unset the passwords.
  97              unset($this->data->password1, $this->data->password2);
  98  
  99              $registry           = new Registry($this->data->params);
 100              $this->data->params = $registry->toArray();
 101          }
 102  
 103          return $this->data;
 104      }
 105  
 106      /**
 107       * Method to get the profile form.
 108       *
 109       * The base form is loaded from XML and then an event is fired
 110       * for users plugins to extend the form with extra fields.
 111       *
 112       * @param   array    $data      An optional array of data for the form to interrogate.
 113       * @param   boolean  $loadData  True if the form is to load its own data (default case), false if not.
 114       *
 115       * @return  Form|bool  A Form object on success, false on failure
 116       *
 117       * @since   1.6
 118       */
 119      public function getForm($data = array(), $loadData = true)
 120      {
 121          // Get the form.
 122          $form = $this->loadForm('com_users.profile', 'profile', array('control' => 'jform', 'load_data' => $loadData));
 123  
 124          if (empty($form)) {
 125              return false;
 126          }
 127  
 128          // Check for username compliance and parameter set
 129          $isUsernameCompliant = true;
 130          $username = $loadData ? $form->getValue('username') : $this->loadFormData()->username;
 131  
 132          if ($username) {
 133              $isUsernameCompliant  = !(preg_match('#[<>"\'%;()&\\\\]|\\.\\./#', $username) || strlen(utf8_decode($username)) < 2
 134                  || trim($username) !== $username);
 135          }
 136  
 137          $this->setState('user.username.compliant', $isUsernameCompliant);
 138  
 139          if ($isUsernameCompliant && !ComponentHelper::getParams('com_users')->get('change_login_name')) {
 140              $form->setFieldAttribute('username', 'class', '');
 141              $form->setFieldAttribute('username', 'filter', '');
 142              $form->setFieldAttribute('username', 'description', 'COM_USERS_PROFILE_NOCHANGE_USERNAME_DESC');
 143              $form->setFieldAttribute('username', 'validate', '');
 144              $form->setFieldAttribute('username', 'message', '');
 145              $form->setFieldAttribute('username', 'readonly', 'true');
 146              $form->setFieldAttribute('username', 'required', 'false');
 147          }
 148  
 149          // When multilanguage is set, a user's default site language should also be a Content Language
 150          if (Multilanguage::isEnabled()) {
 151              $form->setFieldAttribute('language', 'type', 'frontend_language', 'params');
 152          }
 153  
 154          // If the user needs to change their password, mark the password fields as required
 155          if (Factory::getUser()->requireReset) {
 156              $form->setFieldAttribute('password1', 'required', 'true');
 157              $form->setFieldAttribute('password2', 'required', 'true');
 158          }
 159  
 160          return $form;
 161      }
 162  
 163      /**
 164       * Method to get the data that should be injected in the form.
 165       *
 166       * @return  mixed  The data for the form.
 167       *
 168       * @since   1.6
 169       */
 170      protected function loadFormData()
 171      {
 172          $data = $this->getData();
 173  
 174          $this->preprocessData('com_users.profile', $data, 'user');
 175  
 176          return $data;
 177      }
 178  
 179      /**
 180       * Override preprocessForm to load the user plugin group instead of content.
 181       *
 182       * @param   Form    $form   A Form object.
 183       * @param   mixed   $data   The data expected for the form.
 184       * @param   string  $group  The name of the plugin group to import (defaults to "content").
 185       *
 186       * @return  void
 187       *
 188       * @throws  \Exception if there is an error in the form event.
 189       *
 190       * @since   1.6
 191       */
 192      protected function preprocessForm(Form $form, $data, $group = 'user')
 193      {
 194          if (ComponentHelper::getParams('com_users')->get('frontend_userparams')) {
 195              $form->loadFile('frontend', false);
 196  
 197              if (Factory::getUser()->authorise('core.login.admin')) {
 198                  $form->loadFile('frontend_admin', false);
 199              }
 200          }
 201  
 202          parent::preprocessForm($form, $data, $group);
 203      }
 204  
 205      /**
 206       * Method to auto-populate the model state.
 207       *
 208       * Note. Calling getState in this method will result in recursion.
 209       *
 210       * @return  void
 211       *
 212       * @since   1.6
 213       * @throws  \Exception
 214       */
 215      protected function populateState()
 216      {
 217          // Get the application object.
 218          $params = Factory::getApplication()->getParams('com_users');
 219  
 220          // Get the user id.
 221          $userId = Factory::getApplication()->getUserState('com_users.edit.profile.id');
 222          $userId = !empty($userId) ? $userId : (int) Factory::getUser()->get('id');
 223  
 224          // Set the user id.
 225          $this->setState('user.id', $userId);
 226  
 227          // Load the parameters.
 228          $this->setState('params', $params);
 229      }
 230  
 231      /**
 232       * Method to save the form data.
 233       *
 234       * @param   array  $data  The form data.
 235       *
 236       * @return  mixed  The user id on success, false on failure.
 237       *
 238       * @since   1.6
 239       * @throws  \Exception
 240       */
 241      public function save($data)
 242      {
 243          $userId = (!empty($data['id'])) ? $data['id'] : (int) $this->getState('user.id');
 244  
 245          $user = new User($userId);
 246  
 247          // Prepare the data for the user object.
 248          $data['email']    = PunycodeHelper::emailToPunycode($data['email1']);
 249          $data['password'] = $data['password1'];
 250  
 251          // Unset the username if it should not be overwritten
 252          $isUsernameCompliant = $this->getState('user.username.compliant');
 253  
 254          if ($isUsernameCompliant && !ComponentHelper::getParams('com_users')->get('change_login_name')) {
 255              unset($data['username']);
 256          }
 257  
 258          // Unset block and sendEmail so they do not get overwritten
 259          unset($data['block'], $data['sendEmail']);
 260  
 261          // Bind the data.
 262          if (!$user->bind($data)) {
 263              $this->setError($user->getError());
 264  
 265              return false;
 266          }
 267  
 268          // Load the users plugin group.
 269          PluginHelper::importPlugin('user');
 270  
 271          // Retrieve the user groups so they don't get overwritten
 272          unset($user->groups);
 273          $user->groups = Access::getGroupsByUser($user->id, false);
 274  
 275          // Store the data.
 276          if (!$user->save()) {
 277              $this->setError($user->getError());
 278  
 279              return false;
 280          }
 281  
 282          // Destroy all active sessions for the user after changing the password
 283          if ($data['password']) {
 284              UserHelper::destroyUserSessions($user->id, true);
 285          }
 286  
 287          return $user->id;
 288      }
 289  
 290      /**
 291       * Gets the configuration forms for all two-factor authentication methods
 292       * in an array.
 293       *
 294       * @param   integer  $userId  The user ID to load the forms for (optional)
 295       *
 296       * @return  array
 297       *
 298       * @since   3.2
 299       * @deprecated 4.2.0 Will be removed in 5.0.
 300       */
 301      public function getTwofactorform($userId = null)
 302      {
 303          return [];
 304      }
 305  
 306      /**
 307       * No longer used
 308       *
 309       * @param   integer  $userId  Ignored
 310       *
 311       * @return  \stdClass
 312       *
 313       * @since   3.2
 314       * @deprecated 4.2.0  Will be removed in 5.0
 315       */
 316      public function getOtpConfig($userId = null)
 317      {
 318          @trigger_error(
 319              sprintf(
 320                  '%s() is deprecated. Use \Joomla\Component\Users\Administrator\Helper\Mfa::getUserMfaRecords() instead.',
 321                  __METHOD__
 322              ),
 323              E_USER_DEPRECATED
 324          );
 325  
 326          /** @var UserModel $model */
 327          $model = $this->bootComponent('com_users')
 328              ->getMVCFactory()->createModel('User', 'Administrator');
 329  
 330          return $model->getOtpConfig();
 331      }
 332  }


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