partkeepr

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

commit 50ec8155577b58898d01644145ac724d16ecd71d
parent 3753d651479c0db4207f7632b00f6088905698c1
Author: Felicitus <felicitus@felicitus.org>
Date:   Wed, 16 Sep 2015 15:18:45 +0200

Refactored user preferences, added user preference service and unit tests

Diffstat:
Asrc/PartKeepr/AuthBundle/Entity/UserPreference.php | 105+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/PartKeepr/AuthBundle/Resources/config/services.xml | 3+++
Msrc/PartKeepr/AuthBundle/Response/LoginResponse.php | 5++---
Asrc/PartKeepr/AuthBundle/Services/UserPreferenceService.php | 156+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/PartKeepr/AuthBundle/Services/UserService.php | 7++++++-
Asrc/PartKeepr/AuthBundle/Tests/Services/UserPreferenceServiceTest.php | 107+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/backend/PartKeepr/PartKeepr.php | 5++---
Dsrc/backend/PartKeepr/UserPreference/UserPreference.php | 236-------------------------------------------------------------------------------
Dsrc/backend/PartKeepr/UserPreference/UserPreferenceService.php | 101-------------------------------------------------------------------------------
9 files changed, 381 insertions(+), 344 deletions(-)

diff --git a/src/PartKeepr/AuthBundle/Entity/UserPreference.php b/src/PartKeepr/AuthBundle/Entity/UserPreference.php @@ -0,0 +1,105 @@ +<?php +namespace PartKeepr\AuthBundle\Entity; + +use Doctrine\ORM\Mapping as ORM; +use PartKeepr\AuthBundle\Entity\User; + +/** + * Represents a user preference entry. + * + * User preferences are a simple key => value mechanism, where the developer can + * specify the key and value himself. + * + * Note that values are stored internally as serialized PHP values to keep their type. + * + * @ORM\Entity + **/ +class UserPreference +{ + /** + * Defines the key of the user preference + * @ORM\Column(type="string",length=255) + * + * @ORM\Id + * @var string + */ + private $preferenceKey; + + /** + * Defines the value. Note that the value is internally stored as a serialized string. + * + * @ORM\Column(type="text") + * @var mixed + */ + private $preferenceValue; + + /** + * Defines the user + * @ORM\ManyToOne(targetEntity="PartKeepr\AuthBundle\Entity\User") + * + * @ORM\Id + * @var \PartKeepr\AuthBundle\Entity\User + */ + private $user; + + + /** + * Sets the user for this entry + * + * @param \PartKeepr\AuthBundle\Entity\User $user + */ + public function setUser(User $user) + { + $this->user = $user; + } + + /** + * Returns the user associated with this entry + * + * @return \PartKeepr\AuthBundle\Entity\User + */ + public function getUser() + { + return $this->user; + } + + /** + * Sets the key for this user preference + * + * @param string $key The key name + */ + public function setKey($key) + { + $this->preferenceKey = $key; + } + + /** + * Returns the key of this entry + * + * @return string + */ + public function getKey() + { + return $this->preferenceKey; + } + + /** + * Sets the value for this entry + * + * @param mixed $value + */ + public function setValue($value) + { + $this->preferenceValue = serialize($value); + } + + /** + * Returns the value for this entry + * + * @return mixed The value + */ + public function getValue() + { + return unserialize($this->preferenceValue); + } +} diff --git a/src/PartKeepr/AuthBundle/Resources/config/services.xml b/src/PartKeepr/AuthBundle/Resources/config/services.xml @@ -13,5 +13,8 @@ <argument type="service" id="security.token_storage"/> <argument type="service" id="doctrine.orm.entity_manager"/> </service> + <service id="partkeepr.user_preference_service" class="PartKeepr\AuthBundle\Services\UserPreferenceService"> + <argument type="service" id="doctrine.orm.entity_manager"/> + </service> </services> </container> diff --git a/src/PartKeepr/AuthBundle/Response/LoginResponse.php b/src/PartKeepr/AuthBundle/Response/LoginResponse.php @@ -1,7 +1,7 @@ <?php namespace PartKeepr\AuthBundle\Response; -use PartKeepr\UserPreference\UserPreference; +use PartKeepr\AuthBundle\Entity\UserPreference; /** * Class LoginResponse @@ -32,4 +32,4 @@ class LoginResponse { */ public $userPreferences; -}- \ No newline at end of file +} diff --git a/src/PartKeepr/AuthBundle/Services/UserPreferenceService.php b/src/PartKeepr/AuthBundle/Services/UserPreferenceService.php @@ -0,0 +1,156 @@ +<?php +namespace PartKeepr\AuthBundle\Services; + +use Doctrine\ORM\EntityManager; +use Doctrine\ORM\NoResultException; +use PartKeepr\AuthBundle\Entity\User; +use PartKeepr\AuthBundle\Entity\UserPreference; +use PartKeepr\UserPreference\Exceptions\UserPreferenceNotFoundException; +use PartKeepr\Util\Exceptions\EntityNotPersistantException; + +class UserPreferenceService +{ + /* + * @var EntityManager + */ + private $entityManager; + + public function __construct(EntityManager $entityManager) + { + $this->entityManager = $entityManager; + } + + /** + * Creates or updates a preference for a given user. + * + * @param User $user The user to set the preference for + * @param string $key The key to set + * @param string $value The value to set + * + * @throws EntityNotPersistantException Thrown if the entity is not persistant + */ + public function setPreference(User $user, $key, $value) + { + if (!$this->entityManager->contains($user)) { + throw new EntityNotPersistantException(); + } + + $dql = 'SELECT up FROM PartKeepr\AuthBundle\Entity\UserPreference up WHERE up.user = :user AND '; + $dql .= "up.preferenceKey = :key"; + + $query = $this->entityManager->createQuery($dql); + $query->setParameter("user", $user); + $query->setParameter("key", $key); + + try { + $userPreference = $query->getSingleResult(); + } catch (\Exception $e) { + $userPreference = new UserPreference(); + $userPreference->setUser($user); + $userPreference->setKey($key); + + $this->entityManager->persist($userPreference); + } + + $userPreference->setValue($value); + + $this->entityManager->flush(); + + return $userPreference; + } + + /** + * Returns a specific preference value for the given user + * + * @param User $user The user to retrieve the preference for + * @param string $key The preference key to retrieve + * + * @return string The preference string + * @throws UserPreferenceNotFoundException Thrown if the preference key was not found + * @throws EntityNotPersistantException Thrown if the entity is not persistant + */ + public function getPreferenceValue(User $user, $key) + { + $userPreference = $this->getPreference($user, $key); + + return $userPreference->getValue(); + } + + /** + * Returns all preferences for the given user + * + * @param \PartKeepr\AuthBundle\Entity\User $user The user + * + * @throws EntityNotPersistantException Thrown if the user entity is not persistent + */ + public function getPreferences(User $user) + { + if (!$this->entityManager->contains($user)) { + throw new EntityNotPersistantException(); + } + + $dql = "SELECT up FROM PartKeepr\AuthBundle\Entity\UserPreference up WHERE up.user = :user"; + + $query = $this->entityManager->createQuery($dql); + $query->setParameter("user", $user); + + return $query->getResult(); + } + + /** + * Returns a specific preference object for the given user + * + * @param \PartKeepr\AuthBundle\Entity\User $user The user to retrieve the preference for + * @param string $key The preference key to retrieve + * + * @return UserPreference The preference object + * @throws UserPreferenceNotFoundException Thrown if the preference key was not found + * @throws EntityNotPersistantException Thrown if the entity is not persistant + */ + public function getPreference(User $user, $key) + { + if (!$this->entityManager->contains($user)) { + throw new EntityNotPersistantException(); + } + + $dql = "SELECT up FROM PartKeepr\AuthBundle\Entity\UserPreference up WHERE up.user = :user AND "; + $dql .= "up.preferenceKey = :key"; + + $query = $this->entityManager->createQuery($dql); + $query->setParameter("user", $user); + $query->setParameter("key", $key); + + try { + $up = $query->getSingleResult(); + + return $up; + } catch (NoResultException $e) { + throw new UserPreferenceNotFoundException($user, $key); + } + } + + /** + * Removes a specific setting for a specific user. + * + * @param \PartKeepr\AuthBundle\Entity\User $user The user to delete the preference for + * @param string $key The key to delete + * + * @throws EntityNotPersistantException Thrown if the entity is not persistant + */ + public function deletePreference(User $user, $key) + { + if (!$this->entityManager->contains($user)) { + throw new EntityNotPersistantException(); + } + + $dql = "DELETE FROM PartKeepr\AuthBundle\Entity\UserPreference up WHERE up.user = :user AND "; + $dql .= "up.preferenceKey = :key"; + + $query = $this->entityManager->createQuery($dql); + $query->setParameter("user", $user); + $query->setParameter("key", $key); + + $query->execute(); + } + +} diff --git a/src/PartKeepr/AuthBundle/Services/UserService.php b/src/PartKeepr/AuthBundle/Services/UserService.php @@ -12,7 +12,7 @@ class UserService { private $tokenStorage; - /* + /** * @var EntityManager */ private $entityManager; @@ -28,6 +28,11 @@ class UserService $provider = $this->tokenStorage->getToken()->getAttribute("provider"); $username = $this->tokenStorage->getToken()->getUsername(); + return $this->getProxyUser($username, $provider); + } + + public function getProxyUser($username, $provider) + { /** * @var QueryBuilder $queryBuilder */ diff --git a/src/PartKeepr/AuthBundle/Tests/Services/UserPreferenceServiceTest.php b/src/PartKeepr/AuthBundle/Tests/Services/UserPreferenceServiceTest.php @@ -0,0 +1,107 @@ +<?php +namespace PartKeepr\AuthBundle\Tests\Services; + +use Doctrine\Common\DataFixtures\ProxyReferenceRepository; +use Liip\FunctionalTestBundle\Test\WebTestCase; +use PartKeepr\AuthBundle\Entity\User; + +class UserPreferenceServiceTest extends WebTestCase +{ + /** + * @var ProxyReferenceRepository + */ + private $fixtures; + + public function setUp() + { + $this->fixtures = $this->loadFixtures( + array( + 'PartKeepr\AuthBundle\DataFixtures\LoadUserData', + ) + )->getReferenceRepository(); + } + + public function testBasics() + { + $service = $this->getContainer()->get("partkeepr.user_preference_service"); + + /** + * @var User $user + */ + $user = $this->getContainer()->get("partkeepr.userservice")->getProxyUser("admin", "test"); + + $service->setPreference($user, "foo", "bar"); + $this->assertEquals("bar", $service->getPreferenceValue($user, "foo")); + + $preferences = $service->getPreferences($user); + + $this->assertInternalType("array", $preferences); + + $this->assertArrayHasKey(0, $preferences); + $this->assertEquals(get_class($preferences[0]), "PartKeepr\AuthBundle\Entity\UserPreference"); + + $this->assertEquals("bar", $preferences[0]->getValue()); + $this->assertEquals("foo", $preferences[0]->getKey()); + $this->assertEquals($user, $preferences[0]->getUser()); + + $preference = $service->getPreference($user, "foo"); + + $this->assertEquals(get_class($preference), "PartKeepr\AuthBundle\Entity\UserPreference"); + + $this->assertEquals("bar", $preference->getValue()); + $this->assertEquals("foo", $preference->getKey()); + $this->assertEquals($user, $preference->getUser()); + + $service->deletePreference($user, "foo"); + + $preferences = $service->getPreferences($user); + + $this->assertEquals(0, count($preferences)); + + } + + public function testGetPreferencesException() + { + $service = $this->getContainer()->get("partkeepr.user_preference_service"); + $user = new User("admin", "test"); + $this->setExpectedException("PartKeepr\Util\Exceptions\EntityNotPersistantException"); + $service->getPreferences($user); + } + + public function testGetPreferenceUserException() + { + $service = $this->getContainer()->get("partkeepr.user_preference_service"); + $user = new User("admin", "test"); + $this->setExpectedException("PartKeepr\Util\Exceptions\EntityNotPersistantException"); + $service->getPreference($user, "BLA"); + } + + public function testGetPreferenceException() + { + $service = $this->getContainer()->get("partkeepr.user_preference_service"); + + /** + * @var User $user + */ + $user = $this->getContainer()->get("partkeepr.userservice")->getProxyUser("admin", "test"); + + $this->setExpectedException("PartKeepr\UserPreference\Exceptions\UserPreferenceNotFoundException"); + $service->getPreference($user, "BLA"); + } + + public function testSetPreferenceException() + { + $service = $this->getContainer()->get("partkeepr.user_preference_service"); + $user = new User("admin", "test"); + $this->setExpectedException("PartKeepr\Util\Exceptions\EntityNotPersistantException"); + $service->setPreference($user, "FOO", "BAR"); + } + + public function testDeletePreferenceException() + { + $service = $this->getContainer()->get("partkeepr.user_preference_service"); + $user = new User("admin", "test"); + $this->setExpectedException("PartKeepr\Util\Exceptions\EntityNotPersistantException"); + $service->deletePreference($user, "FOO"); + } +} diff --git a/src/backend/PartKeepr/PartKeepr.php b/src/backend/PartKeepr/PartKeepr.php @@ -199,7 +199,7 @@ class PartKeepr { 'PartKeepr\TipOfTheDayBundle\Entity\TipOfTheDay', 'PartKeepr\TipOfTheDayBundle\Entity\TipOfTheDayHistory', - 'PartKeepr\UserPreference\UserPreference', + 'PartKeepr\AuthBundle\Entity\UserPreference', 'PartKeepr\SystemNotice\SystemNotice', 'PartKeepr\CronLogger\CronLogger' @@ -305,4 +305,4 @@ class PartKeepr { default: return $size_str; } } -}- \ No newline at end of file +} diff --git a/src/backend/PartKeepr/UserPreference/UserPreference.php b/src/backend/PartKeepr/UserPreference/UserPreference.php @@ -1,235 +0,0 @@ -<?php -namespace PartKeepr\UserPreference; - -use Doctrine\ORM\Mapping as ORM; -use Doctrine\ORM\NoResultException; -use PartKeepr\AuthBundle\Entity\User; -use PartKeepr\PartKeepr; -use PartKeepr\UserPreference\Exceptions\UserPreferenceNotFoundException; -use PartKeepr\Util\Exceptions\EntityNotPersistantException; -use PartKeepr\Util\Serializable; - -/** - * Represents a user preference entry. - * - * User preferences are a simple key => value mechanism, where the developer can - * specify the key and value himself. - * - * Note that values are stored internally as serialized PHP values to keep their type. - * - * @ORM\Entity - * - **/ -class UserPreference implements Serializable { - /** - * Defines the key of the user preference - * @ORM\Column(type="string",length=255) - * @ORM\Id - * @var string - */ - private $preferenceKey; - - /** - * Defines the value. Note that the value is internally stored as a serialized string. - * - * @ORM\Column(type="text") - * @var mixed - */ - private $preferenceValue; - - /** - * Defines the user - * @ORM\ManyToOne(targetEntity="PartKeepr\AuthBundle\Entity\User") - * -*@ORM\Id - * @var \PartKeepr\AuthBundle\Entity\User - */ - private $user; - - - /** - * Sets the user for this entry - * -*@param \PartKeepr\AuthBundle\Entity\User $user - */ - public function setUser (User $user) { - $this->user = $user; - } - - /** - * Returns the user associated with this entry - * -*@return \PartKeepr\AuthBundle\Entity\User - */ - public function getUser () { - return $this->user; - } - - /** - * Sets the key for this user preference - * @param string $key The key name - */ - public function setKey ($key) { - $this->preferenceKey = $key; - } - - /** - * Returns the key of this entry - * @return string - */ - public function getKey () { - return $this->preferenceKey; - } - - /** - * Sets the value for this entry - * @param mixed $value - */ - public function setValue ($value) { - $this->preferenceValue = serialize($value); - } - - /** - * Returns the value for this entry - * @return mixed The value - */ - public function getValue () { - return unserialize($this->preferenceValue); - } - - /** - */ - public function serialize () { - return array( - "key" => $this->getKey(), - "value" => $this->getValue(), - "user_id" => $this->getUser()->getId() - ); - } - - /** - * Creates or updates a preference for a given user. - * - * @param User $user The user to set the preference for - * @param string $key The key to set - * @param string $value The value to set - * @throws EntityNotPersistantException Thrown if the entity is not persistant - */ - public static function setPreference (User $user, $key, $value) { - if (!PartKeepr::getEM()->contains($user)) { - throw new EntityNotPersistantException(); - } - - $dql = "SELECT up FROM PartKeepr\UserPreference\UserPreference up WHERE up.user = :user AND "; - $dql .= "up.preferenceKey = :key"; - - $query = PartKeepr::getEM()->createQuery($dql); - $query->setParameter("user", $user); - $query->setParameter("key", $key); - - try { - $userPreference = $query->getSingleResult(); - } catch (\Exception $e) { - $userPreference = new UserPreference(); - $userPreference->setUser($user); - $userPreference->setKey($key); - - PartKeepr::getEM()->persist($userPreference); - } - - $userPreference->setValue($value); - - PartKeepr::getEM()->flush(); - - return $userPreference; - } - - /** - * Returns a specific preference value for the given user - * - * @param User $user The user to retrieve the preference for - * @param string $key The preference key to retrieve - * @return string The preference string - * @throws UserPreferenceNotFoundException Thrown if the preference key was not found - * @throws EntityNotPersistantException Thrown if the entity is not persistant - */ - public static function getPreferenceValue (User $user, $key) { - $userPreference = self::getPreference($user, $key); - - return $userPreference->getValue(); - } - - /** - * Returns all preferences for the given user - * -*@param \PartKeepr\AuthBundle\Entity\User $user The user - * -*@throws EntityNotPersistantException Thrown if the user entity is not persistent - */ - public static function getPreferences (User $user) { - if (!PartKeepr::getEM()->contains($user)) { - throw new EntityNotPersistantException(); - } - - $dql = "SELECT up FROM PartKeepr\UserPreference\UserPreference up WHERE up.user = :user"; - - $query = PartKeepr::getEM()->createQuery($dql); - $query->setParameter("user", $user); - - return $query->getResult(); - } - - /** - * Returns a specific preference object for the given user - * - * @param \PartKeepr\AuthBundle\Entity\User $user The user to retrieve the preference for - * @param string $key The preference key to retrieve - * -*@return UserPreference The preference object - * @throws UserPreferenceNotFoundException Thrown if the preference key was not found - * @throws EntityNotPersistantException Thrown if the entity is not persistant - */ - public static function getPreference (User $user, $key) { - if (!PartKeepr::getEM()->contains($user)) { - throw new EntityNotPersistantException(); - } - - $dql = "SELECT up FROM PartKeepr\UserPreference\UserPreference up WHERE up.user = :user AND "; - $dql .= "up.preferenceKey = :key"; - - $query = PartKeepr::getEM()->createQuery($dql); - $query->setParameter("user", $user); - $query->setParameter("key", $key); - - try { - $up = $query->getSingleResult(); - return $up; - } catch (NoResultException $e) { - throw new UserPreferenceNotFoundException($user, $key); - } - } - - /** - * Removes a specific setting for a specific user. - * - * @param \PartKeepr\AuthBundle\Entity\User $user The user to delete the preference for - * @param string $key The key to delete - * -*@throws EntityNotPersistantException Thrown if the entity is not persistant - */ - public static function deletePreference (User $user, $key) { - if (!PartKeepr::getEM()->contains($user)) { - throw new EntityNotPersistantException(); - } - - $dql = "DELETE FROM PartKeepr\UserPreference\UserPreference up WHERE up.user = :user AND "; - $dql .= "up.preferenceKey = :key"; - - $query = PartKeepr::getEM()->createQuery($dql); - $query->setParameter("user", $user); - $query->setParameter("key", $key); - - $query->execute(); - } - -}- \ No newline at end of file diff --git a/src/backend/PartKeepr/UserPreference/UserPreferenceService.php b/src/backend/PartKeepr/UserPreference/UserPreferenceService.php @@ -1,100 +0,0 @@ -<?php -namespace PartKeepr\UserPreference; - -use PartKeepr\AuthBundle\Entity\User; -use PartKeepr\PartKeepr; -use PartKeepr\Service\RestfulService; -use PartKeepr\Service\Service; -use PartKeepr\Session\SessionManager; -use PartKeepr\Util\Configuration; - -/** - * Represents the user preference service. This service is implemented as a RestfulService, however, - * only setting and deleting properties is supported, as we don't want to have duplicate values per key. - * - * For convinience, create() and update() perform the exact same function. - * @author felicitus - * - */ -class UserPreferenceService extends Service implements RestfulService { - /** - * Returns the preferences for the current user, or a user specified by user_id (admin only). - * - * (non-PHPdoc) - * @see PartKeepr\Service.RestfulService::get() - */ - public function get () { - $user = null; - - if ($this->hasParameter("user_id") && SessionManager::getCurrentSession()->getUser()->isAdmin()) { - if ($this->getParameter("user_id") != 0) { - $user = User::loadById($this->getParameter("user_id")); - } - } else { - $user = SessionManager::getCurrentSession()->getUser(); - } - - if ($user === null) { - return array("data" => array()); - } - - $aPreferences = array(); - - foreach ($user->getPreferences() as $result) { - $aPreferences[] = $result->serialize(); - } - - return array("data" => $aPreferences); - } - - /** - * Creates or updates a value for a specific key. - * - * (non-PHPdoc) - * @see PartKeepr\Service.RestfulService::create() - */ - public function create() { - $userPreference = UserPreference::setPreference($this->getUser(), $this->getParameter("key"), $this->getParameter("value")); - - return array("data" => $userPreference->serialize()); - } - - /** - * Creates or updates a value for a specific key. - * - * (non-PHPdoc) - * @see PartKeepr\Service.RestfulService::update() - */ - public function update () { - return $this->create(); - } - - /** - * Deletes a key-value combination from the database. - * - * (non-PHPdoc) - * @see PartKeepr\Service.RestfulService::destroy() - */ - public function destroy () { - if ($this->hasParameter("user_id") && SessionManager::getCurrentSession()->getUser()->isAdmin()) { - UserPreference::deletePreference(User::loadById($this->getParameter("user_id")), $this->getParameter("key")); - } else { - UserPreference::deletePreference($this->getUser(), $this->getParameter("key")); - } - } - - public function changePassword () { - if (Configuration::getOption("partkeepr.frontend.allow_password_change", true) === false) { - throw new \Exception("Password changing has been disabled on this server"); - } - - if (!$this->getUser()->compareHashedPassword($this->getParameter("oldpassword"))) { - throw new \Exception("Invalid Password"); - } else { - $this->getUser()->setHashedPassword($this->getParameter("newpassword")); - } - - return array("data" => PartKeepr::i18n("Password changed successfully")); - - } -}- \ No newline at end of file