Passed
Pull Request — master (#653)
by Aleksei
06:27
created

AuthController   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 70
rs 10
c 0
b 0
f 0
wmc 13
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\App\Controller;
13
14
use Spiral\Auth\AuthContextInterface;
15
use Spiral\Auth\AuthScope;
16
use Spiral\Auth\TokenStorageInterface;
17
use Spiral\Core\Exception\ControllerException;
18
use Spiral\Security\GuardInterface;
19
20
class AuthController
21
{
22
    private $auth;
23
24
    public function __construct(AuthScope $auth)
25
    {
26
        $this->auth = $auth;
27
    }
28
29
    public function do(GuardInterface $guard)
30
    {
31
        if (!$guard->allows('do')) {
32
            throw new ControllerException("Unauthorized permission 'do'", ControllerException::FORBIDDEN);
33
        }
34
35
        return 'ok';
36
    }
37
38
    public function token(AuthContextInterface $authContext)
39
    {
40
        if ($authContext->getToken() !== null) {
41
            return $authContext->getToken()->getID();
42
        }
43
44
        return 'none';
45
    }
46
47
    public function login(AuthContextInterface $authContext, TokenStorageInterface $tokenStorage)
48
    {
49
        $authContext->start(
50
            $tokenStorage->create(['userID' => 1])
51
        );
52
53
        return 'OK';
54
    }
55
56
    public function logout()
57
    {
58
        $this->auth->close();
59
60
        return 'closed';
61
    }
62
63
    public function token2()
64
    {
65
        if ($this->auth->getToken() !== null) {
66
            return $this->auth->getToken()->getID();
67
        }
68
69
        return 'none';
70
    }
71
72
    public function token3()
73
    {
74
        return $this->auth->getToken()->getPayload();
75
    }
76
77
    public function login2(TokenStorageInterface $tokenStorage)
78
    {
79
        $this->auth->start(
80
            $tokenStorage->create(['userID' => 1])
81
        );
82
83
        return 'OK';
84
    }
85
86
    public function actor()
87
    {
88
        $actor = $this->auth->getActor();
89
        return $actor ? $actor->getName() : 'none';
90
    }
91
}
92