| 1 |
import React, { useState } from "react"; |
| 2 |
// PatternFly |
| 3 |
import { Badge, Tab, Tabs, TabTitleText } from "@patternfly/react-core"; |
| 4 |
// Data types |
| 5 |
import { Host } from "src/utils/datatypes/globalDataTypes"; |
| 6 |
// Navigation |
| 7 |
import { useNavigate } from "react-router"; |
| 8 |
// Layouts |
| 9 |
import TabLayout from "src/components/layouts/TabLayout"; |
| 10 |
// Hooks |
| 11 |
import useUpdateRoute from "src/hooks/useUpdateRoute"; |
| 12 |
// RPC |
| 13 |
import { useGetHostByIdQuery } from "src/services/rpcHosts"; |
| 14 |
// 'Is a member of' sections |
| 15 |
import MemberOfHostGroups from "src/components/MemberOf/MemberOfHostGroups"; |
| 16 |
import MemberOfNetgroups from "src/components/MemberOf/MemberOfNetgroups"; |
| 17 |
import MemberOfRoles from "src/components/MemberOf/MemberOfRoles"; |
| 18 |
import MemberOfHbacRules from "src/components/MemberOf/MemberOfHbacRules"; |
| 19 |
import MemberOfSudoRules from "src/components/MemberOf/MemberOfSudoRules"; |
| 20 |
import { MembershipDirection } from "src/components/MemberOf/MemberOfToolbar"; |
| 21 |
|
| 22 |
interface PropsToHostsMemberOf { |
| 23 |
host: Host; |
| 24 |
tabSection: string; |
| 25 |
} |
| 26 |
|
| 27 |
const HostsMemberOf = (props: PropsToHostsMemberOf) => { |
| 28 |
const navigate = useNavigate(); |
| 29 |
|
| 30 |
// Host's full data |
| 31 |
const hostQuery = useGetHostByIdQuery(props.host.fqdn); |
| 32 |
const hostData = hostQuery.data || {}; |
| 33 |
|
| 34 |
const [host, setHost] = useState<Partial<Host>>({}); |
| 35 |
|
| 36 |
React.useEffect(() => { |
| 37 |
if (!hostQuery.isFetching && hostData) { |
| 38 |
setHost({ ...hostData }); |
| 39 |
} |
| 40 |
}, [hostData, hostQuery.isFetching]); |
| 41 |
|
| 42 |
const onRefreshHostData = () => { |
| 43 |
hostQuery.refetch(); |
| 44 |
}; |
| 45 |
|
| 46 |
// Update current route data to Redux and highlight the current page in the Nav bar |
| 47 |
useUpdateRoute({ pathname: "hosts", noBreadcrumb: true }); |
| 48 |
|
| 49 |
const handleTabClick = ( |
| 50 |
_event: React.MouseEvent<HTMLElement, MouseEvent>, |
| 51 |
tabIndex: number | string |
| 52 |
) => { |
| 53 |
navigate("/hosts/" + props.host.fqdn + "/" + tabIndex); |
| 54 |
}; |
| 55 |
|
| 56 |
const [groupCount, setGroupCount] = React.useState(0); |
| 57 |
const [netgroupCount, setNetgroupCount] = React.useState(0); |
| 58 |
const [roleCount, setRoleCount] = React.useState(0); |
| 59 |
const [hbacCount, setHbacCount] = React.useState(0); |
| 60 |
const [sudoCount, setSudoCount] = React.useState(0); |
| 61 |
const [groupDirection, setGroupDirection] = React.useState( |
| 62 |
"direct" as MembershipDirection |
| 63 |
); |
| 64 |
const [netgroupDirection, setNetgroupDirection] = React.useState( |
| 65 |
"direct" as MembershipDirection |
| 66 |
); |
| 67 |
const [roleDirection, setRoleDirection] = React.useState( |
| 68 |
"direct" as MembershipDirection |
| 69 |
); |
| 70 |
const [hbacDirection, setHbacDirection] = React.useState( |
| 71 |
"direct" as MembershipDirection |
| 72 |
); |
| 73 |
const [sudoDirection, setSudoDirection] = React.useState( |
| 74 |
"direct" as MembershipDirection |
| 75 |
); |
| 76 |
|
| 77 |
const updateGroupDirection = (direction: MembershipDirection) => { |
| 78 |
if (direction === "direct") { |
| 79 |
setGroupCount( |
| 80 |
host && host.memberof_hostgroup ? host.memberof_hostgroup.length : 0 |
| 81 |
); |
| 82 |
} else { |
| 83 |
setGroupCount( |
| 84 |
host && host.memberofindirect_hostgroup |
| 85 |
? host.memberofindirect_hostgroup.length |
| 86 |
: 0 |
| 87 |
); |
| 88 |
} |
| 89 |
setGroupDirection(direction); |
| 90 |
}; |
| 91 |
const updateNetgroupDirection = (direction: MembershipDirection) => { |
| 92 |
if (direction === "direct") { |
| 93 |
setNetgroupCount( |
| 94 |
host && host.memberof_netgroup ? host.memberof_netgroup.length : 0 |
| 95 |
); |
| 96 |
} else { |
| 97 |
setNetgroupCount( |
| 98 |
host && host.memberofindirect_netgroup |
| 99 |
? host.memberofindirect_netgroup.length |
| 100 |
: 0 |
| 101 |
); |
| 102 |
} |
| 103 |
setNetgroupDirection(direction); |
| 104 |
}; |
| 105 |
const updateRoleDirection = (direction: MembershipDirection) => { |
| 106 |
if (direction === "direct") { |
| 107 |
setRoleCount(host && host.memberof_role ? host.memberof_role.length : 0); |
| 108 |
} else { |
| 109 |
setRoleCount( |
| 110 |
host && host.memberofindirect_role |
| 111 |
? host.memberofindirect_role.length |
| 112 |
: 0 |
| 113 |
); |
| 114 |
} |
| 115 |
setRoleDirection(direction); |
| 116 |
}; |
| 117 |
const updateHbacDirection = (direction: MembershipDirection) => { |
| 118 |
if (direction === "direct") { |
| 119 |
setHbacCount( |
| 120 |
host && host.memberof_hbacrule ? host.memberof_hbacrule.length : 0 |
| 121 |
); |
| 122 |
} else { |
| 123 |
setHbacCount( |
| 124 |
host && host.memberofindirect_hbacrule |
| 125 |
? host.memberofindirect_hbacrule.length |
| 126 |
: 0 |
| 127 |
); |
| 128 |
} |
| 129 |
setHbacDirection(direction); |
| 130 |
}; |
| 131 |
const updateSudoDirection = (direction: MembershipDirection) => { |
| 132 |
if (direction === "direct") { |
| 133 |
setSudoCount( |
| 134 |
host && host.memberof_sudorule ? host.memberof_sudorule.length : 0 |
| 135 |
); |
| 136 |
} else { |
| 137 |
setSudoCount( |
| 138 |
host && host.memberofindirect_sudorule |
| 139 |
? host.memberofindirect_sudorule.length |
| 140 |
: 0 |
| 141 |
); |
| 142 |
} |
| 143 |
setSudoDirection(direction); |
| 144 |
}; |
| 145 |
React.useEffect(() => { |
| 146 |
if (groupDirection === "direct") { |
| 147 |
setGroupCount( |
| 148 |
host && host.memberof_hostgroup ? host.memberof_hostgroup.length : 0 |
| 149 |
); |
| 150 |
} else { |
| 151 |
setGroupCount( |
| 152 |
host && host.memberofindirect_hostgroup |
| 153 |
? host.memberofindirect_hostgroup.length |
| 154 |
: 0 |
| 155 |
); |
| 156 |
} |
| 157 |
if (roleDirection === "direct") { |
| 158 |
setRoleCount(host && host.memberof_role ? host.memberof_role.length : 0); |
| 159 |
} else { |
| 160 |
setRoleCount( |
| 161 |
host && host.memberofindirect_role |
| 162 |
? host.memberofindirect_role.length |
| 163 |
: 0 |
| 164 |
); |
| 165 |
} |
| 166 |
if (hbacDirection === "direct") { |
| 167 |
setHbacCount( |
| 168 |
host && host.memberof_hbacrule ? host.memberof_hbacrule.length : 0 |
| 169 |
); |
| 170 |
} else { |
| 171 |
setHbacCount( |
| 172 |
host && host.memberofindirect_hbacrule |
| 173 |
? host.memberofindirect_hbacrule.length |
| 174 |
: 0 |
| 175 |
); |
| 176 |
} |
| 177 |
if (sudoDirection === "direct") { |
| 178 |
setSudoCount( |
| 179 |
host && host.memberof_sudorule ? host.memberof_sudorule.length : 0 |
| 180 |
); |
| 181 |
} else { |
| 182 |
setSudoCount( |
| 183 |
host && host.memberofindirect_sudorule |
| 184 |
? host.memberofindirect_sudorule.length |
| 185 |
: 0 |
| 186 |
); |
| 187 |
} |
| 188 |
if (netgroupDirection === "direct") { |
| 189 |
setNetgroupCount( |
| 190 |
host && host.memberof_netgroup ? host.memberof_netgroup.length : 0 |
| 191 |
); |
| 192 |
} else { |
| 193 |
setNetgroupCount( |
| 194 |
host && host.memberofindirect_netgroup |
| 195 |
? host.memberofindirect_netgroup.length |
| 196 |
: 0 |
| 197 |
); |
| 198 |
} |
| 199 |
}, [host]); |
| 200 |
|
| 201 |
// Render component |
| 202 |
return ( |
| 203 |
<div style={{ height: `var(--memberof-calc)` }}> |
| 204 |
<TabLayout id="memberof"> |
| 205 |
<Tabs |
| 206 |
activeKey={props.tabSection} |
| 207 |
onSelect={handleTabClick} |
| 208 |
isBox={false} |
| 209 |
mountOnEnter |
| 210 |
unmountOnExit |
| 211 |
> |
| 212 |
<Tab |
| 213 |
eventKey={"memberof_hostgroup"} |
| 214 |
name="memberof_hostgroup" |
| 215 |
title={ |
| 216 |
<TabTitleText> |
| 217 |
Host groups{" "} |
| 218 |
<Badge key={0} isRead> |
| 219 |
{groupCount} |
| 220 |
</Badge> |
| 221 |
</TabTitleText> |
| 222 |
} |
| 223 |
> |
| 224 |
<MemberOfHostGroups |
| 225 |
entity={host} |
| 226 |
id={host.fqdn as string} |
| 227 |
from={"hosts"} |
| 228 |
isDataLoading={hostQuery.isFetching} |
| 229 |
onRefreshData={onRefreshHostData} |
| 230 |
setDirection={updateGroupDirection} |
| 231 |
direction={groupDirection} |
| 232 |
/> |
| 233 |
</Tab> |
| 234 |
<Tab |
| 235 |
eventKey={"memberof_role"} |
| 236 |
name="memberof_role" |
| 237 |
title={ |
| 238 |
<TabTitleText> |
| 239 |
Roles{" "} |
| 240 |
<Badge key={0} isRead> |
| 241 |
{roleCount} |
| 242 |
</Badge> |
| 243 |
</TabTitleText> |
| 244 |
} |
| 245 |
> |
| 246 |
<MemberOfRoles |
| 247 |
entity={host} |
| 248 |
id={host.fqdn as string} |
| 249 |
from={"hosts"} |
| 250 |
isDataLoading={hostQuery.isFetching} |
| 251 |
onRefreshData={onRefreshHostData} |
| 252 |
setDirection={updateRoleDirection} |
| 253 |
direction={groupDirection} |
| 254 |
/> |
| 255 |
</Tab> |
| 256 |
<Tab |
| 257 |
eventKey={"memberof_netgroup"} |
| 258 |
name="memberof_netgroup" |
| 259 |
title={ |
| 260 |
<TabTitleText> |
| 261 |
Netgroups{" "} |
| 262 |
<Badge key={1} isRead> |
| 263 |
{netgroupCount} |
| 264 |
</Badge> |
| 265 |
</TabTitleText> |
| 266 |
} |
| 267 |
> |
| 268 |
<MemberOfNetgroups |
| 269 |
entity={host} |
| 270 |
id={host.fqdn as string} |
| 271 |
from={"hosts"} |
| 272 |
isDataLoading={hostQuery.isFetching} |
| 273 |
onRefreshData={onRefreshHostData} |
| 274 |
setDirection={updateNetgroupDirection} |
| 275 |
direction={netgroupDirection} |
| 276 |
/> |
| 277 |
</Tab> |
| 278 |
<Tab |
| 279 |
eventKey={"memberof_hbacrule"} |
| 280 |
name="memberof_hbacrule" |
| 281 |
title={ |
| 282 |
<TabTitleText> |
| 283 |
HBAC rules{" "} |
| 284 |
<Badge key={3} isRead> |
| 285 |
{hbacCount} |
| 286 |
</Badge> |
| 287 |
</TabTitleText> |
| 288 |
} |
| 289 |
> |
| 290 |
<MemberOfHbacRules |
| 291 |
entity={host} |
| 292 |
id={host.fqdn as string} |
| 293 |
from={"hosts"} |
| 294 |
isDataLoading={hostQuery.isFetching} |
| 295 |
onRefreshData={onRefreshHostData} |
| 296 |
setDirection={updateHbacDirection} |
| 297 |
direction={hbacDirection} |
| 298 |
/> |
| 299 |
</Tab> |
| 300 |
<Tab |
| 301 |
eventKey={"memberof_sudorule"} |
| 302 |
name="memberof_sudorule" |
| 303 |
title={ |
| 304 |
<TabTitleText> |
| 305 |
Sudo rules{" "} |
| 306 |
<Badge key={4} isRead> |
| 307 |
{sudoCount} |
| 308 |
</Badge> |
| 309 |
</TabTitleText> |
| 310 |
} |
| 311 |
> |
| 312 |
<MemberOfSudoRules |
| 313 |
entity={host} |
| 314 |
id={host.fqdn as string} |
| 315 |
from={"hosts"} |
| 316 |
isDataLoading={hostQuery.isFetching} |
| 317 |
onRefreshData={onRefreshHostData} |
| 318 |
setDirection={updateSudoDirection} |
| 319 |
direction={sudoDirection} |
| 320 |
/> |
| 321 |
</Tab> |
| 322 |
</Tabs> |
| 323 |
</TabLayout> |
| 324 |
</div> |
| 325 |
); |
| 326 |
}; |
| 327 |
|
| 328 |
export default HostsMemberOf; |