Skip to content

Commit

Permalink
users/delete-user-photo too
Browse files Browse the repository at this point in the history
Resolves #3932
  • Loading branch information
brandonkelly committed May 14, 2019
1 parent 1bb9357 commit c2a84fb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG-v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
44 changes: 24 additions & 20 deletions src/controllers/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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 */
Expand Down Expand Up @@ -1289,7 +1276,6 @@ public function actionUploadUserPhoto()
*/
public function actionDeleteUserPhoto(): Response
{
$this->requireCpRequest();
$this->requireAcceptsJson();
$this->requireLogin();

Expand All @@ -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]);
}

/**
Expand Down Expand Up @@ -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;
}
}

0 comments on commit c2a84fb

Please sign in to comment.