2019-01-24 09:53:26 +01:00
|
|
|
// @flow
|
|
|
|
|
import React from "react";
|
2019-05-15 17:30:48 +02:00
|
|
|
import type { RepositoryRole, Permission } from "@scm-manager/ui-types";
|
2019-01-24 09:53:26 +01:00
|
|
|
import { translate } from "react-i18next";
|
|
|
|
|
import {
|
|
|
|
|
modifyPermission,
|
|
|
|
|
isModifyPermissionPending,
|
|
|
|
|
deletePermission,
|
2019-05-16 12:19:52 +02:00
|
|
|
isDeletePermissionPending,
|
|
|
|
|
findVerbsForRole
|
2019-01-24 09:53:26 +01:00
|
|
|
} from "../modules/permissions";
|
|
|
|
|
import { connect } from "react-redux";
|
|
|
|
|
import type { History } from "history";
|
2019-06-12 15:29:53 +02:00
|
|
|
import { Button, Icon } from "@scm-manager/ui-components";
|
2019-01-24 09:53:26 +01:00
|
|
|
import DeletePermissionButton from "../components/buttons/DeletePermissionButton";
|
2019-01-25 08:26:18 +01:00
|
|
|
import RoleSelector from "../components/RoleSelector";
|
2019-01-24 11:53:57 +01:00
|
|
|
import AdvancedPermissionsDialog from "./AdvancedPermissionsDialog";
|
2019-02-01 15:31:14 +01:00
|
|
|
import classNames from "classnames";
|
|
|
|
|
import injectSheet from "react-jss";
|
2019-01-24 09:53:26 +01:00
|
|
|
|
|
|
|
|
type Props = {
|
2019-05-13 15:44:43 +02:00
|
|
|
availableRepositoryRoles: RepositoryRole[],
|
|
|
|
|
availableRepositoryVerbs: string[],
|
2019-01-24 09:53:26 +01:00
|
|
|
submitForm: Permission => void,
|
2019-02-01 14:17:43 +01:00
|
|
|
modifyPermission: (
|
|
|
|
|
permission: Permission,
|
|
|
|
|
namespace: string,
|
|
|
|
|
name: string
|
|
|
|
|
) => void,
|
2019-01-24 09:53:26 +01:00
|
|
|
permission: Permission,
|
|
|
|
|
t: string => string,
|
|
|
|
|
namespace: string,
|
|
|
|
|
repoName: string,
|
|
|
|
|
match: any,
|
|
|
|
|
history: History,
|
|
|
|
|
loading: boolean,
|
2019-02-01 14:17:43 +01:00
|
|
|
deletePermission: (
|
|
|
|
|
permission: Permission,
|
|
|
|
|
namespace: string,
|
|
|
|
|
name: string
|
|
|
|
|
) => void,
|
2019-02-01 15:31:14 +01:00
|
|
|
deleteLoading: boolean,
|
|
|
|
|
classes: any
|
2019-01-24 09:53:26 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type State = {
|
2019-01-24 11:53:57 +01:00
|
|
|
permission: Permission,
|
|
|
|
|
showAdvancedDialog: boolean
|
2019-01-24 09:53:26 +01:00
|
|
|
};
|
|
|
|
|
|
2019-02-01 15:31:14 +01:00
|
|
|
const styles = {
|
2019-02-01 16:25:32 +01:00
|
|
|
centerMiddle: {
|
|
|
|
|
display: "table-cell",
|
|
|
|
|
verticalAlign: "middle !important"
|
|
|
|
|
},
|
|
|
|
|
columnWidth: {
|
|
|
|
|
width: "100%"
|
2019-02-01 15:31:14 +01:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2019-01-24 09:53:26 +01:00
|
|
|
class SinglePermission extends React.Component<Props, State> {
|
|
|
|
|
constructor(props: Props) {
|
|
|
|
|
super(props);
|
|
|
|
|
|
2019-05-15 17:30:48 +02:00
|
|
|
const defaultPermission = props.availableRepositoryRoles
|
|
|
|
|
? props.availableRepositoryRoles[0]
|
2019-01-24 09:53:26 +01:00
|
|
|
: {};
|
|
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
|
permission: {
|
|
|
|
|
name: "",
|
2019-05-15 17:30:48 +02:00
|
|
|
role: undefined,
|
2019-01-24 09:53:26 +01:00
|
|
|
verbs: defaultPermission.verbs,
|
|
|
|
|
groupPermission: false,
|
|
|
|
|
_links: {}
|
|
|
|
|
},
|
2019-01-24 11:53:57 +01:00
|
|
|
showAdvancedDialog: false
|
2019-01-24 09:53:26 +01:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentDidMount() {
|
2019-05-15 17:30:48 +02:00
|
|
|
const { permission } = this.props;
|
2019-01-24 09:53:26 +01:00
|
|
|
|
|
|
|
|
if (permission) {
|
|
|
|
|
this.setState({
|
|
|
|
|
permission: {
|
|
|
|
|
name: permission.name,
|
2019-05-15 17:30:48 +02:00
|
|
|
role: permission.role,
|
2019-01-24 09:53:26 +01:00
|
|
|
verbs: permission.verbs,
|
|
|
|
|
groupPermission: permission.groupPermission,
|
|
|
|
|
_links: permission._links
|
2019-05-15 17:30:48 +02:00
|
|
|
}
|
2019-01-24 09:53:26 +01:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
deletePermission = () => {
|
|
|
|
|
this.props.deletePermission(
|
|
|
|
|
this.props.permission,
|
|
|
|
|
this.props.namespace,
|
|
|
|
|
this.props.repoName
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
render() {
|
2019-05-15 17:30:48 +02:00
|
|
|
const { permission, showAdvancedDialog } = this.state;
|
2019-01-24 13:34:05 +01:00
|
|
|
const {
|
|
|
|
|
t,
|
2019-05-13 15:44:43 +02:00
|
|
|
availableRepositoryRoles,
|
|
|
|
|
availableRepositoryVerbs,
|
2019-01-24 13:34:05 +01:00
|
|
|
loading,
|
|
|
|
|
namespace,
|
2019-02-01 15:31:14 +01:00
|
|
|
repoName,
|
|
|
|
|
classes
|
2019-01-24 13:34:05 +01:00
|
|
|
} = this.props;
|
2019-05-15 17:30:48 +02:00
|
|
|
const availableRoleNames =
|
|
|
|
|
!!availableRepositoryRoles && availableRepositoryRoles.map(r => r.name);
|
2019-01-25 13:55:04 +01:00
|
|
|
const readOnly = !this.mayChangePermissions();
|
|
|
|
|
const roleSelector = readOnly ? (
|
2019-05-15 17:30:48 +02:00
|
|
|
<td>{permission.role ? permission.role : t("permission.custom")}</td>
|
2019-01-25 13:55:04 +01:00
|
|
|
) : (
|
|
|
|
|
<td>
|
|
|
|
|
<RoleSelector
|
|
|
|
|
handleRoleChange={this.handleRoleChange}
|
|
|
|
|
availableRoles={availableRoleNames}
|
2019-05-15 17:30:48 +02:00
|
|
|
role={permission.role}
|
2019-01-25 13:55:04 +01:00
|
|
|
loading={loading}
|
|
|
|
|
/>
|
|
|
|
|
</td>
|
|
|
|
|
);
|
2019-01-24 09:53:26 +01:00
|
|
|
|
2019-05-16 12:19:52 +02:00
|
|
|
const selectedVerbs = permission.role
|
|
|
|
|
? findVerbsForRole(availableRepositoryRoles, permission.role)
|
|
|
|
|
: permission.verbs;
|
|
|
|
|
|
|
|
|
|
const advancedDialog = showAdvancedDialog ? (
|
2019-01-24 11:53:57 +01:00
|
|
|
<AdvancedPermissionsDialog
|
2019-01-25 13:55:04 +01:00
|
|
|
readOnly={readOnly}
|
2019-05-13 15:44:43 +02:00
|
|
|
availableVerbs={availableRepositoryVerbs}
|
2019-05-16 12:19:52 +02:00
|
|
|
selectedVerbs={selectedVerbs}
|
2019-01-24 11:53:57 +01:00
|
|
|
onClose={this.closeAdvancedPermissionsDialog}
|
|
|
|
|
onSubmit={this.submitAdvancedPermissionsDialog}
|
|
|
|
|
/>
|
|
|
|
|
) : null;
|
|
|
|
|
|
2019-02-01 15:31:14 +01:00
|
|
|
const iconType =
|
|
|
|
|
permission && permission.groupPermission ? (
|
2019-06-12 15:29:53 +02:00
|
|
|
<Icon title={t("permission.group")} name="user-friends" />
|
2019-02-01 15:31:14 +01:00
|
|
|
) : (
|
2019-06-12 15:29:53 +02:00
|
|
|
<Icon title={t("permission.user")} name="user" />
|
2019-02-01 15:31:14 +01:00
|
|
|
);
|
|
|
|
|
|
2019-01-24 09:53:26 +01:00
|
|
|
return (
|
2019-02-01 16:25:32 +01:00
|
|
|
<tr className={classes.columnWidth}>
|
|
|
|
|
<td className={classes.centerMiddle}>
|
2019-02-01 15:31:14 +01:00
|
|
|
{iconType} {permission.name}
|
|
|
|
|
</td>
|
2019-01-25 08:26:18 +01:00
|
|
|
{roleSelector}
|
2019-02-01 16:25:32 +01:00
|
|
|
<td className={classes.centerMiddle}>
|
2019-01-25 13:55:04 +01:00
|
|
|
<Button
|
|
|
|
|
label={t("permission.advanced-button.label")}
|
|
|
|
|
action={this.handleDetailedPermissionsPressed}
|
|
|
|
|
/>
|
|
|
|
|
</td>
|
2019-06-12 15:29:53 +02:00
|
|
|
<td className={classNames("is-darker", classes.centerMiddle)}>
|
2019-01-24 09:53:26 +01:00
|
|
|
<DeletePermissionButton
|
|
|
|
|
permission={permission}
|
|
|
|
|
namespace={namespace}
|
|
|
|
|
repoName={repoName}
|
|
|
|
|
deletePermission={this.deletePermission}
|
|
|
|
|
loading={this.props.deleteLoading}
|
|
|
|
|
/>
|
2019-05-16 12:19:52 +02:00
|
|
|
{advancedDialog}
|
2019-01-24 09:53:26 +01:00
|
|
|
</td>
|
|
|
|
|
</tr>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-25 13:55:04 +01:00
|
|
|
mayChangePermissions = () => {
|
|
|
|
|
return this.props.permission._links && this.props.permission._links.update;
|
|
|
|
|
};
|
|
|
|
|
|
2019-01-24 11:53:57 +01:00
|
|
|
handleDetailedPermissionsPressed = () => {
|
|
|
|
|
this.setState({ showAdvancedDialog: true });
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
closeAdvancedPermissionsDialog = () => {
|
|
|
|
|
this.setState({ showAdvancedDialog: false });
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
submitAdvancedPermissionsDialog = (newVerbs: string[]) => {
|
|
|
|
|
const { permission } = this.state;
|
|
|
|
|
this.setState(
|
|
|
|
|
{
|
|
|
|
|
showAdvancedDialog: false,
|
2019-05-15 17:30:48 +02:00
|
|
|
permission: { ...permission, role: undefined, verbs: newVerbs }
|
2019-01-24 11:53:57 +01:00
|
|
|
},
|
2019-05-15 17:30:48 +02:00
|
|
|
() => this.modifyPermissionVerbs(newVerbs)
|
2019-01-24 11:53:57 +01:00
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2019-01-25 08:26:18 +01:00
|
|
|
handleRoleChange = (role: string) => {
|
2019-05-15 17:30:48 +02:00
|
|
|
const { permission } = this.state;
|
2019-01-24 11:53:57 +01:00
|
|
|
this.setState(
|
|
|
|
|
{
|
2019-05-15 17:30:48 +02:00
|
|
|
permission: { ...permission, role: role, verbs: undefined }
|
2019-01-24 10:45:23 +01:00
|
|
|
},
|
2019-05-15 17:30:48 +02:00
|
|
|
() => this.modifyPermissionRole(role)
|
2019-01-24 11:53:57 +01:00
|
|
|
);
|
2019-01-24 10:29:51 +01:00
|
|
|
};
|
|
|
|
|
|
2019-01-25 08:26:18 +01:00
|
|
|
findAvailableRole = (roleName: string) => {
|
2019-05-13 15:44:43 +02:00
|
|
|
const { availableRepositoryRoles } = this.props;
|
2019-05-15 17:30:48 +02:00
|
|
|
return availableRepositoryRoles.find(role => role.name === roleName);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
modifyPermissionRole = (role: string) => {
|
|
|
|
|
let permission = this.state.permission;
|
|
|
|
|
permission.role = role;
|
|
|
|
|
this.props.modifyPermission(
|
|
|
|
|
permission,
|
|
|
|
|
this.props.namespace,
|
|
|
|
|
this.props.repoName
|
2019-01-24 10:29:51 +01:00
|
|
|
);
|
2019-01-24 09:53:26 +01:00
|
|
|
};
|
|
|
|
|
|
2019-05-15 17:30:48 +02:00
|
|
|
modifyPermissionVerbs = (verbs: string[]) => {
|
2019-01-24 09:53:26 +01:00
|
|
|
let permission = this.state.permission;
|
2019-01-24 10:29:51 +01:00
|
|
|
permission.verbs = verbs;
|
2019-01-24 09:53:26 +01:00
|
|
|
this.props.modifyPermission(
|
|
|
|
|
permission,
|
|
|
|
|
this.props.namespace,
|
|
|
|
|
this.props.repoName
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const mapStateToProps = (state, ownProps) => {
|
|
|
|
|
const permission = ownProps.permission;
|
|
|
|
|
const loading = isModifyPermissionPending(
|
|
|
|
|
state,
|
|
|
|
|
ownProps.namespace,
|
|
|
|
|
ownProps.repoName,
|
|
|
|
|
permission
|
|
|
|
|
);
|
|
|
|
|
const deleteLoading = isDeletePermissionPending(
|
|
|
|
|
state,
|
|
|
|
|
ownProps.namespace,
|
|
|
|
|
ownProps.repoName,
|
|
|
|
|
permission
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return { loading, deleteLoading };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const mapDispatchToProps = dispatch => {
|
|
|
|
|
return {
|
|
|
|
|
modifyPermission: (
|
|
|
|
|
permission: Permission,
|
|
|
|
|
namespace: string,
|
|
|
|
|
repoName: string
|
|
|
|
|
) => {
|
|
|
|
|
dispatch(modifyPermission(permission, namespace, repoName));
|
|
|
|
|
},
|
|
|
|
|
deletePermission: (
|
|
|
|
|
permission: Permission,
|
|
|
|
|
namespace: string,
|
|
|
|
|
repoName: string
|
|
|
|
|
) => {
|
|
|
|
|
dispatch(deletePermission(permission, namespace, repoName));
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
export default connect(
|
|
|
|
|
mapStateToProps,
|
|
|
|
|
mapDispatchToProps
|
2019-02-01 15:31:14 +01:00
|
|
|
)(translate("repos")(injectSheet(styles)(SinglePermission)));
|