Skip to content

Commit

Permalink
Merge pull request #11916 from craftcms/make-current-user-static
Browse files Browse the repository at this point in the history
Make getCurrentUser static
  • Loading branch information
brandonkelly authored Sep 13, 2022
2 parents 7c232ea + f11df40 commit a56ed26
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 35 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG-WIP.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
- Added `craft\services\Elements::startCollectingCacheInfo()`. ([#11901](https://github.com/craftcms/cms/pull/11901))
- Added `craft\services\Elements::stopCollectingCacheInfo()`. ([#11901](https://github.com/craftcms/cms/pull/11901))
- Added `craft\services\Search::EVENT_BEFORE_SCORE_RESULTS`. ([#11882](https://github.com/craftcms/cms/discussions/11882))
- Added `craft\web\Controller::getCurrentUser()`. ([#11754](https://github.com/craftcms/cms/pull/11754))
- Added `craft\web\Controller::currentUser()`. ([#11754](https://github.com/craftcms/cms/pull/11754), [#11916](https://github.com/craftcms/cms/pull/11916))
- Added `craft\web\View::EVENT_AFTER_CREATE_TWIG`. ([#11774](https://github.com/craftcms/cms/pull/11774))
- Added the `Craft.useMobileStyles()` JavaScript method. ([#11636](https://github.com/craftcms/cms/pull/11636))
- Added `Craft.BaseElementIndex::getParentSource()`.
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/AppController.php
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ public function actionShunCpAlert(): Response
$this->requirePermission('accessCp');

$message = $this->request->getRequiredBodyParam('message');
$user = $this->getCurrentUser();
$user = static::currentUser();

$currentTime = DateTimeHelper::currentUTCDateTime();
$tomorrow = $currentTime->add(new DateInterval('P1D'));
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/AssetsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,7 @@ public function actionSaveImage(): Response
$folder = $asset->getFolder();

// Do what you want with your own photo.
if ($asset->id != $this->getCurrentUser()->photoId) {
if ($asset->id != static::currentUser()->photoId) {
$this->requireVolumePermissionByAsset('editImages', $asset);
$this->requirePeerVolumePermissionByAsset('editPeerImages', $asset);
}
Expand Down Expand Up @@ -1095,7 +1095,7 @@ public function actionPreviewFile(): Response
$variables = [];

if ($previewHandler instanceof ImagePreview) {
if ($asset->id != $this->getCurrentUser()->photoId) {
if ($asset->id != static::currentUser()->photoId) {
$variables['editFocal'] = true;

try {
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ public function actionSendSupportRequest(): Response
],
[
'name' => 'name',
'contents' => $this->getCurrentUser()->getName(),
'contents' => static::currentUser()->getName(),
],
[
'name' => 'message',
Expand Down
22 changes: 11 additions & 11 deletions src/controllers/ElementsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public function actionCreate(): Response
}
$element->setAttributes($this->_attributes);

$user = $this->getCurrentUser();
$user = static::currentUser();

if (!Craft::$app->getElements()->canSave($element, $user)) {
throw new ForbiddenHttpException('User not authorized to create this element.');
Expand Down Expand Up @@ -267,7 +267,7 @@ public function actionEdit(?ElementInterface $element, ?int $elementId = null):
}

$elementsService = Craft::$app->getElements();
$user = $this->getCurrentUser();
$user = static::currentUser();

// Figure out what we're dealing with here
$isCanonical = $element->getIsCanonical();
Expand Down Expand Up @@ -863,7 +863,7 @@ public function actionSave(): ?Response

$this->_applyParamsToElement($element);
$elementsService = Craft::$app->getElements();
$user = $this->getCurrentUser();
$user = static::currentUser();

if (!$elementsService->canSave($element, $user)) {
throw new ForbiddenHttpException('User not authorized to save this element.');
Expand Down Expand Up @@ -998,7 +998,7 @@ public function actionDelete(): ?Response
}

$elementsService = Craft::$app->getElements();
$user = $this->getCurrentUser();
$user = static::currentUser();

if (!$elementsService->canDelete($element, $user)) {
throw new ForbiddenHttpException('User not authorized to delete this element.');
Expand Down Expand Up @@ -1095,7 +1095,7 @@ public function actionSaveDraft(): ?Response
}

$elementsService = Craft::$app->getElements();
$user = $this->getCurrentUser();
$user = static::currentUser();

if (!$element->getIsDraft() && !$this->_provisional) {
if (!$elementsService->canCreateDrafts($element, $user)) {
Expand Down Expand Up @@ -1246,7 +1246,7 @@ public function actionApplyDraft(): ?Response
}

$this->_applyParamsToElement($element);
$user = $this->getCurrentUser();
$user = static::currentUser();

if (!$elementsService->canSave($element, $user)) {
throw new ForbiddenHttpException('User not authorized to save this draft.');
Expand Down Expand Up @@ -1357,7 +1357,7 @@ public function actionDeleteDraft(): ?Response
}

$elementsService = Craft::$app->getElements();
$user = $this->getCurrentUser();
$user = static::currentUser();

if (!$elementsService->canDelete($element, $user)) {
throw new ForbiddenHttpException('User not authorized to delete this draft.');
Expand Down Expand Up @@ -1408,7 +1408,7 @@ public function actionRevert(): Response
throw new BadRequestHttpException('No revision was identified by the request.');
}

$user = $this->getCurrentUser();
$user = static::currentUser();

if (!Craft::$app->getElements()->canSave($element->getCanonical(true), $user)) {
throw new ForbiddenHttpException('User not authorized to save this element.');
Expand Down Expand Up @@ -1469,7 +1469,7 @@ private function _element(?int $elementId = null, ?string $elementUid = null, ?b

$sitesService = Craft::$app->getSites();
$elementsService = Craft::$app->getElements();
$user = $this->getCurrentUser();
$user = static::currentUser();

if ($this->_siteId) {
$site = $sitesService->getSiteById($this->_siteId, true);
Expand Down Expand Up @@ -1564,7 +1564,7 @@ private function _element(?int $elementId = null, ?string $elementUid = null, ?b
return null;
}

if (!$element->canView($this->getCurrentUser())) {
if (!$element->canView(static::currentUser())) {
throw new ForbiddenHttpException('User not authorized to edit this element.');
}

Expand Down Expand Up @@ -1707,7 +1707,7 @@ private function _asSuccess(string $message, ElementInterface $element, array $d
]);

if ($addAnother && $this->_addAnother) {
$user = $this->getCurrentUser();
$user = static::currentUser();
$newElement = $element->createAnother();

if (!$newElement || !Craft::$app->getElements()->canSave($newElement, $user)) {
Expand Down
10 changes: 5 additions & 5 deletions src/controllers/EntriesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function actionCreate(?string $section = null): ?Response
$site = $sitesService->getSiteById($editableSiteIds[0]);
}

$user = $this->getCurrentUser();
$user = static::currentUser();

// Create & populate the draft
$entry = Craft::createObject(Entry::class);
Expand Down Expand Up @@ -231,7 +231,7 @@ public function actionSaveEntry(bool $duplicate = false): ?Response
// Permission enforcement
$this->enforceSitePermission($entry->getSite());
$this->enforceEditEntryPermissions($entry, $duplicate);
$currentUser = $this->getCurrentUser();
$currentUser = static::currentUser();
$section = $entry->getSection();

// Is this another user’s entry (and it’s not a Single)?
Expand Down Expand Up @@ -336,7 +336,7 @@ public function actionSaveEntry(bool $duplicate = false): ?Response
$provisional = Entry::find()
->provisionalDrafts()
->draftOf($entry->id)
->draftCreator($this->getCurrentUser())
->draftCreator(static::currentUser())
->siteId($entry->siteId)
->status(null)
->one();
Expand Down Expand Up @@ -395,7 +395,7 @@ private function _getEntryModel(): Entry
$entry = Entry::find()
->provisionalDrafts()
->draftOf($entryId)
->draftCreator($this->getCurrentUser())
->draftCreator(static::currentUser())
->siteId($siteId)
->status(null)
->one();
Expand Down Expand Up @@ -463,7 +463,7 @@ private function _populateEntryModel(Entry $entry): void
$entry->setFieldValuesFromRequest($fieldsLocation);

// Author
$authorId = $this->request->getBodyParam('author', ($entry->authorId ?: $this->getCurrentUser()->id));
$authorId = $this->request->getBodyParam('author', ($entry->authorId ?: static::currentUser()->id));

if (is_array($authorId)) {
$authorId = $authorId[0] ?? null;
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/PluginStoreController.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ public function actionCraftData(): Response
$data = [];

// Current user
$currentUser = $this->getCurrentUser();
$currentUser = static::currentUser();
$data['currentUser'] = $currentUser->getAttributes(['email']);

// Craft license/edition info
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/SystemSettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ public function actionTestEmailSettings(): void
->composeFromKey('test_email', [
'settings' => MailerHelper::settingsReport($mailer, $adapter),
])
->setTo($this->getCurrentUser());
->setTo(static::currentUser());

if ($message->send()) {
$this->setSuccessFlash(Craft::t('app', 'Email sent successfully! Check your inbox.'));
Expand Down
20 changes: 10 additions & 10 deletions src/controllers/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ public function actionImpersonateWithToken(int $userId, int $prevUserId): ?Respo
*/
private function _enforceImpersonatePermission(User $user): void
{
if (!Craft::$app->getUsers()->canImpersonate($this->getCurrentUser(), $user)) {
if (!Craft::$app->getUsers()->canImpersonate(static::currentUser(), $user)) {
throw new ForbiddenHttpException('You do not have sufficient permissions to impersonate this user');
}
}
Expand Down Expand Up @@ -738,7 +738,7 @@ public function actionEditUser(mixed $userId = null, ?User $user = null, ?array
// ---------------------------------------------------------------------

$edition = Craft::$app->getEdition();
$currentUser = $this->getCurrentUser();
$currentUser = static::currentUser();

if ($user === null) {
// Are we editing a specific user account?
Expand Down Expand Up @@ -1510,7 +1510,7 @@ public function actionUploadUserPhoto(): ?Response

$userId = $this->request->getRequiredBodyParam('userId');

if ($userId != $this->getCurrentUser()->id) {
if ($userId != static::currentUser()->id) {
$this->requirePermission('editUsers');
}

Expand Down Expand Up @@ -1559,7 +1559,7 @@ public function actionDeleteUserPhoto(): Response

$userId = $this->request->getRequiredBodyParam('userId');

if ($userId != $this->getCurrentUser()->id) {
if ($userId != static::currentUser()->id) {
$this->requirePermission('editUsers');
}

Expand Down Expand Up @@ -1638,7 +1638,7 @@ public function actionUnlockUser(): Response

// Even if you have moderateUsers permissions, only and admin should be able to unlock another admin.
if ($user->admin) {
$currentUser = $this->getCurrentUser();
$currentUser = static::currentUser();
if (!$currentUser->admin) {
throw new ForbiddenHttpException('Only admins can unlock other admins.');
}
Expand Down Expand Up @@ -1675,7 +1675,7 @@ public function actionSuspendUser(): ?Response
}

$usersService = Craft::$app->getUsers();
$currentUser = $this->getCurrentUser();
$currentUser = static::currentUser();

if (!$usersService->canSuspend($currentUser, $user) || !$usersService->suspendUser($user)) {
$this->setFailFlash(Craft::t('app', 'Couldn’t suspend user.'));
Expand All @@ -1698,7 +1698,7 @@ public function actionUserContentSummary(): Response

$userIds = $this->request->getRequiredBodyParam('userId');

if ($userIds !== (string)$this->getCurrentUser()->id) {
if ($userIds !== (string)static::currentUser()->id) {
$this->requirePermission('deleteUsers');
}

Expand Down Expand Up @@ -1840,7 +1840,7 @@ public function actionUnsuspendUser(): ?Response

// Even if you have moderateUsers permissions, only and admin should be able to unsuspend another admin.
$usersService = Craft::$app->getUsers();
$currentUser = $this->getCurrentUser();
$currentUser = static::currentUser();

if (!$usersService->canSuspend($currentUser, $user) || !$usersService->unsuspendUser($user)) {
$this->setFailFlash(Craft::t('app', 'Couldn’t unsuspend user.'));
Expand All @@ -1862,7 +1862,7 @@ public function actionUnsuspendUser(): ?Response
public function actionSaveAddress(): ?Response
{
$elementsService = Craft::$app->getElements();
$user = $this->getCurrentUser();
$user = static::currentUser();
$userId = (int)($this->request->getBodyParam('userId') ?? $user->id);
$addressId = $this->request->getBodyParam('addressId');

Expand Down Expand Up @@ -2122,7 +2122,7 @@ private function _verifyElevatedSession(): bool
*/
private function _verifyExistingPassword(): bool
{
$currentUser = $this->getCurrentUser();
$currentUser = static::currentUser();

if (!$currentUser) {
return false;
Expand Down
4 changes: 2 additions & 2 deletions src/web/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,14 +178,14 @@ public function beforeAction($action): bool
}

/**
* Returns the current logged-in user.
* Returns the currently logged-in user.
*
* @param bool $autoRenew
* @return ?User
* @see \yii\web\User::getIdentity()
* @since 4.3.0
*/
public function getCurrentUser(bool $autoRenew = true): ?User
public static function currentUser(bool $autoRenew = true): ?User
{
return Craft::$app->getUser()->getIdentity($autoRenew);
}
Expand Down

0 comments on commit a56ed26

Please sign in to comment.