diff --git a/lib/Alchemy/Phrasea/Controller/Api/V1.php b/lib/Alchemy/Phrasea/Controller/Api/V1.php index 14ba57963e..8b14102a00 100644 --- a/lib/Alchemy/Phrasea/Controller/Api/V1.php +++ b/lib/Alchemy/Phrasea/Controller/Api/V1.php @@ -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; } } diff --git a/lib/classes/API/V1/adapter.php b/lib/classes/API/V1/adapter.php index 7e6f0756c2..782ed7dd50 100644 --- a/lib/classes/API/V1/adapter.php +++ b/lib/classes/API/V1/adapter.php @@ -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 * diff --git a/tests/Alchemy/Tests/Phrasea/Application/ApiAbstract.php b/tests/Alchemy/Tests/Phrasea/Application/ApiAbstract.php index 4a8b36f024..3f1d2321c4 100644 --- a/tests/Alchemy/Tests/Phrasea/Application/ApiAbstract.php +++ b/tests/Alchemy/Tests/Phrasea/Application/ApiAbstract.php @@ -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);