Skip to content

Commit

Permalink
Show result JSON only on failure, otherwise redirect to route 'zfcuser'.
Browse files Browse the repository at this point in the history
  • Loading branch information
dillchuk committed Sep 9, 2017
1 parent 77a6a13 commit d375707
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
],
"require": {
"php": "^5.5|^7.0",
"zf-commons/zfc-user": "^3.0"
"zf-commons/zfc-user": "^3.0",
"phpoption/phpoption": "^1.0"
},
"autoload": {
"psr-4": {
Expand Down
14 changes: 10 additions & 4 deletions src/Controller/Substitute.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,19 @@ public function __construct(SubstituteService $substituteService) {
}

public function substituteAction() {
$result = $this->substituteService->substitute($this->params('user'));
return new JsonModel($result);
$errorOption = $this->substituteService->substitute($this->params('user'));
foreach ($errorOption as $error) {
return new JsonModel(['error' => $error]);
}
return $this->redirect()->toRoute('zfcuser');
}

public function unsubstituteAction() {
$result = $this->substituteService->unsubstitute();
return new JsonModel($result);
$errorOption = $this->substituteService->unsubstitute();
foreach ($errorOption as $error) {
return new JsonModel(['error' => $error]);
}
return $this->redirect()->toRoute('zfcuser');
}

}
26 changes: 14 additions & 12 deletions src/Service/Substitute.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Zend\Authentication\Storage\StorageInterface;
use Zend\Authentication\AuthenticationService;
use ZfcUser\Mapper\User as UserMapper;
use PhpOption\Option as PhpOption;

class Substitute {

Expand Down Expand Up @@ -32,38 +33,39 @@ public function __construct(
$this->setStorage($storage);
}

/**
* @param string $userId
* @return PhpOption error reason
*/
public function substitute($userId) {
$error['result'] = 'error';
$success['result'] = 'success';

$identity = $this->getAuthService()->getIdentity();
if (!$identity) {
return $error + ['message' => 'Not logged in'];
return PhpOption::ensure('Not logged in');
}
if (!$this->getStorage()->isEmpty()) {
return $error + ['message' => 'Already substituted'];
return PhpOption::ensure('Already substituted');
}
if (!$this->getUserMapper()->findById($userId)) {
return $error + ['message' => 'Substitution user does not exist'];
return PhpOption::ensure('Substitution user does not exist');
}

$this->getStorage()->write($identity);
$this->getAuthService()->getStorage()->write($userId);
return $success;
return PhpOption::ensure(null);
}

/**
* @return PhpOption error reason
*/
public function unsubstitute() {
$error['result'] = 'error';
$success['result'] = 'success';

if ($this->getStorage()->isEmpty()) {
return $error + ['message' => 'Not substituted'];
return PhpOption::ensure('Not substituted in the first place');
}
$originalUser = $this->getStorage()->read();
$this->getStorage()->clear();

$this->getAuthService()->getStorage()->write($originalUser->getId());
return $success;
return PhpOption::ensure(null);
}

public function isSubstituted() {
Expand Down

0 comments on commit d375707

Please sign in to comment.