partkeepr

fork of partkeepr
git clone https://git.e1e0.net/partkeepr.git
Log | Files | Refs | Submodules | README | LICENSE

commit e975a15f3034338fb8a4db11ab63d16708360b7c
parent c6737cc6711c6f1e24a6e23ed434e41ebbd75296
Author: Felicitus <felicitus@felicitus.org>
Date:   Sun, 29 Mar 2015 22:12:13 +0200

Implemented a proper LoginResponse type for use with the REST auth/login method.

Diffstat:
Mapp/config/config.yml | 7+++++--
Msrc/PartKeepr/AuthBundle/Controller/DefaultController.php | 22++++++++++------------
Asrc/PartKeepr/AuthBundle/Response/LoginResponse.php | 41+++++++++++++++++++++++++++++++++++++++++
Msrc/PartKeepr/FrontendBundle/Resources/public/js/Components/Session/SessionManager.js | 4++--
Msrc/backend/PartKeepr/UserPreference/UserPreference.php | 14++++----------
5 files changed, 62 insertions(+), 26 deletions(-)

diff --git a/app/config/config.yml b/app/config/config.yml @@ -79,4 +79,7 @@ doctrine: is_bundle: false twig: - exception_controller: 'FOS\RestBundle\Controller\ExceptionController::showAction'- \ No newline at end of file + exception_controller: 'FOS\RestBundle\Controller\ExceptionController::showAction' + +parameters: + jms_serializer.camel_case_naming_strategy.class: JMS\Serializer\Naming\IdenticalPropertyNamingStrategy+ \ No newline at end of file diff --git a/src/PartKeepr/AuthBundle/Controller/DefaultController.php b/src/PartKeepr/AuthBundle/Controller/DefaultController.php @@ -3,6 +3,7 @@ namespace PartKeepr\AuthBundle\Controller; use FOS\RestBundle\Controller\FOSRestController; +use PartKeepr\AuthBundle\Response\LoginResponse; use Sensio\Bundle\FrameworkExtraBundle\Configuration as Routing; use PartKeepr\AuthBundle\Entity\User\User; use Nelmio\ApiDocBundle\Annotation\ApiDoc; @@ -25,12 +26,12 @@ class DefaultController extends FOSRestController * @Routing\Method({"POST"}) * @RequestParam(name="username", strict=true, description="The username, 3-50 characters. Allowed characters: a-z, A-Z, 0-9, an underscore (_), a backslash (\), a slash (/), a dot (.) or a dash (-)", requirements=@Username, allowBlank=false) * @RequestParam(name="password", strict=true, description="The password in MD5 format", requirements=@PasswordMD5Hash, allowBlank=false) - * @ApiDoc() + * @ApiDoc(output="PartKeepr\AuthBundle\Response\LoginResponse") * @View() * * @param ParamFetcher $paramFetcher * - * @return array + * @return LoginResponse * @throws InvalidLoginDataException */ public function loginAction(ParamFetcher $paramFetcher) @@ -57,15 +58,12 @@ class DefaultController extends FOSRestController $aPreferences[] = $result->serialize(); } - return array( - "sessionid" => $session->getSessionID(), - "username" => $paramFetcher->get("username"), - "admin" => $session->getUser()->isAdmin(), - "userPreferences" => array( - "response" => array( - "data" => $aPreferences - ) - ) - ); + $loginResponse = new LoginResponse(); + $loginResponse->sessionId = $session->getSessionID(); + $loginResponse->username = $paramFetcher->get("username"); + $loginResponse->isAdmin = $session->getUser()->isAdmin(); + $loginResponse->userPreferences = $session->getUser()->getPreferences(); + + return $loginResponse; } } \ No newline at end of file diff --git a/src/PartKeepr/AuthBundle/Response/LoginResponse.php b/src/PartKeepr/AuthBundle/Response/LoginResponse.php @@ -0,0 +1,40 @@ +<?php +namespace PartKeepr\AuthBundle\Response; + +use PartKeepr\UserPreference\UserPreference; +use JMS\Serializer\Annotation as JMS; + +/** + * Class LoginResponse + * + * Defines the login response for use with the REST API. + * + * @package PartKeepr\AuthBundle\Response + */ +class LoginResponse { + + /** + * Holds the session id + * @JMS\Type("string") + */ + public $sessionId; + + /** + * Holds the username + * @JMS\Type("string") + */ + public $username; + + /** + * Defines if the current user is an administrator + * @JMS\Type("boolean") + */ + public $isAdmin; + + /** + * @JMS\Type("array<PartKeepr\UserPreference\UserPreference>"); + * @var UserPreference[] The user preferences + */ + public $userPreferences; + +}+ \ No newline at end of file diff --git a/src/PartKeepr/FrontendBundle/Resources/public/js/Components/Session/SessionManager.js b/src/PartKeepr/FrontendBundle/Resources/public/js/Components/Session/SessionManager.js @@ -68,10 +68,10 @@ Ext.define("PartKeepr.SessionManager", { */ onAfterLogin: function (response) { console.log(response); - this.setSession(response.sessionid); + this.setSession(response.sessionId); this.loginDialog.destroy(); - PartKeepr.getApplication().setAdmin(response.admin); + PartKeepr.getApplication().setAdmin(response.isAdmin); PartKeepr.getApplication().setUsername(response.username); PartKeepr.getApplication().setInitialUserPreferences(response.userPreferences); diff --git a/src/backend/PartKeepr/UserPreference/UserPreference.php b/src/backend/PartKeepr/UserPreference/UserPreference.php @@ -5,11 +5,10 @@ use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\NoResultException; use PartKeepr\AuthBundle\Entity\User\User; use PartKeepr\PartKeepr; -use PartKeepr\Service\Annotations\ApiTypeOutput; -use PartKeepr\Service\Annotations\ApiTypeOutputs; use PartKeepr\UserPreference\Exceptions\UserPreferenceNotFoundException; use PartKeepr\Util\Exceptions\EntityNotPersistantException; use PartKeepr\Util\Serializable; +use JMS\Serializer\Annotation as JMS; /** * Represents a user preference entry. @@ -20,12 +19,14 @@ use PartKeepr\Util\Serializable; * Note that values are stored internally as serialized PHP values to keep their type. * * @ORM\Entity + * @JMS\ExclusionPolicy("ALL") **/ class UserPreference implements Serializable { /** * Defines the key of the user preference * @ORM\Column(type="string",length=255) * @ORM\Id + * @JMS\Expose * @var string */ private $preferenceKey; @@ -33,6 +34,7 @@ class UserPreference implements Serializable { /** * Defines the value. Note that the value is internally stored as a serialized string. * @ORM\Column(type="text") + * @JMS\Expose * @var mixed */ private $preferenceValue; @@ -96,14 +98,6 @@ class UserPreference implements Serializable { } /** - * (non-PHPdoc) - * @see PartKeepr\Util.Serializable::serialize() - * - * @ApiTypeOutputFields(outputFields={ - * @ApiTypeOutputField(name="key", type="string:255", description="The preference key"), - * @ApiTypeOutputField(name="value", type="text", description="The preference value"), - * @ApiTypeOutputField(name="user_id", type="integer", description="The user ID this preference belongs to") - * }) */ public function serialize () { return array(