partkeepr

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

commit c603b6a7678c98bfb62b41fe24c2aa273ab7e2f4
parent 8c9bb98674ed56254f8dcca60f8f33f1b80e89bc
Author: Felicitus <felicitus@felicitus.org>
Date:   Sun, 15 Mar 2015 23:21:21 +0100

Implemented authentification bundle

Diffstat:
Mapp/AppKernel.php | 3++-
Mapp/config/config.yml | 10+++++++---
Mapp/config/routing.yml | 5+++++
Aextras/library/extjs5 | 1+
Asrc/PartKeepr/AuthBundle/Controller/DefaultController.php | 67+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/PartKeepr/AuthBundle/DependencyInjection/Configuration.php | 29+++++++++++++++++++++++++++++
Asrc/PartKeepr/AuthBundle/DependencyInjection/PartKeeprAuthExtension.php | 28++++++++++++++++++++++++++++
Asrc/PartKeepr/AuthBundle/Entity/User/Exceptions/InvalidLoginDataException.php | 15+++++++++++++++
Asrc/PartKeepr/AuthBundle/Entity/User/Exceptions/UserAlreadyExistsException.php | 20++++++++++++++++++++
Asrc/PartKeepr/AuthBundle/Entity/User/Exceptions/UserDoesNotExistException.php | 18++++++++++++++++++
Asrc/PartKeepr/AuthBundle/Entity/User/User.php | 268+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/PartKeepr/AuthBundle/Entity/UserManager.php | 103+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/PartKeepr/AuthBundle/PartKeeprAuthBundle.php | 9+++++++++
Asrc/PartKeepr/AuthBundle/Resources/config/services.xml | 16++++++++++++++++
Asrc/PartKeepr/AuthBundle/Resources/doc/index.rst | 0
Asrc/PartKeepr/AuthBundle/Resources/translations/messages.fr.xlf | 11+++++++++++
Asrc/PartKeepr/AuthBundle/Resources/views/Default/index.html.twig | 1+
Asrc/PartKeepr/AuthBundle/Tests/Controller/DefaultControllerTest.php | 17+++++++++++++++++
Asrc/PartKeepr/AuthBundle/Validator/Constraints/PasswordMD5Hash.php | 14++++++++++++++
Asrc/PartKeepr/AuthBundle/Validator/Constraints/PasswordMD5HashValidator.php | 19+++++++++++++++++++
Asrc/PartKeepr/AuthBundle/Validator/Constraints/Username.php | 14++++++++++++++
Asrc/PartKeepr/AuthBundle/Validator/Constraints/UsernameValidator.php | 19+++++++++++++++++++
Msrc/PartKeepr/FrontendBundle/Controller/IndexController.php | 11++++-------
Asrc/PartKeepr/FrontendBundle/Resources/public/images/favicon.ico | 0
Msrc/PartKeepr/FrontendBundle/Resources/public/js/Components/Session/SessionManager.js | 3++-
Msrc/PartKeepr/FrontendBundle/Resources/public/js/PartKeepr.js | 2+-
Msrc/PartKeepr/FrontendBundle/Resources/public/js/Util/ServiceCall.js | 25+++++++++++++++----------
Msrc/backend/PartKeepr/Auth/AuthService.php | 14+++++---------
Msrc/backend/PartKeepr/Part/PartService.php | 19+++++++++----------
Msrc/backend/PartKeepr/PartKeepr.php | 9++++-----
Msrc/backend/PartKeepr/Printing/PrintingJob/PrintingJob.php | 17++++++++---------
Msrc/backend/PartKeepr/Printing/PrintingService.php | 20+++++++-------------
Msrc/backend/PartKeepr/Project/Project.php | 12++++++------
Msrc/backend/PartKeepr/Service/Service.php | 7+++----
Msrc/backend/PartKeepr/Service/ServiceManager.php | 14+++++++-------
Msrc/backend/PartKeepr/Session/Session.php | 8++++----
Msrc/backend/PartKeepr/Session/SessionManager.php | 8++++----
Msrc/backend/PartKeepr/Setup/UserSetup.php | 4++--
Msrc/backend/PartKeepr/Stock/StockEntry.php | 16++++++++--------
Msrc/backend/PartKeepr/Stock/StockService.php | 13++++++-------
Msrc/backend/PartKeepr/TipOfTheDay/TipOfTheDayHistory.php | 15++++-----------
Msrc/backend/PartKeepr/TipOfTheDay/TipOfTheDayService.php | 11+++++------
Dsrc/backend/PartKeepr/User/Exceptions/InvalidLoginDataException.php | 15---------------
Dsrc/backend/PartKeepr/User/Exceptions/UserAlreadyExistsException.php | 20--------------------
Dsrc/backend/PartKeepr/User/Exceptions/UserDoesNotExistException.php | 18------------------
Dsrc/backend/PartKeepr/User/User.php | 269-------------------------------------------------------------------------------
Dsrc/backend/PartKeepr/User/UserManager.php | 104-------------------------------------------------------------------------------
Msrc/backend/PartKeepr/User/UserService.php | 16+++++++---------
Msrc/backend/PartKeepr/UserPreference/Exceptions/UserPreferenceNotFoundException.php | 6+++---
Msrc/backend/PartKeepr/UserPreference/UserPreference.php | 27++++++++++++---------------
Msrc/backend/PartKeepr/UserPreference/UserPreferenceService.php | 12++++++------
Msrc/frontend/index.php | 8+-------
Mtests/Auth/UserTest.php | 2+-
Mtests/User/UserTest.php | 6+++---
Mtests/UserPreference/UserPreferenceTest.php | 6+++---
Mtests/bootstrap.php | 6+++---
56 files changed, 826 insertions(+), 604 deletions(-)

diff --git a/app/AppKernel.php b/app/AppKernel.php @@ -1,8 +1,8 @@ <?php -use Symfony\Component\HttpKernel\Kernel; use Symfony\Component\Config\Loader\LoaderInterface; use Symfony\Component\DependencyInjection\Container; +use Symfony\Component\HttpKernel\Kernel; class AppKernel extends Kernel { @@ -56,6 +56,7 @@ class AppKernel extends Kernel new JMS\SerializerBundle\JMSSerializerBundle(), new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(), new Nelmio\ApiDocBundle\NelmioApiDocBundle(), + new PartKeepr\AuthBundle\PartKeeprAuthBundle(), ); // Developer bundles diff --git a/app/config/config.yml b/app/config/config.yml @@ -3,8 +3,9 @@ imports: - { resource: security.yml } fos_rest: + body_listener: true format_listener: true - param_fetcher_listener: true + param_fetcher_listener: force view: default_engine: php formats: @@ -75,4 +76,7 @@ doctrine: type: annotation dir: %kernel.root_dir%/../src/backend/PartKeepr/ prefix: PartKeepr - is_bundle: false- \ No newline at end of file + is_bundle: false + +twig: + exception_controller: 'FOS\RestBundle\Controller\ExceptionController::showAction'+ \ No newline at end of file diff --git a/app/config/routing.yml b/app/config/routing.yml @@ -1,3 +1,8 @@ +part_keepr_auth: + resource: "@PartKeeprAuthBundle/Controller/" + type: annotation + prefix: / + partkeepr_siprefixbundle: resource: "@PartKeeprSiPrefixBundle/Controller/" type: annotation diff --git a/extras/library/extjs5 b/extras/library/extjs5 @@ -0,0 +1 @@ +Subproject commit 6adaee3015360d0bdb9bf8dfcb685905651da576 diff --git a/src/PartKeepr/AuthBundle/Controller/DefaultController.php b/src/PartKeepr/AuthBundle/Controller/DefaultController.php @@ -0,0 +1,66 @@ +<?php + +namespace PartKeepr\AuthBundle\Controller; + +use FOS\RestBundle\Controller\FOSRestController; +use FOS\RestBundle\Request\ParamFetcher; +use PartKeepr\AuthBundle\Entity\User\Exceptions\InvalidLoginDataException; +use PartKeepr\AuthBundle\Entity\User\User; +use PartKeepr\AuthBundle\Entity\UserManager; +use PartKeepr\Session\SessionManager; +use Sensio\Bundle\FrameworkExtraBundle\Configuration as Routing; +use Symfony\Component\HttpKernel\Exception\HttpException; + +class DefaultController extends FOSRestController +{ + /** + * Logs the user in using a given username and password. + * + * @Routing\Route("/auth/login", defaults={"method" = "get","_format" = "json"}) + * @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() + * @View() + * + * @param ParamFetcher $paramFetcher + * + * @return array + * @throws InvalidLoginDataException + */ + public function loginAction(ParamFetcher $paramFetcher) + { + /* Build a temporary user */ + $user = new User(); + $user->setRawUsername($paramFetcher->get("username")); + $user->setHashedPassword($paramFetcher->get("password")); + + try { + $authenticatedUser = UserManager::getInstance()->authenticate($user); + } catch (InvalidLoginDataException $e) { + throw new HttpException(401, "Username or password invalid."); + } + + /* Start Session */ + $session = SessionManager::getInstance()->startSession($authenticatedUser); + + $session->getUser()->updateSeen(); + + $aPreferences = array(); + + foreach ($session->getUser()->getPreferences() as $result) { + $aPreferences[] = $result->serialize(); + } + + return array( + "sessionid" => $session->getSessionID(), + "username" => $paramFetcher->get("username"), + "admin" => $session->getUser()->isAdmin(), + "userPreferences" => array( + "response" => array( + "data" => $aPreferences + ) + ) + ); + } +}+ \ No newline at end of file diff --git a/src/PartKeepr/AuthBundle/DependencyInjection/Configuration.php b/src/PartKeepr/AuthBundle/DependencyInjection/Configuration.php @@ -0,0 +1,29 @@ +<?php + +namespace PartKeepr\AuthBundle\DependencyInjection; + +use Symfony\Component\Config\Definition\Builder\TreeBuilder; +use Symfony\Component\Config\Definition\ConfigurationInterface; + +/** + * This is the class that validates and merges configuration from your app/config files + * + * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class} + */ +class Configuration implements ConfigurationInterface +{ + /** + * {@inheritdoc} + */ + public function getConfigTreeBuilder() + { + $treeBuilder = new TreeBuilder(); + $rootNode = $treeBuilder->root('part_keepr_auth'); + + // Here you should define the parameters that are allowed to + // configure your bundle. See the documentation linked above for + // more information on that topic. + + return $treeBuilder; + } +} diff --git a/src/PartKeepr/AuthBundle/DependencyInjection/PartKeeprAuthExtension.php b/src/PartKeepr/AuthBundle/DependencyInjection/PartKeeprAuthExtension.php @@ -0,0 +1,28 @@ +<?php + +namespace PartKeepr\AuthBundle\DependencyInjection; + +use Symfony\Component\Config\FileLocator; +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Loader; +use Symfony\Component\HttpKernel\DependencyInjection\Extension; + +/** + * This is the class that loads and manages your bundle configuration + * + * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html} + */ +class PartKeeprAuthExtension extends Extension +{ + /** + * {@inheritdoc} + */ + public function load(array $configs, ContainerBuilder $container) + { + $configuration = new Configuration(); + $config = $this->processConfiguration($configuration, $configs); + + $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); + $loader->load('services.xml'); + } +} diff --git a/src/PartKeepr/AuthBundle/Entity/User/Exceptions/InvalidLoginDataException.php b/src/PartKeepr/AuthBundle/Entity/User/Exceptions/InvalidLoginDataException.php @@ -0,0 +1,14 @@ +<?php +namespace PartKeepr\AuthBundle\Entity\User\Exceptions; + +use PartKeepr\PartKeepr; +use PartKeepr\Util\SerializableException; + +/** + * Is thrown when the user has given wrong credentials. + */ +class InvalidLoginDataException extends SerializableException { + public function __construct () { + parent::__construct(PartKeepr::i18n("Username or Password wrong.")); + } +}+ \ No newline at end of file diff --git a/src/PartKeepr/AuthBundle/Entity/User/Exceptions/UserAlreadyExistsException.php b/src/PartKeepr/AuthBundle/Entity/User/Exceptions/UserAlreadyExistsException.php @@ -0,0 +1,19 @@ +<?php +namespace PartKeepr\AuthBundle\Entity\User\Exceptions; + +use PartKeepr\PartKeepr; +use PartKeepr\Util\SerializableException; + +/** +* Is thrown when the user already exists. This usually happens +* if someone tries to create a user with the same name of an existing +* user. +*/ +class UserAlreadyExistsException extends SerializableException { + public function __construct ($username) { + parent::__construct( + sprintf( + PartKeepr::i18n("User %s already exists."), + $username)); + } +}+ \ No newline at end of file diff --git a/src/PartKeepr/AuthBundle/Entity/User/Exceptions/UserDoesNotExistException.php b/src/PartKeepr/AuthBundle/Entity/User/Exceptions/UserDoesNotExistException.php @@ -0,0 +1,17 @@ +<?php +namespace PartKeepr\AuthBundle\Entity\User\Exceptions; + +use PartKeepr\PartKeepr; +use PartKeepr\Util\SerializableException; + +/** +* Is thrown when the user doesn't exist. +*/ +class UserDoesNotExistException extends SerializableException { + public function __construct ($username) { + parent::__construct( + sprintf( + PartKeepr::i18n("The user %s doesn't exist. Maybe the user was already deleted."), + $username)); + } +}+ \ No newline at end of file diff --git a/src/PartKeepr/AuthBundle/Entity/User/User.php b/src/PartKeepr/AuthBundle/Entity/User/User.php @@ -0,0 +1,267 @@ +<?php +namespace PartKeepr\AuthBundle\Entity\User; + +use Doctrine\ORM\Mapping as ORM; +use Doctrine\ORM\Mapping\Table; +use PartKeepr\PartKeepr; +use PartKeepr\UserPreference\UserPreference; +use PartKeepr\Util\BaseEntity; +use PartKeepr\Util\Deserializable; +use PartKeepr\Util\Serializable; + +/** @ORM\Entity @ORM\Table(name="PartKeeprUser") */ +class User extends BaseEntity implements Serializable, Deserializable { + /** @ORM\Column(length=50,unique=true) */ + private $username; + + /** @ORM\Column(length=32) */ + private $password; + + /** @ORM\Column(type="boolean") */ + private $admin; + + /** @ORM\Column(type="datetime",nullable=true) */ + private $lastSeen; + + /** + * Creates a new user object. + * + * @param string $username The username to set (optional) + * @param string $password The password to set (optional) + */ + public function __construct ($username = null, $password = null) { + if ($username !== null) { + $this->setUsername($username); + } + + if ($password !== null) { + $this->setPassword($password); + } + + $this->setAdmin(false); + } + + /** + * Sets the username. + * + * @param string $username The username to set. + * @return nothing + */ + public function setUsername ($username) { + $this->username = $username; + } + + /** + * Sets the raw username, without replacing any special chars. + * + * This method should only be used for building a temporary user + * for login checks. + * + * @param string $username The raw username + * @return nothing + */ + public function setRawUsername ($username) { + $this->username = $username; + } + + /** + * Returns the username. + * @param none + * @return string The username + */ + public function getUsername () { + return $this->username; + } + + /** + * Sets the admin flag + * @param boolean $bAdmin True if the user is an admin, false otherwise + */ + public function setAdmin ($bAdmin) { + $this->admin = (boolean)$bAdmin; + } + + /** + * Returns the admin flag + * @return boolean True if the user is an admin + */ + public function isAdmin () { + return $this->admin; + } + + /** + * Sets the user's password. Automatically + * applies md5 hashing. + * + * @param string $password + */ + public function setPassword ($password) { + $this->setHashedPassword(md5($password)); + } + + /** + * Returns the user's md5-hashed password. + * @param none + * @return string The md5-hashed password + */ + public function getHashedPassword () { + return $this->password; + } + + /** + * Sets the user's password. Expects a hash + * and does not apply md5 hasing. + * + * @param string $hashedPassword + */ + public function setHashedPassword ($hashedPassword) { + $this->password = $hashedPassword; + } + + /** + * Compares the given un-hashed password with the + * object's hashed password. + * + * + * @param string $password The unhashed password + * @return boolean true if the passwords match, false otherwise + */ + public function comparePassword ($password) { + return $this->compareHashedPassword(md5($password)); + } + + /** + * Compares the given hashed password with the object's + * hashed password. + * + * @param string $hashedPassword The md5-hashed password + * @return boolean true if the passwords match, false otherwise + */ + public function compareHashedPassword ($hashedPassword) { + if ($hashedPassword == $this->password) { + return true; + } else { + return false; + } + } + + /** + * Updates the last seen field to the current time. + */ + public function updateSeen() { + $this->lastSeen = new \DateTime("now"); + } + + /** + * Retrieve the last seen flag for a user. + * @return \DateTime + */ + public function getLastSeen() { + return $this->lastSeen; + } + + /** + * Serializes the user object and returns it as array, suitable + * to process via json_encode. + * @param none + * @return array An array containing the object information + */ + public function serialize () { + return array( + "id" => $this->getId(), + "username" => $this->getUsername() + ); + } + + /** + * Deserializes the user + * @param array $parameters The array with the parameters to set + */ + public function deserialize (array $parameters) { + foreach ($parameters as $key => $value) { + switch ($key) { + case "username": + $this->setUsername($value); + break; + case "password": + if ($value !== "") { + $this->setPassword($value); + } + break; + } + } + } + + /** + * Sets a user preference + * + * @param string $preferenceKey The preference key + * @param string $preferenceValue The preference value + * @throws EntityNotPersistantException Thrown if the entity is not persistant + */ + public function setPreference ($preferenceKey, $preferenceValue) { + return UserPreference::setPreference($this, $preferenceKey, $preferenceValue); + } + + /** + * Returns a given preference object + * + * @param string $preferenceKey The preference key + * @return UserPreference The user preference object + * @throws UserPreferenceNotFoundException If the preference key was not found + * @throws EntityNotPersistantException Thrown if the entity is not persistant + */ + public function getPreference ($preferenceKey) { + return UserPreference::getPreference($this, $preferenceKey); + } + + /** + * Returns a given preference value + * + * @param string $preferenceKey The preference key + * @return UserPreference The user preference object + * @throws UserPreferenceNotFoundException If the preference key was not found + * @throws EntityNotPersistantException Thrown if the entity is not persistant + */ + public function getPreferenceValue ($preferenceKey) { + return UserPreference::getPreferenceValue($this, $preferenceKey); + } + + /** + * Deletes the given preference + * + * @param string $preferenceKey The preference key + * @return UserPreference The user preference object + * @throws UserPreferenceNotFoundException If the preference key was not found + * @throws EntityNotPersistantException Thrown if the entity is not persistant + */ + public function deletePreference ($preferenceKey) { + UserPreference::deletePreference($this, $preferenceKey); + } + + /** + * Returns all user preferences for this user + * + * @param none + * @return Array An array of UserPreference objects + * @throws EntityNotPersistantException Thrown if the entity is not persistant + */ + public function getPreferences () { + return UserPreference::getPreferences($this); + } + /** + * Loads a user by name. + * + * @param $username string The username to query + * @return User A user object + * @throws Doctrine\ORM\NoResultException If no user was found + */ + public static function loadByName ($username) { + $dql = "SELECT u FROM PartKeepr\AuthBundle\Entity\User\User u WHERE u.username = :username"; + + $query = PartKeepr::getEM()->createQuery($dql); + $query->setParameter("username", $username); + + return $query->getSingleResult(); + } +}+ \ No newline at end of file diff --git a/src/PartKeepr/AuthBundle/Entity/UserManager.php b/src/PartKeepr/AuthBundle/Entity/UserManager.php @@ -0,0 +1,102 @@ +<?php +namespace PartKeepr\AuthBundle\Entity; + +use PartKeepr\AuthBundle\Entity\User\Exceptions\InvalidLoginDataException; +use PartKeepr\AuthBundle\Entity\User\Exceptions\UserAlreadyExistsException; +use PartKeepr\AuthBundle\Entity\User\Exceptions\UserNotFoundException; +use PartKeepr\AuthBundle\Entity\User\User; +use PartKeepr\Category\CategoryManager; +use PartKeepr\Manager\AbstractManager; +use PartKeepr\PartKeepr; + +class UserManager extends AbstractManager { + public function getEntityName () { + return 'PartKeepr\AuthBundle\Entity\User\User'; + } + + public function getQueryFields () { + return array("id","username"); + } + + public function getDefaultSortField () { + return "username"; + } + + /** + * Checks if the passed user already exists. + * + * @param $username string The username to check + */ + public function userExists ($username) { + $dql = "SELECT COUNT(u) FROM PartKeepr\AuthBundle\Entity\User\User u WHERE u.username = :name"; + + $query = PartKeepr::getEM()->createQuery($dql); + $query->setParameter("name", $username); + + $count = $query->getSingleScalarResult(); + + if ($count == 0) { + return false; + } else { + return true; + } + } + + /** + * Creates the given user. Checks if the user already exists + * + * @param User $user The user to create + * @throws UserAlreadyExistsException + */ + public function createUser (User $user) { + if ($this->userExists($user->getUsername())) { + throw new UserAlreadyExistsException($user->getUsername()); + } + + PartKeepr::getEM()->persist($user); + PartKeepr::getEM()->flush(); + } + + /** + * Returns the user for a given user id + * @param integer $id The user id + */ + public function getUser ($id) { + return User::loadById($id); + } + + /** + * Deletes an user by id + * @param int $id The user's id + */ + public function deleteUser ($id) { + $user = User::loadById($id); + + PartKeepr::getEM()->remove($user); + PartKeepr::getEM()->flush(); + } + + /** + * Authenticates the given user. If successful, an instance + * of the user is returned. + * + * @param User $user The user to authenticate + * @throws InvalidLoginDataException Thrown if the user's credentials are not valid + */ + public function authenticate (User $user) { + $result = PartKeepr::getEM() + ->getRepository("PartKeepr\AuthBundle\Entity\User\User") + ->findOneBy( + array( + "username" => $user->getUsername(), + "password" => $user->getHashedPassword() + ) + ); + + if ($result == null) { + throw new InvalidLoginDataException(); + } else { + return $result; + } + } +}+ \ No newline at end of file diff --git a/src/PartKeepr/AuthBundle/PartKeeprAuthBundle.php b/src/PartKeepr/AuthBundle/PartKeeprAuthBundle.php @@ -0,0 +1,9 @@ +<?php + +namespace PartKeepr\AuthBundle; + +use Symfony\Component\HttpKernel\Bundle\Bundle; + +class PartKeeprAuthBundle extends Bundle +{ +} diff --git a/src/PartKeepr/AuthBundle/Resources/config/services.xml b/src/PartKeepr/AuthBundle/Resources/config/services.xml @@ -0,0 +1,16 @@ +<?xml version="1.0" ?> + +<container xmlns="http://symfony.com/schema/dic/services" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> + + <!-- + <services> + <service id="part_keepr_auth.example" class="PartKeepr\AuthBundle\Example"> + <argument type="service" id="service_id" /> + <argument>plain_value</argument> + <argument>%parameter_name%</argument> + </service> + </services> + --> +</container> diff --git a/src/PartKeepr/AuthBundle/Resources/doc/index.rst b/src/PartKeepr/AuthBundle/Resources/doc/index.rst diff --git a/src/PartKeepr/AuthBundle/Resources/translations/messages.fr.xlf b/src/PartKeepr/AuthBundle/Resources/translations/messages.fr.xlf @@ -0,0 +1,11 @@ +<?xml version="1.0"?> +<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2"> + <file source-language="en" datatype="plaintext" original="file.ext"> + <body> + <trans-unit id="1"> + <source>Symfony2 is great</source> + <target>J'aime Symfony2</target> + </trans-unit> + </body> + </file> +</xliff> diff --git a/src/PartKeepr/AuthBundle/Resources/views/Default/index.html.twig b/src/PartKeepr/AuthBundle/Resources/views/Default/index.html.twig @@ -0,0 +1 @@ +Hello {{ name }}! diff --git a/src/PartKeepr/AuthBundle/Tests/Controller/DefaultControllerTest.php b/src/PartKeepr/AuthBundle/Tests/Controller/DefaultControllerTest.php @@ -0,0 +1,17 @@ +<?php + +namespace PartKeepr\AuthBundle\Tests\Controller; + +use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; + +class DefaultControllerTest extends WebTestCase +{ + public function testIndex() + { + $client = static::createClient(); + + $crawler = $client->request('GET', '/hello/Fabien'); + + $this->assertTrue($crawler->filter('html:contains("Hello Fabien")')->count() > 0); + } +} diff --git a/src/PartKeepr/AuthBundle/Validator/Constraints/PasswordMD5Hash.php b/src/PartKeepr/AuthBundle/Validator/Constraints/PasswordMD5Hash.php @@ -0,0 +1,13 @@ +<?php +namespace PartKeepr\AuthBundle\Validator\Constraints; + +use Symfony\Component\Validator\Constraint; + +/** + * @Annotation + * @Target({"PROPERTY", "METHOD", "ANNOTATION"}) + */ +class PasswordMD5Hash extends Constraint +{ + public $message = "The password must be an MD5 hash of exactly 32 characters in length"; +}+ \ No newline at end of file diff --git a/src/PartKeepr/AuthBundle/Validator/Constraints/PasswordMD5HashValidator.php b/src/PartKeepr/AuthBundle/Validator/Constraints/PasswordMD5HashValidator.php @@ -0,0 +1,18 @@ +<?php +namespace PartKeepr\AuthBundle\Validator\Constraints; + +use Symfony\Component\Validator\Constraint; +use Symfony\Component\Validator\ConstraintValidator; + +class PasswordMD5HashValidator extends ConstraintValidator +{ + public function validate($value, Constraint $constraint) + { + if (!preg_match('/^[a-f0-9]{32}$/', $value, $matches)) { + // If you're using the new 2.5 validation API (you probably are!) + $this->context->buildViolation($constraint->message) + ->setParameter('%string%', $value) + ->addViolation(); + } + } +}+ \ No newline at end of file diff --git a/src/PartKeepr/AuthBundle/Validator/Constraints/Username.php b/src/PartKeepr/AuthBundle/Validator/Constraints/Username.php @@ -0,0 +1,13 @@ +<?php +namespace PartKeepr\AuthBundle\Validator\Constraints; + +use Symfony\Component\Validator\Constraint; + +/** + * @Annotation + * @Target({"PROPERTY", "METHOD", "ANNOTATION"}) + */ + +class Username extends Constraint { + public $message = "The username must be 3-50 characters in length and may only contain the following characters: a-z, A-Z, 0-9, an underscore (_), a backslash (\), a slash (/), a dot (.) or a dash (-)"; +}+ \ No newline at end of file diff --git a/src/PartKeepr/AuthBundle/Validator/Constraints/UsernameValidator.php b/src/PartKeepr/AuthBundle/Validator/Constraints/UsernameValidator.php @@ -0,0 +1,18 @@ +<?php +namespace PartKeepr\AuthBundle\Validator\Constraints; + +use Symfony\Component\Validator\Constraint; +use Symfony\Component\Validator\ConstraintValidator; + +class UsernameValidator extends ConstraintValidator +{ + public function validate($value, Constraint $constraint) + { + if (!preg_match('/^[a-zA-Za0-9.-_\/\\\]{3,50}$/', $value, $matches)) { + // If you're using the new 2.5 validation API (you probably are!) + $this->context->buildViolation($constraint->message) + ->setParameter('%string%', $value) + ->addViolation(); + } + } +}+ \ No newline at end of file diff --git a/src/PartKeepr/FrontendBundle/Controller/IndexController.php b/src/PartKeepr/FrontendBundle/Controller/IndexController.php @@ -2,14 +2,11 @@ namespace PartKeepr\FrontendBundle\Controller; +use PartKeepr\AuthBundle\Entity\User\User; +use PartKeepr\PartKeepr; +use PartKeepr\Session\SessionManager; +use PartKeepr\Util\Configuration; use Symfony\Bundle\FrameworkBundle\Controller\Controller; -use PartKeepr\User\User, - PartKeepr\Service\ServiceManager, - PartKeepr\PartKeepr, - PartKeepr\Session\SessionManager, - PartKeepr\Util\Configuration; - -use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; class IndexController extends Controller { diff --git a/src/PartKeepr/FrontendBundle/Resources/public/images/favicon.ico b/src/PartKeepr/FrontendBundle/Resources/public/images/favicon.ico Binary files differ. diff --git a/src/PartKeepr/FrontendBundle/Resources/public/js/Components/Session/SessionManager.js b/src/PartKeepr/FrontendBundle/Resources/public/js/Components/Session/SessionManager.js @@ -53,7 +53,7 @@ Ext.define("PartKeepr.SessionManager", { * @param password The password, as entered */ onLogin: function (username, password) { - var k = new PartKeepr.ServiceCall("Auth", "login"); + var k = new PartKeepr.ServiceCall("auth", "login"); k.setParameter("username", username); k.setParameter("password", md5(password)); @@ -67,6 +67,7 @@ Ext.define("PartKeepr.SessionManager", { * @param response The session ID */ onAfterLogin: function (response) { + console.log(response); this.setSession(response.sessionid); this.loginDialog.destroy(); diff --git a/src/PartKeepr/FrontendBundle/Resources/public/js/PartKeepr.js b/src/PartKeepr/FrontendBundle/Resources/public/js/PartKeepr.js @@ -630,7 +630,7 @@ PartKeepr.getApplication = function () { }; PartKeepr.getBasePath = function () { - return "rest.php"; + return "app_dev.php"; }; PartKeepr.getImagePath = function () { diff --git a/src/PartKeepr/FrontendBundle/Resources/public/js/Util/ServiceCall.js b/src/PartKeepr/FrontendBundle/Resources/public/js/Util/ServiceCall.js @@ -49,9 +49,11 @@ Ext.define('PartKeepr.ServiceCall', { doCall: function () { /* Update the status bar to indicate that the call is in progress. */ PartKeepr.getApplication().getStatusbar().startLoad(this.loadMessage); - + + this.parameters._format = "json"; + var callDefinition = Ext.encode(this.parameters); - + var headers = { "call": this.call, "lang": Ext.getLocale() @@ -66,13 +68,15 @@ Ext.define('PartKeepr.ServiceCall', { success: Ext.bind(this.onSuccess, this), failure: Ext.bind(this.onError, this), method: "POST", - params: callDefinition, + jsonData: this.parameters, headers: headers }); }, onSuccess: function (responseObj, options) { PartKeepr.getApplication().getStatusbar().endLoad(); - + + console.log(responseObj); + try { var response = Ext.decode(responseObj.responseText); } catch (ex) { @@ -91,7 +95,7 @@ Ext.define('PartKeepr.ServiceCall', { return; } - + /* Check the status */ if (response.status == "error") { this.displayError(response.exception); @@ -125,20 +129,21 @@ Ext.define('PartKeepr.ServiceCall', { if (this.sHandler) { - this.sHandler(response.response); + this.sHandler(response); } }, onError: function (response, options) { var request; - + + console.log(response); try { var data = Ext.decode(response.responseText); - + request = { response: response.responseText, request: Ext.encode(options) }; - + PartKeepr.ExceptionWindow.showException(data.exception, request); } catch (ex) { var exception = { @@ -146,7 +151,7 @@ Ext.define('PartKeepr.ServiceCall', { detail: i18n("The server returned a response which we were not able to interpret."), backtrace: response.responseText }; - + request = { response: response.responseText, request: Ext.encode(options) diff --git a/src/backend/PartKeepr/Auth/AuthService.php b/src/backend/PartKeepr/Auth/AuthService.php @@ -1,15 +1,11 @@ <?php namespace PartKeepr\Auth; -use PartKeepr\Service\AnonService, - PartKeepr\User\User, - PartKeepr\User\UserManager, - PartKeepr\User\Exceptions\InvalidLoginDataException, - PartKeepr\Session\SessionManager, - PartKeepr\Service\Annotations\ServiceParameter as ServiceParameter, - PartKeepr\Service\Annotations\ServiceCall as ServiceCall, - PartKeepr\Service\Annotations\ServiceReturnValue as ServiceReturnValue, - PartKeepr\Service\Annotations\Service as ServiceDescription; +use PartKeepr\AuthBundle\Entity\User\Exceptions\InvalidLoginDataException; +use PartKeepr\AuthBundle\Entity\User\User; +use PartKeepr\AuthBundle\Entity\User\UserManager; +use PartKeepr\Service\AnonService; +use PartKeepr\Session\SessionManager; /** diff --git a/src/backend/PartKeepr/Part/PartService.php b/src/backend/PartKeepr/Part/PartService.php @@ -1,16 +1,15 @@ <?php namespace PartKeepr\Part; -use PartKeepr\User\User, - PartKeepr\Service\RestfulService, - PartKeepr\Service\Service, - PartKeepr\Manager\ManagerFilter, - PartKeepr\Part\PartManager, - PartKeepr\Stock\StockEntry, - PartKeepr\PartKeepr, - PartKeepr\PartCategory\PartCategory, - PartKeepr\PartCategory\PartCategoryManager, - PartKeepr\Session\SessionManager; +use PartKeepr\AuthBundle\Entity\User\User; +use PartKeepr\Manager\ManagerFilter; +use PartKeepr\PartCategory\PartCategory; +use PartKeepr\PartCategory\PartCategoryManager; +use PartKeepr\PartKeepr; +use PartKeepr\Service\RestfulService; +use PartKeepr\Service\Service; +use PartKeepr\Session\SessionManager; +use PartKeepr\Stock\StockEntry; class PartService extends Service implements RestfulService { public function get () { diff --git a/src/backend/PartKeepr/PartKeepr.php b/src/backend/PartKeepr/PartKeepr.php @@ -1,10 +1,9 @@ <?php namespace PartKeepr; -use PartKeepr\SystemNotice\SystemNoticeManager, - Doctrine\ORM\EntityManager, - PartKeepr\Util\Configuration as PartKeeprConfiguration; - +use Doctrine\ORM\EntityManager; +use PartKeepr\SystemNotice\SystemNoticeManager; +use PartKeepr\Util\Configuration as PartKeeprConfiguration; class PartKeepr { @@ -149,7 +148,7 @@ class PartKeepr { */ public static function getEntityClasses () { return array( - 'PartKeepr\User\User', + 'PartKeepr\AuthBundle\Entity\User\User', 'PartKeepr\Session\Session', 'PartKeepr\EventNotification\Event', diff --git a/src/backend/PartKeepr/Printing/PrintingJob/PrintingJob.php b/src/backend/PartKeepr/Printing/PrintingJob/PrintingJob.php @@ -1,14 +1,13 @@ <?php namespace PartKeepr\Printing\PrintingJob; +use Doctrine\ORM\Mapping as ORM; +use PartKeepr\AuthBundle\Entity\User\User; use PartKeepr\EventNotification\EventManager; -use PartKeepr\Session\SessionManager, - PartKeepr\UploadedFile\TempUploadedFile, - PartKeepr\User\User, - PartKeepr\Util\BaseEntity, - PartKeepr\Util\Deserializable, - PartKeepr\Util\Serializable, - Doctrine\ORM\Mapping as ORM; +use PartKeepr\Session\SessionManager; +use PartKeepr\UploadedFile\TempUploadedFile; +use PartKeepr\Util\BaseEntity; +use PartKeepr\Util\Serializable; /** * This is a single job waiting for beeing processed. @@ -31,14 +30,14 @@ class PrintingJob extends BaseEntity implements Serializable { /** * This is the user which has created this printing job. - * @ORM\ManyToOne(targetEntity="PartKeepr\User\User") + * @ORM\ManyToOne(targetEntity="PartKeepr\AuthBundle\Entity\User\User") */ private $owner; /** * Target user the printing job is for. This is mostly the printer or output queue * which should be used to process this job. - * @ORM\ManyToOne(targetEntity="PartKeepr\User\User") + * @ORM\ManyToOne(targetEntity="PartKeepr\AuthBundle\Entity\User\User") */ private $target; diff --git a/src/backend/PartKeepr/Printing/PrintingService.php b/src/backend/PartKeepr/Printing/PrintingService.php @@ -1,20 +1,14 @@ <?php namespace PartKeepr\Printing; +use PartKeepr\AuthBundle\Entity\User\UserManager; +use PartKeepr\PartKeepr; +use PartKeepr\Printing\Exceptions\RendererNotFoundException; +use PartKeepr\Printing\PDFLabelRenderer; use PartKeepr\Printing\PrintingJob\PrintingJob; - -use PartKeepr\PartKeepr, - PartKeepr\Printing\Exceptions\InvalidArgumentException, - PartKeepr\Printing\Exceptions\RendererNotFoundException, - PartKeepr\Printing\PageBasicLayout\PageBasicLayoutManager, - PartKeepr\Printing\PDFLabelRenderer, - PartKeepr\Printing\PrintingJobConfiguration\PrintingJobConfigurationManager, - PartKeepr\Printing\Utils\DecodeConfiguration, - PartKeepr\Service\Service, - PartKeepr\StorageLocation\StorageLocation, - PartKeepr\UploadedFile\TempUploadedFile, - PartKeepr\User\UserManager, - PartKeepr\Util\Configuration as PartKeeprConfiguration; +use PartKeepr\Printing\PrintingJobConfiguration\PrintingJobConfigurationManager; +use PartKeepr\Service\Service; +use PartKeepr\UploadedFile\TempUploadedFile; /** * This service is the entry point for our printing/exporting diff --git a/src/backend/PartKeepr/Project/Project.php b/src/backend/PartKeepr/Project/Project.php @@ -1,11 +1,11 @@ <?php namespace PartKeepr\Project; -use PartKeepr\User\User, - PartKeepr\Util\Serializable, - PartKeepr\Util\Deserializable, - PartKeepr\Util\BaseEntity, - Doctrine\ORM\Mapping as ORM; +use Doctrine\ORM\Mapping as ORM; +use PartKeepr\AuthBundle\Entity\User\User; +use PartKeepr\Util\BaseEntity; +use PartKeepr\Util\Deserializable; +use PartKeepr\Util\Serializable; /** * Represents a part in the database. The heart of our project. Handle with care! @@ -19,7 +19,7 @@ class Project extends BaseEntity implements Serializable, Deserializable { /** * Specifies the user this project belongs to - * @ORM\ManyToOne(targetEntity="PartKeepr\User\User") + * @ORM\ManyToOne(targetEntity="PartKeepr\AuthBundle\Entity\User\User") */ private $user; diff --git a/src/backend/PartKeepr/Service/Service.php b/src/backend/PartKeepr/Service/Service.php @@ -1,10 +1,9 @@ <?php namespace PartKeepr\Service; -use PartKeepr\User\User, - PartKeepr\Session\Session, - PartKeepr\Session\SessionManager, - PartKeepr\Service\Exceptions\ServiceException; +use PartKeepr\AuthBundle\Entity\User\User; +use PartKeepr\Service\Exceptions\ServiceException; +use PartKeepr\Session\SessionManager; class Service { private $params; diff --git a/src/backend/PartKeepr/Service/ServiceManager.php b/src/backend/PartKeepr/Service/ServiceManager.php @@ -1,13 +1,13 @@ <?php namespace PartKeepr\Service; -use PartKeepr\Session\SessionManager, - PartKeepr\Service\Exceptions\ServiceException, - PartKeepr\Util\Singleton, - PartKeepr\PartKeepr, - PartKeepr\User\User, - PartKeepr\User\UserManager, - PartKeepr\REST\Request; +use PartKeepr\AuthBundle\Entity\User\User; +use PartKeepr\AuthBundle\Entity\User\UserManager; +use PartKeepr\PartKeepr; +use PartKeepr\REST\Request; +use PartKeepr\Service\Exceptions\ServiceException; +use PartKeepr\Session\SessionManager; +use PartKeepr\Util\Singleton; class ServiceManager extends Singleton { private $request = null; diff --git a/src/backend/PartKeepr/Session/Session.php b/src/backend/PartKeepr/Session/Session.php @@ -1,9 +1,9 @@ <?php namespace PartKeepr\Session; -use PartKeepr\User\User, - PartKeepr\PartKeepr, - Doctrine\ORM\Mapping as ORM; +use Doctrine\ORM\Mapping as ORM; +use PartKeepr\AuthBundle\Entity\User\User; +use PartKeepr\PartKeepr; /** @ORM\Entity */ class Session { @@ -17,7 +17,7 @@ class Session { private $sessionid; /** - * @ORM\ManyToOne(targetEntity="PartKeepr\User\User") + * @ORM\ManyToOne(targetEntity="PartKeepr\AuthBundle\Entity\User\User") */ private $user; diff --git a/src/backend/PartKeepr/Session/SessionManager.php b/src/backend/PartKeepr/Session/SessionManager.php @@ -1,10 +1,10 @@ <?php namespace PartKeepr\Session; -use PartKeepr\Util\Singleton, - PartKeepr\User\User, - PartKeepr\Session\Exceptions\SessionNotFoundException, - PartKeepr\PartKeepr; +use PartKeepr\AuthBundle\Entity\User\User; +use PartKeepr\PartKeepr; +use PartKeepr\Session\Exceptions\SessionNotFoundException; +use PartKeepr\Util\Singleton; class SessionManager extends Singleton { public static $currentSession = null; diff --git a/src/backend/PartKeepr/Setup/UserSetup.php b/src/backend/PartKeepr/Setup/UserSetup.php @@ -1,14 +1,14 @@ <?php namespace PartKeepr\Setup; -use PartKeepr\User\User; +use PartKeepr\AuthBundle\Entity\User\User; /** * Creates a new admin user, but only if no admin user exists. */ class UserSetup extends AbstractSetup { public function run () { - $dql = "SELECT COUNT(u) FROM PartKeepr\User\User u WHERE u.username = :username OR u.admin = :admin"; + $dql = "SELECT COUNT(u) FROM PartKeepr\AuthBundle\Entity\User\User u WHERE u.username = :username OR u.admin = :admin"; $query = $this->entityManager->createQuery($dql); $query->setParameter("username", "admin"); $query->setParameter("admin", true); diff --git a/src/backend/PartKeepr/Stock/StockEntry.php b/src/backend/PartKeepr/Stock/StockEntry.php @@ -1,13 +1,13 @@ <?php namespace PartKeepr\Stock; -use PartKeepr\Part\Part, - PartKeepr\User\User, - PartKeepr\PartKeepr, - PartKeepr\Util\BaseEntity, - PartKeepr\Util\Serializable, - Doctrine\ORM\Mapping as ORM, - Doctrine\ORM\Mapping\HasLifecycleCallbacks; +use Doctrine\ORM\Mapping as ORM; +use Doctrine\ORM\Mapping\HasLifecycleCallbacks; +use PartKeepr\AuthBundle\Entity\User\User; +use PartKeepr\Part\Part; +use PartKeepr\PartKeepr; +use PartKeepr\Util\BaseEntity; +use PartKeepr\Util\Serializable; /** @ORM\Entity @ORM\HasLifecycleCallbacks **/ class StockEntry extends BaseEntity implements Serializable { @@ -22,7 +22,7 @@ class StockEntry extends BaseEntity implements Serializable { private $part; /** - * @ORM\ManyToOne(targetEntity="PartKeepr\User\User") + * @ORM\ManyToOne(targetEntity="PartKeepr\AuthBundle\Entity\User\User") */ private $user; diff --git a/src/backend/PartKeepr/Stock/StockService.php b/src/backend/PartKeepr/Stock/StockService.php @@ -1,13 +1,12 @@ <?php namespace PartKeepr\Stock; -use PartKeepr\Stock\StockEntry, - PartKeepr\PartKeepr, - PartKeepr\User\User, - PartKeepr\Manager\ManagerFilter, - PartKeepr\Session\SessionManager, - PartKeepr\Service\RestfulService, - PartKeepr\Service\Service; +use PartKeepr\AuthBundle\Entity\User\User; +use PartKeepr\Manager\ManagerFilter; +use PartKeepr\PartKeepr; +use PartKeepr\Service\RestfulService; +use PartKeepr\Service\Service; +use PartKeepr\Session\SessionManager; class StockService extends Service implements RestfulService { /** diff --git a/src/backend/PartKeepr/TipOfTheDay/TipOfTheDayHistory.php b/src/backend/PartKeepr/TipOfTheDay/TipOfTheDayHistory.php @@ -1,16 +1,9 @@ <?php namespace PartKeepr\TipOfTheDay; -use PartKeepr\User\User; - -use PartKeepr\Util\Serializable; - -use PartKeepr\PartKeepr; - -use PartKeepr\Util\Configuration; - -use PartKeepr\Util\BaseEntity, - Doctrine\ORM\Mapping as ORM; +use Doctrine\ORM\Mapping as ORM; +use PartKeepr\AuthBundle\Entity\User\User; +use PartKeepr\Util\BaseEntity; /** * Represents a tip of the day history entry. @@ -28,7 +21,7 @@ class TipOfTheDayHistory extends BaseEntity { /** * Defines the user - * @ORM\ManyToOne(targetEntity="PartKeepr\User\User") + * @ORM\ManyToOne(targetEntity="PartKeepr\AuthBundle\Entity\User\User") * @var StorageLocation */ private $user; diff --git a/src/backend/PartKeepr/TipOfTheDay/TipOfTheDayService.php b/src/backend/PartKeepr/TipOfTheDay/TipOfTheDayService.php @@ -1,12 +1,11 @@ <?php namespace PartKeepr\TipOfTheDay; -use PartKeepr\Util\Configuration, - PartKeepr\User\User, - PartKeepr\Service\RestfulService, - PartKeepr\Session\SessionManager, - PartKeepr\Service\Service, - PartKeepr\PartKeepr; +use PartKeepr\PartKeepr; +use PartKeepr\Service\RestfulService; +use PartKeepr\Service\Service; +use PartKeepr\Session\SessionManager; +use PartKeepr\Util\Configuration; class TipOfTheDayService extends Service implements RestfulService { /** diff --git a/src/backend/PartKeepr/User/Exceptions/InvalidLoginDataException.php b/src/backend/PartKeepr/User/Exceptions/InvalidLoginDataException.php @@ -1,14 +0,0 @@ -<?php -namespace PartKeepr\User\Exceptions; - -use PartKeepr\Util\SerializableException, - PartKeepr\PartKeepr; - -/** - * Is thrown when the user has given wrong credentials. - */ -class InvalidLoginDataException extends SerializableException { - public function __construct () { - parent::__construct(PartKeepr::i18n("Username or Password wrong.")); - } -}- \ No newline at end of file diff --git a/src/backend/PartKeepr/User/Exceptions/UserAlreadyExistsException.php b/src/backend/PartKeepr/User/Exceptions/UserAlreadyExistsException.php @@ -1,19 +0,0 @@ -<?php -namespace PartKeepr\User\Exceptions; - -use PartKeepr\Util\SerializableException, - PartKeepr\PartKeepr; - -/** -* Is thrown when the user already exists. This usually happens -* if someone tries to create a user with the same name of an existing -* user. -*/ -class UserAlreadyExistsException extends SerializableException { - public function __construct ($username) { - parent::__construct( - sprintf( - PartKeepr::i18n("User %s already exists."), - $username)); - } -}- \ No newline at end of file diff --git a/src/backend/PartKeepr/User/Exceptions/UserDoesNotExistException.php b/src/backend/PartKeepr/User/Exceptions/UserDoesNotExistException.php @@ -1,17 +0,0 @@ -<?php -namespace PartKeepr\User\Exceptions; - -use PartKeepr\Util\SerializableException, - PartKeepr\PartKeepr; - -/** -* Is thrown when the user doesn't exist. -*/ -class UserDoesNotExistException extends SerializableException { - public function __construct ($username) { - parent::__construct( - sprintf( - PartKeepr::i18n("The user %s doesn't exist. Maybe the user was already deleted."), - $username)); - } -}- \ No newline at end of file diff --git a/src/backend/PartKeepr/User/User.php b/src/backend/PartKeepr/User/User.php @@ -1,268 +0,0 @@ -<?php -namespace PartKeepr\User; - -use PartKeepr\UserPreference\UserPreference; - -use PartKeepr\Util\Deserializable, - PartKeepr\Util\Serializable, - PartKeepr\Util\BaseEntity, - PartKeepr\PartKeepr, - Doctrine\ORM\Mapping as ORM, - Doctrine\ORM\Mapping\Table; - -/** @ORM\Entity @ORM\Table(name="PartKeeprUser") */ -class User extends BaseEntity implements Serializable, Deserializable { - /** @ORM\Column(length=50,unique=true) */ - private $username; - - /** @ORM\Column(length=32) */ - private $password; - - /** @ORM\Column(type="boolean") */ - private $admin; - - /** @ORM\Column(type="datetime",nullable=true) */ - private $lastSeen; - - /** - * Creates a new user object. - * - * @param string $username The username to set (optional) - * @param string $password The password to set (optional) - */ - public function __construct ($username = null, $password = null) { - if ($username !== null) { - $this->setUsername($username); - } - - if ($password !== null) { - $this->setPassword($password); - } - - $this->setAdmin(false); - } - - /** - * Sets the username. - * - * @param string $username The username to set. - * @return nothing - */ - public function setUsername ($username) { - $this->username = $username; - } - - /** - * Sets the raw username, without replacing any special chars. - * - * This method should only be used for building a temporary user - * for login checks. - * - * @param string $username The raw username - * @return nothing - */ - public function setRawUsername ($username) { - $this->username = $username; - } - - /** - * Returns the username. - * @param none - * @return string The username - */ - public function getUsername () { - return $this->username; - } - - /** - * Sets the admin flag - * @param boolean $bAdmin True if the user is an admin, false otherwise - */ - public function setAdmin ($bAdmin) { - $this->admin = (boolean)$bAdmin; - } - - /** - * Returns the admin flag - * @return boolean True if the user is an admin - */ - public function isAdmin () { - return $this->admin; - } - - /** - * Sets the user's password. Automatically - * applies md5 hashing. - * - * @param string $password - */ - public function setPassword ($password) { - $this->setHashedPassword(md5($password)); - } - - /** - * Returns the user's md5-hashed password. - * @param none - * @return string The md5-hashed password - */ - public function getHashedPassword () { - return $this->password; - } - - /** - * Sets the user's password. Expects a hash - * and does not apply md5 hasing. - * - * @param string $hashedPassword - */ - public function setHashedPassword ($hashedPassword) { - $this->password = $hashedPassword; - } - - /** - * Compares the given un-hashed password with the - * object's hashed password. - * - * - * @param string $password The unhashed password - * @return boolean true if the passwords match, false otherwise - */ - public function comparePassword ($password) { - return $this->compareHashedPassword(md5($password)); - } - - /** - * Compares the given hashed password with the object's - * hashed password. - * - * @param string $hashedPassword The md5-hashed password - * @return boolean true if the passwords match, false otherwise - */ - public function compareHashedPassword ($hashedPassword) { - if ($hashedPassword == $this->password) { - return true; - } else { - return false; - } - } - - /** - * Updates the last seen field to the current time. - */ - public function updateSeen() { - $this->lastSeen = new \DateTime("now"); - } - - /** - * Retrieve the last seen flag for a user. - * @return \DateTime - */ - public function getLastSeen() { - return $this->lastSeen; - } - - /** - * Serializes the user object and returns it as array, suitable - * to process via json_encode. - * @param none - * @return array An array containing the object information - */ - public function serialize () { - return array( - "id" => $this->getId(), - "username" => $this->getUsername() - ); - } - - /** - * Deserializes the user - * @param array $parameters The array with the parameters to set - */ - public function deserialize (array $parameters) { - foreach ($parameters as $key => $value) { - switch ($key) { - case "username": - $this->setUsername($value); - break; - case "password": - if ($value !== "") { - $this->setPassword($value); - } - break; - } - } - } - - /** - * Sets a user preference - * - * @param string $preferenceKey The preference key - * @param string $preferenceValue The preference value - * @throws EntityNotPersistantException Thrown if the entity is not persistant - */ - public function setPreference ($preferenceKey, $preferenceValue) { - return UserPreference::setPreference($this, $preferenceKey, $preferenceValue); - } - - /** - * Returns a given preference object - * - * @param string $preferenceKey The preference key - * @return UserPreference The user preference object - * @throws UserPreferenceNotFoundException If the preference key was not found - * @throws EntityNotPersistantException Thrown if the entity is not persistant - */ - public function getPreference ($preferenceKey) { - return UserPreference::getPreference($this, $preferenceKey); - } - - /** - * Returns a given preference value - * - * @param string $preferenceKey The preference key - * @return UserPreference The user preference object - * @throws UserPreferenceNotFoundException If the preference key was not found - * @throws EntityNotPersistantException Thrown if the entity is not persistant - */ - public function getPreferenceValue ($preferenceKey) { - return UserPreference::getPreferenceValue($this, $preferenceKey); - } - - /** - * Deletes the given preference - * - * @param string $preferenceKey The preference key - * @return UserPreference The user preference object - * @throws UserPreferenceNotFoundException If the preference key was not found - * @throws EntityNotPersistantException Thrown if the entity is not persistant - */ - public function deletePreference ($preferenceKey) { - UserPreference::deletePreference($this, $preferenceKey); - } - - /** - * Returns all user preferences for this user - * - * @param none - * @return Array An array of UserPreference objects - * @throws EntityNotPersistantException Thrown if the entity is not persistant - */ - public function getPreferences () { - return UserPreference::getPreferences($this); - } - /** - * Loads a user by name. - * - * @param $username string The username to query - * @return User A user object - * @throws Doctrine\ORM\NoResultException If no user was found - */ - public static function loadByName ($username) { - $dql = "SELECT u FROM PartKeepr\User\User u WHERE u.username = :username"; - - $query = PartKeepr::getEM()->createQuery($dql); - $query->setParameter("username", $username); - - return $query->getSingleResult(); - } -}- \ No newline at end of file diff --git a/src/backend/PartKeepr/User/UserManager.php b/src/backend/PartKeepr/User/UserManager.php @@ -1,103 +0,0 @@ -<?php -namespace PartKeepr\User; - -use - PartKeepr\Category\CategoryManager, - PartKeepr\Manager\AbstractManager, - PartKeepr\PartKeepr, - PartKeepr\User\Exceptions\InvalidLoginDataException, - PartKeepr\User\Exceptions\UserAlreadyExistsException, - PartKeepr\User\Exceptions\UserNotFoundException, - PartKeepr\User\User; - -class UserManager extends AbstractManager { - public function getEntityName () { - return 'PartKeepr\User\User'; - } - - public function getQueryFields () { - return array("id","username"); - } - - public function getDefaultSortField () { - return "username"; - } - - /** - * Checks if the passed user already exists. - * - * @param $username string The username to check - */ - public function userExists ($username) { - $dql = "SELECT COUNT(u) FROM PartKeepr\User\User u WHERE u.username = :name"; - - $query = PartKeepr::getEM()->createQuery($dql); - $query->setParameter("name", $username); - - $count = $query->getSingleScalarResult(); - - if ($count == 0) { - return false; - } else { - return true; - } - } - - /** - * Creates the given user. Checks if the user already exists - * - * @param User $user The user to create - * @throws UserAlreadyExistsException - */ - public function createUser (User $user) { - if ($this->userExists($user->getUsername())) { - throw new UserAlreadyExistsException($user->getUsername()); - } - - PartKeepr::getEM()->persist($user); - PartKeepr::getEM()->flush(); - } - - /** - * Returns the user for a given user id - * @param integer $id The user id - */ - public function getUser ($id) { - return User::loadById($id); - } - - /** - * Deletes an user by id - * @param int $id The user's id - */ - public function deleteUser ($id) { - $user = User::loadById($id); - - PartKeepr::getEM()->remove($user); - PartKeepr::getEM()->flush(); - } - - /** - * Authenticates the given user. If successful, an instance - * of the user is returned. - * - * @param User $user The user to authenticate - * @throws InvalidLoginDataException Thrown if the user's credentials are not valid - */ - public function authenticate (User $user) { - $result = PartKeepr::getEM() - ->getRepository("PartKeepr\User\User") - ->findOneBy( - array( - "username" => $user->getUsername(), - "password" => $user->getHashedPassword() - ) - ); - - if ($result == null) { - throw new InvalidLoginDataException(); - } else { - return $result; - } - } -}- \ No newline at end of file diff --git a/src/backend/PartKeepr/User/UserService.php b/src/backend/PartKeepr/User/UserService.php @@ -1,14 +1,12 @@ <?php -namespace PartKeepr\User; +namespace PartKeepr\AuthBundle\Entity\User; -use - PartKeepr\PartKeepr, - PartKeepr\Service\FilterExtractor, - PartKeepr\Manager\ManagerFilter, - PartKeepr\Service\RestfulService, - PartKeepr\Service\Service, - PartKeepr\Session\SessionManager, - PartKeepr\User\User; +use PartKeepr\Manager\ManagerFilter; +use PartKeepr\PartKeepr; +use PartKeepr\Service\FilterExtractor; +use PartKeepr\Service\RestfulService; +use PartKeepr\Service\Service; +use PartKeepr\Session\SessionManager; class UserService extends Service implements RestfulService { diff --git a/src/backend/PartKeepr/UserPreference/Exceptions/UserPreferenceNotFoundException.php b/src/backend/PartKeepr/UserPreference/Exceptions/UserPreferenceNotFoundException.php @@ -1,9 +1,9 @@ <?php namespace PartKeepr\UserPreference\Exceptions; -use PartKeepr\User\User, - PartKeepr\Util\SerializableException, - PartKeepr\PartKeepr; +use PartKeepr\AuthBundle\Entity\User\User; +use PartKeepr\PartKeepr; +use PartKeepr\Util\SerializableException; /** * Is thrown when the user has given wrong credentials. diff --git a/src/backend/PartKeepr/UserPreference/UserPreference.php b/src/backend/PartKeepr/UserPreference/UserPreference.php @@ -1,19 +1,15 @@ <?php namespace PartKeepr\UserPreference; -use PartKeepr\Util\Serializable, - PartKeepr\PartKeepr, - PartKeepr\User\User, - PartKeepr\Util\Configuration, - PartKeepr\Util\BaseEntity, - PartKeepr\UserPreference\Exceptions\UserPreferenceNotFoundException, - PartKeepr\Util\Exceptions\EntityNotPersistantException, - Doctrine\ORM\NoResultException, - PartKeepr\Service\Annotations\ApiType, - PartKeepr\Service\Annotations\Service, - PartKeepr\Service\Annotations\ApiTypeOutput, - PartKeepr\Service\Annotations\ApiTypeOutputs, - Doctrine\ORM\Mapping as ORM; +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; /** * Represents a user preference entry. @@ -45,7 +41,7 @@ class UserPreference implements Serializable { /** * Defines the user - * @ORM\ManyToOne(targetEntity="PartKeepr\User\User") + * @ORM\ManyToOne(targetEntity="PartKeepr\AuthBundle\Entity\User\User") * @ORM\Id * @var User */ @@ -62,7 +58,8 @@ class UserPreference implements Serializable { /** * Returns the user associated with this entry - * @return \PartKeepr\User\User + * +*@return \PartKeepr\AuthBundle\Entity\User\User */ public function getUser () { return $this->user; diff --git a/src/backend/PartKeepr/UserPreference/UserPreferenceService.php b/src/backend/PartKeepr/UserPreference/UserPreferenceService.php @@ -1,12 +1,12 @@ <?php namespace PartKeepr\UserPreference; -use PartKeepr\User\User, - PartKeepr\Util\Configuration, - PartKeepr\Service\RestfulService, - PartKeepr\Session\SessionManager, - PartKeepr\Service\Service, - PartKeepr\PartKeepr; +use PartKeepr\AuthBundle\Entity\User\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, diff --git a/src/frontend/index.php b/src/frontend/index.php @@ -1,13 +1,7 @@ <?php namespace PartKeepr\Frontend; -use PartKeepr\User\User, - PartKeepr\Service\ServiceManager, - PartKeepr\PartKeepr, - PartKeepr\Session\SessionManager, - PartKeepr\Util\Configuration, - Symfony\Component\ClassLoader\ApcClassLoader, - Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Request; include("../src/backend/PartKeepr/PartKeepr.php"); diff --git a/tests/Auth/UserTest.php b/tests/Auth/UserTest.php @@ -1,7 +1,7 @@ <?php namespace PartKeepr\Tests\Auth; -use PartKeepr\User\User; +use PartKeepr\AuthBundle\Entity\User\User; class UserTest extends \PHPUnit_Framework_TestCase { public function testBasics () { diff --git a/tests/User/UserTest.php b/tests/User/UserTest.php @@ -1,9 +1,9 @@ <?php namespace PartKeepr\Tests\User; -use PartKeepr\User\UserManager, - PartKeepr\PartKeepr, - PartKeepr\User\User; +use PartKeepr\AuthBundle\Entity\User\User; +use PartKeepr\AuthBundle\Entity\User\UserManager; +use PartKeepr\PartKeepr; class UserTest extends \PHPUnit_Framework_TestCase { protected $backupGlobals = false; diff --git a/tests/UserPreference/UserPreferenceTest.php b/tests/UserPreference/UserPreferenceTest.php @@ -1,9 +1,9 @@ <?php namespace PartKeepr\Tests\UserPreference; -use PartKeepr\PartKeepr, - PartKeepr\User\User, - PartKeepr\UserPreference\UserPreference; +use PartKeepr\AuthBundle\Entity\User\User; +use PartKeepr\PartKeepr; +use PartKeepr\UserPreference\UserPreference; class UserPreferenceTest extends \PHPUnit_Framework_TestCase { protected $user; diff --git a/tests/bootstrap.php b/tests/bootstrap.php @@ -1,9 +1,9 @@ <?php namespace PartKeepr\Tests; -use PartKeepr\PartCategory\PartCategoryManager, - PartKeepr\PartKeepr, - PartKeepr\User\User; +use PartKeepr\AuthBundle\Entity\User\User; +use PartKeepr\PartCategory\PartCategoryManager; +use PartKeepr\PartKeepr; include(dirname(__DIR__). "/src/backend/PartKeepr/PartKeepr.php");