Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix managed cluster access button for read only users #146

Merged
merged 11 commits into from
Dec 5, 2022
Merged
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file.

## Unreleased

### Fixed
- Fix access to cluster manage buttons shown for users without cluster write permission from [mabhi](https://github.com/mabhi)

## [0.1.5]- 2022-11-04
### Added
- Show last access time in users list page from [akshay196](https://github.com/akshay196)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ const DataTableToolbar = (props) => {
callGetEdges,
userAndRoleDetail,
data,
hasWriteAccessInCluster,
} = props;
const isClusterListAvailable = data.length !== 0;
const classes = useStyles();
Expand Down Expand Up @@ -221,15 +222,17 @@ const DataTableToolbar = (props) => {
)}
<div className="d-inline-block">
<DownloadKubeconfig user={userAndRoleDetail} withIcon />
<Button
variant="contained"
className="jr-btn jr-btn-label left text-nowrap text-white mr-3"
onClick={(event) => handleCreateClick(event)}
color="primary"
>
<i className="zmdi zmdi-plus zmdi-hc-fw " />
<span>New Cluster</span>
</Button>
{hasWriteAccessInCluster && (
<Button
variant="contained"
className="jr-btn jr-btn-label left text-nowrap text-white mr-3"
onClick={(event) => handleCreateClick(event)}
color="primary"
>
<i className="zmdi zmdi-plus zmdi-hc-fw " />
<span>New Cluster</span>
</Button>
)}
</div>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const dateFormatOptions = {

const SlatList = (props) => {
const [openClusterSharing, setOpenClusterSharing] = useState(false);
const { disableActions } = props;
const { hasWriteAccessInCluster, disableActions } = props;
const { UserSession, Projects, sshEdges, partnerDetail, alertsConfig } =
props.parentProps;

Expand Down Expand Up @@ -162,6 +162,7 @@ const SlatList = (props) => {
sshEdges,
partnerDetail,
alertsConfig,
hasWriteAccessInCluster,
}}
>
<ClusterActions
Expand Down
26 changes: 23 additions & 3 deletions src/app/routes/edges/routes/PrivateEdgeList/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1197,8 +1197,8 @@ class PrivateEdgeList extends React.Component {
const { order, orderBy, selected, rowsPerPage, page } = this.state;
const { match, UserSession, Projects, sshEdges, partnerDetail } =
this.props;
console.log(this.props);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's remove this if it was used just for debugging

let data = [];
const roles = [];
if (!this.state.edges) {
return null;
}
Expand Down Expand Up @@ -1250,6 +1250,20 @@ class PrivateEdgeList extends React.Component {
return ready;
};

const hasWriteAccessInCluster = (projectName) => {
let hasWriteAccess = false;
// if user has access to the current project or the user has org wide roles
var allPermissions = this.props.userAndRoleDetail.spec.permissions
.filter(
(obj) => obj.project === projectName || obj.scope === "organization"
)
.map((item) => item.permissions);
let deduplicatedPermissions = new Set(allPermissions.flat(1));
// only look out for cluster write permissions
hasWriteAccess = deduplicatedPermissions.has("cluster.write");
return hasWriteAccess;
};

return (
<Spinner loading={!this.state.edgesLoaded} hideChildren addHeight>
<div>
Expand All @@ -1273,6 +1287,9 @@ class PrivateEdgeList extends React.Component {
</div>
<Paper className="mb-4">
<DataTableToolbar
hasWriteAccessInCluster={hasWriteAccessInCluster(
this.props.UserSession.projectId
)}
renderInTable={this.state.renderInTable}
callGetEdges={this.callGetEdges}
setDefaultValue={this.setDefaultValue}
Expand Down Expand Up @@ -1349,7 +1366,7 @@ class PrivateEdgeList extends React.Component {
iconOnly={true}
/>
)}
{this.state.userRole !== "READ_ONLY_OPS" && (
{hasWriteAccessInCluster(n.metadata.project) && (
<Tooltip title="Kubectl Settings">
<IconButton
aria-label="edit"
Expand All @@ -1362,7 +1379,7 @@ class PrivateEdgeList extends React.Component {
</IconButton>
</Tooltip>
)}
{this.state.userRole !== "READ_ONLY_OPS" && (
{hasWriteAccessInCluster(n.metadata.project) && (
<DeleteIconComponent
key={n.metadata.name}
button={{
Expand Down Expand Up @@ -1411,6 +1428,9 @@ class PrivateEdgeList extends React.Component {
parentProps={{ ...this.props }}
index={index}
UserSession={this.props.UserSession}
hasWriteAccessInCluster={hasWriteAccessInCluster(
n.metadata.project
)}
isUpdateEndpointsSuccess={this.props.isUpdateEndpointsSuccess}
isUpdateEndpointsError={this.props.isUpdateEndpointsError}
UpdateEndpointsError={this.props.UpdateEndpointsError}
Expand Down
13 changes: 10 additions & 3 deletions src/components/ClusterActions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
DEFAULT_MENU_ITEMS,
MENU_ITEMS,
NOT_READY_MENU_ITEMS,
CLUSTER_RO_MENU_ITEMS,
} from "constants/ClusterActions";
import { downloadFile, parseError, useSnack } from "utils";
import {
Expand All @@ -28,9 +29,8 @@ const ClusterActions = ({
resumeAutoRefresh = null,
pauseAutoRefresh = null,
}) => {
const { UserSession, project, partnerDetail } = useContext(
ClusterActionsContext
);
const { hasWriteAccessInCluster, UserSession, project, partnerDetail } =
useContext(ClusterActionsContext);

const dispatch = useDispatch();
const history = useHistory();
Expand Down Expand Up @@ -86,6 +86,13 @@ const ClusterActions = ({
menuItems = menuItems.filter((item) =>
NOT_READY_MENU_ITEMS.includes(item)
);
// Remove actions for read-only clusters
if (!hasWriteAccessInCluster) {
menuItems = menuItems.filter(
(item) => !CLUSTER_RO_MENU_ITEMS.includes(item)
);
}

setMenuItems(menuItems);
};

Expand Down
2 changes: 2 additions & 0 deletions src/constants/ClusterActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ export const NOT_READY_MENU_ITEMS = [
"Download Kube Config",
"Delete",
];

export const CLUSTER_RO_MENU_ITEMS = ["Edit Labels", "Delete"];