[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/components/com_users/src/Controller/ -> RegistrationController.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\Controller;
  12  
  13  use Joomla\CMS\Component\ComponentHelper;
  14  use Joomla\CMS\Factory;
  15  use Joomla\CMS\Language\Text;
  16  use Joomla\CMS\MVC\Controller\BaseController;
  17  use Joomla\CMS\Router\Route;
  18  
  19  // phpcs:disable PSR1.Files.SideEffects
  20  \defined('_JEXEC') or die;
  21  // phpcs:enable PSR1.Files.SideEffects
  22  
  23  /**
  24   * Registration controller class for Users.
  25   *
  26   * @since  1.6
  27   */
  28  class RegistrationController extends BaseController
  29  {
  30      /**
  31       * Method to activate a user.
  32       *
  33       * @return  boolean  True on success, false on failure.
  34       *
  35       * @since   1.6
  36       * @throws  \Exception
  37       */
  38      public function activate()
  39      {
  40          $user    = $this->app->getIdentity();
  41          $input   = $this->input;
  42          $uParams = ComponentHelper::getParams('com_users');
  43  
  44          // Check for admin activation. Don't allow non-super-admin to delete a super admin
  45          if ($uParams->get('useractivation') != 2 && $user->get('id')) {
  46              $this->setRedirect('index.php');
  47  
  48              return true;
  49          }
  50  
  51          // If user registration or account activation is disabled, throw a 403.
  52          if ($uParams->get('useractivation') == 0 || $uParams->get('allowUserRegistration') == 0) {
  53              throw new \Exception(Text::_('JLIB_APPLICATION_ERROR_ACCESS_FORBIDDEN'), 403);
  54          }
  55  
  56          /** @var \Joomla\Component\Users\Site\Model\RegistrationModel $model */
  57          $model = $this->getModel('Registration', 'Site');
  58          $token = $input->getAlnum('token');
  59  
  60          // Check that the token is in a valid format.
  61          if ($token === null || strlen($token) !== 32) {
  62              throw new \Exception(Text::_('JINVALID_TOKEN'), 403);
  63          }
  64  
  65          // Get the User ID
  66          $userIdToActivate = $model->getUserIdFromToken($token);
  67  
  68          if (!$userIdToActivate) {
  69              $this->setMessage(Text::_('COM_USERS_ACTIVATION_TOKEN_NOT_FOUND'));
  70              $this->setRedirect(Route::_('index.php?option=com_users&view=login', false));
  71  
  72              return false;
  73          }
  74  
  75          // Get the user we want to activate
  76          $userToActivate = Factory::getUser($userIdToActivate);
  77  
  78          // Admin activation is on and admin is activating the account
  79          if (($uParams->get('useractivation') == 2) && $userToActivate->getParam('activate', 0)) {
  80              // If a user admin is not logged in, redirect them to the login page with an error message
  81              if (!$user->authorise('core.create', 'com_users') || !$user->authorise('core.manage', 'com_users')) {
  82                  $activationUrl = 'index.php?option=com_users&task=registration.activate&token=' . $token;
  83                  $loginUrl      = 'index.php?option=com_users&view=login&return=' . base64_encode($activationUrl);
  84  
  85                  // In case we still run into this in the second step the user does not have the right permissions
  86                  $message = Text::_('COM_USERS_REGISTRATION_ACL_ADMIN_ACTIVATION_PERMISSIONS');
  87  
  88                  // When we are not logged in we should login
  89                  if ($user->guest) {
  90                      $message = Text::_('COM_USERS_REGISTRATION_ACL_ADMIN_ACTIVATION');
  91                  }
  92  
  93                  $this->setMessage($message);
  94                  $this->setRedirect(Route::_($loginUrl, false));
  95  
  96                  return false;
  97              }
  98          }
  99  
 100          // Attempt to activate the user.
 101          $return = $model->activate($token);
 102  
 103          // Check for errors.
 104          if ($return === false) {
 105              // Redirect back to the home page.
 106              $this->setMessage(Text::sprintf('COM_USERS_REGISTRATION_SAVE_FAILED', $model->getError()), 'error');
 107              $this->setRedirect('index.php');
 108  
 109              return false;
 110          }
 111  
 112          $useractivation = $uParams->get('useractivation');
 113  
 114          // Redirect to the login screen.
 115          if ($useractivation == 0) {
 116              $this->setMessage(Text::_('COM_USERS_REGISTRATION_SAVE_SUCCESS'));
 117              $this->setRedirect(Route::_('index.php?option=com_users&view=login', false));
 118          } elseif ($useractivation == 1) {
 119              $this->setMessage(Text::_('COM_USERS_REGISTRATION_ACTIVATE_SUCCESS'));
 120              $this->setRedirect(Route::_('index.php?option=com_users&view=login', false));
 121          } elseif ($return->getParam('activate')) {
 122              $this->setMessage(Text::_('COM_USERS_REGISTRATION_VERIFY_SUCCESS'));
 123              $this->setRedirect(Route::_('index.php?option=com_users&view=registration&layout=complete', false));
 124          } else {
 125              $this->setMessage(Text::_('COM_USERS_REGISTRATION_ADMINACTIVATE_SUCCESS'));
 126              $this->setRedirect(Route::_('index.php?option=com_users&view=registration&layout=complete', false));
 127          }
 128  
 129          return true;
 130      }
 131  
 132      /**
 133       * Method to register a user.
 134       *
 135       * @return  boolean  True on success, false on failure.
 136       *
 137       * @since   1.6
 138       * @throws  \Exception
 139       */
 140      public function register()
 141      {
 142          // Check for request forgeries.
 143          $this->checkToken();
 144  
 145          // If registration is disabled - Redirect to login page.
 146          if (ComponentHelper::getParams('com_users')->get('allowUserRegistration') == 0) {
 147              $this->setRedirect(Route::_('index.php?option=com_users&view=login', false));
 148  
 149              return false;
 150          }
 151  
 152          $app   = $this->app;
 153  
 154          /** @var \Joomla\Component\Users\Site\Model\RegistrationModel $model */
 155          $model = $this->getModel('Registration', 'Site');
 156  
 157          // Get the user data.
 158          $requestData = $this->input->post->get('jform', array(), 'array');
 159  
 160          // Validate the posted data.
 161          $form = $model->getForm();
 162  
 163          if (!$form) {
 164              throw new \Exception($model->getError(), 500);
 165          }
 166  
 167          $data = $model->validate($form, $requestData);
 168  
 169          // Check for validation errors.
 170          if ($data === false) {
 171              // Get the validation messages.
 172              $errors = $model->getErrors();
 173  
 174              // Push up to three validation messages out to the user.
 175              for ($i = 0, $n = count($errors); $i < $n && $i < 3; $i++) {
 176                  if ($errors[$i] instanceof \Exception) {
 177                      $app->enqueueMessage($errors[$i]->getMessage(), 'error');
 178                  } else {
 179                      $app->enqueueMessage($errors[$i], 'error');
 180                  }
 181              }
 182  
 183              // Save the data in the session.
 184              $app->setUserState('com_users.registration.data', $requestData);
 185  
 186              // Redirect back to the registration screen.
 187              $this->setRedirect(Route::_('index.php?option=com_users&view=registration', false));
 188  
 189              return false;
 190          }
 191  
 192          // Attempt to save the data.
 193          $return = $model->register($data);
 194  
 195          // Check for errors.
 196          if ($return === false) {
 197              // Save the data in the session.
 198              $app->setUserState('com_users.registration.data', $data);
 199  
 200              // Redirect back to the edit screen.
 201              $this->setMessage($model->getError(), 'error');
 202              $this->setRedirect(Route::_('index.php?option=com_users&view=registration', false));
 203  
 204              return false;
 205          }
 206  
 207          // Flush the data from the session.
 208          $app->setUserState('com_users.registration.data', null);
 209  
 210          // Redirect to the profile screen.
 211          if ($return === 'adminactivate') {
 212              $this->setMessage(Text::_('COM_USERS_REGISTRATION_COMPLETE_VERIFY'));
 213              $this->setRedirect(Route::_('index.php?option=com_users&view=registration&layout=complete', false));
 214          } elseif ($return === 'useractivate') {
 215              $this->setMessage(Text::_('COM_USERS_REGISTRATION_COMPLETE_ACTIVATE'));
 216              $this->setRedirect(Route::_('index.php?option=com_users&view=registration&layout=complete', false));
 217          } else {
 218              $this->setMessage(Text::_('COM_USERS_REGISTRATION_SAVE_SUCCESS'));
 219              $this->setRedirect(Route::_('index.php?option=com_users&view=login', false));
 220          }
 221  
 222          return true;
 223      }
 224  }


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