Skip to content

Commit 4a60a88

Browse files
authored
Use the injected user factory in MVC and plugins (#40744)
* Use the injected user factory in MVC and plugins * User::getInstance * cs
1 parent 6979688 commit 4a60a88

File tree

28 files changed

+123
-58
lines changed

28 files changed

+123
-58
lines changed

administrator/components/com_messages/src/Model/MessageModel.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
use Joomla\CMS\Table\Asset;
2525
use Joomla\CMS\Table\Table;
2626
use Joomla\CMS\User\User;
27+
use Joomla\CMS\User\UserFactoryAwareInterface;
28+
use Joomla\CMS\User\UserFactoryAwareTrait;
2729
use Joomla\Database\ParameterType;
2830
use PHPMailer\PHPMailer\Exception as phpMailerException;
2931

@@ -36,8 +38,10 @@
3638
*
3739
* @since 1.6
3840
*/
39-
class MessageModel extends AdminModel
41+
class MessageModel extends AdminModel implements UserFactoryAwareInterface
4042
{
43+
use UserFactoryAwareTrait;
44+
4145
/**
4246
* Message
4347
*
@@ -311,7 +315,7 @@ public function save($data)
311315
}
312316

313317
// Load the user details (already valid from table check).
314-
$toUser = User::getInstance($table->user_id_to);
318+
$toUser = $this->getUserFactory()->loadUserById($table->user_id_to);
315319

316320
// Check if recipient can access com_messages.
317321
if (!$toUser->authorise('core.login.admin') || !$toUser->authorise('core.manage', 'com_messages')) {
@@ -352,7 +356,7 @@ public function save($data)
352356
}
353357

354358
if ($config->get('mail_on_new', true)) {
355-
$fromUser = User::getInstance($table->user_id_from);
359+
$fromUser = $this->getUserFactory()->loadUserById($table->user_id_from);
356360
$debug = Factory::getApplication()->get('debug_lang');
357361
$default_language = ComponentHelper::getParams('com_languages')->get('administrator');
358362
$lang = Language::getInstance($toUser->getParam('admin_language', $default_language), $debug);

administrator/components/com_messages/src/View/Message/HtmlView.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
use Joomla\CMS\MVC\View\HtmlView as BaseHtmlView;
1717
use Joomla\CMS\Toolbar\Toolbar;
1818
use Joomla\CMS\Toolbar\ToolbarHelper;
19-
use Joomla\CMS\User\User;
19+
use Joomla\CMS\User\UserFactoryAwareInterface;
20+
use Joomla\CMS\User\UserFactoryAwareTrait;
2021

2122
// phpcs:disable PSR1.Files.SideEffects
2223
\defined('_JEXEC') or die;
@@ -27,8 +28,10 @@
2728
*
2829
* @since 1.6
2930
*/
30-
class HtmlView extends BaseHtmlView
31+
class HtmlView extends BaseHtmlView implements UserFactoryAwareInterface
3132
{
33+
use UserFactoryAwareTrait;
34+
3235
/**
3336
* The Form object
3437
*
@@ -98,7 +101,7 @@ protected function addToolbar()
98101
$toolbar->help('Private_Messages:_Write');
99102
} else {
100103
ToolbarHelper::title(Text::_('COM_MESSAGES_VIEW_PRIVATE_MESSAGE'), 'envelope inbox');
101-
$sender = User::getInstance($this->item->user_id_from);
104+
$sender = $this->getUserFactory()->loadUserById($this->item->user_id_from);
102105

103106
if (
104107
$sender->id !== $app->getIdentity()->get('id') && ($sender->authorise('core.admin')

administrator/components/com_privacy/src/Model/ExportModel.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
use Joomla\CMS\Plugin\PluginHelper;
2020
use Joomla\CMS\Table\Table;
2121
use Joomla\CMS\Uri\Uri;
22-
use Joomla\CMS\User\User;
22+
use Joomla\CMS\User\UserFactoryAwareInterface;
23+
use Joomla\CMS\User\UserFactoryAwareTrait;
2324
use Joomla\Component\Actionlogs\Administrator\Model\ActionlogModel;
2425
use Joomla\Component\Privacy\Administrator\Export\Domain;
2526
use Joomla\Component\Privacy\Administrator\Helper\PrivacyHelper;
@@ -35,8 +36,10 @@
3536
*
3637
* @since 3.9.0
3738
*/
38-
class ExportModel extends BaseDatabaseModel
39+
class ExportModel extends BaseDatabaseModel implements UserFactoryAwareInterface
3940
{
41+
use UserFactoryAwareTrait;
42+
4043
/**
4144
* Create the export document for an information request.
4245
*
@@ -89,7 +92,7 @@ public function collectDataForExportRequest($id = null)
8992
->setLimit(1)
9093
)->loadResult();
9194

92-
$user = $userId ? User::getInstance($userId) : null;
95+
$user = $userId ? $this->getUserFactory()->loadUserById($userId) : null;
9396

9497
// Log the export
9598
$this->logExport($table);
@@ -180,7 +183,7 @@ public function emailDataExport($id = null)
180183
)->loadResult();
181184

182185
if ($userId) {
183-
$receiver = User::getInstance($userId);
186+
$receiver = $this->getUserFactory()->loadUserById($userId);
184187

185188
/*
186189
* We don't know if the user has admin access, so we will check if they have an admin language in their parameters,

administrator/components/com_privacy/src/Model/RemoveModel.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
use Joomla\CMS\MVC\Model\BaseDatabaseModel;
1717
use Joomla\CMS\Plugin\PluginHelper;
1818
use Joomla\CMS\Table\Table;
19-
use Joomla\CMS\User\User;
19+
use Joomla\CMS\User\UserFactoryAwareInterface;
20+
use Joomla\CMS\User\UserFactoryAwareTrait;
2021
use Joomla\Component\Actionlogs\Administrator\Model\ActionlogModel;
2122
use Joomla\Component\Privacy\Administrator\Removal\Status;
2223
use Joomla\Component\Privacy\Administrator\Table\RequestTable;
@@ -30,8 +31,10 @@
3031
*
3132
* @since 3.9.0
3233
*/
33-
class RemoveModel extends BaseDatabaseModel
34+
class RemoveModel extends BaseDatabaseModel implements UserFactoryAwareInterface
3435
{
36+
use UserFactoryAwareTrait;
37+
3538
/**
3639
* Remove the user data.
3740
*
@@ -84,7 +87,7 @@ public function removeDataForRequest($id = null)
8487
->setLimit(1)
8588
)->loadResult();
8689

87-
$user = $userId ? User::getInstance($userId) : null;
90+
$user = $userId ? $this->getUserFactory()->loadUserById($userId) : null;
8891

8992
$canRemove = true;
9093

administrator/components/com_privacy/src/Model/RequestModel.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
use Joomla\CMS\Router\Route;
2222
use Joomla\CMS\Table\Table;
2323
use Joomla\CMS\Uri\Uri;
24-
use Joomla\CMS\User\User;
24+
use Joomla\CMS\User\UserFactoryAwareInterface;
25+
use Joomla\CMS\User\UserFactoryAwareTrait;
2526
use Joomla\CMS\User\UserHelper;
2627
use Joomla\Component\Actionlogs\Administrator\Model\ActionlogModel;
2728
use Joomla\Component\Privacy\Administrator\Table\RequestTable;
@@ -37,8 +38,10 @@
3738
*
3839
* @since 3.9.0
3940
*/
40-
class RequestModel extends AdminModel
41+
class RequestModel extends AdminModel implements UserFactoryAwareInterface
4142
{
43+
use UserFactoryAwareTrait;
44+
4245
/**
4346
* Clean the cache
4447
*
@@ -270,7 +273,7 @@ public function notifyUserAdminCreatedRequest($id)
270273
)->loadResult();
271274

272275
if ($userId) {
273-
$receiver = User::getInstance($userId);
276+
$receiver = $this->getUserFactory()->loadUserById($userId);
274277

275278
/*
276279
* We don't know if the user has admin access, so we will check if they have an admin language in their parameters,

administrator/components/com_users/src/Model/DebuguserModel.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
1616
use Joomla\CMS\MVC\Model\ListModel;
1717
use Joomla\CMS\User\User;
18+
use Joomla\CMS\User\UserFactoryAwareInterface;
19+
use Joomla\CMS\User\UserFactoryAwareTrait;
1820
use Joomla\Component\Users\Administrator\Helper\DebugHelper;
1921
use Joomla\Database\DatabaseQuery;
2022
use Joomla\Database\ParameterType;
@@ -28,8 +30,10 @@
2830
*
2931
* @since 1.6
3032
*/
31-
class DebuguserModel extends ListModel
33+
class DebuguserModel extends ListModel implements UserFactoryAwareInterface
3234
{
35+
use UserFactoryAwareTrait;
36+
3337
/**
3438
* Constructor.
3539
*
@@ -78,7 +82,7 @@ public function getDebugActions()
7882
public function getItems()
7983
{
8084
$userId = $this->getState('user_id');
81-
$user = Factory::getUser($userId);
85+
$user = $this->getUserFactory()->loadUserById($userId);
8286

8387
if (($assets = parent::getItems()) && $userId) {
8488
$actions = $this->getDebugActions();
@@ -179,7 +183,7 @@ public function getUser()
179183
{
180184
$userId = $this->getState('user_id');
181185

182-
return Factory::getUser($userId);
186+
return $this->getUserFactory()->loadUserById($userId);
183187
}
184188

185189
/**

administrator/components/com_users/src/Model/UserModel.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
use Joomla\CMS\MVC\Model\AdminModel;
2121
use Joomla\CMS\Plugin\PluginHelper;
2222
use Joomla\CMS\Table\Table;
23-
use Joomla\CMS\User\User;
23+
use Joomla\CMS\User\UserFactoryAwareInterface;
24+
use Joomla\CMS\User\UserFactoryAwareTrait;
2425
use Joomla\CMS\User\UserHelper;
2526
use Joomla\Database\ParameterType;
2627
use Joomla\Utilities\ArrayHelper;
@@ -34,8 +35,10 @@
3435
*
3536
* @since 1.6
3637
*/
37-
class UserModel extends AdminModel
38+
class UserModel extends AdminModel implements UserFactoryAwareInterface
3839
{
40+
use UserFactoryAwareTrait;
41+
3942
/**
4043
* An item.
4144
*
@@ -222,7 +225,7 @@ protected function preprocessForm(Form $form, $data, $group = 'user')
222225
public function save($data)
223226
{
224227
$pk = (!empty($data['id'])) ? $data['id'] : (int) $this->getState('user.id');
225-
$user = User::getInstance($pk);
228+
$user = $this->getUserFactory()->loadUserById($pk);
226229

227230
$my = $this->getCurrentUser();
228231
$iAmSuperAdmin = $my->authorise('core.admin');
@@ -327,7 +330,7 @@ public function delete(&$pks)
327330

328331
if ($allow) {
329332
// Get users data for the users to delete.
330-
$user_to_delete = Factory::getUser($pk);
333+
$user_to_delete = $this->getUserFactory()->loadUserById($pk);
331334

332335
// Fire the before delete event.
333336
Factory::getApplication()->triggerEvent($this->event_before_delete, [$table->getProperties()]);

api/components/com_contact/src/Controller/ContactController.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
use Joomla\CMS\Router\Exception\RouteNotFoundException;
2424
use Joomla\CMS\String\PunycodeHelper;
2525
use Joomla\CMS\Uri\Uri;
26-
use Joomla\CMS\User\User;
26+
use Joomla\CMS\User\UserFactoryAwareInterface;
27+
use Joomla\CMS\User\UserFactoryAwareTrait;
2728
use Joomla\Component\Fields\Administrator\Helper\FieldsHelper;
2829
use Joomla\Registry\Registry;
2930
use Joomla\String\Inflector;
@@ -39,8 +40,10 @@
3940
*
4041
* @since 4.0.0
4142
*/
42-
class ContactController extends ApiController
43+
class ContactController extends ApiController implements UserFactoryAwareInterface
4344
{
45+
use UserFactoryAwareTrait;
46+
4447
/**
4548
* The content type of the item.
4649
*
@@ -191,7 +194,7 @@ private function _sendEmail($data, $contact, $emailCopyToSender)
191194
$app->getLanguage()->load('com_contact', JPATH_SITE, $app->getLanguage()->getTag(), true);
192195

193196
if ($contact->email_to == '' && $contact->user_id != 0) {
194-
$contact_user = User::getInstance($contact->user_id);
197+
$contact_user = $this->getUserFactory()->loadUserById($contact->user_id);
195198
$contact->email_to = $contact_user->get('email');
196199
}
197200

components/com_contact/src/Controller/ContactController.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
use Joomla\CMS\Router\Route;
2121
use Joomla\CMS\String\PunycodeHelper;
2222
use Joomla\CMS\Uri\Uri;
23-
use Joomla\CMS\User\User;
23+
use Joomla\CMS\User\UserFactoryAwareInterface;
24+
use Joomla\CMS\User\UserFactoryAwareTrait;
2425
use Joomla\CMS\Versioning\VersionableControllerTrait;
2526
use Joomla\Component\Fields\Administrator\Helper\FieldsHelper;
2627
use Joomla\Utilities\ArrayHelper;
@@ -35,8 +36,9 @@
3536
*
3637
* @since 1.5.19
3738
*/
38-
class ContactController extends FormController
39+
class ContactController extends FormController implements UserFactoryAwareInterface
3940
{
41+
use UserFactoryAwareTrait;
4042
use VersionableControllerTrait;
4143

4244
/**
@@ -223,7 +225,7 @@ private function _sendEmail($data, $contact, $emailCopyToSender)
223225
$app = $this->app;
224226

225227
if ($contact->email_to == '' && $contact->user_id != 0) {
226-
$contact_user = User::getInstance($contact->user_id);
228+
$contact_user = $this->getUserFactory()->loadUserById($contact->user_id);
227229
$contact->email_to = $contact_user->get('email');
228230
}
229231

components/com_contact/src/View/Contact/HtmlView.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
use Joomla\CMS\MVC\View\HtmlView as BaseHtmlView;
1919
use Joomla\CMS\Plugin\PluginHelper;
2020
use Joomla\CMS\Router\Route;
21+
use Joomla\CMS\User\UserFactoryAwareInterface;
22+
use Joomla\CMS\User\UserFactoryAwareTrait;
2123
use Joomla\Component\Contact\Site\Helper\RouteHelper;
2224

2325
// phpcs:disable PSR1.Files.SideEffects
@@ -29,8 +31,10 @@
2931
*
3032
* @since 1.5
3133
*/
32-
class HtmlView extends BaseHtmlView
34+
class HtmlView extends BaseHtmlView implements UserFactoryAwareInterface
3335
{
36+
use UserFactoryAwareTrait;
37+
3438
/**
3539
* The item model state
3640
*
@@ -348,7 +352,7 @@ public function display($tpl = null)
348352

349353
$contactUser = null;
350354

351-
if ($item->params->get('show_user_custom_fields') && $item->user_id && $contactUser = Factory::getUser($item->user_id)) {
355+
if ($item->params->get('show_user_custom_fields') && $item->user_id && $contactUser = $this->getUserFactory()->loadUserById($item->user_id)) {
352356
$contactUser->text = '';
353357
$app->triggerEvent('onContentPrepare', ['com_users.user', &$contactUser, &$item->params, 0]);
354358

0 commit comments

Comments
 (0)