aboutsummaryrefslogtreecommitdiff
path: root/app/controllers/tfa.php
blob: 370d8837a9110ff0eea7e1485054fb98a94d1b59 (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
class TfaController extends AuthenticatedController
{
    public function before_filter(&$action, &$args)
    {
        parent::before_filter($action, $args);

        Navigation::activateItem('/profile/settings/tfa');
        PageLayout::setTitle(_('Zwei-Faktor-Authentifizierung'));

        $this->user = User::findCurrent();
        $this->is_root = $GLOBALS['perm']->have_perm('root');

        $this->own_profile = true;

        if ($this->is_root && Request::submitted('username')) {
            $username = Request::username('username');
            $this->user = User::findOneByUsername($username);

            if (!$this->user) {
                throw new Exception(_('Diesen Nutzer gibt es nicht'));
            }

            $this->own_profile = false;

            URLHelper::addLinkParam('username', Request::username('username'));

            PageLayout::postMessage(
                MessageBox::info(sprintf(
                    _('Daten von: %1$s (%2$s), Status: %3$s'),
                    htmlReady($this->user->getFullName()),
                    htmlReady($username),
                    htmlReady($this->user->perms)
                )),
                'settings-user-anncouncement'
            );
        }

        $this->secret = new TFASecret($this->user->id);

        if (!$this->own_profile) {
            PageLayout::postWarning(_('Sie können die Zwei-Faktor-Authentifizierung nicht für andere Personen einrichten.'));
        }
    }

    public function index_action()
    {
        if ($this->secret->isNew()) {
            $this->render_action('setup');
        } elseif (!$this->secret->confirmed) {
            $this->confirm_action();
        }
    }

    public function setup_action()
    {
    }

    public function create_action()
    {
        CSRFProtection::verifyUnsafeRequest();

        $this->secret->type = Request::option('type', 'email');
        $this->secret->store();

        PageLayout::postSuccess(_('Die Zwei-Faktor-Authentifizierung wurde eingerichtet'));
        $this->redirect('tfa/confirm');
    }

    public function confirm_action()
    {
        if ($this->secret->isNew()) {
            $this->redirect('tfa/index');
            return;
        }

        TwoFactorAuth::get()->confirm(
            '2fa',
            _('Bitte bestätigen Sie die Aktivierung.'),
            ['global' => true]
        );

        PageLayout::postSuccess(_('Die Zwei-Faktor-Authentifizierung wurde aktiviert.'));
        $this->redirect('tfa/index');
    }

    public function abort_action()
    {
        if ($this->secret && $this->secret->confirmed) {
            $this->redirect('tfa/revoke');
            return;
        }

        $this->secret->delete();

        PageLayout::postSuccess(_('Das Einrichten der Zwei-Faktor-Authentifizierung wurde abgebrochen.'));
        $this->redirect('tfa/index');
    }

    public function revoke_action()
    {
        if (!$this->is_root || $this->user->id === $GLOBALS['user']->id) {
            TwoFactorAuth::get()->confirm(
                '2fa-revoke',
                _('Bestätigen Sie das Aufheben der Methode')
            );
        }

        $this->secret->delete();

        if (!$this->is_root || $this->user->id === $GLOBALS['user']->id) {
            TwoFactorAuth::removeCookie();
        }

        PageLayout::postSuccess(_('Die Zwei-Faktor-Authentifizierung wurde deaktiviert.'));
        $this->redirect('tfa/index');
    }
}