diff --git a/lib/Alchemy/Phrasea/Application.php b/lib/Alchemy/Phrasea/Application.php index 5a8625886e..5e5b69a45e 100644 --- a/lib/Alchemy/Phrasea/Application.php +++ b/lib/Alchemy/Phrasea/Application.php @@ -12,7 +12,6 @@ namespace Alchemy\Phrasea; use Alchemy\Geonames\GeonamesServiceProvider; -use Alchemy\Phrasea\ControllerProvider\Root\Developers; use Alchemy\Phrasea\ControllerProvider\Root\Login; use Alchemy\Phrasea\ControllerProvider\Root\Root; use Alchemy\Phrasea\ControllerProvider\Root\RSSFeeds; @@ -317,6 +316,7 @@ class Application extends SilexApplication 'Alchemy\Phrasea\ControllerProvider\Report\Information' => [], 'Alchemy\Phrasea\ControllerProvider\Report\Root' => [], 'Alchemy\Phrasea\ControllerProvider\Root\Account' => [], + 'Alchemy\Phrasea\ControllerProvider\Root\Developers' => [], 'Alchemy\Phrasea\ControllerProvider\Datafiles' => [], 'Alchemy\Phrasea\ControllerProvider\Lightbox' => [], 'Alchemy\Phrasea\ControllerProvider\MediaAccessor' => [], @@ -624,7 +624,6 @@ class Application extends SilexApplication $this->mount('/', new Root()); $this->mount('/feeds/', new RSSFeeds()); $this->mount('/login/', new Login()); - $this->mount('/developers/', new Developers()); $this->mount('/user/preferences/', new Preferences()); $this->mount('/user/notifications/', new Notifications()); @@ -651,6 +650,7 @@ class Application extends SilexApplication '/admin/users' => 'Alchemy\Phrasea\ControllerProvider\Admin\Users', '/client/' => 'Alchemy\Phrasea\ControllerProvider\Client\Root', '/datafiles' => 'Alchemy\Phrasea\ControllerProvider\Datafiles', + '/developers/' => 'Alchemy\Phrasea\ControllerProvider\Root\Developers', '/download/' => 'Alchemy\Phrasea\ControllerProvider\Prod\DoDownload', '/include/minify' => 'Alchemy\Phrasea\ControllerProvider\Minifier', '/lightbox' => 'Alchemy\Phrasea\ControllerProvider\Lightbox', diff --git a/lib/Alchemy/Phrasea/Controller/Root/DeveloperController.php b/lib/Alchemy/Phrasea/Controller/Root/DeveloperController.php new file mode 100644 index 0000000000..82d329cdd3 --- /dev/null +++ b/lib/Alchemy/Phrasea/Controller/Root/DeveloperController.php @@ -0,0 +1,231 @@ +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 + ]); + } +} diff --git a/lib/Alchemy/Phrasea/ControllerProvider/Root/Developers.php b/lib/Alchemy/Phrasea/ControllerProvider/Root/Developers.php index bff7296a9a..624da4429e 100644 --- a/lib/Alchemy/Phrasea/ControllerProvider/Root/Developers.php +++ b/lib/Alchemy/Phrasea/ControllerProvider/Root/Developers.php @@ -11,23 +11,31 @@ 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\Exception\InvalidArgumentException; -use Alchemy\Phrasea\Model\Entities\ApiApplication; use Silex\Application; use Silex\ControllerProviderInterface; -use Symfony\Component\HttpFoundation\JsonResponse; -use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\HttpFoundation\Response; +use Silex\ServiceProviderInterface; -class Developers implements ControllerProviderInterface +class Developers implements ControllerProviderInterface, ServiceProviderInterface { 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) { - $app['controller.account.developers'] = $this; - $controllers = $this->createAuthenticatedCollection($app); $controllers->get('/applications/', 'controller.account.developers:listApps') @@ -71,214 +79,4 @@ class Developers implements ControllerProviderInterface 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 - ]); - } }