Advertisement
Guest User

Untitled

a guest
Jul 27th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.63 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. namespace App\Model\Facades;
  5.  
  6. use App\Model\Entities\User,
  7.     App\Model\Entities\UserSettings,
  8.     App\Model\Queries\UsersListQuery,
  9.     Kdyby\Doctrine\ResultSet,
  10.     Nette\Security\AuthenticationException,
  11.     Nette\Security\IAuthenticator,
  12.     Nette\Security\Identity,
  13.     Nette\Security\Passwords,
  14.     Nette\Utils\ArrayHash,
  15.     Nette\Utils\DateTime;
  16. use Nette\Utils\Image;
  17. use Nette\Utils\UnknownImageFileException;
  18.  
  19. class UserFacade extends BaseFacade implements IAuthenticator
  20. {
  21.  
  22.     public $onRegisteredUser = array();
  23.  
  24.  
  25.     public function getUser($id, $throwException = FALSE)
  26.     {
  27.  
  28.         if (is_null($id)) {
  29.             throw new \App\Exceptions\UserDoesntExistException();
  30.         }
  31.  
  32.         $user = $this->em->find(User::class, $id);
  33.         if ($user === NULL && $throwException) {
  34.             throw new \App\Exceptions\UserDoesntExistException();
  35.         }
  36.  
  37.         return $user;
  38.     }
  39.  
  40.     public function registerUser($values)
  41.     {
  42.         $user = new User();
  43.         $user->name = $values->name;
  44.         $user->password = Passwords::hash($values->password);
  45.         $user->email = $values->email;
  46.         $user->ip = $values->ip;
  47.         $user->role = User::ROLE_USER;
  48.         $user->registrationDate = new DateTime();
  49.  
  50.         $settings = new UserSettings();
  51.         $settings->user = $user;
  52.  
  53.         $this->em->persist($user);
  54.         $this->em->persist($settings);
  55.  
  56.         $this->em->flush();
  57.         //event REGISTER USER
  58.         $this->onRegisteredUser($user);
  59.  
  60.     }
  61.  
  62.     function authenticate(array $credentials)
  63.     {
  64.         list($username, $password) = $credentials;
  65.  
  66.         $user = $this->em->getRepository(User::class)->findOneBy(array("name" => $username));
  67.  
  68.         if (!isset($user) || !Passwords::verify($password, $user->password)) {
  69.             throw new \App\Exceptions\YouFilledBadNameOrPasswordException();
  70.         }
  71.  
  72.         // Zjistí, jestli heslo potřebuje rehashovat a případně to udělá.
  73.         if (Passwords::needsRehash($user->password)) {
  74.             $user->password = Passwords::hash($password);
  75.             $this->em->flush();
  76.         }
  77.  
  78.         return new Identity($user->id);
  79.     }
  80.  
  81.     public function updateSettings(User $user, $values)
  82.     {
  83.         $settings = $user->settings;
  84.         $settings->description = $values->description;
  85.  
  86.         $user->name = $values->name;
  87.         //aktualizace hesla
  88.         if (!empty($values->oldPassword)) {
  89.             $this->changePass($user, $values);
  90.         }
  91.  
  92.         if (!is_null($values->avatar->name)) {
  93.             //smaže původní
  94.             $path = UserSettings::AVATAR_PATH . $user->settings->avatar . ".jpg";
  95.             if (file_exists($path)) {
  96.                 unlink($path);
  97.             }
  98.             //načte nový
  99.             $avatar = $values->avatar;
  100.             $hashImgName = sha1($avatar->getName() . new DateTime());
  101.             $avatar = $avatar->toImage();
  102.             $avatar->resize(NULL, UserSettings::AVATAR_SIZE)->crop("50%", "50%", 260, 400);
  103.             $avatar->save(UserSettings::AVATAR_PATH . $hashImgName . ".jpg", NULL, Image::JPEG);
  104.             $settings->avatar = $hashImgName;
  105.         }
  106.         $this->em->flush();
  107.  
  108.     }
  109.  
  110.     public function changePass(User $user, $values)
  111.     {
  112.  
  113.         $oldPass = Passwords::verify($values->oldPassword, $user->password);
  114.  
  115.         if (!$oldPass) {
  116.             throw new \App\Exceptions\InvalidOldPasswordException();
  117.         }
  118.  
  119.         if ($values->newPassword !== $values->repeatNewPassword) {
  120.             throw new \App\Exceptions\PasswordConfirmationFailedException();
  121.         }
  122.  
  123.         $user->password = Passwords::hash($values->newPassword);
  124.  
  125.         $this->em->flush();
  126.     }
  127.  
  128.     public function getUsersList()
  129.     {
  130.         $query = new UsersListQuery();
  131.         $query->orderByName();
  132.         return $this->em->getRepository(User::class)->fetch($query);
  133.     }
  134.  
  135.     public function addUser($values)
  136.     {
  137.  
  138.         $user = new User();
  139.         $user->name = $values->name;
  140.         $user->password = Passwords::hash($values->password);
  141.         $user->email = $values->email;
  142.         $user->ip = "";
  143.         $user->role = $values->role;
  144.         $user->registrationDate = new DateTime();
  145.  
  146.         $settings = new UserSettings();
  147.         $settings->user = $user;
  148.  
  149.         $this->em->persist($user);
  150.         $this->em->persist($settings);
  151.         $this->em->flush();
  152.     }
  153.  
  154.     public function editUser(User $user, $values)
  155.     {
  156.  
  157.         $user->name = $values->name;
  158.         $user->email = $values->email;
  159.         $user->role = $values->role;
  160.  
  161.         $this->em->flush();
  162.     }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement