Add /me route on API v1

This commit is contained in:
Romain Neutron
2014-02-28 17:48:49 +01:00
parent c853f37c89
commit 1952910c83
3 changed files with 100 additions and 0 deletions

View File

@@ -728,6 +728,12 @@ class V1 implements ControllerProviderInterface
})->assert('databox_id', '\d+')->assert('story_id', '\d+');
$controllers->get('/stories/{any_id}/{anyother_id}/', $bad_request_exception);
$controllers->get('/me/', function (SilexApplication $app, Request $request) {
$result = $app['api']->get_current_user($app, $request);
return $result->get_response();
});
return $controllers;
}
}

View File

@@ -1949,6 +1949,53 @@ class API_V1_adapter extends API_V1_Abstract
);
}
public function get_current_user(Application $app, Request $request)
{
$result = new API_V1_result($app, $request, $this);
$result->set_datas(array('user' => $this->list_user($app['authentication']->getUser())));
return $result;
}
private function list_user(\User_Adapter $user)
{
switch ($user->get_gender()) {
case 2;
$gender = 'Mr';
break;
case 1;
$gender = 'Mrs';
break;
case 0;
$gender = 'Miss';
break;
}
return array(
'id' => $user->get_id(),
'email' => $user->get_email() ?: null,
'login' => $user->get_login() ?: null,
'first_name' => $user->get_firstname() ?: null,
'last_name' => $user->get_lastname() ?: null,
'display_name' => $user->get_display_name() ?: null,
'gender' => $gender,
'address' => $user->get_address() ?: null,
'zip_code' => $user->get_zipcode() ?: null,
'city' => $user->get_city() ?: null,
'country' => $user->get_country() ?: null,
'phone' => $user->get_tel() ?: null,
'fax' => $user->get_fax() ?: null,
'job' => $user->get_job() ?: null,
'position' => $user->get_position() ?: null,
'company' => $user->get_company() ?: null,
'geoname_id' => $user->get_geonameid() ?: null,
'last_connection' => $user->get_last_connection() ? $user->get_last_connection()->format(DATE_ATOM) : null,
'created_on' => $user->get_creation_date() ? $user->get_creation_date()->format(DATE_ATOM) : null,
'updated_on' => $user->get_modification_date() ? $user->get_modification_date()->format(DATE_ATOM) : null,
'locale' => $user->get_locale() ?: null,
);
}
/**
* List all databoxes of the current appbox
*

View File

@@ -2051,6 +2051,53 @@ abstract class ApiAbstract extends \PhraseanetWebTestCaseAbstract
$this->assertDateAtom($item['created_on']);
}
public function testRouteMe()
{
$this->setToken(self::$token);
$route = '/api/v1/me/';
$this->evaluateMethodNotAllowedRoute($route, array('POST', 'PUT', 'DELETE'));
self::$DI['client']->request('GET', $route, $this->getParameters(), array(), array('HTTP_Accept' => $this->getAcceptMimeType()));
$content = $this->unserialize(self::$DI['client']->getResponse()->getContent());
$this->assertArrayHasKey('user', $content['response']);
$this->evaluateGoodUserItem($content['response']['user'], self::$DI['user_notAdmin']);
}
protected function evaluateGoodUserItem($data, \User_Adapter $user)
{
foreach (array(
'id' => $user->get_id(),
'email' => $user->get_email() ?: null,
'login' => $user->get_login() ?: null,
'first_name' => $user->get_firstname() ?: null,
'last_name' => $user->get_lastname() ?: null,
'display_name' => $user->get_display_name() ?: null,
'address' => $user->get_address() ?: null,
'zip_code' => $user->get_zipcode() ?: null,
'city' => $user->get_city() ?: null,
'country' => $user->get_country() ?: null,
'phone' => $user->get_tel() ?: null,
'fax' => $user->get_fax() ?: null,
'job' => $user->get_job() ?: null,
'position' => $user->get_position() ?: null,
'company' => $user->get_company() ?: null,
'geoname_id' => $user->get_geonameid() ?: null,
'last_connection' => $user->get_last_connection() ? $user->get_last_connection()->format(DATE_ATOM) : null,
'created_on' => $user->get_creation_date() ? $user->get_creation_date()->format(DATE_ATOM) : null,
'updated_on' => $user->get_modification_date() ? $user->get_modification_date()->format(DATE_ATOM) : null,
'locale' => $user->get_locale() ?: null,
) as $key => $value) {
$this->assertArrayHasKey($key, $data);
if ($value) {
$this->assertEquals($value, $data[$key], 'Check key '.$key);
}
}
}
protected function evaluateGoodFeed($feed)
{
$this->assertArrayHasKey('id', $feed);