diff --git a/src/entity/user/User.php b/src/entity/user/User.php index 461523f..34a2cf3 100644 --- a/src/entity/user/User.php +++ b/src/entity/user/User.php @@ -54,6 +54,15 @@ final class User implements UserInterface { $this->passwordHash = $hash; } + + /** + * In a real application you should use a salt ;) + * @param string $password + */ + public function setPasswordHashByPassword(string $password): void + { + $this->passwordHash = password_hash($password, PASSWORD_BCRYPT); + } public function getEmail(): string { diff --git a/src/entity/user/UserInterface.php b/src/entity/user/UserInterface.php index 6667fd8..d8ce399 100644 --- a/src/entity/user/UserInterface.php +++ b/src/entity/user/UserInterface.php @@ -18,6 +18,8 @@ interface UserInterface extends UniqueInterface */ public function setPasswordHash(string $hash):void; + public function setPasswordHashByPassword(string $password): void; + /** * @return string */ diff --git a/src/repository/user/User.php b/src/repository/user/User.php new file mode 100644 index 0000000..7510811 --- /dev/null +++ b/src/repository/user/User.php @@ -0,0 +1,28 @@ +database->prepare('INSERT INTO `user` (`name`, `email`, `hash`) VALUES (?, ?, ?);'); + $statement->execute([ + $user->getName(), + $user->getEmail(), + $user->getPasswordHash() + ]); + } + + public function getUserByMailAndHash(string $mail, string $hash): UserEntityInterface + {} +} + diff --git a/src/repository/user/UserInterface.php b/src/repository/user/UserInterface.php index 69fd181..f9901d9 100644 --- a/src/repository/user/UserInterface.php +++ b/src/repository/user/UserInterface.php @@ -12,11 +12,6 @@ interface UserInterface { public function addUser(UserEntityInterface $user):void; - /** - * This function just exist for maintaining reasons - */ - public function deleteAllUsers():void; - public function getUserByMailAndHash(string $mail,string $hash):UserEntityInterface; } diff --git a/src/setup/database/user.sql b/src/setup/database/user.sql index 43d870a..5fc1c8d 100644 --- a/src/setup/database/user.sql +++ b/src/setup/database/user.sql @@ -1,8 +1,8 @@ CREATE TABLE `test_db`.`user` ( `id` INT NOT NULL AUTO_INCREMENT, - `name` VARCHAR(45) NULL, - `email` VARCHAR(45) NULL, - `hash` VARCHAR(45) NULL, + `name` VARCHAR(120) NULL, + `email` VARCHAR(120) NULL, + `hash` VARCHAR(60) NULL, PRIMARY KEY (`id`), UNIQUE INDEX `id_UNIQUE` (`id` ASC), UNIQUE INDEX `email_UNIQUE` (`email` ASC)); diff --git a/src/setup/setup.php b/src/setup/setup.php index 82a2f98..e02700f 100644 --- a/src/setup/setup.php +++ b/src/setup/setup.php @@ -2,6 +2,8 @@ use core\Core; use Doctrine\Common\Collections\ArrayCollection; use repository\product\Product as ProductRepository; +use repository\user\User as UserRepository; +use entity\user\User; require __DIR__ . '/../vendor/autoload.php'; @@ -44,6 +46,14 @@ foreach ($lines as $number=>$line) { $products->add($product); } -$productRepository = new ProductRepository((new Core())->getDatabase()); +$productRepository = new ProductRepository(new Core()); $productRepository->addProducts($products); + +echo "Insert dummy user...\n"; +$user= new User(); +$user->setName('Maxla Mustermensch'); +$user->setPasswordHashByPassword('passwort:)'); +$user->setEmail('test@mail.test'); +$userRepository = new UserRepository(new Core()); +$userRepository->addUser($user); ?> \ No newline at end of file