Authorizer.php 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  1. <?php
  2. class Authorizer {
  3. public static function getInstance() {
  4. return new Authorizer(new SessionWrapper(), UserDataCache::getInstance(), new HeaderWrapper());
  5. }
  6. private $session;
  7. private $userDataCache;
  8. private $header;
  9. private function __construct(SessionWrapper $session, UserDataCache $userDataCache, HeaderWrapper $header) {
  10. $this->session = $session;
  11. $this->userDataCache = $userDataCache;
  12. $this->header = $header;
  13. }
  14. public function logInUser($email, $password) {
  15. $userData = $this->userDataCache->getUserByLogin($email, $password);
  16. if(!$userData) {
  17. return;
  18. }
  19. $this->session->set("isLoggedIn", true);
  20. $this->session->set("userId", $userData['user_id']);
  21. }
  22. public function logOutUser() {
  23. $this->session->unsetKey("userId");
  24. $this->session->unsetKey("isLoggedIn");
  25. }
  26. public function isLoggedIn() {
  27. return $this->session->getDefault("isLoggedIn", false) === true;
  28. }
  29. public function rejectIfNotAuthorized() {
  30. if($this->isLoggedIn()) {
  31. return;
  32. }
  33. $this->header->location("/");
  34. }
  35. }