CoreFiles by mySites.guru
UserModel.php1,215 lines, 39,807 bytes, from Joomla_6.2.0-rc1-Release_Candidate-Full_Package.zip
Raw History Compare with 6.1.4-rc1
md5
4fed954fab02f60c63ce86747c4452b5
sha1
f36874e21bd2eaf84faf6ff4e6adf50be46e8319
sha256
e5eadbafe488f77f14d23f1d0fe40fa5ec570f7d31e1bed4329a1f8d33beeb1c
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215
<?php

/**
 * @package     Joomla.Administrator
 * @subpackage  com_users
 *
 * @copyright   (C) 2008 Open Source Matters, Inc. <https://www.joomla.org>
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */

namespace Joomla\Component\Users\Administrator\Model;

use Joomla\CMS\Access\Access;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Event\User\AfterDeleteEvent;
use Joomla\CMS\Event\User\AfterSaveEvent;
use Joomla\CMS\Event\User\BeforeDeleteEvent;
use Joomla\CMS\Event\User\BeforeSaveEvent;
use Joomla\CMS\Factory;
use Joomla\CMS\Form\Form;
use Joomla\CMS\Language\LanguageFactoryAwareInterface;
use Joomla\CMS\Language\LanguageFactoryAwareTrait;
use Joomla\CMS\Language\Multilanguage;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Mail\MailerFactoryAwareInterface;
use Joomla\CMS\Mail\MailerFactoryAwareTrait;
use Joomla\CMS\Mail\MailTemplate;
use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
use Joomla\CMS\MVC\Model\AdminModel;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\CMS\Table\Table;
use Joomla\CMS\User\UserFactoryAwareInterface;
use Joomla\CMS\User\UserFactoryAwareTrait;
use Joomla\CMS\User\UserHelper;
use Joomla\Database\ParameterType;
use Joomla\Utilities\ArrayHelper;

// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
// phpcs:enable PSR1.Files.SideEffects

/**
 * User model.
 *
 * @since  1.6
 */
class UserModel extends AdminModel implements UserFactoryAwareInterface, MailerFactoryAwareInterface, LanguageFactoryAwareInterface
{
    use UserFactoryAwareTrait;
    use MailerFactoryAwareTrait;
    use LanguageFactoryAwareTrait;

    /**
     * An item.
     *
     * @var    array
     */
    protected $_item = null;

    /**
     * Constructor.
     *
     * @param   array                 $config   An optional associative array of configuration settings.
     * @param   ?MVCFactoryInterface  $factory  The factory.
     *
     * @see     \Joomla\CMS\MVC\Model\BaseDatabaseModel
     * @since   3.2
     */
    public function __construct($config = [], ?MVCFactoryInterface $factory = null)
    {
        $config = array_merge(
            [
                'event_after_delete'  => 'onUserAfterDelete',
                'event_after_save'    => 'onUserAfterSave',
                'event_before_delete' => 'onUserBeforeDelete',
                'event_before_save'   => 'onUserBeforeSave',
                'events_map'          => ['save' => 'user', 'delete' => 'user', 'validate' => 'user'],
            ],
            $config
        );

        parent::__construct($config, $factory);
    }

    /**
     * Returns a reference to the a Table object, always creating it.
     *
     * @param   string  $type    The table type to instantiate
     * @param   string  $prefix  A prefix for the table class name. Optional.
     * @param   array   $config  Configuration array for model. Optional.
     *
     * @return  Table  A database object
     *
     * @since   1.6
     */
    public function getTable($type = 'User', $prefix = 'Joomla\\CMS\\Table\\', $config = [])
    {
        $table = Table::getInstance($type, $prefix, $config);

        return $table;
    }

    /**
     * Method to get a single record.
     *
     * @param   integer  $pk  The id of the primary key.
     *
     * @return  mixed  Object on success, false on failure.
     *
     * @since   1.6
     */
    public function getItem($pk = null)
    {
        $pk = (!empty($pk)) ? $pk : (int) $this->getState('user.id');

        if ($this->_item === null) {
            $this->_item = [];
        }

        if (!isset($this->_item[$pk])) {
            $this->_item[$pk] = parent::getItem($pk);
        }

        return $this->_item[$pk];
    }

    /**
     * Method to get the record form.
     *
     * @param   array    $data      An optional array of data for the form to interrogate.
     * @param   boolean  $loadData  True if the form is to load its own data (default case), false if not.
     *
     * @return  Form  A Form object
     *
     * @since   1.6
     * @throws  \Exception on failure
     */
    public function getForm($data = [], $loadData = true)
    {
        // Get the form.
        $form = $this->loadForm('com_users.user', 'user', ['control' => 'jform', 'load_data' => $loadData]);

        $user = $this->getCurrentUser();

        // If the user needs to change their password, mark the password fields as required
        if ($user->requireReset) {
            $form->setFieldAttribute('password', 'required', 'true');
            $form->setFieldAttribute('password2', 'required', 'true');
        }

        // When multilanguage is set, a user's default site language should also be a Content Language
        if (Multilanguage::isEnabled()) {
            $form->setFieldAttribute('language', 'type', 'frontendlanguage', 'params');
        }

        $userId = (int) $form->getValue('id');

        // The user should not be able to set the requireReset value on their own account
        if ($userId === (int) $user->id) {
            $form->removeField('requireReset');
        }

        /**
         * If users without core.manage permission editing their own account, remove some fields which they should
         * not be allowed to change and prevent them to change user name if configured
         */
        if (!$user->authorise('core.manage', 'com_users') && (int) $user->id === $userId) {
            if (!ComponentHelper::getParams('com_users')->get('change_login_name')) {
                $form->setFieldAttribute('username', 'required', 'false');
                $form->setFieldAttribute('username', 'readonly', 'true');
                $form->setFieldAttribute('username', 'description', 'COM_USERS_USER_FIELD_NOCHANGE_USERNAME_DESC');
            }

            $form->removeField('lastResetTime');
            $form->removeField('resetCount');
            $form->removeField('sendEmail');
            $form->removeField('block');
        }

        return $form;
    }

    /**
     * Method to get the data that should be injected in the form.
     *
     * @return  mixed  The data for the form.
     *
     * @since   1.6
     * @throws  \Exception
     */
    protected function loadFormData()
    {
        // Check the session for previously entered form data.
        $data = Factory::getApplication()->getUserState('com_users.edit.user.data', []);

        if (empty($data)) {
            $data = $this->getItem();
        }

        $this->preprocessData('com_users.profile', $data, 'user');

        return $data;
    }

    /**
     * Override Joomla\CMS\MVC\Model\AdminModel::preprocessForm to ensure the correct plugin group is loaded.
     *
     * @param   Form    $form   A Form object.
     * @param   mixed   $data   The data expected for the form.
     * @param   string  $group  The name of the plugin group to import (defaults to "content").
     *
     * @return  void
     *
     * @since   1.6
     *
     * @throws  \Exception if there is an error in the form event.
     */
    protected function preprocessForm(Form $form, $data, $group = 'user')
    {
        parent::preprocessForm($form, $data, $group);
    }

    /**
     * Method to save the form data.
     *
     * @param   array  $data  The form data.
     *
     * @return  boolean  True on success.
     *
     * @since   1.6
     * @throws  \Exception
     */
    public function save($data)
    {
        $pk            = (!empty($data['id'])) ? $data['id'] : (int) $this->getState('user.id');
        $user          = $this->getUserFactory()->loadUserById($pk);
        $my            = $this->getCurrentUser();
        $iAmSuperAdmin = $my->authorise('core.admin');

        // User cannot modify own user groups
        if ((int) $user->id == (int) $my->id && !$iAmSuperAdmin && isset($data['groups'])) {
            // Form was probably tampered with
            Factory::getApplication()->enqueueMessage(Text::_('COM_USERS_USERS_ERROR_CANNOT_EDIT_OWN_GROUP'), 'warning');

            $data['groups'] = null;
        }

        if ($data['block'] && $pk == $my->id && !$my->block) {
            $this->setError(Text::_('COM_USERS_USERS_ERROR_CANNOT_BLOCK_SELF'));

            return false;
        }

        // Make sure user groups is selected when add/edit an account
        if (empty($data['groups']) && ((int) $user->id != (int) $my->id || $iAmSuperAdmin)) {
            $this->setError(Text::_('COM_USERS_USERS_ERROR_CANNOT_SAVE_ACCOUNT_WITHOUT_GROUPS'));

            return false;
        }

        // Make sure that we are not removing ourself from Super Admin group
        if ($iAmSuperAdmin && $my->id == $pk) {
            // Check that at least one of our new groups is Super Admin
            $stillSuperAdmin = false;
            $myNewGroups     = $data['groups'];

            foreach ($myNewGroups as $group) {
                $stillSuperAdmin = $stillSuperAdmin ?: Access::checkGroup($group, 'core.admin');
            }

            if (!$stillSuperAdmin) {
                $this->setError(Text::_('COM_USERS_USERS_ERROR_CANNOT_DEMOTE_SELF'));

                return false;
            }
        }

        // Unset the username if it should not be overwritten
        if (
            !$my->authorise('core.manage', 'com_users')
            && (int) $user->id === (int) $my->id
            && !ComponentHelper::getParams('com_users')->get('change_login_name')
        ) {
            unset($data['username']);
        }

        // Bind the data.
        if (!$user->bind($data)) {
            $this->setError($user->getError());

            return false;
        }

        // Store the data.
        if (!$user->save()) {
            $this->setError($user->getError());

            return false;
        }

        // Destroy all active sessions for the user after changing the password or blocking him
        if (!empty($data['password2']) || !empty($data['block'])) {
            UserHelper::destroyUserSessions($user->id, true);
        }

        $this->setState('user.id', $user->id);

        return true;
    }

    /**
     * Method to delete rows.
     *
     * @param   array  &$pks  An array of item ids.
     *
     * @return  boolean  Returns true on success, false on failure.
     *
     * @since   1.6
     * @throws  \Exception
     */
    public function delete(&$pks)
    {
        $user  = $this->getCurrentUser();
        $table = $this->getTable();
        $pks   = (array) $pks;

        // Check if I am a Super Admin
        $iAmSuperAdmin = $user->authorise('core.admin');

        $dispatcher = $this->getDispatcher();
        PluginHelper::importPlugin($this->events_map['delete'], null, true, $dispatcher);

        if (\in_array($user->id, $pks)) {
            $this->setError(Text::_('COM_USERS_USERS_ERROR_CANNOT_DELETE_SELF'));

            return false;
        }

        // Iterate the items to delete each one.
        foreach ($pks as $i => $pk) {
            if ($table->load($pk)) {
                // Access checks.
                $allow = $user->authorise('core.delete', 'com_users');

                // Don't allow non-super-admin to delete a super admin
                $allow = (!$iAmSuperAdmin && Access::check($pk, 'core.admin')) ? false : $allow;

                if ($allow) {
                    // Get users data for the users to delete.
                    $user_to_delete = $this->getUserFactory()->loadUserById($pk);

                    // Fire the before delete event.
                    $dispatcher->dispatch(
                        $this->event_before_delete,
                        new BeforeDeleteEvent($this->event_before_delete, [
                            'subject' => $table->getProperties(),
                        ])
                    );

                    if (!$table->delete($pk)) {
                        $this->setError($table->getError());

                        return false;
                    }

                    // Trigger the after delete event.
                    $dispatcher->dispatch(
                        $this->event_after_delete,
                        new AfterDeleteEvent($this->event_after_delete, [
                            'subject'        => ArrayHelper::fromObject($user_to_delete, false),
                            'deletingResult' => true,
                            'errorMessage'   => $this->getError(),
                        ])
                    );
                } else {
                    // Prune items that you can't change.
                    unset($pks[$i]);
                    Factory::getApplication()->enqueueMessage(Text::_('JERROR_CORE_DELETE_NOT_PERMITTED'), 'error');
                }
            } else {
                $this->setError($table->getError());

                return false;
            }
        }

        return true;
    }

    /**
     * Method to block user records.
     *
     * @param   array    &$pks   The ids of the items to publish.
     * @param   integer  $value  The value of the published state
     *
     * @return  boolean  True on success.
     *
     * @since   1.6
     * @throws  \Exception
     */
    public function block(&$pks, $value = 1)
    {
        $app  = Factory::getApplication();
        $user = $this->getCurrentUser();

        // Check if I am a Super Admin
        $iAmSuperAdmin = $user->authorise('core.admin');
        $table         = $this->getTable();
        $pks           = (array) $pks;

        $dispatcher = $this->getDispatcher();
        PluginHelper::importPlugin($this->events_map['save'], null, true, $dispatcher);

        // Prepare the logout options.
        $options = [
            'clientid' => $app->get('shared_session', '0') ? null : 0,
        ];

        // Access checks.
        foreach ($pks as $i => $pk) {
            if ($value == 1 && $pk == $user->id) {
                // Cannot block yourself.
                unset($pks[$i]);
                Factory::getApplication()->enqueueMessage(Text::_('COM_USERS_USERS_ERROR_CANNOT_BLOCK_SELF'), 'error');
            } elseif ($table->load($pk)) {
                $old   = $table->getProperties();
                $allow = $user->authorise('core.edit.state', 'com_users');

                // Don't allow non-super-admin to delete a super admin
                $allow = (!$iAmSuperAdmin && Access::check($pk, 'core.admin')) ? false : $allow;

                if ($allow) {
                    // Skip changing of same state
                    if ($table->block == $value) {
                        unset($pks[$i]);
                        continue;
                    }

                    $table->block = (int) $value;

                    // If unblocking, also change password reset count to zero to unblock reset
                    if ($table->block === 0) {
                        $table->resetCount = 0;
                    }

                    // Allow an exception to be thrown.
                    try {
                        if (!$table->check()) {
                            $this->setError($table->getError());

                            return false;
                        }

                        // Trigger the before save event.
                        $result = $dispatcher->dispatch(
                            $this->event_before_save,
                            new BeforeSaveEvent($this->event_before_save, [
                                'subject' => $old,
                                'isNew'   => false,
                                'data'    => $table->getProperties(),
                            ])
                        )->getArgument('result', []);

                        if (\in_array(false, $result, true)) {
                            // Plugin will have to raise its own error or throw an exception.
                            return false;
                        }

                        // Store the table.
                        if (!$table->store()) {
                            $this->setError($table->getError());

                            return false;
                        }

                        if ($table->block) {
                            UserHelper::destroyUserSessions($table->id);
                        }

                        // Trigger the after save event
                        $dispatcher->dispatch($this->event_after_save, new AfterSaveEvent($this->event_after_save, [
                            'subject'      => $table->getProperties(),
                            'isNew'        => false,
                            'savingResult' => true,
                            'errorMessage' => null,
                        ]));
                    } catch (\Exception $e) {
                        $this->setError($e->getMessage());

                        return false;
                    }

                    // Log the user out.
                    if ($value) {
                        $app->logout($table->id, $options);
                    }
                } else {
                    // Prune items that you can't change.
                    unset($pks[$i]);
                    Factory::getApplication()->enqueueMessage(Text::_('JLIB_APPLICATION_ERROR_EDITSTATE_NOT_PERMITTED'), 'error');
                }
            }
        }

        return true;
    }

    /**
     * Method to activate user records.
     *
     * @param   array  &$pks  The ids of the items to activate.
     *
     * @return  boolean  True on success.
     *
     * @since   1.6
     * @throws  \Exception
     */
    public function activate(&$pks)
    {
        $app  = Factory::getApplication();
        $user = $app->getIdentity();

        // Check if I am a super admin
        $iAmSuperAdmin = $user->authorise('core.admin');

        // Load user table
        $table = $this->getTable();
        $pks   = (array) $pks;

        // Compile the user activated notification mail default values.
        $mailData             = [];
        $mailData['siteurl']  = \Joomla\CMS\Uri\Uri::root();
        $mailData['fromname'] = $app->get('fromname');
        $mailData['mailfrom'] = $app->get('mailfrom');
        $mailData['sitename'] = $app->get('sitename');

        // Load com_users site language strings, the mail template use it
        $app->getLanguage()->load('com_users', JPATH_SITE);

        $sendMailTo = function ($userData) use ($app, $mailData) {
            $mailData['name']     = $userData['name'];
            $mailData['username'] = $userData['username'];

            // Use the default language
            $langTag = ComponentHelper::getParams('com_languages')->get('site', 'en-GB');

            $mailer = new MailTemplate(
                'com_users.registration.user.admin_activated',
                $langTag,
                $this->getMailerFactory()->createMailer(),
                $this->getLanguageFactory(),
                $this->getDatabase()
            );
            $mailer->addTemplateData($mailData);
            $mailer->addRecipient($userData['email']);
            $mailer->addUnsafeTags(['username', 'name']);

            try {
                $return = $mailer->send();
            } catch (\Exception $exception) {
                try {
                    \Joomla\CMS\Log\Log::add(Text::_($exception->getMessage()), \Joomla\CMS\Log\Log::WARNING, 'jerror');

                    $return = false;
                } catch (\RuntimeException $exception) {
                    $app->enqueueMessage(Text::_($exception->errorMessage()), $app::MSG_WARNING);

                    $return = false;
                }
            }

            // Check for an error.
            if ($return !== true) {
                $app->enqueueMessage(Text::_('COM_USERS_REGISTRATION_ACTIVATION_NOTIFY_SEND_MAIL_FAILED'), $app::MSG_WARNING);

                return false;
            }

            $app->enqueueMessage(Text::_('COM_USERS_REGISTRATION_ACTIVATION_NOTIFY_SEND_MAIL_SUCCESS'), $app::MSG_INFO);
            return true;
        };

        $dispatcher = $this->getDispatcher();
        PluginHelper::importPlugin($this->events_map['save'], null, true, $dispatcher);

        // Activate and send the notification email
        foreach ($pks as $i => $pk) {
            if ($table->load($pk)) {
                $prevUserData = $table->getProperties();
                $allow        = $user->authorise('core.edit.state', 'com_users');

                // Don't allow non-super-admin to edit the active status of a super admin
                $allow = (!$iAmSuperAdmin && Access::check($pk, 'core.admin')) ? false : $allow;

                // Ignore activated accounts but check if we can still
                // resend the notification email
                if (empty($table->activation)) {
                    if (\is_null($table->lastvisitDate)) {
                        $sendMailTo($prevUserData);
                    }

                    unset($pks[$i]);
                } elseif ($allow) {
                    $table->block      = 0;
                    $table->activation = '';

                    // Allow an exception to be thrown.
                    try {
                        if (!$table->check()) {
                            $this->setError($table->getError());

                            return false;
                        }

                        // Trigger the before save event.
                        $result = $dispatcher->dispatch(
                            $this->event_before_save,
                            new BeforeSaveEvent($this->event_before_save, [
                                'subject' => $prevUserData,
                                'isNew'   => false,
                                'data'    => $table->getProperties(),
                            ])
                        )->getArgument('result', []);


                        if (\in_array(false, $result, true)) {
                            // Plugin will have to raise it's own error or throw an exception.
                            return false;
                        }

                        // Store the table.
                        if (!$table->store()) {
                            $this->setError($table->getError());

                            return false;
                        }

                        // Fire the after save event
                        $dispatcher->dispatch($this->event_after_save, new AfterSaveEvent($this->event_after_save, [
                            'subject'      => $table->getProperties(),
                            'isNew'        => false,
                            'savingResult' => true,
                            'errorMessage' => null,
                        ]));

                        // Send the email
                        if (!$sendMailTo($prevUserData)) {
                            return false;
                        }
                    } catch (\Exception $e) {
                        $this->setError($e->getMessage());

                        return false;
                    }
                } else {
                    // Prune items that you can't change.
                    unset($pks[$i]);
                    $app->enqueueMessage(Text::_('JLIB_APPLICATION_ERROR_EDITSTATE_NOT_PERMITTED'), 'error');
                }
            }
        }

        return true;
    }

    /**
     * Method to perform batch operations on an item or a set of items.
     *
     * @param   array  $commands  An array of commands to perform.
     * @param   array  $pks       An array of item ids.
     * @param   array  $contexts  An array of item contexts.
     *
     * @return  boolean  Returns true on success, false on failure.
     *
     * @since   2.5
     */
    public function batch($commands, $pks, $contexts)
    {
        // Sanitize user ids.
        $pks = array_unique($pks);
        $pks = ArrayHelper::toInteger($pks);

        // Remove any values of zero.
        if (array_search(0, $pks, true)) {
            unset($pks[array_search(0, $pks, true)]);
        }

        if (empty($pks)) {
            $this->setError(Text::_('COM_USERS_USERS_NO_ITEM_SELECTED'));

            return false;
        }

        $done = false;

        if (!empty($commands['group_id'])) {
            $cmd = ArrayHelper::getValue($commands, 'group_action', 'add');

            if (!$this->batchUser((int) $commands['group_id'], $pks, $cmd)) {
                return false;
            }

            $done = true;
        }

        if (!empty($commands['reset_id'])) {
            if (!$this->batchReset($pks, $commands['reset_id'])) {
                return false;
            }

            $done = true;
        }

        if (!$done) {
            $this->setError(Text::_('JLIB_APPLICATION_ERROR_INSUFFICIENT_BATCH_INFORMATION'));

            return false;
        }

        // Clear the cache
        $this->cleanCache();

        return true;
    }

    /**
     * Batch flag users as being required to reset their passwords
     *
     * @param   array   $userIds  An array of user IDs on which to operate
     * @param   string  $action   The action to perform
     *
     * @return  boolean  True on success, false on failure
     *
     * @since   3.2
     */
    public function batchReset($userIds, $action)
    {
        $userIds = ArrayHelper::toInteger($userIds);

        // Check if user can perform management tasks in com_users
        if (!$this->getCurrentUser()->authorise('core.manage', 'com_users')) {
            $this->setError(Text::_('JLIB_APPLICATION_ERROR_BATCH_CANNOT_EDIT'));

            return false;
        }

        // Check if I am a Super Admin
        $iAmSuperAdmin = $this->getCurrentUser()->authorise('core.admin');

        // Non-super super user cannot work with super-admin user.
        if (!$iAmSuperAdmin && UserHelper::checkSuperUserInUsers($userIds)) {
            $this->setError(Text::_('COM_USERS_ERROR_CANNOT_BATCH_SUPERUSER'));

            return false;
        }

        // Set the action to perform
        if ($action === 'yes') {
            $value = 1;
        } else {
            $value = 0;
        }

        // Prune out the current user if they are in the supplied user ID array
        $userIds = array_diff($userIds, [$this->getCurrentUser()->id]);

        if (empty($userIds)) {
            $this->setError(Text::_('COM_USERS_USERS_ERROR_CANNOT_REQUIRERESET_SELF'));

            return false;
        }

        // Get the DB object
        $db = $this->getDatabase();

        $userIds = ArrayHelper::toInteger($userIds);

        $query = $db->createQuery();

        // Update the reset flag
        $query->update($db->quoteName('#__users'))
            ->set($db->quoteName('requireReset') . ' = :requireReset')
            ->whereIn($db->quoteName('id'), $userIds)
            ->bind(':requireReset', $value, ParameterType::INTEGER);

        $db->setQuery($query);

        try {
            $db->execute();
        } catch (\RuntimeException $e) {
            $this->setError($e->getMessage());

            return false;
        }

        return true;
    }

    /**
     * Perform batch operations
     *
     * @param   integer  $groupId  The group ID which assignments are being edited
     * @param   array    $userIds  An array of user IDs on which to operate
     * @param   string   $action   The action to perform
     *
     * @return  boolean  True on success, false on failure
     *
     * @since   1.6
     */
    public function batchUser($groupId, $userIds, $action)
    {
        $userIds = ArrayHelper::toInteger($userIds);

        // Check if user can perform management tasks in com_users
        if (!$this->getCurrentUser()->authorise('core.manage', 'com_users')) {
            $this->setError(Text::_('JLIB_APPLICATION_ERROR_BATCH_CANNOT_EDIT'));

            return false;
        }

        // Check if I am a Super Admin
        $iAmSuperAdmin = $this->getCurrentUser()->authorise('core.admin');

        // Non-super super user cannot work with super-admin user.
        if (!$iAmSuperAdmin && UserHelper::checkSuperUserInUsers($userIds)) {
            $this->setError(Text::_('COM_USERS_ERROR_CANNOT_BATCH_SUPERUSER'));

            return false;
        }

        // Non-super admin cannot work with super-admin group.
        if ((!$iAmSuperAdmin && Access::checkGroup($groupId, 'core.admin')) || $groupId < 1) {
            $this->setError(Text::_('COM_USERS_ERROR_INVALID_GROUP'));

            return false;
        }

        // Get the DB object
        $db = $this->getDatabase();

        switch ($action) {
            case 'set':
                // Sets users to a selected group
                $doDelete = 'all';
                $doAssign = true;
                break;

            case 'del':
                // Remove users from a selected group
                $doDelete = 'group';
                break;

            case 'add':
            default:
                // Add users to a selected group
                $doAssign = true;
                break;
        }

        // Remove the users from the group if requested.
        if (isset($doDelete)) {
            /*
             * First we need to check that the user is part of more than one group
             * otherwise we will end up with a user that is not part of any group
             * unless we are moving the user to a new group.
             */
            if ($doDelete === 'group') {
                $query = $db->createQuery();
                $query->select($db->quoteName('user_id'))
                    ->from($db->quoteName('#__user_usergroup_map'))
                    ->whereIn($db->quoteName('user_id'), $userIds);

                // Add the group by clause to remove users who are only in one group
                $query->group($db->quoteName('user_id'))
                    ->having('COUNT(user_id) > 1');
                $db->setQuery($query);
                $users = $db->loadColumn();

                // If we have no users to process, throw an error to notify the user
                if (empty($users)) {
                    $this->setError(Text::_('COM_USERS_ERROR_ONLY_ONE_GROUP'));

                    return false;
                }

                // Check to see if the users are in the group to be removed
                $query->clear()
                    ->select($db->quoteName('user_id'))
                    ->from($db->quoteName('#__user_usergroup_map'))
                    ->whereIn($db->quoteName('user_id'), $users)
                    ->where($db->quoteName('group_id') . ' = :group_id')
                    ->bind(':group_id', $groupId, ParameterType::INTEGER);
                $db->setQuery($query);
                $users = $db->loadColumn();

                // If we have no users to process, throw an error to notify the user
                if (empty($users)) {
                    $this->setError(Text::_('COM_USERS_ERROR_NOT_IN_GROUP'));

                    return false;
                }

                // Finally remove the users from the group
                $query->clear()
                    ->delete($db->quoteName('#__user_usergroup_map'))
                    ->whereIn($db->quoteName('user_id'), $users)
                    ->where($db->quoteName('group_id') . '= :group_id')
                    ->bind(':group_id', $groupId, ParameterType::INTEGER);
                $db->setQuery($query);
            } elseif ($doDelete === 'all') {
                $query = $db->createQuery();
                $query->delete($db->quoteName('#__user_usergroup_map'))
                    ->whereIn($db->quoteName('user_id'), $userIds);
            }
            $db->setQuery($query);

            try {
                $db->execute();
            } catch (\RuntimeException $e) {
                $this->setError($e->getMessage());

                return false;
            }
        }

        // Assign the users to the group if requested.
        if (isset($doAssign)) {
            $query = $db->createQuery();

            // First, we need to check if the user is already assigned to a group
            $query->select($db->quoteName('user_id'))
                ->from($db->quoteName('#__user_usergroup_map'))
                ->where($db->quoteName('group_id') . ' = :group_id')
                ->bind(':group_id', $groupId, ParameterType::INTEGER);
            $db->setQuery($query);
            $users = $db->loadColumn();

            // Build the values clause for the assignment query.
            $query->clear();
            $groups = false;

            foreach ($userIds as $id) {
                if (!\in_array($id, $users)) {
                    $query->values($id . ',' . $groupId);
                    $groups = true;
                }
            }

            // If we have no users to process, throw an error to notify the user
            if (!$groups) {
                $this->setError(Text::_('COM_USERS_ERROR_NO_ADDITIONS'));

                return false;
            }

            $query->insert($db->quoteName('#__user_usergroup_map'))
                ->columns([$db->quoteName('user_id'), $db->quoteName('group_id')]);
            $db->setQuery($query);

            try {
                $db->execute();
            } catch (\RuntimeException $e) {
                $this->setError($e->getMessage());

                return false;
            }
        }

        return true;
    }

    /**
     * Gets the available groups.
     *
     * @return  array  An array of groups
     *
     * @since   1.6
     */
    public function getGroups()
    {
        $user = $this->getCurrentUser();

        if ($user->authorise('core.edit', 'com_users') && $user->authorise('core.manage', 'com_users')) {
            $model = $this->bootComponent('com_users')
                ->getMVCFactory()->createModel('Groups', 'Administrator', ['ignore_request' => true]);

            return $model->getItems();
        }

        return null;
    }

    /**
     * Gets the groups this object is assigned to
     *
     * @param   integer  $userId  The user ID to retrieve the groups for
     *
     * @return  array  An array of assigned groups
     *
     * @since   1.6
     */
    public function getAssignedGroups($userId = null)
    {
        $userId = (!empty($userId)) ? $userId : (int) $this->getState('user.id');

        if (empty($userId)) {
            $result = [];
            $form   = $this->getForm();

            if ($form) {
                $groupsIDs = $form->getValue('groups');
            }

            if (!empty($groupsIDs)) {
                $result = $groupsIDs;
            } else {
                $params = ComponentHelper::getParams('com_users');

                if ($groupId = $params->get('new_usertype', $params->get('guest_usergroup', 1))) {
                    $result[] = $groupId;
                }
            }
        } else {
            $result = UserHelper::getUserGroups($userId);
        }

        return $result;
    }

    /**
     * No longer used
     *
     * @param   integer  $userId  Ignored
     *
     * @return  \stdClass
     *
     * @since   3.2
     *
     * @deprecated   4.2 will be removed in 7.0.
     *               Will be removed without replacement
     */
    public function getOtpConfig($userId = null)
    {
        @trigger_error(
            \sprintf(
                '%s() is deprecated. Use \Joomla\Component\Users\Administrator\Helper\Mfa::getUserMfaRecords() instead.',
                __METHOD__
            ),
            E_USER_DEPRECATED
        );

        // Return the configuration object
        return (object) [
            'method' => 'none',
            'config' => [],
            'otep'   => [],
        ];
    }

    /**
     * No longer used
     *
     * @param   integer    $userId     Ignored
     * @param   \stdClass  $otpConfig  Ignored
     *
     * @return  boolean  True on success
     *
     * @since   3.2
     *
     * @deprecated   4.2 will be removed in 7.0.
     *               Will be removed without replacement
     */
    public function setOtpConfig($userId, $otpConfig)
    {
        @trigger_error(
            \sprintf(
                '%s() is deprecated. Multi-factor Authentication actions are handled by plugins in the multifactorauth folder.',
                __METHOD__
            ),
            E_USER_DEPRECATED
        );

        return true;
    }

    /**
     * No longer used
     *
     * @return  string
     *
     * @since   3.2
     *
     * @deprecated   4.2 will be removed in 7.0.
     *               Use \Joomla\CMS\Factory::getApplication()->get('secret') instead'
     */
    public function getOtpConfigEncryptionKey()
    {
        @trigger_error(
            \sprintf(
                '%s() is deprecated. Use \Joomla\CMS\Factory::getApplication()->get(\'secret\') instead',
                __METHOD__
            ),
            E_USER_DEPRECATED
        );

        return Factory::getApplication()->get('secret');
    }

    /**
     * No longer used
     *
     * @param   integer  $userId  Ignored
     *
     * @return  array  Empty array
     *
     * @since   3.2
     * @throws  \Exception
     *
     * @deprecated   4.2 will be removed in 7.0.
     *               Will be removed without replacement
     */
    public function getTwofactorform($userId = null)
    {
        @trigger_error(
            \sprintf(
                '%s() is deprecated. Use \Joomla\Component\Users\Administrator\Helper\Mfa::getConfigurationInterface()',
                __METHOD__
            ),
            E_USER_DEPRECATED
        );

        return [];
    }

    /**
     * No longer used
     *
     * @param   integer  $userId  Ignored
     * @param   integer  $count   Ignored
     *
     * @return  array  Empty array
     *
     * @since   3.2
     *
     * @deprecated   4.2 will be removed in 7.0
     *               Will be removed without replacement
     */
    public function generateOteps($userId, $count = 10)
    {
        @trigger_error(
            \sprintf(
                '%s() is deprecated. See \Joomla\Component\Users\Administrator\Model\BackupcodesModel::saveBackupCodes()',
                __METHOD__
            ),
            E_USER_DEPRECATED
        );

        return [];
    }

    /**
     * No longer used. Always returns true.
     *
     * @param   integer  $userId     Ignored
     * @param   string   $secretKey  Ignored
     * @param   array    $options    Ignored
     *
     * @return  boolean  Always true
     *
     * @since   3.2
     * @throws  \Exception
     *
     * @deprecated   4.2 will be removed in 7.0
     *               Will be removed without replacement
     */
    public function isValidSecretKey($userId, $secretKey, $options = [])
    {
        @trigger_error(
            \sprintf(
                '%s() is deprecated. Multi-factor Authentication actions are handled by plugins in the multifactorauth folder.',
                __METHOD__
            ),
            E_USER_DEPRECATED
        );

        return true;
    }

    /**
     * No longer used
     *
     * @param   integer  $userId     Ignored
     * @param   string   $otep       Ignored
     * @param   object   $otpConfig  Ignored
     *
     * @return  boolean  Always true
     *
     * @since   3.2
     *
     * @deprecated   4.2 will be removed in 7.0
     *               Will be removed without replacement
     */
    public function isValidOtep($userId, $otep, $otpConfig = null)
    {
        @trigger_error(
            \sprintf(
                '%s() is deprecated. Multi-factor Authentication actions are handled by plugins in the multifactorauth folder.',
                __METHOD__
            ),
            E_USER_DEPRECATED
        );

        return true;
    }
}

The same content is in 4 releases, Joomla 6.2.0-rc1 to 6.2.1-rc1. See them all, or every version of this file.