Add API action to create account

This commit is contained in:
Thibaud Fabre
2015-06-25 17:18:21 +02:00
committed by Aztech
parent 2ebc4e1558
commit 979770953e
5 changed files with 49 additions and 10 deletions

View File

@@ -87,7 +87,7 @@ class RegistrationService
return null;
}
public function registerUser(array $data, array $selectedCollections, $providerId = null)
public function registerUser(array $data, array $selectedCollections = null, $providerId = null)
{
require_once $this->app['root.path'] . '/lib/classes/deprecated/inscript.api.php';
@@ -117,7 +117,6 @@ class RegistrationService
$this->app['EM']->flush();
}
if ($this->app['phraseanet.registry']->get('GV_autoregister')) {
$this->applyAclsToUser($authorizedCollections, $user);
}
@@ -128,6 +127,19 @@ class RegistrationService
return $user;
}
public function getAccountUnlockToken(\User_Adapter $user)
{
$expire = new \DateTime('+3 days');
$token = $this->app['tokens']->getUrlToken(
\random::TYPE_PASSWORD,
$user->get_id(),
$expire,
$user->get_email()
);
return $token;
}
private function attachProviderToUser(EntityManager $em, ProviderInterface $provider, \User_Adapter $user)
{
$usrAuthProvider = new UsrAuthProvider();
@@ -192,7 +204,7 @@ class RegistrationService
* @return mixed
* @throws \Exception
*/
private function applyAclsToUser($authorizedCollections, $user)
private function applyAclsToUser(array $authorizedCollections, \User_Adapter $user)
{
$template_user_id = \User_Adapter::get_usr_id_from_login($this->app, 'autoregister');
$template_user = \User_Adapter::getInstance($template_user_id, $this->app);

View File

@@ -978,8 +978,11 @@ class V1 implements ControllerProviderInterface
return $result->get_response();
})->before($requirePasswordGrant);
$controllers->post('accounts/access-demand/', function (Request $request) use ($app) {
$controllers->post('/accounts/access-demand/', function (Request $request) use ($app) {
$data = json_decode($request->getContent(false), true);
$result = $app['api']->create_account($data);
return $result->get_response();
})->before($requirePasswordGrant);
return $controllers;

View File

@@ -277,12 +277,7 @@ class Login implements ControllerProviderInterface
$form->bind($requestData);
$data = $form->getData();
$registrationService = new RegistrationService(
$app,
$app['phraseanet.appbox'],
$app['authentication.providers'],
$app['EM']->getRepository('Entities\UsrAuthProvider')
);
$registrationService = $app['authentication.registration_service'];
if ($data['provider-id']) {
try {
@@ -325,6 +320,7 @@ class Login implements ControllerProviderInterface
}
$user = $registrationService->registerUser(
$data,
$selected,
isset($data['provider-id']) ? $data['provider-id'] : null
);

View File

@@ -23,6 +23,7 @@ use Alchemy\Phrasea\Authentication\Phrasea\NativeAuthentication;
use Alchemy\Phrasea\Authentication\Phrasea\OldPasswordEncoder;
use Alchemy\Phrasea\Authentication\Phrasea\PasswordEncoder;
use Alchemy\Phrasea\Authentication\RecoveryService;
use Alchemy\Phrasea\Authentication\RegistrationService;
use Alchemy\Phrasea\Authentication\SuggestionFinder;
use Alchemy\Phrasea\Authentication\Token\TokenValidator;
use Silex\Application;
@@ -103,6 +104,15 @@ class AuthenticationManagerServiceProvider implements ServiceProviderInterface
);
});
$app['authentication.registration_service'] = $app->share(function (Application $app) {
return new RegistrationService(
$app,
$app['phraseanet.appbox'],
$app['authentication.providers'],
$app['EM']->getRepository('Entities\UsrAuthProvider')
);
});
$app['auth.password-encoder'] = $app->share(function (Application $app) {
return new PasswordEncoder($app['phraseanet.configuration']['main']['key']);
});

View File

@@ -1911,6 +1911,24 @@ class API_V1_adapter extends API_V1_Abstract
return $grants;
}
public function create_account(array $data)
{
/** @var \Alchemy\Phrasea\Authentication\RegistrationService $service */
$service = $this->app['authentication.registration_service'];
$user = $service->registerUser($data);
$token = $service->getAccountUnlockToken($user);
$result = new API_V1_result($this->app, $this->app['request'], $this);
$result->set_datas(array(
'user' => $this->list_user($user),
'token' => $token
));
return $result;
}
/**
* Resets the password for a given email address
*