Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Model\Facades;
- use App\Model\Entities\User,
- App\Model\Entities\UserSettings,
- App\Model\Queries\UsersListQuery,
- Kdyby\Doctrine\ResultSet,
- Nette\Security\AuthenticationException,
- Nette\Security\IAuthenticator,
- Nette\Security\Identity,
- Nette\Security\Passwords,
- Nette\Utils\ArrayHash,
- Nette\Utils\DateTime;
- use Nette\Utils\Image;
- use Nette\Utils\UnknownImageFileException;
- class UserFacade extends BaseFacade implements IAuthenticator
- {
- public $onRegisteredUser = array();
- public function getUser($id, $throwException = FALSE)
- {
- if (is_null($id)) {
- throw new \App\Exceptions\UserDoesntExistException();
- }
- $user = $this->em->find(User::class, $id);
- if ($user === NULL && $throwException) {
- throw new \App\Exceptions\UserDoesntExistException();
- }
- return $user;
- }
- public function registerUser($values)
- {
- $user = new User();
- $user->name = $values->name;
- $user->password = Passwords::hash($values->password);
- $user->email = $values->email;
- $user->ip = $values->ip;
- $user->role = User::ROLE_USER;
- $user->registrationDate = new DateTime();
- $settings = new UserSettings();
- $settings->user = $user;
- $this->em->persist($user);
- $this->em->persist($settings);
- $this->em->flush();
- //event REGISTER USER
- $this->onRegisteredUser($user);
- }
- function authenticate(array $credentials)
- {
- list($username, $password) = $credentials;
- $user = $this->em->getRepository(User::class)->findOneBy(array("name" => $username));
- if (!isset($user) || !Passwords::verify($password, $user->password)) {
- throw new \App\Exceptions\YouFilledBadNameOrPasswordException();
- }
- // Zjistí, jestli heslo potřebuje rehashovat a případně to udělá.
- if (Passwords::needsRehash($user->password)) {
- $user->password = Passwords::hash($password);
- $this->em->flush();
- }
- return new Identity($user->id);
- }
- public function updateSettings(User $user, $values)
- {
- $settings = $user->settings;
- $settings->description = $values->description;
- $user->name = $values->name;
- //aktualizace hesla
- if (!empty($values->oldPassword)) {
- $this->changePass($user, $values);
- }
- if (!is_null($values->avatar->name)) {
- //smaže původní
- $path = UserSettings::AVATAR_PATH . $user->settings->avatar . ".jpg";
- if (file_exists($path)) {
- unlink($path);
- }
- //načte nový
- $avatar = $values->avatar;
- $hashImgName = sha1($avatar->getName() . new DateTime());
- $avatar = $avatar->toImage();
- $avatar->resize(NULL, UserSettings::AVATAR_SIZE)->crop("50%", "50%", 260, 400);
- $avatar->save(UserSettings::AVATAR_PATH . $hashImgName . ".jpg", NULL, Image::JPEG);
- $settings->avatar = $hashImgName;
- }
- $this->em->flush();
- }
- public function changePass(User $user, $values)
- {
- $oldPass = Passwords::verify($values->oldPassword, $user->password);
- if (!$oldPass) {
- throw new \App\Exceptions\InvalidOldPasswordException();
- }
- if ($values->newPassword !== $values->repeatNewPassword) {
- throw new \App\Exceptions\PasswordConfirmationFailedException();
- }
- $user->password = Passwords::hash($values->newPassword);
- $this->em->flush();
- }
- public function getUsersList()
- {
- $query = new UsersListQuery();
- $query->orderByName();
- return $this->em->getRepository(User::class)->fetch($query);
- }
- public function addUser($values)
- {
- $user = new User();
- $user->name = $values->name;
- $user->password = Passwords::hash($values->password);
- $user->email = $values->email;
- $user->ip = "";
- $user->role = $values->role;
- $user->registrationDate = new DateTime();
- $settings = new UserSettings();
- $settings->user = $user;
- $this->em->persist($user);
- $this->em->persist($settings);
- $this->em->flush();
- }
- public function editUser(User $user, $values)
- {
- $user->name = $values->name;
- $user->email = $values->email;
- $user->role = $values->role;
- $this->em->flush();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement