Exemplo n.º 1
0
 /**
  * {@inheritdoc}
  */
 public function provide()
 {
     $this->container->htmlHelper = function () {
         return new HtmlHelper($this->container->flash, $this->container->urlHelper);
     };
     $this->container->twig = function (Container $container) {
         $twigLoader = new Twig_Loader_Filesystem(dirname(dirname(dirname(__FILE__))) . '/views');
         $twig = new Twig_Environment($twigLoader, ['autoescape' => false]);
         $isLoggedInFunction = new Twig_SimpleFunction('isLoggedIn', function () use($container) {
             $userMapper = new UserMapper($container->pdo);
             $authenticator = new Authenticator($userMapper);
             return $authenticator->isLoggedIn();
         });
         $assetFunction = new Twig_SimpleFunction('asset', function ($fileName) {
             return $this->container->urlHelper->mainUrl() . 'assets/' . $fileName;
         });
         $urlFunction = new Twig_SimpleFunction('url', function ($name, $parameters = []) {
             return $this->container->urlHelper->toRoute($name, $parameters);
         });
         $twig->addFunction($assetFunction);
         $twig->addFunction($urlFunction);
         $twig->addFunction($isLoggedInFunction);
         $twig->addExtension(new HtmlHelper($this->container->flash, $this->container->urlHelper));
         return $twig;
     };
 }
Exemplo n.º 2
0
 public function logout()
 {
     $this->authorize();
     $authenticator = new Authenticator($this->userMapper);
     $authenticator->logOut();
     return $this->redirect('login');
 }
Exemplo n.º 3
0
 /**
  * Allows only logged in users to proceed.
  *
  * @param bool $guestsOnly
  * @throws \Basalt\Auth\AuthenticationException
  */
 protected function authorize($guestsOnly = false)
 {
     $userMapper = new UserMapper($this->app['pdo']);
     $authenticator = new Authenticator($userMapper);
     $isLoggedIn = $authenticator->isLoggedIn();
     if ($guestsOnly && $isLoggedIn) {
         throw new AuthenticationException('Only guests can access.', AuthenticationException::LOGGED_IN);
     }
     if (!$guestsOnly && !$isLoggedIn) {
         throw new AuthenticationException('Only authorized users can access.', AuthenticationException::NOT_LOGGED_IN);
     }
 }