/*
* Copyright (C) 2020 Red Hat, Inc.
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
import cockpit from 'cockpit';
import React from 'react';
import { superuser } from "superuser";
import { admins } from './users.js';
import { Button } from "@patternfly/react-core/dist/esm/components/Button/index.js";
import { Badge } from "@patternfly/react-core/dist/esm/components/Badge/index.js";
import { Card, CardExpandableContent, CardHeader, CardTitle } from '@patternfly/react-core/dist/esm/components/Card/index.js';
import { Divider } from '@patternfly/react-core/dist/esm/components/Divider/index.js';
import { DropdownItem } from '@patternfly/react-core/dist/esm/components/Dropdown/index.js';
import { Flex, FlexItem } from "@patternfly/react-core/dist/esm/layouts/Flex/index.js";
import { HelperText, HelperTextItem } from "@patternfly/react-core/dist/esm/components/HelperText/index.js";
import { Label } from "@patternfly/react-core/dist/esm/components/Label/index.js";
import { Page, PageSection } from "@patternfly/react-core/dist/esm/components/Page/index.js";
import { SearchInput } from "@patternfly/react-core/dist/esm/components/SearchInput/index.js";
import { Stack } from "@patternfly/react-core/dist/esm/layouts/Stack/index.js";
import { Content, ContentVariants } from "@patternfly/react-core/dist/esm/components/Content/index.js";
import { Toolbar, ToolbarContent, ToolbarItem } from "@patternfly/react-core/dist/esm/components/Toolbar/index.js";
import * as timeformat from "timeformat";
import { EmptyStatePanel } from 'cockpit-components-empty-state.jsx';
import { ListingTable } from 'cockpit-components-table.jsx';
import { SearchIcon } from '@patternfly/react-icons';
import { SortByDirection } from "@patternfly/react-table";
import { account_create_dialog } from "./account-create-dialog.js";
import { delete_account_dialog } from "./delete-account-dialog.js";
import { group_create_dialog } from "./group-create-dialog.js";
import { lockAccountDialog } from "./lock-account-dialog.js";
import { logoutAccountDialog } from "./logout-account-dialog.js";
import { GroupActions } from "./group-actions.jsx";
import { usePageLocation } from "hooks";
import { KebabDropdown } from "cockpit-components-dropdown";
const _ = cockpit.gettext;
const UserActions = ({ account, current }) => {
const actions = [
{ ev.preventDefault(); cockpit.location.go(account.name) }}>
{_("Edit user")}
,
];
superuser.allowed && actions.push(
,
logoutAccountDialog(account) }>
{_("Log user out")}
,
,
lockAccountDialog(account) }>
{_("Lock account")}
,
delete_account_dialog(account) }>
{_("Delete account")}
,
);
return ;
};
const getGroupRow = (group, accounts) => {
let groupColorClass;
if (group.isAdmin)
groupColorClass = "group-yellow";
else if (group.members > 0)
groupColorClass = "group-blue";
else
groupColorClass = "group-grey";
const columns = [
{
sortKey: group.name,
title: (
{group.name}
),
props: { width: 20, },
},
{
title: group.gid,
props: { width: 10, },
},
{
title: group.members,
props: { width: 20, },
},
{
title: (
{(group.userlistPrimary.concat(group.userlist))
.map(account => {
if (accounts.map(account => account.name).includes(account))
return {account};
else
return account;
})
.reduce((acc, curr) => [...acc, ", ", curr], [])
.slice(1)}
),
props: { width: 50, },
},
];
if (superuser.allowed) {
columns.push(
{
title: ,
props: { className: "pf-v6-c-table__action" }
}
);
}
return { columns, props: { "data-row-id": group.name, key: group.name } };
};
const getAccountRow = (account, current, groups) => {
const userGroups = groups.filter(group => group.gid === account.gid || group.userlist.find(accountName => accountName === account.name));
const userGroupLabels = userGroups.map(group => {
const color = group.isAdmin ? "yellow" : "blue";
return (
);
});
let loginText = "";
let loginSortKey = null;
if (account.loggedIn) {
loginText = _("Logged in");
loginSortKey = "logged in";
} else if (!account.lastLogin) {
loginText = _("Never logged in");
loginSortKey = "never";
} else {
loginSortKey = new Date(account.lastLogin);
loginText = timeformat.dateTime(loginSortKey);
}
const columns = [
{
title: (
{account.name}
{current && {_("Your account")}}
),
sortKey: account.name,
props: { width: 25, },
},
{
title: account.gecos.split(',')[0],
props: { width: 20, },
},
{
title: account.uid,
props: { width: 10, },
},
{
title: loginText,
sortKey: loginSortKey,
props: { width: 25, },
},
{
title: (
{userGroupLabels}
),
props: { width: 20 },
},
{
title: ,
props: { className: "pf-v6-c-table__action" }
},
];
return { columns, props: { "data-row-id": account.name, key: account.name } };
};
const mapGroupsToAccount = (accounts, groups) => {
return accounts.map(account => {
const accountGroups = [];
groups.forEach(group => {
if (group.userlist.find(accountName => accountName === account.name))
accountGroups.push(group.name);
});
account.groups = accountGroups;
return account;
});
};
const GroupsList = ({ groups, accounts, isExpanded, setIsExpanded, min_gid, max_gid }) => {
const { options } = usePageLocation();
const currentTextFilter = typeof options.group == "string" ? options.group : '';
const setCurrentTextFilter = val => {
const newOptions = { ...cockpit.location.options };
if (val)
newOptions.group = val;
else
delete newOptions.group;
cockpit.location.replace(cockpit.location.path, newOptions);
};
const filtered_groups = groups.filter(group => {
if (currentTextFilter !== "" &&
(group.name.toLowerCase().indexOf(currentTextFilter.toLowerCase()) === -1) &&
(group.gid.toString().indexOf(currentTextFilter.toLowerCase()) === -1))
return false;
return true;
});
const columns = [
{ title: _("Group name"), sortable: true },
{ title: _("ID"), sortable: true },
{ title: _("# of users"), sortable: true },
{ title: _("Accounts") },
];
const sortRows = (rows, direction, idx) => {
// GID and members columns are numeric
const isNumeric = idx == 1 || idx == 2;
const sortedRows = rows.sort((a, b) => {
const aitem = a.columns[idx].sortKey || a.columns[idx].title;
const bitem = b.columns[idx].sortKey || b.columns[idx].title;
const aname = a.columns[0].sortKey;
const bname = b.columns[0].sortKey;
// administrator groups are always first
if (admins.includes(aname))
return direction === SortByDirection.asc ? -1 : 1;
if (admins.includes(bname))
return direction === SortByDirection.asc ? 1 : -1;
if (isNumeric)
return bitem - aitem;
else
return aitem.localeCompare(bitem);
});
return direction === SortByDirection.asc ? sortedRows : sortedRows.reverse();
};
const tableToolbar = (
{isExpanded &&
setCurrentTextFilter(val)}
onClear={() => setCurrentTextFilter('')} />
}
{ superuser.allowed &&
<>
{isExpanded && }
>
}
);
return (
setIsExpanded(!isExpanded)}
toggleButtonProps={{
id: 'groups-view-toggle',
'aria-label': _("Groups"),
'aria-expanded': isExpanded
}}>
{_("Groups")}
{(!isExpanded && !groups.length) && {_("Loading...")}}
{(!isExpanded && filtered_groups.length > 0) && <>
{filtered_groups.slice(0, 3)
.map(group => {
const color = group.isAdmin ? "yellow" : "blue";
return (
);
})}
{filtered_groups.length > 3 && }
>}
getGroupRow(a, accounts)) }
loading={ groups.length && accounts.length ? '' : _("Loading...") }
sortMethod={sortRows}
emptyComponent={ setCurrentTextFilter('')} actionVariant="link" />}
variant="compact" sortBy={{ index: 2, direction: SortByDirection.asc }} />
);
};
const AccountsList = ({ accounts, current_user, groups, min_uid, max_uid, shells }) => {
const { options } = usePageLocation();
const currentTextFilter = typeof options.user == "string" ? options.user : '';
const setCurrentTextFilter = val => {
const newOptions = { ...cockpit.location.options };
if (val)
newOptions.user = val;
else
delete newOptions.user;
cockpit.location.replace(cockpit.location.path, newOptions);
};
const filtered_accounts = accounts.filter(account => {
if (currentTextFilter !== "" &&
(account.name.toLowerCase().indexOf(currentTextFilter.toLowerCase()) === -1) &&
(account.gecos.toLowerCase().indexOf(currentTextFilter.toLowerCase()) === -1) &&
(account.uid.toString().indexOf(currentTextFilter.toLowerCase()) === -1) &&
(!account.groups.find(group => group.toLowerCase().indexOf(currentTextFilter.toLowerCase()) !== -1)))
return false;
return true;
});
const columns = [
{ title: _("Username"), sortable: true },
{ title: _("Full name"), sortable: true },
{ title: _("ID"), sortable: true },
{ title: _("Last active"), sortable: true },
{ title: _("Group") },
{ title: "", sortable: false, props: { screenReaderText: _("Actions") } },
];
const sortRows = (rows, direction, idx) => {
const sortedRows = rows.sort((a, b) => {
const aitem = a.columns[idx];
const bitem = b.columns[idx];
const aname = a.columns[0];
const bname = b.columns[0];
// current user is always first
if (aname.sortKey === current_user)
return direction === SortByDirection.asc ? -1 : 1;
if (bname.sortKey === current_user)
return direction === SortByDirection.asc ? 1 : -1;
// sorting last login
if (idx === 3) {
if (aitem.sortKey === "logged in")
return -1;
if (bitem.sortKey === "logged in")
return 1;
if (aitem.sortKey === "never")
return 1;
if (bitem.sortKey === "never")
return -1;
return bitem.sortKey - aitem.sortKey;
}
if (idx == 2)
return bitem.title - aitem.title;
return ((typeof aitem == 'string' ? aitem : (aitem.sortKey || aitem.title)).localeCompare(typeof bitem == 'string' ? bitem : (bitem.sortKey || bitem.title)));
});
return direction === SortByDirection.asc ? sortedRows : sortedRows.reverse();
};
const tableToolbar = (
setCurrentTextFilter(val)}
onClear={() => setCurrentTextFilter('')} />
{ superuser.allowed &&
<>
>
}
);
return (
{_("Accounts")}
getAccountRow(a, current_user === a.name, groups)) }
loading={ accounts.length ? '' : _("Loading...") }
sortMethod={sortRows}
emptyComponent={ setCurrentTextFilter('')} actionVariant="link" />}
variant="compact" sortBy={{ index: 0, direction: SortByDirection.asc }} />
);
};
export const AccountsMain = ({ accountsInfo, current_user, groups, isGroupsExpanded, setIsGroupsExpanded, min_gid, max_gid, min_uid, max_uid, shells }) => {
const accounts = mapGroupsToAccount(accountsInfo, groups).filter(account => {
if ((account.uid < 1000 && account.uid !== 0) ||
account.shell.match(/^(\/usr)?\/sbin\/nologin/) ||
account.shell === '/bin/false')
return false;
return true;
});
return (
);
};