summaryrefslogtreecommitdiffstats
path: root/library/Icinga/Web/RememberMeUserDevicesList.php
blob: 66609de72d3ce2ee83256fdae29384e476580877 (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
/* Icinga Web 2 | (c) 2021 Icinga GmbH | GPLv2+ */

namespace Icinga\Web;

use ipl\Html\BaseHtmlElement;
use ipl\Html\Html;
use ipl\Web\Url as iplWebUrl;  //alias is needed for php5.6
use ipl\Web\Widget\Icon;
use ipl\Web\Widget\Link;

class RememberMeUserDevicesList extends BaseHtmlElement
{
    protected $tag = 'table';

    protected $defaultAttributes = [
        'class'             => 'common-table',
        'data-base-target'  => '_self'
    ];

    /**
     * @var array
     */
    protected $devicesList;

    /**
     * @var string
     */
    protected $username;

    /**
     * @var string
     */
    protected $url;

    /**
     * @return string
     */
    public function getUrl()
    {
        return $this->url;
    }

    /**
     * @param string $url
     *
     * @return $this
     */
    public function setUrl($url)
    {
        $this->url = $url;

        return $this;
    }

    /**
     * @return string
     */
    public function getUsername()
    {
        return $this->username;
    }

    /**
     * @param string $username
     *
     * @return $this
     */
    public function setUsername($username)
    {
        $this->username = $username;

        return $this;
    }

    /**
     * @return array List of devices. Each device contains user agent and fingerprint string
     */
    public function getDevicesList()
    {
        return $this->devicesList;
    }

    /**
     * @param $devicesList
     *
     * @return $this
     */
    public function setDevicesList($devicesList)
    {
        $this->devicesList = $devicesList;

        return $this;
    }

    protected function assemble()
    {
        $thead = Html::tag('thead');
        $theadRow = Html::tag('tr')
            ->add(Html::tag(
                'th',
                sprintf(t('List of devices and browsers %s is currently logged in:'), $this->getUsername())
            ));

        $thead->add($theadRow);

        $head = Html::tag('tr')
            ->add(Html::tag('th', t('OS')))
            ->add(Html::tag('th', t('Browser')))
            ->add(Html::tag('th', t('Fingerprint')));

        $thead->add($head);
        $tbody = Html::tag('tbody');

        if (empty($this->getDevicesList())) {
            $tbody->add(Html::tag('td', t('No device found')));
        } else {
            foreach ($this->getDevicesList() as $device) {
                $agent = new UserAgent($device);
                $element = Html::tag('tr')
                    ->add(Html::tag('td', $agent->getOs()))
                    ->add(Html::tag('td', $agent->getBrowser()))
                    ->add(Html::tag('td', $device->random_iv));

                $link = (new Link(
                    new Icon('trash'),
                    iplWebUrl::fromPath($this->getUrl())
                        ->addParams(
                            [
                                'name'          => $this->getUsername(),
                                'fingerprint'   => $device->random_iv,
                            ]
                        )
                ));

                $element->add(Html::tag('td', $link));
                $tbody->add($element);
            }
        }

        $this->add($thead);
        $this->add($tbody);
    }
}