Skip to content

Commit

Permalink
Merge pull request #2 from nlegoff/3.8login
Browse files Browse the repository at this point in the history
3.8 [WIP] Silex App
  • Loading branch information
ysoline committed Jul 16, 2012
2 parents fe3ff0f + 5873eca commit 3ba7b9a
Show file tree
Hide file tree
Showing 31 changed files with 2,265 additions and 1,263 deletions.
2 changes: 2 additions & 0 deletions config/nginx.rewrite.rules
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ rewrite ^/prod/notifications/.*$ /prod/router.php last;

rewrite ^/robots.txt$ /index.php last;
rewrite ^/feeds/.*$ /index.php last;
rewrite ^/account/.*$ /index.php last;
rewrite ^/developers/.*$ /index.php last;

rewrite ^/lightbox/.*$ /lightbox/index.php last;
rewrite ^/api/v1/.*$ /api/v1/index.php last;
Expand Down
201 changes: 0 additions & 201 deletions lib/Alchemy/Phrasea/Application/OAuth2.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,207 +201,6 @@
return;
});

/******************************************************************
* MANAGEMENT APPS
*
*
*/
/**
* list of all authorized apps by logged user
*/
$route = '/applications';
$app->get($route, function() use ($app) {
$apps = \API_OAuth2_Application::load_app_by_user($app['appbox'], $app['Core']->getAuthenticatedUser());

return $app['response']('api/auth/applications.twig', array("apps" => $apps, 'user' => $app['Core']->getAuthenticatedUser()));
});

/**
* list of apps created by user
*/
$route = "/applications/dev";
$app->get($route, function() use ($app) {
$rs = \API_OAuth2_Application::load_dev_app_by_user($app['appbox'], $app['Core']->getAuthenticatedUser());

return $app['response']('api/auth/application_dev.twig', array("apps" => $rs));
});

/**
* display a new app form
*/
$route = "/applications/dev/new";
$app->get($route, function() use ($app) {
$var = array("violations" => null, 'form' => null, 'request' => $app['request']);

return $app['response']('api/auth/application_dev_new.twig', $var);
});

$route = "/applications/dev/create";
$app->post($route, function() use ($app) {
$submit = false;
if ($app['request']->get("type") == "desktop") {
$post = new \API_OAuth2_Form_DevAppDesktop($app['request']);
} else {
$post = new \API_OAuth2_Form_DevAppInternet($app['request']);
}

$violations = $app['validator']->validate($post);

if ($violations->count() == 0)
$submit = true;

$request = $app['request'];

if ($submit) {
$application = \API_OAuth2_Application::create($app['appbox'], $app['Core']->getAuthenticatedUser(), $post->getName());
$application->set_description($post->getDescription())
->set_redirect_uri($post->getSchemeCallback() . $post->getCallback())
->set_type($post->getType())
->set_website($post->getSchemeWebsite() . $post->getWebsite());

return $app->redirect("/api/oauthv2/applications/dev/" . $application->get_id() . "/show");
}

$var = array(
"violations" => $violations,
"form" => $post
);

return $app['response']('api/auth/application_dev_new.twig', $var);
});

/**
* show details of app identified by its id
*/
$route = "/applications/dev/{id}/show";
$app->get($route, function($id) use ($app) {
$client = new \API_OAuth2_Application($app['appbox'], $id);
$token = $client->get_user_account($app['Core']->getAuthenticatedUser())->get_token()->get_value();
$var = array("app" => $client, "user" => $app['Core']->getAuthenticatedUser(), "token" => $token);

return $app['response']('api/auth/application_dev_show.twig', $var);
})->assert('id', '\d+');

/**
* revoke access from a user to the app
* identified by account id
*/
$route = "/applications/revoke_access/";
$app->post($route, function() use ($app) {
$result = array("ok" => false);
try {
$account = new \API_OAuth2_Account($app['appbox'], $app['request']->get('account_id'));
$account->set_revoked((bool) $app['request']->get('revoke'));
$result['ok'] = true;
} catch (\Exception $e) {

}

$Serializer = $app['Core']['Serializer'];

return new Response(
$Serializer->serialize($result, 'json')
, 200
, array("content-type" => "application/json")
);
});

/**
* revoke access from a user to the app
* identified by account id
*/
$route = "/applications/{appId}/grant_password/";
$app->post($route, function($appId) use ($app) {
$result = array("ok" => false);
try {
$client = new \API_OAuth2_Application($app['appbox'], $appId);
$client->set_grant_password((bool) $app['request']->get('grant'));
$result['ok'] = true;
} catch (\Exception $e) {

}

$Serializer = $app['Core']['Serializer'];

return new Response(
$Serializer->serialize($result, 'json')
, 200
, array("content-type" => "application/json")
);
});

$route = "/applications/{id}/generate_access_token/";
$app->post($route, function($id) use ($app) {
$result = array("ok" => false);
try {
$client = new \API_OAuth2_Application($app['appbox'], $id);
$account = $client->get_user_account($app['Core']->getAuthenticatedUser());

$token = $account->get_token();

if ($token instanceof API_OAuth2_Token)
$token->renew();
else
$token = \API_OAuth2_Token::create($app['appbox'], $account);

$result = array(
"ok" => true
, 'token' => $token->get_value()
);
} catch (\Exception $e) {

}

$Serializer = $app['Core']['Serializer'];

return new Response(
$Serializer->serialize($result, 'json')
, 200
, array("content-type" => "application/json")
);
})->assert('id', '\d+');

$route = "/applications/oauth_callback";
$app->post($route, function() use ($app) {
$app_id = $app['request']->request->get("app_id");
$app_callback = $app["request"]->request->get("callback");
$result = array("success" => false);
try {
$client = new \API_OAuth2_Application($app['appbox'], $app_id);
$client->set_redirect_uri($app_callback);
$result['success'] = true;
} catch (\Exception $e) {

}

$Serializer = $app['Core']['Serializer'];

return new Response(
$Serializer->serialize($result, 'json')
, 200
, array("content-type" => "application/json")
);
});

$route = "/applications/{id}";
$app->delete($route, function($id) use ($app) {
$result = array("success" => false);
try {
$client = new \API_OAuth2_Application($app['appbox'], $id);
$client->delete();
$result['success'] = true;
} catch (\Exception $e) {

}

$Serializer = $app['Core']['Serializer'];

return new Response(
$Serializer->serialize($result, 'json')
, 200
, array("content-type" => "application/json")
);
})->assert('id', '\d+');
/**
* *******************************************************************
*
Expand Down
31 changes: 15 additions & 16 deletions lib/Alchemy/Phrasea/Application/Root.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,28 @@

namespace Alchemy\Phrasea\Application;

use Symfony\Component\HttpFoundation\Response;
use Alchemy\Phrasea\Controller\Root as Controller;
use Silex\Application as SilexApp;
use Silex\Provider\ValidatorServiceProvider;
use Symfony\Component\HttpFoundation\Response;

/**
*
* @license http://opensource.org/licenses/gpl-3.0 GPLv3
* @link www.phraseanet.com
*/
return call_user_func(function() {
$app = new \Silex\Application();
$app = new SilexApp();

$app['Core'] = \bootstrap::getCore();

if ( ! \setup::is_installed()) {
$response = new \Symfony\Component\HttpFoundation\RedirectResponse('/setup/');
$app->register(new ValidatorServiceProvider());

return $response->send();
}
$app->before(function () use ($app) {
$app['Core']['Firewall']->requireSetup($app);
});

$app->get('/', function() use ($app) {
$app->get('/', function(SilexApp $app) {
$browser = \Browser::getInstance();
if ($browser->isMobile()) {
return $app->redirect("/login/?redirect=/lightbox");
Expand All @@ -41,17 +43,12 @@
}
});

$app->get('/robots.txt', function() use ($app) {
$appbox = \appbox::get_instance($app['Core']);

$registry = $appbox->get_registry();
$app->get('/robots.txt', function(SilexApp $app) {

if ($registry->get('GV_allow_search_engine') === true) {
$buffer = "User-Agent: *\n"
. "Allow: /\n";
if ($app['Core']['Registry']->get('GV_allow_search_engine') === true) {
$buffer = "User-Agent: *\n" . "Allow: /\n";
} else {
$buffer = "User-Agent: *\n"
. "Disallow: /\n";
$buffer = "User-Agent: *\n" . "Disallow: /\n";
}

$response = new Response($buffer, 200, array('Content-Type' => 'text/plain'));
Expand All @@ -61,6 +58,8 @@
});

$app->mount('/feeds/', new Controller\RSSFeeds());
$app->mount('/account/', new Controller\Account());
$app->mount('/developers/', new Controller\Developers());

return $app;
}
Expand Down
Loading

0 comments on commit 3ba7b9a

Please sign in to comment.