| 1 |
/* |
| 2 |
* Copyright (C) 2020 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
import cockpit from 'cockpit'; |
| 7 |
import React from 'react'; |
| 8 |
import { superuser } from "superuser"; |
| 9 |
|
| 10 |
import { admins } from './users.js'; |
| 11 |
import { Button } from "@patternfly/react-core/dist/esm/components/Button/index.js"; |
| 12 |
import { Badge } from "@patternfly/react-core/dist/esm/components/Badge/index.js"; |
| 13 |
import { Card, CardExpandableContent, CardHeader, CardTitle } from '@patternfly/react-core/dist/esm/components/Card/index.js'; |
| 14 |
import { Divider } from '@patternfly/react-core/dist/esm/components/Divider/index.js'; |
| 15 |
import { DropdownItem } from '@patternfly/react-core/dist/esm/components/Dropdown/index.js'; |
| 16 |
import { Flex, FlexItem } from "@patternfly/react-core/dist/esm/layouts/Flex/index.js"; |
| 17 |
import { HelperText, HelperTextItem } from "@patternfly/react-core/dist/esm/components/HelperText/index.js"; |
| 18 |
import { Label } from "@patternfly/react-core/dist/esm/components/Label/index.js"; |
| 19 |
import { Page, PageSection } from "@patternfly/react-core/dist/esm/components/Page/index.js"; |
| 20 |
import { SearchInput } from "@patternfly/react-core/dist/esm/components/SearchInput/index.js"; |
| 21 |
import { Stack } from "@patternfly/react-core/dist/esm/layouts/Stack/index.js"; |
| 22 |
import { Content, ContentVariants } from "@patternfly/react-core/dist/esm/components/Content/index.js"; |
| 23 |
import { Toolbar, ToolbarContent, ToolbarItem } from "@patternfly/react-core/dist/esm/components/Toolbar/index.js"; |
| 24 |
import * as timeformat from "timeformat"; |
| 25 |
import { EmptyStatePanel } from 'cockpit-components-empty-state.jsx'; |
| 26 |
import { ListingTable } from 'cockpit-components-table.jsx'; |
| 27 |
import { SearchIcon } from '@patternfly/react-icons'; |
| 28 |
import { SortByDirection } from "@patternfly/react-table"; |
| 29 |
import { account_create_dialog } from "./account-create-dialog.js"; |
| 30 |
import { delete_account_dialog } from "./delete-account-dialog.js"; |
| 31 |
import { group_create_dialog } from "./group-create-dialog.js"; |
| 32 |
import { lockAccountDialog } from "./lock-account-dialog.js"; |
| 33 |
import { logoutAccountDialog } from "./logout-account-dialog.js"; |
| 34 |
import { GroupActions } from "./group-actions.jsx"; |
| 35 |
|
| 36 |
import { usePageLocation } from "hooks"; |
| 37 |
import { KebabDropdown } from "cockpit-components-dropdown"; |
| 38 |
|
| 39 |
const _ = cockpit.gettext; |
| 40 |
|
| 41 |
const UserActions = ({ account, current }) => { |
| 42 |
const actions = [ |
| 43 |
<DropdownItem key="edit-user" |
| 44 |
onClick={ev => { ev.preventDefault(); cockpit.location.go(account.name) }}> |
| 45 |
{_("Edit user")} |
| 46 |
</DropdownItem>, |
| 47 |
]; |
| 48 |
|
| 49 |
superuser.allowed && actions.push( |
| 50 |
<Divider key="separator-0" />, |
| 51 |
<DropdownItem key="log-user-out" |
| 52 |
isDisabled={account.uid === 0 || !account.loggedIn || current } |
| 53 |
onClick={() => logoutAccountDialog(account) }> |
| 54 |
{_("Log user out")} |
| 55 |
</DropdownItem>, |
| 56 |
<Divider key="separator-1" />, |
| 57 |
<DropdownItem key="lock-account" |
| 58 |
isDisabled={account.isLocked} |
| 59 |
onClick={() => lockAccountDialog(account) }> |
| 60 |
{_("Lock account")} |
| 61 |
</DropdownItem>, |
| 62 |
<DropdownItem key="delete-account" |
| 63 |
isDisabled={account.uid === 0 || current} |
| 64 |
className={account.uid === 0 || current ? "" : "delete-resource-red"} |
| 65 |
onClick={() => delete_account_dialog(account) }> |
| 66 |
{_("Delete account")} |
| 67 |
</DropdownItem>, |
| 68 |
); |
| 69 |
|
| 70 |
return <KebabDropdown toggleButtonId="accounts-actions" dropdownItems={actions} />; |
| 71 |
}; |
| 72 |
|
| 73 |
const getGroupRow = (group, accounts) => { |
| 74 |
let groupColorClass; |
| 75 |
if (group.isAdmin) |
| 76 |
groupColorClass = "group-yellow"; |
| 77 |
else if (group.members > 0) |
| 78 |
groupColorClass = "group-blue"; |
| 79 |
else |
| 80 |
groupColorClass = "group-grey"; |
| 81 |
|
| 82 |
const columns = [ |
| 83 |
{ |
| 84 |
sortKey: group.name, |
| 85 |
title: ( |
| 86 |
<Flex alignItems={{ default: 'alignItemsCenter' }}> |
| 87 |
<div className={"dot " + groupColorClass} /> |
| 88 |
<FlexItem>{group.name}</FlexItem> |
| 89 |
</Flex> |
| 90 |
), |
| 91 |
props: { width: 20, }, |
| 92 |
}, |
| 93 |
{ |
| 94 |
title: group.gid, |
| 95 |
props: { width: 10, }, |
| 96 |
}, |
| 97 |
{ |
| 98 |
title: group.members, |
| 99 |
props: { width: 20, }, |
| 100 |
}, |
| 101 |
{ |
| 102 |
title: ( |
| 103 |
<Content> |
| 104 |
<Content component={ContentVariants.p}> |
| 105 |
{(group.userlistPrimary.concat(group.userlist)) |
| 106 |
.map(account => { |
| 107 |
if (accounts.map(account => account.name).includes(account)) |
| 108 |
return <Content key={account} component={ContentVariants.a} href={"#" + account}>{account}</Content>; |
| 109 |
else |
| 110 |
return account; |
| 111 |
}) |
| 112 |
.reduce((acc, curr) => [...acc, ", ", curr], []) |
| 113 |
.slice(1)} |
| 114 |
</Content> |
| 115 |
</Content> |
| 116 |
), |
| 117 |
props: { width: 50, }, |
| 118 |
}, |
| 119 |
]; |
| 120 |
|
| 121 |
if (superuser.allowed) { |
| 122 |
columns.push( |
| 123 |
{ |
| 124 |
title: <GroupActions group={group} />, |
| 125 |
props: { className: "pf-v6-c-table__action" } |
| 126 |
} |
| 127 |
); |
| 128 |
} |
| 129 |
|
| 130 |
return { columns, props: { "data-row-id": group.name, key: group.name } }; |
| 131 |
}; |
| 132 |
|
| 133 |
const getAccountRow = (account, current, groups) => { |
| 134 |
const userGroups = groups.filter(group => group.gid === account.gid || group.userlist.find(accountName => accountName === account.name)); |
| 135 |
const userGroupLabels = userGroups.map(group => { |
| 136 |
const color = group.isAdmin ? "yellow" : "blue"; |
| 137 |
return ( |
| 138 |
<Label key={group.name} variant="filled" color={color}> |
| 139 |
{!group.isAdmin ? group.name : ("admin" + " (" + group.name + ")") } |
| 140 |
</Label> |
| 141 |
); |
| 142 |
}); |
| 143 |
|
| 144 |
let loginText = ""; |
| 145 |
let loginSortKey = null; |
| 146 |
if (account.loggedIn) { |
| 147 |
loginText = _("Logged in"); |
| 148 |
loginSortKey = "logged in"; |
| 149 |
} else if (!account.lastLogin) { |
| 150 |
loginText = _("Never logged in"); |
| 151 |
loginSortKey = "never"; |
| 152 |
} else { |
| 153 |
loginSortKey = new Date(account.lastLogin); |
| 154 |
loginText = timeformat.dateTime(loginSortKey); |
| 155 |
} |
| 156 |
|
| 157 |
const columns = [ |
| 158 |
{ |
| 159 |
title: ( |
| 160 |
<span> |
| 161 |
<a href={"#/" + account.name}>{account.name}</a> |
| 162 |
{current && <Badge id="current-account-badge">{_("Your account")}</Badge>} |
| 163 |
</span> |
| 164 |
), |
| 165 |
sortKey: account.name, |
| 166 |
props: { width: 25, }, |
| 167 |
}, |
| 168 |
{ |
| 169 |
title: account.gecos.split(',')[0], |
| 170 |
props: { width: 20, }, |
| 171 |
}, |
| 172 |
{ |
| 173 |
title: account.uid, |
| 174 |
props: { width: 10, }, |
| 175 |
}, |
| 176 |
{ |
| 177 |
title: loginText, |
| 178 |
sortKey: loginSortKey, |
| 179 |
props: { width: 25, }, |
| 180 |
}, |
| 181 |
{ |
| 182 |
title: ( |
| 183 |
<Flex spaceItems={{ default: 'spaceItemsSm' }}> |
| 184 |
{userGroupLabels} |
| 185 |
</Flex> |
| 186 |
), |
| 187 |
props: { width: 20 }, |
| 188 |
}, |
| 189 |
{ |
| 190 |
title: <UserActions account={account} current={current} />, |
| 191 |
props: { className: "pf-v6-c-table__action" } |
| 192 |
}, |
| 193 |
]; |
| 194 |
|
| 195 |
return { columns, props: { "data-row-id": account.name, key: account.name } }; |
| 196 |
}; |
| 197 |
|
| 198 |
const mapGroupsToAccount = (accounts, groups) => { |
| 199 |
return accounts.map(account => { |
| 200 |
const accountGroups = []; |
| 201 |
groups.forEach(group => { |
| 202 |
if (group.userlist.find(accountName => accountName === account.name)) |
| 203 |
accountGroups.push(group.name); |
| 204 |
}); |
| 205 |
account.groups = accountGroups; |
| 206 |
|
| 207 |
return account; |
| 208 |
}); |
| 209 |
}; |
| 210 |
|
| 211 |
const GroupsList = ({ groups, accounts, isExpanded, setIsExpanded, min_gid, max_gid }) => { |
| 212 |
const { options } = usePageLocation(); |
| 213 |
|
| 214 |
const currentTextFilter = typeof options.group == "string" ? options.group : ''; |
| 215 |
const setCurrentTextFilter = val => { |
| 216 |
const newOptions = { ...cockpit.location.options }; |
| 217 |
if (val) |
| 218 |
newOptions.group = val; |
| 219 |
else |
| 220 |
delete newOptions.group; |
| 221 |
cockpit.location.replace(cockpit.location.path, newOptions); |
| 222 |
}; |
| 223 |
|
| 224 |
const filtered_groups = groups.filter(group => { |
| 225 |
if (currentTextFilter !== "" && |
| 226 |
(group.name.toLowerCase().indexOf(currentTextFilter.toLowerCase()) === -1) && |
| 227 |
(group.gid.toString().indexOf(currentTextFilter.toLowerCase()) === -1)) |
| 228 |
return false; |
| 229 |
|
| 230 |
return true; |
| 231 |
}); |
| 232 |
|
| 233 |
const columns = [ |
| 234 |
{ title: _("Group name"), sortable: true }, |
| 235 |
{ title: _("ID"), sortable: true }, |
| 236 |
{ title: _("# of users"), sortable: true }, |
| 237 |
{ title: _("Accounts") }, |
| 238 |
]; |
| 239 |
|
| 240 |
const sortRows = (rows, direction, idx) => { |
| 241 |
// GID and members columns are numeric |
| 242 |
const isNumeric = idx == 1 || idx == 2; |
| 243 |
const sortedRows = rows.sort((a, b) => { |
| 244 |
const aitem = a.columns[idx].sortKey || a.columns[idx].title; |
| 245 |
const bitem = b.columns[idx].sortKey || b.columns[idx].title; |
| 246 |
const aname = a.columns[0].sortKey; |
| 247 |
const bname = b.columns[0].sortKey; |
| 248 |
|
| 249 |
// administrator groups are always first |
| 250 |
if (admins.includes(aname)) |
| 251 |
return direction === SortByDirection.asc ? -1 : 1; |
| 252 |
if (admins.includes(bname)) |
| 253 |
return direction === SortByDirection.asc ? 1 : -1; |
| 254 |
|
| 255 |
if (isNumeric) |
| 256 |
return bitem - aitem; |
| 257 |
else |
| 258 |
return aitem.localeCompare(bitem); |
| 259 |
}); |
| 260 |
return direction === SortByDirection.asc ? sortedRows : sortedRows.reverse(); |
| 261 |
}; |
| 262 |
|
| 263 |
const tableToolbar = ( |
| 264 |
<Toolbar> |
| 265 |
<ToolbarContent className="groups-toolbar-header"> |
| 266 |
{isExpanded && <ToolbarItem> |
| 267 |
<SearchInput id="groups-filter" |
| 268 |
placeholder={_("Search for name or ID")} |
| 269 |
value={currentTextFilter} |
| 270 |
onChange={(_, val) => setCurrentTextFilter(val)} |
| 271 |
onClear={() => setCurrentTextFilter('')} /> |
| 272 |
</ToolbarItem>} |
| 273 |
{ superuser.allowed && |
| 274 |
<> |
| 275 |
{isExpanded && <ToolbarItem variant="separator" />} |
| 276 |
<ToolbarItem align={{ md: "alignEnd" }}> |
| 277 |
<Button variant="secondary" id="groups-create" onClick={() => group_create_dialog(groups, setIsExpanded, min_gid, max_gid)}> |
| 278 |
{_("Create new group")} |
| 279 |
</Button> |
| 280 |
</ToolbarItem> |
| 281 |
</> |
| 282 |
} |
| 283 |
</ToolbarContent> |
| 284 |
</Toolbar> |
| 285 |
); |
| 286 |
|
| 287 |
return ( |
| 288 |
<Card className="ct-card card-groups" isExpanded={isExpanded}> |
| 289 |
<CardHeader actions={{ actions: tableToolbar, hasNoOffset: true }} |
| 290 |
className="ct-card-expandable-header" |
| 291 |
onExpand={() => setIsExpanded(!isExpanded)} |
| 292 |
toggleButtonProps={{ |
| 293 |
id: 'groups-view-toggle', |
| 294 |
'aria-label': _("Groups"), |
| 295 |
'aria-expanded': isExpanded |
| 296 |
}}> |
| 297 |
<CardTitle className="pf-v6-l-flex pf-m-align-items-center pf-m-space-items-md"> |
| 298 |
{_("Groups")} |
| 299 |
{(!isExpanded && !groups.length) && <HelperText> <HelperTextItem variant="indeterminate">{_("Loading...")}</HelperTextItem></HelperText>} |
| 300 |
{(!isExpanded && filtered_groups.length > 0) && <> |
| 301 |
{filtered_groups.slice(0, 3) |
| 302 |
.map(group => { |
| 303 |
const color = group.isAdmin ? "yellow" : "blue"; |
| 304 |
return ( |
| 305 |
<Label key={group.name} variant="filled" color={color}> |
| 306 |
{group.name + ": " + (group.userlistPrimary.length + group.userlist.length)} |
| 307 |
</Label> |
| 308 |
); |
| 309 |
})} |
| 310 |
{filtered_groups.length > 3 && <Button key="more" className="group-more-btn" isInline variant='link' onClick={() => setIsExpanded(!isExpanded)}> |
| 311 |
{cockpit.format(_("$0 more..."), filtered_groups.length - 3)} |
| 312 |
</Button>} |
| 313 |
</>} |
| 314 |
</CardTitle> |
| 315 |
</CardHeader> |
| 316 |
<CardExpandableContent> |
| 317 |
<ListingTable columns={columns} |
| 318 |
id="groups-list" |
| 319 |
rows={ filtered_groups.map(a => getGroupRow(a, accounts)) } |
| 320 |
loading={ groups.length && accounts.length ? '' : _("Loading...") } |
| 321 |
sortMethod={sortRows} |
| 322 |
emptyComponent={<EmptyStatePanel title={_("No matching results")} icon={SearchIcon} action={_("Clear filter")} |
| 323 |
onAction={() => setCurrentTextFilter('')} actionVariant="link" />} |
| 324 |
variant="compact" sortBy={{ index: 2, direction: SortByDirection.asc }} /> |
| 325 |
</CardExpandableContent> |
| 326 |
</Card> |
| 327 |
); |
| 328 |
}; |
| 329 |
|
| 330 |
const AccountsList = ({ accounts, current_user, groups, min_uid, max_uid, shells }) => { |
| 331 |
const { options } = usePageLocation(); |
| 332 |
|
| 333 |
const currentTextFilter = typeof options.user == "string" ? options.user : ''; |
| 334 |
const setCurrentTextFilter = val => { |
| 335 |
const newOptions = { ...cockpit.location.options }; |
| 336 |
if (val) |
| 337 |
newOptions.user = val; |
| 338 |
else |
| 339 |
delete newOptions.user; |
| 340 |
cockpit.location.replace(cockpit.location.path, newOptions); |
| 341 |
}; |
| 342 |
|
| 343 |
const filtered_accounts = accounts.filter(account => { |
| 344 |
if (currentTextFilter !== "" && |
| 345 |
(account.name.toLowerCase().indexOf(currentTextFilter.toLowerCase()) === -1) && |
| 346 |
(account.gecos.toLowerCase().indexOf(currentTextFilter.toLowerCase()) === -1) && |
| 347 |
(account.uid.toString().indexOf(currentTextFilter.toLowerCase()) === -1) && |
| 348 |
(!account.groups.find(group => group.toLowerCase().indexOf(currentTextFilter.toLowerCase()) !== -1))) |
| 349 |
return false; |
| 350 |
|
| 351 |
return true; |
| 352 |
}); |
| 353 |
|
| 354 |
const columns = [ |
| 355 |
{ title: _("Username"), sortable: true }, |
| 356 |
{ title: _("Full name"), sortable: true }, |
| 357 |
{ title: _("ID"), sortable: true }, |
| 358 |
{ title: _("Last active"), sortable: true }, |
| 359 |
{ title: _("Group") }, |
| 360 |
{ title: "", sortable: false, props: { screenReaderText: _("Actions") } }, |
| 361 |
]; |
| 362 |
|
| 363 |
const sortRows = (rows, direction, idx) => { |
| 364 |
const sortedRows = rows.sort((a, b) => { |
| 365 |
const aitem = a.columns[idx]; |
| 366 |
const bitem = b.columns[idx]; |
| 367 |
const aname = a.columns[0]; |
| 368 |
const bname = b.columns[0]; |
| 369 |
|
| 370 |
// current user is always first |
| 371 |
if (aname.sortKey === current_user) |
| 372 |
return direction === SortByDirection.asc ? -1 : 1; |
| 373 |
if (bname.sortKey === current_user) |
| 374 |
return direction === SortByDirection.asc ? 1 : -1; |
| 375 |
// sorting last login |
| 376 |
if (idx === 3) { |
| 377 |
if (aitem.sortKey === "logged in") |
| 378 |
return -1; |
| 379 |
if (bitem.sortKey === "logged in") |
| 380 |
return 1; |
| 381 |
if (aitem.sortKey === "never") |
| 382 |
return 1; |
| 383 |
if (bitem.sortKey === "never") |
| 384 |
return -1; |
| 385 |
|
| 386 |
return bitem.sortKey - aitem.sortKey; |
| 387 |
} |
| 388 |
|
| 389 |
if (idx == 2) |
| 390 |
return bitem.title - aitem.title; |
| 391 |
return ((typeof aitem == 'string' ? aitem : (aitem.sortKey || aitem.title)).localeCompare(typeof bitem == 'string' ? bitem : (bitem.sortKey || bitem.title))); |
| 392 |
}); |
| 393 |
return direction === SortByDirection.asc ? sortedRows : sortedRows.reverse(); |
| 394 |
}; |
| 395 |
|
| 396 |
const tableToolbar = ( |
| 397 |
<Toolbar> |
| 398 |
<ToolbarContent className="accounts-toolbar-header"> |
| 399 |
<ToolbarItem> |
| 400 |
<SearchInput id="accounts-filter" |
| 401 |
placeholder={_("Search for name, group or ID")} |
| 402 |
value={currentTextFilter} |
| 403 |
onChange={(_, val) => setCurrentTextFilter(val)} |
| 404 |
onClear={() => setCurrentTextFilter('')} /> |
| 405 |
</ToolbarItem> |
| 406 |
{ superuser.allowed && |
| 407 |
<> |
| 408 |
<ToolbarItem variant="separator" /> |
| 409 |
<ToolbarItem align={{ md: "alignEnd" }}> |
| 410 |
<Button id="accounts-create" onClick={() => account_create_dialog(accounts, min_uid, max_uid, shells)}> |
| 411 |
{_("Create new account")} |
| 412 |
</Button> |
| 413 |
</ToolbarItem> |
| 414 |
</> |
| 415 |
} |
| 416 |
</ToolbarContent> |
| 417 |
</Toolbar> |
| 418 |
); |
| 419 |
|
| 420 |
return ( |
| 421 |
<Card isPlain className="ct-card"> |
| 422 |
<CardHeader actions={{ actions: tableToolbar }}> |
| 423 |
<CardTitle component="h2">{_("Accounts")}</CardTitle> |
| 424 |
</CardHeader> |
| 425 |
<ListingTable columns={columns} |
| 426 |
id="accounts-list" |
| 427 |
isEmptyStateInTable={currentTextFilter !== "" && filtered_accounts.length !== accounts.length} |
| 428 |
rows={ filtered_accounts.map(a => getAccountRow(a, current_user === a.name, groups)) } |
| 429 |
loading={ accounts.length ? '' : _("Loading...") } |
| 430 |
sortMethod={sortRows} |
| 431 |
emptyComponent={<EmptyStatePanel title={_("No matching results")} icon={SearchIcon} action={_("Clear filter")} |
| 432 |
onAction={() => setCurrentTextFilter('')} actionVariant="link" />} |
| 433 |
variant="compact" sortBy={{ index: 0, direction: SortByDirection.asc }} /> |
| 434 |
</Card> |
| 435 |
|
| 436 |
); |
| 437 |
}; |
| 438 |
|
| 439 |
export const AccountsMain = ({ accountsInfo, current_user, groups, isGroupsExpanded, setIsGroupsExpanded, min_gid, max_gid, min_uid, max_uid, shells }) => { |
| 440 |
const accounts = mapGroupsToAccount(accountsInfo, groups).filter(account => { |
| 441 |
if ((account.uid < 1000 && account.uid !== 0) || |
| 442 |
account.shell.match(/^(\/usr)?\/sbin\/nologin/) || |
| 443 |
account.shell === '/bin/false') |
| 444 |
return false; |
| 445 |
return true; |
| 446 |
}); |
| 447 |
|
| 448 |
return ( |
| 449 |
<Page id="accounts" className="pf-m-no-sidebar"> |
| 450 |
<PageSection hasBodyWrapper={false}> |
| 451 |
<Stack hasGutter> |
| 452 |
<GroupsList accounts={accounts} groups={groups} isExpanded={isGroupsExpanded} setIsExpanded={setIsGroupsExpanded} min_gid={min_gid} max_gid={max_gid} /> |
| 453 |
<AccountsList accounts={accounts} current_user={current_user} groups={groups} shells={shells} min_uid={min_uid} max_uid={max_uid} /> |
| 454 |
</Stack> |
| 455 |
</PageSection> |
| 456 |
</Page> |
| 457 |
); |
| 458 |
}; |