From c2a84fbc7fccb8ad0a962e10ca8127179f67a3fc Mon Sep 17 00:00:00 2001 From: brandonkelly Date: Tue, 14 May 2019 05:46:33 -0700 Subject: [PATCH] users/delete-user-photo too Resolves #3932 --- CHANGELOG-v3.md | 2 +- src/controllers/UsersController.php | 44 ++++++++++++++++------------- 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/CHANGELOG-v3.md b/CHANGELOG-v3.md index f698b28d0e2..449bca008ba 100644 --- a/CHANGELOG-v3.md +++ b/CHANGELOG-v3.md @@ -11,7 +11,7 @@ - Element URI formats can now conditionally output an empty string, opting the element out of getting its own system URI. ([#4254](https://github.com/craftcms/cms/issues/4254)) - Table fields now get validation errors if any column handles are entered in the format of “colX”. - Craft no longer clear out users’ verification codes after login. ([#4257](https://github.com/craftcms/cms/issues/4257)) -- The `users/upload-user-photo` action is now available to front-end requests. ([#3932](https://github.com/craftcms/cms/issues/3932)) +- The `users/upload-user-photo` and `users/delete-user-photo` actions are now available to front-end requests. ([#3932](https://github.com/craftcms/cms/issues/3932)) ### Fixed - Fixed a bug where rebuilding the project config could set an incorrect value for the user field layout. diff --git a/src/controllers/UsersController.php b/src/controllers/UsersController.php index a8a481ed8b5..0afd77553d1 100644 --- a/src/controllers/UsersController.php +++ b/src/controllers/UsersController.php @@ -1224,8 +1224,7 @@ public function actionUploadUserPhoto() $this->requireAcceptsJson(); $this->requireLogin(); - $request = Craft::$app->getRequest(); - $userId = $request->getRequiredBodyParam('userId'); + $userId = Craft::$app->getRequest()->getRequiredBodyParam('userId'); if ($userId != Craft::$app->getUser()->getIdentity()->id) { $this->requirePermission('editUsers'); @@ -1248,20 +1247,8 @@ public function actionUploadUserPhoto() move_uploaded_file($file->tempName, $fileLocation); $users->saveUserPhoto($fileLocation, $user, $file->name); - $view = $this->getView(); - $templateMode = $view->getTemplateMode(); - if ($templateMode === View::TEMPLATE_MODE_SITE && !$view->doesTemplateExist('users/_photo')) { - $view->setTemplateMode(View::TEMPLATE_MODE_CP); - } - - $html = $view->renderTemplate('users/_photo', [ - 'user' => $user - ]); - - $view->setTemplateMode($templateMode); - return $this->asJson([ - 'html' => $html, + 'html' => $this->_renderPhotoTemplate($user), ]); } catch (\Throwable $exception) { /** @noinspection UnSafeIsSetOverArrayInspection - FP */ @@ -1289,7 +1276,6 @@ public function actionUploadUserPhoto() */ public function actionDeleteUserPhoto(): Response { - $this->requireCpRequest(); $this->requireAcceptsJson(); $this->requireLogin(); @@ -1308,11 +1294,9 @@ public function actionDeleteUserPhoto(): Response $user->photoId = null; Craft::$app->getElements()->saveElement($user, false); - $html = $this->getView()->renderTemplate('users/_photo', [ - 'user' => $user + return $this->asJson([ + 'html' => $this->_renderPhotoTemplate($user), ]); - - return $this->asJson(['html' => $html]); } /** @@ -2074,4 +2058,24 @@ private function _handleSendPasswordResetError(array $errors, string $loginName return null; } + + /** + * Renders the user photo template. + * + * @param User $user + * @return string The rendered HTML + */ + private function _renderPhotoTemplate(User $user): string + { + $view = $this->getView(); + $templateMode = $view->getTemplateMode(); + if ($templateMode === View::TEMPLATE_MODE_SITE && !$view->doesTemplateExist('users/_photo')) { + $view->setTemplateMode(View::TEMPLATE_MODE_CP); + } + $html = $view->renderTemplate('users/_photo', [ + 'user' => $user + ]); + $view->setTemplateMode($templateMode); + return $html; + } }