Add registration routes

This commit is contained in:
Thibaud Fabre
2015-10-20 10:42:02 +02:00
parent 41963d13db
commit ce9e5fbd45
2 changed files with 53 additions and 5 deletions

View File

@@ -17,6 +17,8 @@ use Alchemy\Phrasea\Account\Command\UpdatePasswordCommand;
use Alchemy\Phrasea\Application\Helper\DataboxLoggerAware; use Alchemy\Phrasea\Application\Helper\DataboxLoggerAware;
use Alchemy\Phrasea\Application\Helper\DispatcherAware; use Alchemy\Phrasea\Application\Helper\DispatcherAware;
use Alchemy\Phrasea\Authentication\Context; use Alchemy\Phrasea\Authentication\Context;
use Alchemy\Phrasea\Authentication\Exception\RegistrationException;
use Alchemy\Phrasea\Authentication\RegistrationService;
use Alchemy\Phrasea\Border\Attribute\Status; use Alchemy\Phrasea\Border\Attribute\Status;
use Alchemy\Phrasea\Border\Checker\Response as CheckerResponse; use Alchemy\Phrasea\Border\Checker\Response as CheckerResponse;
use Alchemy\Phrasea\Border\File; use Alchemy\Phrasea\Border\File;
@@ -850,10 +852,7 @@ class V1Controller extends Controller
public function unlockAccount(Request $request, $token) public function unlockAccount(Request $request, $token)
{ {
/** @var \Alchemy\Phrasea\Authentication\RegistrationService $service */ $this->getRegistrationService()->unlockAccount($token);
$service = $this->app['authentication.registration_service'];
$service->unlockAccount($token);
} }
public function addRecordAction(Request $request) public function addRecordAction(Request $request)
@@ -2450,6 +2449,41 @@ class V1Controller extends Controller
return Result::create($request, $ret)->createResponse(); return Result::create($request, $ret)->createResponse();
} }
public function createAccessDemand(Request $request)
{
$service = $this->getRegistrationService();
$data = json_decode($request->getContent(false), true);
$collections = null;
if (isset($data['collections'])) {
$collections = $data['collections'];
}
try {
$user = $service->registerUser($data, $collections);
$token = $service->getAccountUnlockToken($user);
}
catch (RegistrationException $exception) {
return Result::createError($request, 500, $exception->getMessage())->createResponse();
}
return Result::create($request, [
'user' => $user,
'token' => $token
])->createResponse();
}
public function createCollectionRequests(Request $request)
{
$service = $this->getRegistrationService();
$user = $this->getAuthenticatedUser();
$data = json_decode($request->getContent(false), true);
$service->createCollectionRequests($user, $data);
return Result::create($request, $this->listUserDemands($user))->createResponse();
}
public function ensureAdmin(Request $request) public function ensureAdmin(Request $request)
{ {
if (!$user = $this->getApiAuthenticatedUser()->isAdmin()) { if (!$user = $this->getApiAuthenticatedUser()->isAdmin()) {
@@ -2556,6 +2590,14 @@ class V1Controller extends Controller
return $this->app['accounts.service']; return $this->app['accounts.service'];
} }
/**
* @return RegistrationService
*/
public function getRegistrationService()
{
return $this->app['authentication.registration_service'];
}
/** /**
* @return ApiOauthTokenRepository * @return ApiOauthTokenRepository
*/ */

View File

@@ -248,9 +248,9 @@ class V1 implements ControllerProviderInterface, ServiceProviderInterface
->assert('story_id', '\d+'); ->assert('story_id', '\d+');
$controllers->get('/me/', 'controller.api.v1:getCurrentUserAction'); $controllers->get('/me/', 'controller.api.v1:getCurrentUserAction');
$controllers->delete('/me/', 'controller.api.v1:deleteCurrentUserAction'); $controllers->delete('/me/', 'controller.api.v1:deleteCurrentUserAction');
$controllers->post('/me/request-collections/', 'controller.api.v1:createCollectionRequests');
$controllers->post('/me/update-account/', 'controller.api.v1:updateCurrentUserAction'); $controllers->post('/me/update-account/', 'controller.api.v1:updateCurrentUserAction');
$controllers->post('/me/update-password/', 'controller.api.v1:updateCurrentUserPasswordAction'); $controllers->post('/me/update-password/', 'controller.api.v1:updateCurrentUserPasswordAction');
@@ -260,6 +260,12 @@ class V1 implements ControllerProviderInterface, ServiceProviderInterface
$controllers->post('/accounts/update-password/{token}/', 'controller.api.v1:resetPassword') $controllers->post('/accounts/update-password/{token}/', 'controller.api.v1:resetPassword')
->before('controller.api.v1:ensureAdmin'); ->before('controller.api.v1:ensureAdmin');
$controllers->post('/accounts/access-demand/', 'controller.api.v1:createAccessDemand')
->before('controller.api.v1:ensureAdmin');
$controllers->post('/accounts/unlock/{token}/', 'controller.api.v1:unlockAccount')
->before('controller.api.v1:ensureAdmin');
return $controllers; return $controllers;
} }
} }