| 1 |
/* |
| 2 |
* Copyright (C) 2021 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
import cockpit from 'cockpit'; |
| 7 |
import React, { useState } from 'react'; |
| 8 |
|
| 9 |
import { Card, CardBody, CardTitle } from "@patternfly/react-core/dist/esm/components/Card/index.js"; |
| 10 |
import { ListingTable } from 'cockpit-components-table.jsx'; |
| 11 |
|
| 12 |
import * as timeformat from "timeformat"; |
| 13 |
import { useInit } from "hooks"; |
| 14 |
|
| 15 |
const _ = cockpit.gettext; |
| 16 |
|
| 17 |
export function AccountLogs({ name }) { |
| 18 |
const [logins, setLogins] = useState([]); |
| 19 |
useInit(() => { |
| 20 |
cockpit.spawn(["last", "--time-format", "iso", "-n25", "--fullnames", name], { environ: ["LC_ALL=C"] }) |
| 21 |
.then(data => { |
| 22 |
let logins = []; |
| 23 |
data.split('\n').forEach(line => { |
| 24 |
// Exclude still logged in and non user lines |
| 25 |
if (!line.includes(name) || line.includes('still')) { |
| 26 |
return; |
| 27 |
} |
| 28 |
// Exclude tmux/screen lines |
| 29 |
if (line.includes('tmux') || line.includes('screen')) { |
| 30 |
return; |
| 31 |
} |
| 32 |
|
| 33 |
// format: |
| 34 |
// admin web console ::ffff:172.27.0. 2021-09-24T09:02:13+00:00 - 2021-09-24T09:04:20+00:00 (00:02) |
| 35 |
const lines = line.split(/ +/); |
| 36 |
const ended = new Date(lines[lines.length - 2]); |
| 37 |
const started = new Date(lines[lines.length - 4]); |
| 38 |
const from = lines[lines.length - 5]; |
| 39 |
if (isNaN(started.getTime()) || isNaN(ended.getTime())) { |
| 40 |
return; |
| 41 |
} |
| 42 |
|
| 43 |
logins.push({ |
| 44 |
started, |
| 45 |
ended, |
| 46 |
from |
| 47 |
}); |
| 48 |
}); |
| 49 |
|
| 50 |
// Only show 15 login lines |
| 51 |
logins = logins.slice(0, 15); |
| 52 |
setLogins(logins); |
| 53 |
}) |
| 54 |
.catch(ex => console.error("Failed to call last:", ex)); // not-covered: OS error |
| 55 |
}, [name]); |
| 56 |
|
| 57 |
return ( |
| 58 |
<Card isPlain id="account-logs"> |
| 59 |
<CardTitle component="h2">{_("Login history")}</CardTitle> |
| 60 |
<CardBody className="contains-list"> |
| 61 |
<ListingTable variant="compact" aria-label={ _("Login history list") } |
| 62 |
columns={ [ |
| 63 |
{ title: _("Started") }, |
| 64 |
{ title: _("Ended") }, |
| 65 |
{ title: _("From") }, |
| 66 |
] } |
| 67 |
rows={ logins.map((line, index) => ({ |
| 68 |
props: { key: index }, |
| 69 |
columns: [timeformat.dateTime(line.started), timeformat.dateTime(line.ended), line.from] |
| 70 |
}))} /> |
| 71 |
</CardBody> |
| 72 |
</Card> |
| 73 |
); |
| 74 |
} |