summaryrefslogtreecommitdiff
path: root/src/Renderer/Twig/Extensions/Authentication.php
blob: 061a9018f18a8935c40351d4032216b7ed45274a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php

namespace Engelsystem\Renderer\Twig\Extensions;

use Engelsystem\Helpers\Authenticator;
use Twig\Extension\AbstractExtension as TwigExtension;
use Twig\TwigFunction;

class Authentication extends TwigExtension
{
    /** @var Authenticator */
    protected $auth;

    /**
     * @param Authenticator $auth
     */
    public function __construct(Authenticator $auth)
    {
        $this->auth = $auth;
    }

    /**
     * @return TwigFunction[]
     */
    public function getFunctions(): array
    {
        return [
            new TwigFunction('is_user', [$this, 'isAuthenticated']),
            new TwigFunction('is_guest', [$this, 'isGuest']),
            new TwigFunction('has_permission_to', [$this->auth, 'can']),
        ];
    }

    /**
     * @return bool
     */
    public function isAuthenticated(): bool
    {
        return (bool)$this->auth->user();
    }

    /**
     * @return bool
     */
    public function isGuest(): bool
    {
        return !$this->isAuthenticated();
    }
}