Skip to content

Commit

Permalink
update controller
Browse files Browse the repository at this point in the history
  • Loading branch information
ojhaujjwal committed Oct 26, 2014
1 parent e09d896 commit 1197be0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
29 changes: 21 additions & 8 deletions src/Controller/ProfileImageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace HtProfileImage\Controller;

use HtProfileImage\Form\ProfileImageForm;
use Zend\View\Model;
use Zend\Mvc\Controller\AbstractActionController;
use HtProfileImage\Service\ProfileImageServiceInterface;
Expand Down Expand Up @@ -41,12 +40,18 @@ public function __construct(ProfileImageServiceInterface $profileImageService)
*/
public function uploadAction()
{
$authenticationService = $this->getServiceLocator()->get('zfcuser_auth_service');
if (!$authenticationService->hasIdentity()) {
return $this->redirect()->toRoute('zfcuser');
}

$user = $this->getUser();
if (!$user) {
return $this->notFoundAction();
}
$options = $this->getOptions();
$form = $this->getServiceLocator()->get('HtProfileImage\ProfileImageForm');
/** @var \Zend\Http\Request $request */
$request = $this->getRequest();
$imageUploaded = false;
if ($request->isPost()) {
Expand All @@ -66,7 +71,8 @@ public function uploadAction()
$imageUploaded = true;
} else {
$response = $this->getResponse();
$response->setStatus(400);
/** @var \Zend\Http\Response $response */
$response->setStatusCode(400);
if ($format === 'application/json') {
return new Model\JsonModel([
'error' => true,
Expand Down Expand Up @@ -108,6 +114,12 @@ public function deleteAction()
if (!$this->getOptions()->getEnableImageDelete()) {
return $this->notFoundAction();
}

$authenticationService = $this->getServiceLocator()->get('zfcuser_auth_service');
if (!$authenticationService->hasIdentity()) {
return $this->redirect()->toRoute('zfcuser');
}

$user = $this->getUser();
if (!$user) {
return $this->notFoundAction();
Expand All @@ -117,26 +129,27 @@ public function deleteAction()
return call_user_func_array([$this->redirect(), 'toRoute'], (array) $this->getOptions()->getPostImageDeleteRoute());
}

/**
* @return \ZfcUser\Entity\UserInterface|null
*/
protected function getUser()
{
$authenticationService = $this->getServiceLocator()->get('zfcuser_auth_service');
if (!$authenticationService->hasIdentity()) {
return $this->redirect()->toRoute('zfcuser');
}
/** @var \ZfcUser\Entity\UserInterface $user */
$user = $authenticationService->getIdentity();

$userId = $this->params()->fromRoute('userId', null);
if ($userId !== null) {
$currentUser = $user;
$user = $this->getUserMapper()->findById($userId);
if (!$user) {
return $this->notFoundAction();
return null;
}
if (!$this->getOptions()->getEnableInterUserImageUpload() && ($user->getId() !== $currentUser->getId())) {
return $this->notFoundAction();
return null;
}
}

return $user;
}

Expand Down
8 changes: 8 additions & 0 deletions src/Options/ModuleOptionsInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,12 @@ public function getMaxImageFileSize();
public function setEnableInterUserImageUpload($enableInterUserImageUpload);

public function getEnableInterUserImageUpload();

public function setEnableImageDelete($enableImageDelete);

public function getEnableImageDelete();

public function setPostImageDeleteRoute($postImageDeleteRoute);

public function getPostImageDeleteRoute();
}

0 comments on commit 1197be0

Please sign in to comment.