1234567891011121314151617181920212223242526272829303132333435 |
- <?php
- class Authorizer {
- public static function getInstance() {
- return new Authorizer(new SessionWrapper(), UserDataCache::getInstance(), new HeaderWrapper());
- }
- private $session;
- private $userDataCache;
- private $header;
- private function __construct(SessionWrapper $session, UserDataCache $userDataCache, HeaderWrapper $header) {
- $this->session = $session;
- $this->userDataCache = $userDataCache;
- $this->header = $header;
- }
- public function logInUser($email, $password) {
- $userData = $this->userDataCache->getUserByLogin($email, $password);
- if(!$userData) {
- return;
- }
- $this->session->set("isLoggedIn", true);
- $this->session->set("userId", $userData['user_id']);
- }
- public function logOutUser() {
- $this->session->unsetKey("userId");
- $this->session->unsetKey("isLoggedIn");
- }
- public function isLoggedIn() {
- return $this->session->getDefault("isLoggedIn", false) === true;
- }
- public function rejectIfNotAuthorized() {
- if($this->isLoggedIn()) {
- return;
- }
- $this->header->location("/");
- }
- }
|