Merge pull request #1002 from romainneutron/fix-1710

[Ready][3.8] Fix 1710 : Add /me route + add user entities in API response
This commit is contained in:
Nicolas Le Goff
2014-03-03 16:23:08 +01:00
5 changed files with 152 additions and 24 deletions

View File

@@ -9,6 +9,7 @@
* file that was distributed with this source code.
*/
use Alchemy\Phrasea\Application;
use Symfony\Component\HttpFoundation\Request;
/**

View File

@@ -39,6 +39,7 @@ class API_V1_adapter extends API_V1_Abstract
*/
protected $app;
const OBJECT_TYPE_USER = 'http://api.phraseanet.com/api/objects/user';
const OBJECT_TYPE_STORY = 'http://api.phraseanet.com/api/objects/story';
const OBJECT_TYPE_STORY_METADATA_BAG = 'http://api.phraseanet.com/api/objects/story-metadata-bag';
@@ -801,14 +802,16 @@ class API_V1_adapter extends API_V1_Abstract
}
}
$usr_id = null;
$usr_id = $user = null;
if ($file->getSession()->getUser($this->app)) {
$usr_id = $file->getSession()->getUser($this->app)->get_id();
$user = $file->getSession()->getUser($this->app);
$usr_id = $user->get_id();
}
$session = array(
'id' => $file->getSession()->getId(),
'usr_id' => $usr_id,
'user' => $user ? $this->list_user($user) : null,
);
return array(
@@ -1388,6 +1391,7 @@ class API_V1_adapter extends API_V1_Abstract
'can_agree' => $participant->getCanAgree(),
'can_see_others' => $participant->getCanSeeOthers(),
'readonly' => $user->get_id() != $this->app['authentication']->getUser()->get_id(),
'user' => $this->list_user($user),
),
'agreement' => $validation_datas->getAgreement(),
'updated_on' => $validation_datas->getUpdated()->format(DATE_ATOM),
@@ -1809,10 +1813,12 @@ class API_V1_adapter extends API_V1_Abstract
{
$ret = array(
'basket_id' => $basket->getId(),
'owner' => $this->list_user($basket->getOwner($this->app)),
'created_on' => $basket->getCreated()->format(DATE_ATOM),
'description' => (string) $basket->getDescription(),
'name' => $basket->getName(),
'pusher_usr_id' => $basket->getPusherId(),
'pusher' => $basket->getPusher($this->app) ? $this->list_user($basket->getPusher($this->app)) : null,
'updated_on' => $basket->getUpdated()->format(DATE_ATOM),
'unread' => !$basket->getIsRead(),
'validation_basket' => !!$basket->getValidation()
@@ -1832,6 +1838,7 @@ class API_V1_adapter extends API_V1_Abstract
'can_agree' => $participant->getCanAgree(),
'can_see_others' => $participant->getCanSeeOthers(),
'readonly' => $user->get_id() != $this->app['authentication']->getUser()->get_id(),
'user' => $this->list_user($user),
);
}
@@ -1843,11 +1850,12 @@ class API_V1_adapter extends API_V1_Abstract
$ret = array_merge(
array(
'validation_users' => $users,
'expires_on' => $expires_on_atom,
'validation_infos' => $basket->getValidation()->getValidationString($this->app, $this->app['authentication']->getUser()),
'validation_confirmed' => $basket->getValidation()->getParticipant($this->app['authentication']->getUser(), $this->app)->getIsConfirmed(),
'validation_initiator' => $basket->getValidation()->isInitiator($this->app['authentication']->getUser()),
'validation_users' => $users,
'expires_on' => $expires_on_atom,
'validation_infos' => $basket->getValidation()->getValidationString($this->app, $this->app['authentication']->getUser()),
'validation_confirmed' => $basket->getValidation()->getParticipant($this->app['authentication']->getUser(), $this->app)->getIsConfirmed(),
'validation_initiator' => $basket->getValidation()->isInitiator($this->app['authentication']->getUser()),
'validation_initiator_user' => $this->list_user($basket->getValidation()->getInitiator($this->app)),
), $ret
);
}
@@ -1950,6 +1958,54 @@ 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(
'@entity@' => self::OBJECT_TYPE_USER,
'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
*