| 1 |
import React, { useState } from "react"; |
| 2 |
// PatternFly |
| 3 |
import { Badge, Tab, Tabs, TabTitleText } from "@patternfly/react-core"; |
| 4 |
// Data types |
| 5 |
import { HostGroup } from "src/utils/datatypes/globalDataTypes"; |
| 6 |
// Navigation |
| 7 |
import { useNavigate } from "react-router"; |
| 8 |
// Layout |
| 9 |
import TabLayout from "src/components/layouts/TabLayout"; |
| 10 |
// Hooks |
| 11 |
import useUpdateRoute from "src/hooks/useUpdateRoute"; |
| 12 |
// RPC |
| 13 |
import { useGetHostGroupByIdQuery } from "src/services/rpcHostGroups"; |
| 14 |
// 'Is a member of' sections |
| 15 |
import MemberOfHostGroups from "src/components/MemberOf/MemberOfHostGroups"; |
| 16 |
import MemberOfNetgroups from "src/components/MemberOf/MemberOfNetgroups"; |
| 17 |
import MemberOfHbacRules from "src/components/MemberOf/MemberOfHbacRules"; |
| 18 |
import MemberOfSudoRules from "src/components/MemberOf/MemberOfSudoRules"; |
| 19 |
import { MembershipDirection } from "src/components/MemberOf/MemberOfToolbar"; |
| 20 |
|
| 21 |
interface PropsToMemberOf { |
| 22 |
hostGroup: HostGroup; |
| 23 |
tabSection: string; |
| 24 |
} |
| 25 |
|
| 26 |
const HostGroupsMemberOf = (props: PropsToMemberOf) => { |
| 27 |
const navigate = useNavigate(); |
| 28 |
|
| 29 |
// User group's full data |
| 30 |
const groupQuery = useGetHostGroupByIdQuery(props.hostGroup.cn); |
| 31 |
const groupData = groupQuery.data || {}; |
| 32 |
const [group, setGroup] = useState<Partial<HostGroup>>({}); |
| 33 |
|
| 34 |
React.useEffect(() => { |
| 35 |
if (!groupQuery.isFetching && groupData) { |
| 36 |
setGroup({ ...groupData }); |
| 37 |
} |
| 38 |
}, [groupData, groupQuery.isFetching]); |
| 39 |
|
| 40 |
const onRefreshData = () => { |
| 41 |
groupQuery.refetch(); |
| 42 |
}; |
| 43 |
|
| 44 |
// Update current route data to Redux and highlight the current page in the Nav bar |
| 45 |
useUpdateRoute({ pathname: "host-groups", noBreadcrumb: true }); |
| 46 |
|
| 47 |
const handleTabClick = ( |
| 48 |
_event: React.MouseEvent<HTMLElement, MouseEvent>, |
| 49 |
tabIndex: number | string |
| 50 |
) => { |
| 51 |
navigate("/host-groups/" + props.hostGroup.cn + "/" + tabIndex); |
| 52 |
}; |
| 53 |
|
| 54 |
const [groupCount, setGroupCount] = React.useState(0); |
| 55 |
const [hbacCount, setHbacCount] = React.useState(0); |
| 56 |
const [sudoCount, setSudoCount] = React.useState(0); |
| 57 |
const [groupDirection, setGroupDirection] = React.useState( |
| 58 |
"direct" as MembershipDirection |
| 59 |
); |
| 60 |
const [hbacDirection, setHbacDirection] = React.useState( |
| 61 |
"direct" as MembershipDirection |
| 62 |
); |
| 63 |
const [sudoDirection, setSudoDirection] = React.useState( |
| 64 |
"direct" as MembershipDirection |
| 65 |
); |
| 66 |
|
| 67 |
const updateGroupDirection = (direction: MembershipDirection) => { |
| 68 |
if (direction === "direct") { |
| 69 |
setGroupCount( |
| 70 |
group && group.memberof_hostgroup ? group.memberof_hostgroup.length : 0 |
| 71 |
); |
| 72 |
} else { |
| 73 |
setGroupCount( |
| 74 |
group && group.memberofindirect_hostgroup |
| 75 |
? group.memberofindirect_hostgroup.length |
| 76 |
: 0 |
| 77 |
); |
| 78 |
} |
| 79 |
setGroupDirection(direction); |
| 80 |
}; |
| 81 |
const updateHbacDirection = (direction: MembershipDirection) => { |
| 82 |
if (direction === "direct") { |
| 83 |
setHbacCount( |
| 84 |
group && group.memberof_hbacrule ? group.memberof_hbacrule.length : 0 |
| 85 |
); |
| 86 |
} else { |
| 87 |
setHbacCount( |
| 88 |
group && group.memberofindirect_hbacrule |
| 89 |
? group.memberofindirect_hbacrule.length |
| 90 |
: 0 |
| 91 |
); |
| 92 |
} |
| 93 |
setHbacDirection(direction); |
| 94 |
}; |
| 95 |
const updateSudoDirection = (direction: MembershipDirection) => { |
| 96 |
if (direction === "direct") { |
| 97 |
setSudoCount( |
| 98 |
group && group.memberof_sudorule ? group.memberof_sudorule.length : 0 |
| 99 |
); |
| 100 |
} else { |
| 101 |
setSudoCount( |
| 102 |
group && group.memberofindirect_sudorule |
| 103 |
? group.memberofindirect_sudorule.length |
| 104 |
: 0 |
| 105 |
); |
| 106 |
} |
| 107 |
setSudoDirection(direction); |
| 108 |
}; |
| 109 |
|
| 110 |
React.useEffect(() => { |
| 111 |
if (groupDirection === "direct") { |
| 112 |
setGroupCount( |
| 113 |
group && group.memberof_hostgroup ? group.memberof_hostgroup.length : 0 |
| 114 |
); |
| 115 |
} else { |
| 116 |
setGroupCount( |
| 117 |
group && group.memberofindirect_hostgroup |
| 118 |
? group.memberofindirect_hostgroup.length |
| 119 |
: 0 |
| 120 |
); |
| 121 |
} |
| 122 |
if (hbacDirection === "direct") { |
| 123 |
setHbacCount( |
| 124 |
group && group.memberof_hbacrule ? group.memberof_hbacrule.length : 0 |
| 125 |
); |
| 126 |
} else { |
| 127 |
setHbacCount( |
| 128 |
group && group.memberofindirect_hbacrule |
| 129 |
? group.memberofindirect_hbacrule.length |
| 130 |
: 0 |
| 131 |
); |
| 132 |
} |
| 133 |
if (sudoDirection === "direct") { |
| 134 |
setSudoCount( |
| 135 |
group && group.memberof_sudorule ? group.memberof_sudorule.length : 0 |
| 136 |
); |
| 137 |
} else { |
| 138 |
setSudoCount( |
| 139 |
group && group.memberofindirect_sudorule |
| 140 |
? group.memberofindirect_sudorule.length |
| 141 |
: 0 |
| 142 |
); |
| 143 |
} |
| 144 |
}, [group]); |
| 145 |
|
| 146 |
// Render component |
| 147 |
return ( |
| 148 |
<div style={{ height: `var(--subsettings-calc)` }}> |
| 149 |
<TabLayout id="memberof"> |
| 150 |
<Tabs |
| 151 |
activeKey={props.tabSection} |
| 152 |
onSelect={handleTabClick} |
| 153 |
isBox={false} |
| 154 |
mountOnEnter |
| 155 |
unmountOnExit |
| 156 |
> |
| 157 |
<Tab |
| 158 |
eventKey={"memberof_hostgroup"} |
| 159 |
name="memberof_hostgroup" |
| 160 |
title={ |
| 161 |
<TabTitleText> |
| 162 |
Host groups{" "} |
| 163 |
<Badge key={0} id="group_count" isRead> |
| 164 |
{groupCount} |
| 165 |
</Badge> |
| 166 |
</TabTitleText> |
| 167 |
} |
| 168 |
> |
| 169 |
<MemberOfHostGroups |
| 170 |
entity={group} |
| 171 |
from="host-groups" |
| 172 |
id={group.cn as string} |
| 173 |
isDataLoading={groupQuery.isFetching} |
| 174 |
onRefreshData={onRefreshData} |
| 175 |
setDirection={updateGroupDirection} |
| 176 |
direction={groupDirection} |
| 177 |
/> |
| 178 |
</Tab> |
| 179 |
<Tab |
| 180 |
eventKey={"memberof_netgroup"} |
| 181 |
name="memberof_netgroup" |
| 182 |
title={ |
| 183 |
<TabTitleText> |
| 184 |
Netgroups{" "} |
| 185 |
<Badge key={1} id="netgroup_count" isRead> |
| 186 |
{group && group.memberof_netgroup |
| 187 |
? group.memberof_netgroup.length |
| 188 |
: 0} |
| 189 |
</Badge> |
| 190 |
</TabTitleText> |
| 191 |
} |
| 192 |
> |
| 193 |
<MemberOfNetgroups |
| 194 |
entity={group} |
| 195 |
id={group.cn as string} |
| 196 |
from={"host-groups"} |
| 197 |
isDataLoading={groupQuery.isFetching} |
| 198 |
onRefreshData={onRefreshData} |
| 199 |
/> |
| 200 |
</Tab> |
| 201 |
<Tab |
| 202 |
eventKey={"memberof_hbacrule"} |
| 203 |
name="memberof_hbacrule" |
| 204 |
title={ |
| 205 |
<TabTitleText> |
| 206 |
HBAC rules{" "} |
| 207 |
<Badge key={3} id="hbacrule_count" isRead> |
| 208 |
{hbacCount} |
| 209 |
</Badge> |
| 210 |
</TabTitleText> |
| 211 |
} |
| 212 |
> |
| 213 |
<MemberOfHbacRules |
| 214 |
entity={group} |
| 215 |
id={group.cn as string} |
| 216 |
from={"host-groups"} |
| 217 |
isDataLoading={groupQuery.isFetching} |
| 218 |
onRefreshData={onRefreshData} |
| 219 |
setDirection={updateHbacDirection} |
| 220 |
direction={hbacDirection} |
| 221 |
/> |
| 222 |
</Tab> |
| 223 |
<Tab |
| 224 |
eventKey={"memberof_sudorule"} |
| 225 |
name="memberof_sudorule" |
| 226 |
title={ |
| 227 |
<TabTitleText> |
| 228 |
Sudo rules{" "} |
| 229 |
<Badge key={4} id="sudorule_count" isRead> |
| 230 |
{sudoCount} |
| 231 |
</Badge> |
| 232 |
</TabTitleText> |
| 233 |
} |
| 234 |
> |
| 235 |
<MemberOfSudoRules |
| 236 |
entity={group} |
| 237 |
id={group.cn as string} |
| 238 |
from={"host-groups"} |
| 239 |
isDataLoading={groupQuery.isFetching} |
| 240 |
onRefreshData={onRefreshData} |
| 241 |
setDirection={updateSudoDirection} |
| 242 |
direction={sudoDirection} |
| 243 |
/> |
| 244 |
</Tab> |
| 245 |
</Tabs> |
| 246 |
</TabLayout> |
| 247 |
</div> |
| 248 |
); |
| 249 |
}; |
| 250 |
|
| 251 |
export default HostGroupsMemberOf; |