partkeepr

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

commit ede383802049e0576edc24200b701f46fb368b15
parent e82a3190acd077a6b0fc84ec95984ca083d104b3
Author: Felicitus <felicitus@felicitus.org>
Date:   Thu, 16 Jun 2011 09:54:48 +0800

Refactured the user service and added a new base entity class which knows about the field "id" as well as "loadById" and "getId".

Diffstat:
Dsrc/de/RaumZeitLabor/PartKeepr/Auth/AuthManager.php | 14--------------
Msrc/de/RaumZeitLabor/PartKeepr/Auth/AuthService.php | 14++++++++------
Dsrc/de/RaumZeitLabor/PartKeepr/Auth/Exceptions/InvalidLoginDataException.php | 13-------------
Dsrc/de/RaumZeitLabor/PartKeepr/Auth/Exceptions/UserAlreadyExistsException.php | 13-------------
Dsrc/de/RaumZeitLabor/PartKeepr/Auth/Exceptions/UserDoesNotExistException.php | 13-------------
Dsrc/de/RaumZeitLabor/PartKeepr/Auth/User.php | 184-------------------------------------------------------------------------------
Dsrc/de/RaumZeitLabor/PartKeepr/Auth/UserManager.php | 58----------------------------------------------------------
Dsrc/de/RaumZeitLabor/PartKeepr/Auth/UserManagerService.php | 21---------------------
Msrc/de/RaumZeitLabor/PartKeepr/PartKeepr.php | 2+-
Msrc/de/RaumZeitLabor/PartKeepr/Session/Session.php | 4++--
Msrc/de/RaumZeitLabor/PartKeepr/Session/SessionManager.php | 2+-
Msrc/de/RaumZeitLabor/PartKeepr/Stock/StockEntry.php | 4++--
Asrc/de/RaumZeitLabor/PartKeepr/User/Exceptions/InvalidLoginDataException.php | 15+++++++++++++++
Asrc/de/RaumZeitLabor/PartKeepr/User/Exceptions/UserAlreadyExistsException.php | 20++++++++++++++++++++
Asrc/de/RaumZeitLabor/PartKeepr/User/Exceptions/UserDoesNotExistException.php | 18++++++++++++++++++
Asrc/de/RaumZeitLabor/PartKeepr/User/User.php | 162+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/de/RaumZeitLabor/PartKeepr/User/UserManager.php | 91++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------
Msrc/de/RaumZeitLabor/PartKeepr/User/UserService.php | 33+++++++++++++++++++++++++++++----
Asrc/de/RaumZeitLabor/PartKeepr/Util/BaseEntity.php | 38++++++++++++++++++++++++++++++++++++++
Asrc/de/RaumZeitLabor/PartKeepr/Util/Exceptions/EntityNotFoundException.php | 17+++++++++++++++++
Mtesting/SetupDatabase.php | 4++--
21 files changed, 396 insertions(+), 344 deletions(-)

diff --git a/src/de/RaumZeitLabor/PartKeepr/Auth/AuthManager.php b/src/de/RaumZeitLabor/PartKeepr/Auth/AuthManager.php @@ -1,13 +0,0 @@ -<?php -namespace de\RaumZeitLabor\PartKeepr\Auth; - -use de\RaumZeitLabor\PartKeepr\Util\Singleton; - -class AuthManager extends Singleton { - public function login () { - - } - - -} -?>- \ No newline at end of file diff --git a/src/de/RaumZeitLabor/PartKeepr/Auth/AuthService.php b/src/de/RaumZeitLabor/PartKeepr/Auth/AuthService.php @@ -3,11 +3,17 @@ namespace de\raumzeitlabor\PartKeepr\Auth; declare(encoding = 'UTF-8'); use de\RaumZeitLabor\PartKeepr\Service\AnonService, - de\RaumZeitLabor\PartKeepr\Auth\User, - de\RaumZeitLabor\PartKeepr\Auth\UserManager, + de\RaumZeitLabor\PartKeepr\User\User, + de\RaumZeitLabor\PartKeepr\User\UserManager, de\RaumZeitLabor\PartKeepr\Session\SessionManager; class AuthService extends AnonService { + /** + * Logs in the given user. If the login was successful, + * a session is automatically started. + * + * @throws InvalidLoginDataException + */ public function login () { $this->requireParameter("username"); $this->requireParameter("password"); @@ -30,7 +36,4 @@ class AuthService extends AnonService { } - - } -?>- \ No newline at end of file diff --git a/src/de/RaumZeitLabor/PartKeepr/Auth/Exceptions/InvalidLoginDataException.php b/src/de/RaumZeitLabor/PartKeepr/Auth/Exceptions/InvalidLoginDataException.php @@ -1,12 +0,0 @@ -<?php -namespace de\RaumZeitLabor\PartKeepr\Auth\Exceptions; - -use de\RaumZeitLabor\PartKeepr\Util\SerializableException, - de\RaumZeitLabor\PartKeepr\PartKeepr; - -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/de/RaumZeitLabor/PartKeepr/Auth/Exceptions/UserAlreadyExistsException.php b/src/de/RaumZeitLabor/PartKeepr/Auth/Exceptions/UserAlreadyExistsException.php @@ -1,12 +0,0 @@ -<?php -namespace de\RaumZeitLabor\PartKeepr\Auth\Exceptions; - -use de\RaumZeitLabor\PartKeepr\Util\SerializableException, - de\RaumZeitLabor\PartKeepr\PartKeepr; - -class UserAlreadyExistsException extends SerializableException { - public function __construct () { - parent::__construct(PartKeepr::i18n("User already exists.")); - } -} -?>- \ No newline at end of file diff --git a/src/de/RaumZeitLabor/PartKeepr/Auth/Exceptions/UserDoesNotExistException.php b/src/de/RaumZeitLabor/PartKeepr/Auth/Exceptions/UserDoesNotExistException.php @@ -1,12 +0,0 @@ -<?php -namespace de\RaumZeitLabor\PartKeepr\Auth\Exceptions; - -use de\RaumZeitLabor\PartKeepr\Util\SerializableException, - de\RaumZeitLabor\PartKeepr\PartKeepr; - -class UserDoesNotExistException extends SerializableException { - public function __construct () { - parent::__construct(PartKeepr::i18n("The given user doesn't exist. Maybe the user was already deleted.")); - } -} -?>- \ No newline at end of file diff --git a/src/de/RaumZeitLabor/PartKeepr/Auth/User.php b/src/de/RaumZeitLabor/PartKeepr/Auth/User.php @@ -1,183 +0,0 @@ -<?php -namespace de\RaumZeitLabor\PartKeepr\Auth; -declare(encoding = 'UTF-8'); - -/** @Entity @Table(name="PartKeeprUser") */ - -class User { - /** - * @Id @Column(type="integer") - * @GeneratedValue(strategy="AUTO") - * @var unknown_type - */ - private $id; - - /** @Column(length=50,unique=true) */ - private $username; - - /** @Column(length=32) */ - private $password; - - /** @Column(type="boolean") */ - private $admin; - - - /** - * 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. - * - * Forces the username to have - * lowercase a-z characters. - * - * Replaces space with an underscore. - * - * @param string $username The username to set. Applies automatic username modification. - * @return nothing - */ - public function setUsername ($username) { - $username = strtolower($username); - - - preg_match_all("/[a-z\s]+/", $username, $out); - $username = implode('', $out[0]); - - $username = str_replace(" ", "_", $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; - } - } - - /** - * Returns the ID of this object. - * @param none - * @return int The ID of this object - */ - public function getId () { - return $this->id; - } - - /** - * 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() - ); - } - -} -?>- \ No newline at end of file diff --git a/src/de/RaumZeitLabor/PartKeepr/Auth/UserManager.php b/src/de/RaumZeitLabor/PartKeepr/Auth/UserManager.php @@ -1,57 +0,0 @@ -<?php -namespace de\raumzeitlabor\PartKeepr\Auth; -declare(encoding = 'UTF-8'); - -use de\RaumZeitLabor\PartKeepr\Util\Singleton, - de\RaumZeitLabor\PartKeepr\Auth\User, - de\RaumZeitLabor\PartKeepr\PartKeepr, - de\RaumZeitLabor\PartKeepr\Auth\Exceptions\UserAlreadyExistsException, - de\RaumZeitLabor\PartKeepr\Auth\Exceptions\UserDoesNotExistException, - de\RaumZeitLabor\PartKeepr\Auth\Exceptions\InvalidLoginDataException; - -class UserManager extends Singleton { - - public function createUser (User $user) { - if ($this->userExists($user)) { - throw new UserAlreadyExistsException($user->getUsername()); - } - - PartKeepr::getEM()->persist($user); - } - - public function userExists (User $user) { - $user = PartKeepr::getEM()->find("de\RaumZeitLabor\PartKeepr\Auth\User", $user->getUsername()); - - if ($user === null) { - return false; - } else { - return true; - } - } - - public function deleteUser (User $user) { - if (!$this->userExists($user)) { - throw new UserDoesNotExistException($user->getUsername()); - } - PartKeepr::getEM()->remove($user); - } - - public function authenticate (User $user) { - $result = PartKeepr::getEM() - ->getRepository("de\RaumZeitLabor\PartKeepr\Auth\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/de/RaumZeitLabor/PartKeepr/Auth/UserManagerService.php b/src/de/RaumZeitLabor/PartKeepr/Auth/UserManagerService.php @@ -1,20 +0,0 @@ -<?php -namespace de\raumzeitlabor\PartKeepr\Auth; -declare(encoding = 'UTF-8'); - -use de\RaumZeitLabor\PartKeepr\Service\Service; -use de\RaumZeitLabor\PartKeepr\Auth\UserManager; - -class UserManagerService extends Service { - public function createUser () { - $this->requireParameter("username"); - $this->requireParameter("password"); - - $user = new User(); - $user->setUsername($this->getParameter("username")); - $user->setHashedPassword($this->getParameter("password")); - - UserManager::getInstance()->createUser($user); - } -} -?>- \ No newline at end of file diff --git a/src/de/RaumZeitLabor/PartKeepr/PartKeepr.php b/src/de/RaumZeitLabor/PartKeepr/PartKeepr.php @@ -170,7 +170,7 @@ class PartKeepr { */ public static function getEntityClasses () { return array( - 'de\RaumZeitLabor\PartKeepr\Auth\User', + 'de\RaumZeitLabor\PartKeepr\User\User', 'de\RaumZeitLabor\PartKeepr\Session\Session', 'de\RaumZeitLabor\PartKeepr\Footprint\Footprint', 'de\RaumZeitLabor\PartKeepr\Category\Category', diff --git a/src/de/RaumZeitLabor/PartKeepr/Session/Session.php b/src/de/RaumZeitLabor/PartKeepr/Session/Session.php @@ -2,7 +2,7 @@ namespace de\RaumZeitLabor\PartKeepr\Session; declare(encoding = 'UTF-8'); -use de\RaumZeitLabor\PartKeepr\Auth\User; +use de\RaumZeitLabor\PartKeepr\User\User; /** @Entity */ class Session { @@ -16,7 +16,7 @@ class Session { private $sessionid; /** - * @ManyToOne(targetEntity="de\RaumZeitLabor\PartKeepr\Auth\User") + * @ManyToOne(targetEntity="de\RaumZeitLabor\PartKeepr\User\User") */ private $user; diff --git a/src/de/RaumZeitLabor/PartKeepr/Session/SessionManager.php b/src/de/RaumZeitLabor/PartKeepr/Session/SessionManager.php @@ -3,7 +3,7 @@ namespace de\RaumZeitLabor\PartKeepr\Session; declare(encoding = 'UTF-8'); use de\RaumZeitLabor\PartKeepr\Util\Singleton, - de\RaumZeitLabor\PartKeepr\Auth\User, + de\RaumZeitLabor\PartKeepr\User\User, de\RaumZeitLabor\PartKeepr\Session\Exceptions\SessionNotFoundException, de\RaumZeitLabor\PartKeepr\PartKeepr; diff --git a/src/de/RaumZeitLabor/PartKeepr/Stock/StockEntry.php b/src/de/RaumZeitLabor/PartKeepr/Stock/StockEntry.php @@ -3,7 +3,7 @@ namespace de\RaumZeitLabor\PartKeepr\Stock; declare(encoding = 'UTF-8'); use de\RaumZeitLabor\PartKeepr\Part\Part; -use de\RaumZeitLabor\PartKeepr\Auth\User; +use de\RaumZeitLabor\PartKeepr\User\User; use de\RaumZeitLabor\PartKeepr\PartKeepr; /** @Entity @HasLifecycleCallbacks **/ @@ -27,7 +27,7 @@ class StockEntry { private $part; /** - * @ManyToOne(targetEntity="de\RaumZeitLabor\PartKeepr\Auth\User") + * @ManyToOne(targetEntity="de\RaumZeitLabor\PartKeepr\User\User") */ private $user; diff --git a/src/de/RaumZeitLabor/PartKeepr/User/Exceptions/InvalidLoginDataException.php b/src/de/RaumZeitLabor/PartKeepr/User/Exceptions/InvalidLoginDataException.php @@ -0,0 +1,14 @@ +<?php +namespace de\RaumZeitLabor\PartKeepr\User\Exceptions; + +use de\RaumZeitLabor\PartKeepr\Util\SerializableException, + de\RaumZeitLabor\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/de/RaumZeitLabor/PartKeepr/User/Exceptions/UserAlreadyExistsException.php b/src/de/RaumZeitLabor/PartKeepr/User/Exceptions/UserAlreadyExistsException.php @@ -0,0 +1,19 @@ +<?php +namespace de\RaumZeitLabor\PartKeepr\User\Exceptions; + +use de\RaumZeitLabor\PartKeepr\Util\SerializableException, + de\RaumZeitLabor\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/de/RaumZeitLabor/PartKeepr/User/Exceptions/UserDoesNotExistException.php b/src/de/RaumZeitLabor/PartKeepr/User/Exceptions/UserDoesNotExistException.php @@ -0,0 +1,17 @@ +<?php +namespace de\RaumZeitLabor\PartKeepr\User\Exceptions; + +use de\RaumZeitLabor\PartKeepr\Util\SerializableException, + de\RaumZeitLabor\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/de/RaumZeitLabor/PartKeepr/User/User.php b/src/de/RaumZeitLabor/PartKeepr/User/User.php @@ -0,0 +1,161 @@ +<?php +namespace de\RaumZeitLabor\PartKeepr\User; +declare(encoding = 'UTF-8'); + +use de\RaumZeitLabor\PartKeepr\Util\BaseEntity; + +/** @Entity @Table(name="PartKeeprUser") */ +class User extends BaseEntity { + /** @Column(length=50,unique=true) */ + private $username; + + /** @Column(length=32) */ + private $password; + + /** @Column(type="boolean") */ + private $admin; + + + /** + * 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. + * + * Forces the username to have + * lowercase a-z characters. + * + * Replaces space with an underscore. + * + * @param string $username The username to set. Applies automatic username modification. + * @return nothing + */ + public function setUsername ($username) { + $username = strtolower($username); + $username = str_replace(" ", "_", $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; + } + } + + /** + * 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() + ); + } +}+ \ No newline at end of file diff --git a/src/de/RaumZeitLabor/PartKeepr/User/UserManager.php b/src/de/RaumZeitLabor/PartKeepr/User/UserManager.php @@ -3,16 +3,26 @@ namespace de\RaumZeitLabor\PartKeepr\User; declare(encoding = 'UTF-8'); use de\RaumZeitLabor\PartKeepr\Util\Singleton, - de\RaumZeitLabor\PartKeepr\Auth\User, + de\RaumZeitLabor\PartKeepr\User\User, de\RaumZeitLabor\PartKeepr\PartKeepr, de\RaumZeitLabor\PartKeepr\Category\CategoryManager, + de\RaumZeitLabor\PartKeepr\User\Exceptions\UserAlreadyExistsException, de\RaumZeitLabor\PartKeepr\User\Exceptions\UserNotFoundException; class UserManager extends Singleton { + /** + * Returns a list of users. + * + * @param int $start Start of the list, default 0 + * @param int $limit Number of users to list, default 10 + * @param string $sort The field to sort by, default "name" + * @param string $dir The direction to sort (ASC or DESC), default ASC + * @param string $filter A string to filter the user's name by, default empty + */ public function getUsers ($start = 0, $limit = 10, $sort = "name", $dir = "asc", $filter = "") { $qb = PartKeepr::getEM()->createQueryBuilder(); - $qb->select("st.id, st.username")->from("de\RaumZeitLabor\PartKeepr\Auth\User","st"); + $qb->select("st.id, st.username")->from("de\RaumZeitLabor\PartKeepr\User\User","st"); if ($filter != "") { $qb = $qb->where("st.username LIKE :filter"); @@ -31,12 +41,12 @@ class UserManager extends Singleton { $result = $query->getResult(); $totalQueryBuilder = PartKeepr::getEM()->createQueryBuilder(); - $totalQueryBuilder->select("COUNT(st.id)")->from("de\RaumZeitLabor\PartKeepr\Auth\User","st"); + $totalQueryBuilder->select("COUNT(st.id)")->from("de\RaumZeitLabor\PartKeepr\User\User","st"); if ($filter != "") { - $totalQueryBuilder = $totalQueryBuilder->where("st.name LIKE :filter"); + $totalQueryBuilder = $totalQueryBuilder->where("st.username LIKE :filter"); $totalQueryBuilder->setParameter("filter", "%".$filter."%"); } @@ -45,20 +55,81 @@ class UserManager extends Singleton { return array("data" => $result, "totalCount" => $totalQuery->getSingleScalarResult()); } - public function getUser ($id) { - $user = PartKeepr::getEM()->find("de\RaumZeitLabor\PartKeepr\Auth\User", $id); + /** + * Checks if the passed user already exists. + * + * @param $username string The username to check + */ + public function userExists ($username) { + $dql = "SELECT COUNT(u) FROM de\RaumZeitLabor\PartKeepr\User\User u WHERE u.username = :name"; + + $query = PartKeepr::getEM()->createQuery($dql); + $query->setParameter("name", $username); - if ($user) { - return $user; + $count = $query->getSingleScalarResult(); + + if ($count == 0) { + return false; } else { - throw new UserNotFoundException(); + 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 = $this->getUser($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("de\RaumZeitLabor\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/de/RaumZeitLabor/PartKeepr/User/UserService.php b/src/de/RaumZeitLabor/PartKeepr/User/UserService.php @@ -6,11 +6,20 @@ declare(encoding = 'UTF-8'); use de\RaumZeitLabor\PartKeepr\Service\AdminService; use de\RaumZeitLabor\PartKeepr\PartKeepr, - de\RaumZeitLabor\PartKeepr\Auth\User, + de\RaumZeitLabor\PartKeepr\User\User, de\RaumZeitLabor\PartKeepr\Session\SessionManager; class UserService extends AdminService implements RestfulService { - public function get () { + + /** + * Implements the get() call for the RestfulService. + * + * If the "id" parameter is passed, try to return the user by id. If not, + * return a list. + * + * @see de\RaumZeitLabor\PartKeepr\Service.RestfulService::get() + */ + public function get () { if ($this->hasParameter("id")) { return UserManager::getInstance()->getUser($this->getParameter("id"))->serialize(); } else { @@ -32,6 +41,11 @@ class UserService extends AdminService implements RestfulService { } } + /** + * Creates a new user. + * + * @see de\RaumZeitLabor\PartKeepr\Service.RestfulService::create() + */ public function create () { $this->requireParameter("username"); @@ -39,12 +53,15 @@ class UserService extends AdminService implements RestfulService { $this->setUserData($user); - PartKeepr::getEM()->persist($user); - PartKeepr::getEM()->flush(); + UserManager::getInstance()->createUser($user); return array("data" => $user->serialize()); } + /** + * Sets the data for this user. Used by update() and create(). + * @param User $user The user object + */ private function setUserData (User $user) { $user->setUsername($this->getParameter("username")); @@ -55,6 +72,10 @@ class UserService extends AdminService implements RestfulService { } } + /** + * Updates the user informations. + * @see de\RaumZeitLabor\PartKeepr\Service.RestfulService::update() + */ public function update () { $this->requireParameter("id"); $this->requireParameter("username"); @@ -67,6 +88,10 @@ class UserService extends AdminService implements RestfulService { } + /** + * Deletes the user from the database. + * @see de\RaumZeitLabor\PartKeepr\Service.RestfulService::destroy() + */ public function destroy () { $this->requireParameter("id"); diff --git a/src/de/RaumZeitLabor/PartKeepr/Util/BaseEntity.php b/src/de/RaumZeitLabor/PartKeepr/Util/BaseEntity.php @@ -0,0 +1,37 @@ +<?php +namespace de\RaumZeitLabor\PartKeepr\Util; + +use de\RaumZeitLabor\PartKeepr\Util\Exceptions\EntityNotFoundException, + de\RaumZeitLabor\PartKeepr\PartKeepr; + +/** @MappedSuperclass */ +class BaseEntity { + /** + * @Id @Column(type="integer") + * @GeneratedValue(strategy="AUTO") + * @var unknown_type + */ + private $id; + + /** + * Returns the ID of this object. + * @param none + * @return int The ID of this object + */ + public function getId () { + return $this->id; + } + + /** + * Loads the entity from the database. + * @param integer $id The entity's id + */ + public static function loadById ($id) { + $entity = PartKeepr::getEM()->find(get_called_class(), $id); + + if (!is_object($entity)) { + throw new EntityNotFoundException(get_called_class(), $id); + } + return $entity; + } +}+ \ No newline at end of file diff --git a/src/de/RaumZeitLabor/PartKeepr/Util/Exceptions/EntityNotFoundException.php b/src/de/RaumZeitLabor/PartKeepr/Util/Exceptions/EntityNotFoundException.php @@ -0,0 +1,17 @@ +<?php +namespace de\RaumZeitLabor\PartKeepr\Util\Exceptions; +declare(encoding = 'UTF-8'); + +use de\RaumZeitLabor\PartKeepr\Util\SerializableException; + +/** + * Thrown when an entity via loadById() was not found. + */ +class EntityNotFoundException extends SerializableException { + public function __construct ($class, $id) { + parent::__construct( + sprintf( + PartKeepr::i18n("The entity %s with the id %d could not be found"), + $class, $id)); + } +} diff --git a/testing/SetupDatabase.php b/testing/SetupDatabase.php @@ -12,7 +12,7 @@ declare(encoding = 'UTF-8'); include("../src/de/RaumZeitLabor/PartKeepr/PartKeepr.php"); -use de\RaumZeitLabor\PartKeepr\Auth\User; +use de\RaumZeitLabor\PartKeepr\User\User; use de\RaumZeitLabor\PartKeepr\Footprint\Footprint; use de\RaumZeitLabor\PartKeepr\Footprint\FootprintManager; use de\RaumZeitLabor\PartKeepr\PartKeepr; @@ -75,7 +75,7 @@ $tool->createSchema($classes); $user = new User(); $user->setUsername("test"); $user->setPassword("test"); - +$user->setAdmin(true); PartKeepr::getEM()->persist($user); /* Create footprints */