Split Developers into provider/controller

This commit is contained in:
Benoît Burnichon
2015-06-16 14:44:00 +02:00
parent dd0b661dac
commit c1e412a0ec
3 changed files with 249 additions and 220 deletions

View File

@@ -12,7 +12,6 @@
namespace Alchemy\Phrasea; namespace Alchemy\Phrasea;
use Alchemy\Geonames\GeonamesServiceProvider; use Alchemy\Geonames\GeonamesServiceProvider;
use Alchemy\Phrasea\ControllerProvider\Root\Developers;
use Alchemy\Phrasea\ControllerProvider\Root\Login; use Alchemy\Phrasea\ControllerProvider\Root\Login;
use Alchemy\Phrasea\ControllerProvider\Root\Root; use Alchemy\Phrasea\ControllerProvider\Root\Root;
use Alchemy\Phrasea\ControllerProvider\Root\RSSFeeds; use Alchemy\Phrasea\ControllerProvider\Root\RSSFeeds;
@@ -317,6 +316,7 @@ class Application extends SilexApplication
'Alchemy\Phrasea\ControllerProvider\Report\Information' => [], 'Alchemy\Phrasea\ControllerProvider\Report\Information' => [],
'Alchemy\Phrasea\ControllerProvider\Report\Root' => [], 'Alchemy\Phrasea\ControllerProvider\Report\Root' => [],
'Alchemy\Phrasea\ControllerProvider\Root\Account' => [], 'Alchemy\Phrasea\ControllerProvider\Root\Account' => [],
'Alchemy\Phrasea\ControllerProvider\Root\Developers' => [],
'Alchemy\Phrasea\ControllerProvider\Datafiles' => [], 'Alchemy\Phrasea\ControllerProvider\Datafiles' => [],
'Alchemy\Phrasea\ControllerProvider\Lightbox' => [], 'Alchemy\Phrasea\ControllerProvider\Lightbox' => [],
'Alchemy\Phrasea\ControllerProvider\MediaAccessor' => [], 'Alchemy\Phrasea\ControllerProvider\MediaAccessor' => [],
@@ -624,7 +624,6 @@ class Application extends SilexApplication
$this->mount('/', new Root()); $this->mount('/', new Root());
$this->mount('/feeds/', new RSSFeeds()); $this->mount('/feeds/', new RSSFeeds());
$this->mount('/login/', new Login()); $this->mount('/login/', new Login());
$this->mount('/developers/', new Developers());
$this->mount('/user/preferences/', new Preferences()); $this->mount('/user/preferences/', new Preferences());
$this->mount('/user/notifications/', new Notifications()); $this->mount('/user/notifications/', new Notifications());
@@ -651,6 +650,7 @@ class Application extends SilexApplication
'/admin/users' => 'Alchemy\Phrasea\ControllerProvider\Admin\Users', '/admin/users' => 'Alchemy\Phrasea\ControllerProvider\Admin\Users',
'/client/' => 'Alchemy\Phrasea\ControllerProvider\Client\Root', '/client/' => 'Alchemy\Phrasea\ControllerProvider\Client\Root',
'/datafiles' => 'Alchemy\Phrasea\ControllerProvider\Datafiles', '/datafiles' => 'Alchemy\Phrasea\ControllerProvider\Datafiles',
'/developers/' => 'Alchemy\Phrasea\ControllerProvider\Root\Developers',
'/download/' => 'Alchemy\Phrasea\ControllerProvider\Prod\DoDownload', '/download/' => 'Alchemy\Phrasea\ControllerProvider\Prod\DoDownload',
'/include/minify' => 'Alchemy\Phrasea\ControllerProvider\Minifier', '/include/minify' => 'Alchemy\Phrasea\ControllerProvider\Minifier',
'/lightbox' => 'Alchemy\Phrasea\ControllerProvider\Lightbox', '/lightbox' => 'Alchemy\Phrasea\ControllerProvider\Lightbox',

View File

@@ -0,0 +1,231 @@
<?php
/*
* This file is part of Phraseanet
*
* (c) 2005-2015 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Alchemy\Phrasea\Controller\Root;
use Alchemy\Phrasea\Application;
use Alchemy\Phrasea\Controller\Controller;
use Alchemy\Phrasea\Exception\InvalidArgumentException;
use Alchemy\Phrasea\Model\Entities\ApiApplication;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class DeveloperController extends Controller
{
/**
* Delete application.
*
* @param Application $app
* @param Request $request
* @param ApiApplication $application
*
* @return JsonResponse
*/
public function deleteApp(Application $app, Request $request, ApiApplication $application)
{
if (!$request->isXmlHttpRequest() || !array_key_exists($request->getMimeType('json'), array_flip($request->getAcceptableContentTypes()))) {
$app->abort(400, 'Bad request format, only JSON is allowed');
}
$app['manipulator.api-application']->delete($application);
return $app->json(['success' => true]);
}
/**
* Change application callback.
*
* @param Application $app
* @param Request $request
* @param ApiApplication $application
*
* @return JsonResponse
*/
public function renewAppCallback(Application $app, Request $request, ApiApplication $application)
{
if (!$request->isXmlHttpRequest() || !array_key_exists($request->getMimeType('json'), array_flip($request->getAcceptableContentTypes()))) {
$app->abort(400, 'Bad request format, only JSON is allowed');
}
try {
$app['manipulator.api-application']->setRedirectUri($application, $request->request->get("callback"));
} catch (InvalidArgumentException $e) {
return $app->json(['success' => false]);
}
return $app->json(['success' => true]);
}
/**
* Change application webhook
*
* @param Application $app A Silex application where the controller is mounted on
* @param Request $request The current request
* @param integer $id The application id
* @return JsonResponse
*/
public function renewAppWebhook(Application $app, Request $request, ApiApplication $application)
{
if (!$request->isXmlHttpRequest() || !array_key_exists($request->getMimeType('json'), array_flip($request->getAcceptableContentTypes()))) {
$app->abort(400, _('Bad request format, only JSON is allowed'));
}
if (null !== $request->request->get("webhook")) {
$app['manipulator.api-application']->setWebhookUrl($application, $request->request->get("webhook"));
} else {
return $app->json(['success' => false]);
}
return $app->json(['success' => true]);
}
/**
* Authorize application to use a grant password type.
*
* @param Application $app
* @param Request $request
* @param ApiApplication $application
*
* @return JsonResponse
*/
public function renewAccessToken(Application $app, Request $request, ApiApplication $application)
{
if (!$request->isXmlHttpRequest() || !array_key_exists($request->getMimeType('json'), array_flip($request->getAcceptableContentTypes()))) {
$app->abort(400, 'Bad request format, only JSON is allowed');
}
if (null === $account = $app['repo.api-accounts']->findByUserAndApplication($app['authentication']->getUser(), $application)) {
$app->abort(404, sprintf('Account not found for application %s', $application->getName()));
}
if (null !== $devToken = $app['repo.api-oauth-tokens']->findDeveloperToken($account)) {
$app['manipulator.api-oauth-token']->renew($devToken);
} else {
// dev tokens do not expires
$devToken = $app['manipulator.api-oauth-token']->create($account);
}
return $app->json(['success' => true, 'token' => $devToken->getOauthToken()]);
}
/**
* Authorize application to use a grant password type.
*
* @param Application $app
* @param Request $request
* @param ApiApplication $application
*
* @return JsonResponse
*/
public function authorizeGrantPassword(Application $app, Request $request, ApiApplication $application)
{
if (!$request->isXmlHttpRequest() || !array_key_exists($request->getMimeType('json'), array_flip($request->getAcceptableContentTypes()))) {
$app->abort(400, 'Bad request format, only JSON is allowed');
}
$application->setGrantPassword((Boolean) $request->request->get('grant'));
$app['manipulator.api-application']->update($application);
return $app->json(['success' => true]);
}
/**
* Create a new developer applications
*
* @param Application $app A Silex application where the controller is mounted on
* @param Request $request The current request
* @return Response
*/
public function newApp(Application $app, Request $request)
{
if ($request->request->get('type') === ApiApplication::DESKTOP_TYPE) {
$form = new \API_OAuth2_Form_DevAppDesktop($app['request']);
} else {
$form = new \API_OAuth2_Form_DevAppInternet($app['request']);
}
$violations = $app['validator']->validate($form);
if ($violations->count() === 0) {
$application = $app['manipulator.api-application']->create(
$form->getName(),
$form->getType(),
$form->getDescription(),
sprintf('%s%s', $form->getSchemeWebsite(), $form->getWebsite()),
$app['authentication']->getUser(),
sprintf('%s%s', $form->getSchemeCallback(), $form->getCallback())
);
// create an account as well
$app['manipulator.api-account']->create($application, $app['authentication']->getUser());
return $app->redirectPath('developers_application', ['application' => $application->getId()]);
}
return $app['twig']->render('/developers/application_form.html.twig', [
"violations" => $violations,
"form" => $form
]);
}
/**
* List of apps created by the user
*
* @param Application $app A Silex application where the controller is mounted on
* @param Request $request The current request
* @return Response
*/
public function listApps(Application $app, Request $request)
{
return $app['twig']->render('developers/applications.html.twig', [
"applications" => $app['repo.api-applications']->findByCreator($app['authentication']->getUser())
]);
}
/**
* Display form application
*
* @param Application $app A Silex application where the controller is mounted on
* @param Request $request The current request
* @return Response
*/
public function displayFormApp(Application $app, Request $request)
{
return $app['twig']->render('developers/application_form.html.twig', [
"violations" => null,
'form' => null,
'request' => $request
]);
}
/**
* Gets application information.
*
* @param Application $app
* @param Request $request
* @param ApiApplication $application
*
* @return mixed
*/
public function getApp(Application $app, Request $request, ApiApplication $application)
{
$token = null;
if (null !== $account = $app['repo.api-accounts']->findByUserAndApplication($app['authentication']->getUser(), $application)) {
$token = $app['repo.api-oauth-tokens']->findDeveloperToken($account);
}
return $app['twig']->render('developers/application.html.twig', [
"application" => $application,
"user" => $app['authentication']->getUser(),
"token" => $token
]);
}
}

View File

@@ -11,23 +11,31 @@
namespace Alchemy\Phrasea\ControllerProvider\Root; namespace Alchemy\Phrasea\ControllerProvider\Root;
use Alchemy\Phrasea\Application as PhraseaApplication;
use Alchemy\Phrasea\Controller\Root\DeveloperController;
use Alchemy\Phrasea\ControllerProvider\ControllerProviderTrait; use Alchemy\Phrasea\ControllerProvider\ControllerProviderTrait;
use Alchemy\Phrasea\Exception\InvalidArgumentException;
use Alchemy\Phrasea\Model\Entities\ApiApplication;
use Silex\Application; use Silex\Application;
use Silex\ControllerProviderInterface; use Silex\ControllerProviderInterface;
use Symfony\Component\HttpFoundation\JsonResponse; use Silex\ServiceProviderInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class Developers implements ControllerProviderInterface class Developers implements ControllerProviderInterface, ServiceProviderInterface
{ {
use ControllerProviderTrait; use ControllerProviderTrait;
public function register(Application $app)
{
$app['controller.account.developers'] = $app->share(function (PhraseaApplication $app) {
return (new DeveloperController($app));
});
}
public function boot(Application $app)
{
// no-op
}
public function connect(Application $app) public function connect(Application $app)
{ {
$app['controller.account.developers'] = $this;
$controllers = $this->createAuthenticatedCollection($app); $controllers = $this->createAuthenticatedCollection($app);
$controllers->get('/applications/', 'controller.account.developers:listApps') $controllers->get('/applications/', 'controller.account.developers:listApps')
@@ -71,214 +79,4 @@ class Developers implements ControllerProviderInterface
return $controllers; return $controllers;
} }
/**
* Delete application.
*
* @param Application $app
* @param Request $request
* @param ApiApplication $application
*
* @return JsonResponse
*/
public function deleteApp(Application $app, Request $request, ApiApplication $application)
{
if (!$request->isXmlHttpRequest() || !array_key_exists($request->getMimeType('json'), array_flip($request->getAcceptableContentTypes()))) {
$app->abort(400, 'Bad request format, only JSON is allowed');
}
$app['manipulator.api-application']->delete($application);
return $app->json(['success' => true]);
}
/**
* Change application callback.
*
* @param Application $app
* @param Request $request
* @param ApiApplication $application
*
* @return JsonResponse
*/
public function renewAppCallback(Application $app, Request $request, ApiApplication $application)
{
if (!$request->isXmlHttpRequest() || !array_key_exists($request->getMimeType('json'), array_flip($request->getAcceptableContentTypes()))) {
$app->abort(400, 'Bad request format, only JSON is allowed');
}
try {
$app['manipulator.api-application']->setRedirectUri($application, $request->request->get("callback"));
} catch (InvalidArgumentException $e) {
return $app->json(['success' => false]);
}
return $app->json(['success' => true]);
}
/**
* Change application webhook
*
* @param Application $app A Silex application where the controller is mounted on
* @param Request $request The current request
* @param integer $id The application id
* @return JsonResponse
*/
public function renewAppWebhook(Application $app, Request $request, ApiApplication $application)
{
if (!$request->isXmlHttpRequest() || !array_key_exists($request->getMimeType('json'), array_flip($request->getAcceptableContentTypes()))) {
$app->abort(400, _('Bad request format, only JSON is allowed'));
}
if (null !== $request->request->get("webhook")) {
$app['manipulator.api-application']->setWebhookUrl($application, $request->request->get("webhook"));
} else {
return $app->json(['success' => false]);
}
return $app->json(['success' => true]);
}
/**
* Authorize application to use a grant password type.
*
* @param Application $app
* @param Request $request
* @param ApiApplication $application
*
* @return JsonResponse
*/
public function renewAccessToken(Application $app, Request $request, ApiApplication $application)
{
if (!$request->isXmlHttpRequest() || !array_key_exists($request->getMimeType('json'), array_flip($request->getAcceptableContentTypes()))) {
$app->abort(400, 'Bad request format, only JSON is allowed');
}
if (null === $account = $app['repo.api-accounts']->findByUserAndApplication($app['authentication']->getUser(), $application)) {
$app->abort(404, sprintf('Account not found for application %s', $application->getName()));
}
if (null !== $devToken = $app['repo.api-oauth-tokens']->findDeveloperToken($account)) {
$app['manipulator.api-oauth-token']->renew($devToken);
} else {
// dev tokens do not expires
$devToken = $app['manipulator.api-oauth-token']->create($account);
}
return $app->json(['success' => true, 'token' => $devToken->getOauthToken()]);
}
/**
* Authorize application to use a grant password type.
*
* @param Application $app
* @param Request $request
* @param ApiApplication $application
*
* @return JsonResponse
*/
public function authorizeGrantPassword(Application $app, Request $request, ApiApplication $application)
{
if (!$request->isXmlHttpRequest() || !array_key_exists($request->getMimeType('json'), array_flip($request->getAcceptableContentTypes()))) {
$app->abort(400, 'Bad request format, only JSON is allowed');
}
$application->setGrantPassword((Boolean) $request->request->get('grant'));
$app['manipulator.api-application']->update($application);
return $app->json(['success' => true]);
}
/**
* Create a new developer applications
*
* @param Application $app A Silex application where the controller is mounted on
* @param Request $request The current request
* @return Response
*/
public function newApp(Application $app, Request $request)
{
if ($request->request->get('type') === ApiApplication::DESKTOP_TYPE) {
$form = new \API_OAuth2_Form_DevAppDesktop($app['request']);
} else {
$form = new \API_OAuth2_Form_DevAppInternet($app['request']);
}
$violations = $app['validator']->validate($form);
if ($violations->count() === 0) {
$application = $app['manipulator.api-application']->create(
$form->getName(),
$form->getType(),
$form->getDescription(),
sprintf('%s%s', $form->getSchemeWebsite(), $form->getWebsite()),
$app['authentication']->getUser(),
sprintf('%s%s', $form->getSchemeCallback(), $form->getCallback())
);
// create an account as well
$app['manipulator.api-account']->create($application, $app['authentication']->getUser());
return $app->redirectPath('developers_application', ['application' => $application->getId()]);
}
return $app['twig']->render('/developers/application_form.html.twig', [
"violations" => $violations,
"form" => $form
]);
}
/**
* List of apps created by the user
*
* @param Application $app A Silex application where the controller is mounted on
* @param Request $request The current request
* @return Response
*/
public function listApps(Application $app, Request $request)
{
return $app['twig']->render('developers/applications.html.twig', [
"applications" => $app['repo.api-applications']->findByCreator($app['authentication']->getUser())
]);
}
/**
* Display form application
*
* @param Application $app A Silex application where the controller is mounted on
* @param Request $request The current request
* @return Response
*/
public function displayFormApp(Application $app, Request $request)
{
return $app['twig']->render('developers/application_form.html.twig', [
"violations" => null,
'form' => null,
'request' => $request
]);
}
/**
* Gets application information.
*
* @param Application $app
* @param Request $request
* @param ApiApplication $application
*
* @return mixed
*/
public function getApp(Application $app, Request $request, ApiApplication $application)
{
$token = null;
if (null !== $account = $app['repo.api-accounts']->findByUserAndApplication($app['authentication']->getUser(), $application)) {
$token = $app['repo.api-oauth-tokens']->findDeveloperToken($account);
}
return $app['twig']->render('developers/application.html.twig', [
"application" => $application,
"user" => $app['authentication']->getUser(),
"token" => $token
]);
}
} }