mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-12 16:35:45 +01:00
role-ready repositoryPermissionView
This commit is contained in:
@@ -4,7 +4,7 @@ import type {Links} from "./hal";
|
|||||||
export type PermissionCreateEntry = {
|
export type PermissionCreateEntry = {
|
||||||
name: string,
|
name: string,
|
||||||
role?: string,
|
role?: string,
|
||||||
verbs: string[],
|
verbs?: string[],
|
||||||
groupPermission: boolean
|
groupPermission: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -26,9 +26,11 @@ class AdvancedPermissionsDialog extends React.Component<Props, State> {
|
|||||||
|
|
||||||
const verbs = {};
|
const verbs = {};
|
||||||
props.availableVerbs.forEach(
|
props.availableVerbs.forEach(
|
||||||
verb => (verbs[verb] = props.selectedVerbs.includes(verb))
|
verb =>
|
||||||
|
(verbs[verb] = props.selectedVerbs
|
||||||
|
? props.selectedVerbs.includes(verb)
|
||||||
|
: false)
|
||||||
);
|
);
|
||||||
|
|
||||||
this.state = { verbs };
|
this.state = { verbs };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,12 @@
|
|||||||
// @flow
|
// @flow
|
||||||
import React from "react";
|
import React from "react";
|
||||||
import { translate } from "react-i18next";
|
import { translate } from "react-i18next";
|
||||||
|
import type {
|
||||||
|
RepositoryRole,
|
||||||
|
PermissionCollection,
|
||||||
|
PermissionCreateEntry,
|
||||||
|
SelectValue
|
||||||
|
} from "@scm-manager/ui-types";
|
||||||
import {
|
import {
|
||||||
Autocomplete,
|
Autocomplete,
|
||||||
SubmitButton,
|
SubmitButton,
|
||||||
@@ -8,32 +14,27 @@ import {
|
|||||||
LabelWithHelpIcon,
|
LabelWithHelpIcon,
|
||||||
Radio
|
Radio
|
||||||
} from "@scm-manager/ui-components";
|
} from "@scm-manager/ui-components";
|
||||||
import RoleSelector from "../components/RoleSelector";
|
|
||||||
import type {
|
|
||||||
RepositoryRole,
|
|
||||||
PermissionCollection,
|
|
||||||
PermissionCreateEntry,
|
|
||||||
SelectValue
|
|
||||||
} from "@scm-manager/ui-types";
|
|
||||||
import * as validator from "../components/permissionValidation";
|
import * as validator from "../components/permissionValidation";
|
||||||
import { findMatchingRoleName } from "../modules/permissions";
|
import RoleSelector from "../components/RoleSelector";
|
||||||
import AdvancedPermissionsDialog from "./AdvancedPermissionsDialog";
|
import AdvancedPermissionsDialog from "./AdvancedPermissionsDialog";
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
t: string => string,
|
|
||||||
availableRoles: RepositoryRole[],
|
availableRoles: RepositoryRole[],
|
||||||
availableVerbs: string[],
|
availableVerbs: string[],
|
||||||
createPermission: (permission: PermissionCreateEntry) => void,
|
createPermission: (permission: PermissionCreateEntry) => void,
|
||||||
loading: boolean,
|
loading: boolean,
|
||||||
currentPermissions: PermissionCollection,
|
currentPermissions: PermissionCollection,
|
||||||
groupAutoCompleteLink: string,
|
groupAutoCompleteLink: string,
|
||||||
userAutoCompleteLink: string
|
userAutoCompleteLink: string,
|
||||||
|
|
||||||
|
// Context props
|
||||||
|
t: string => string
|
||||||
};
|
};
|
||||||
|
|
||||||
type State = {
|
type State = {
|
||||||
name: string,
|
name: string,
|
||||||
role?: string,
|
role?: string,
|
||||||
verbs: string[],
|
verbs?: string[],
|
||||||
groupPermission: boolean,
|
groupPermission: boolean,
|
||||||
valid: boolean,
|
valid: boolean,
|
||||||
value?: SelectValue,
|
value?: SelectValue,
|
||||||
@@ -47,7 +48,7 @@ class CreatePermissionForm extends React.Component<Props, State> {
|
|||||||
this.state = {
|
this.state = {
|
||||||
name: "",
|
name: "",
|
||||||
role: props.availableRoles[0].name,
|
role: props.availableRoles[0].name,
|
||||||
verbs: props.availableRoles[0].verbs,
|
verbs: undefined,
|
||||||
groupPermission: false,
|
groupPermission: false,
|
||||||
valid: true,
|
valid: true,
|
||||||
value: undefined,
|
value: undefined,
|
||||||
@@ -92,6 +93,7 @@ class CreatePermissionForm extends React.Component<Props, State> {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
renderAutocompletionField = () => {
|
renderAutocompletionField = () => {
|
||||||
const { t } = this.props;
|
const { t } = this.props;
|
||||||
if (this.state.groupPermission) {
|
if (this.state.groupPermission) {
|
||||||
@@ -136,11 +138,9 @@ class CreatePermissionForm extends React.Component<Props, State> {
|
|||||||
|
|
||||||
render() {
|
render() {
|
||||||
const { t, availableRoles, availableVerbs, loading } = this.props;
|
const { t, availableRoles, availableVerbs, loading } = this.props;
|
||||||
|
const { role, verbs, showAdvancedDialog } = this.state;
|
||||||
const { verbs, showAdvancedDialog } = this.state;
|
|
||||||
|
|
||||||
const availableRoleNames = availableRoles.map(r => r.name);
|
const availableRoleNames = availableRoles.map(r => r.name);
|
||||||
const matchingRole = findMatchingRoleName(availableRoles, verbs);
|
|
||||||
|
|
||||||
const advancedDialog = showAdvancedDialog ? (
|
const advancedDialog = showAdvancedDialog ? (
|
||||||
<AdvancedPermissionsDialog
|
<AdvancedPermissionsDialog
|
||||||
@@ -189,7 +189,7 @@ class CreatePermissionForm extends React.Component<Props, State> {
|
|||||||
label={t("permission.role")}
|
label={t("permission.role")}
|
||||||
helpText={t("permission.help.roleHelpText")}
|
helpText={t("permission.help.roleHelpText")}
|
||||||
handleRoleChange={this.handleRoleChange}
|
handleRoleChange={this.handleRoleChange}
|
||||||
role={matchingRole}
|
role={role}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div className="column">
|
<div className="column">
|
||||||
@@ -249,8 +249,8 @@ class CreatePermissionForm extends React.Component<Props, State> {
|
|||||||
removeState = () => {
|
removeState = () => {
|
||||||
this.setState({
|
this.setState({
|
||||||
name: "",
|
name: "",
|
||||||
role: undefined,
|
role: this.props.availableRoles[0].name,
|
||||||
verbs: this.props.availableRoles[0].verbs,
|
verbs: undefined,
|
||||||
valid: true,
|
valid: true,
|
||||||
value: undefined
|
value: undefined
|
||||||
});
|
});
|
||||||
@@ -262,8 +262,7 @@ class CreatePermissionForm extends React.Component<Props, State> {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.setState({
|
this.setState({
|
||||||
role: selectedRole.name,
|
role: selectedRole.name
|
||||||
verbs: selectedRole.verbs
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,16 +1,12 @@
|
|||||||
// @flow
|
// @flow
|
||||||
import React from "react";
|
import React from "react";
|
||||||
import type {
|
import type { RepositoryRole, Permission } from "@scm-manager/ui-types";
|
||||||
RepositoryRole,
|
|
||||||
Permission
|
|
||||||
} from "@scm-manager/ui-types";
|
|
||||||
import { translate } from "react-i18next";
|
import { translate } from "react-i18next";
|
||||||
import {
|
import {
|
||||||
modifyPermission,
|
modifyPermission,
|
||||||
isModifyPermissionPending,
|
isModifyPermissionPending,
|
||||||
deletePermission,
|
deletePermission,
|
||||||
isDeletePermissionPending,
|
isDeletePermissionPending
|
||||||
findMatchingRoleName
|
|
||||||
} from "../modules/permissions";
|
} from "../modules/permissions";
|
||||||
import { connect } from "react-redux";
|
import { connect } from "react-redux";
|
||||||
import type { History } from "history";
|
import type { History } from "history";
|
||||||
@@ -47,7 +43,6 @@ type Props = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
type State = {
|
type State = {
|
||||||
role: string,
|
|
||||||
permission: Permission,
|
permission: Permission,
|
||||||
showAdvancedDialog: boolean
|
showAdvancedDialog: boolean
|
||||||
};
|
};
|
||||||
@@ -69,39 +64,34 @@ class SinglePermission extends React.Component<Props, State> {
|
|||||||
constructor(props: Props) {
|
constructor(props: Props) {
|
||||||
super(props);
|
super(props);
|
||||||
|
|
||||||
const defaultPermission = props.availableRoles
|
const defaultPermission = props.availableRepositoryRoles
|
||||||
? props.availableRoles[0]
|
? props.availableRepositoryRoles[0]
|
||||||
: {};
|
: {};
|
||||||
|
|
||||||
this.state = {
|
this.state = {
|
||||||
permission: {
|
permission: {
|
||||||
name: "",
|
name: "",
|
||||||
|
role: undefined,
|
||||||
verbs: defaultPermission.verbs,
|
verbs: defaultPermission.verbs,
|
||||||
groupPermission: false,
|
groupPermission: false,
|
||||||
_links: {}
|
_links: {}
|
||||||
},
|
},
|
||||||
role: defaultPermission.name,
|
|
||||||
showAdvancedDialog: false
|
showAdvancedDialog: false
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
componentDidMount() {
|
componentDidMount() {
|
||||||
const { availableRepositoryRoles, permission } = this.props;
|
const { permission } = this.props;
|
||||||
|
|
||||||
const matchingRole = findMatchingRoleName(
|
|
||||||
availableRepositoryRoles,
|
|
||||||
permission.verbs
|
|
||||||
);
|
|
||||||
|
|
||||||
if (permission) {
|
if (permission) {
|
||||||
this.setState({
|
this.setState({
|
||||||
permission: {
|
permission: {
|
||||||
name: permission.name,
|
name: permission.name,
|
||||||
|
role: permission.role,
|
||||||
verbs: permission.verbs,
|
verbs: permission.verbs,
|
||||||
groupPermission: permission.groupPermission,
|
groupPermission: permission.groupPermission,
|
||||||
_links: permission._links
|
_links: permission._links
|
||||||
},
|
}
|
||||||
role: matchingRole
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -115,7 +105,7 @@ class SinglePermission extends React.Component<Props, State> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const { role, permission, showAdvancedDialog } = this.state;
|
const { permission, showAdvancedDialog } = this.state;
|
||||||
const {
|
const {
|
||||||
t,
|
t,
|
||||||
availableRepositoryRoles,
|
availableRepositoryRoles,
|
||||||
@@ -125,18 +115,17 @@ class SinglePermission extends React.Component<Props, State> {
|
|||||||
repoName,
|
repoName,
|
||||||
classes
|
classes
|
||||||
} = this.props;
|
} = this.props;
|
||||||
const availableRoleNames = !!availableRepositoryRoles && availableRepositoryRoles.map(
|
const availableRoleNames =
|
||||||
r => r.name
|
!!availableRepositoryRoles && availableRepositoryRoles.map(r => r.name);
|
||||||
);
|
|
||||||
const readOnly = !this.mayChangePermissions();
|
const readOnly = !this.mayChangePermissions();
|
||||||
const roleSelector = readOnly ? (
|
const roleSelector = readOnly ? (
|
||||||
<td>{role}</td>
|
<td>{permission.role ? permission.role : t("permission.custom")}</td>
|
||||||
) : (
|
) : (
|
||||||
<td>
|
<td>
|
||||||
<RoleSelector
|
<RoleSelector
|
||||||
handleRoleChange={this.handleRoleChange}
|
handleRoleChange={this.handleRoleChange}
|
||||||
availableRoles={availableRoleNames}
|
availableRoles={availableRoleNames}
|
||||||
role={role}
|
role={permission.role}
|
||||||
loading={loading}
|
loading={loading}
|
||||||
/>
|
/>
|
||||||
</td>
|
</td>
|
||||||
@@ -154,9 +143,15 @@ class SinglePermission extends React.Component<Props, State> {
|
|||||||
|
|
||||||
const iconType =
|
const iconType =
|
||||||
permission && permission.groupPermission ? (
|
permission && permission.groupPermission ? (
|
||||||
<i title={t("permission.group")} className={classNames("fas fa-user-friends", classes.iconColor)} />
|
<i
|
||||||
|
title={t("permission.group")}
|
||||||
|
className={classNames("fas fa-user-friends", classes.iconColor)}
|
||||||
|
/>
|
||||||
) : (
|
) : (
|
||||||
<i title={t("permission.user")} className={classNames("fas fa-user", classes.iconColor)} />
|
<i
|
||||||
|
title={t("permission.user")}
|
||||||
|
className={classNames("fas fa-user", classes.iconColor)}
|
||||||
|
/>
|
||||||
);
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -199,42 +194,41 @@ class SinglePermission extends React.Component<Props, State> {
|
|||||||
|
|
||||||
submitAdvancedPermissionsDialog = (newVerbs: string[]) => {
|
submitAdvancedPermissionsDialog = (newVerbs: string[]) => {
|
||||||
const { permission } = this.state;
|
const { permission } = this.state;
|
||||||
const newRole = findMatchingRoleName(
|
|
||||||
this.props.availableRoles,
|
|
||||||
newVerbs
|
|
||||||
);
|
|
||||||
this.setState(
|
this.setState(
|
||||||
{
|
{
|
||||||
showAdvancedDialog: false,
|
showAdvancedDialog: false,
|
||||||
permission: { ...permission, verbs: newVerbs },
|
permission: { ...permission, role: undefined, verbs: newVerbs }
|
||||||
role: newRole
|
|
||||||
},
|
},
|
||||||
() => this.modifyPermission(newVerbs)
|
() => this.modifyPermissionVerbs(newVerbs)
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
handleRoleChange = (role: string) => {
|
handleRoleChange = (role: string) => {
|
||||||
const selectedRole = this.findAvailableRole(role);
|
const { permission } = this.state;
|
||||||
this.setState(
|
this.setState(
|
||||||
{
|
{
|
||||||
permission: {
|
permission: { ...permission, role: role, verbs: undefined }
|
||||||
...this.state.permission,
|
|
||||||
verbs: selectedRole.verbs
|
|
||||||
},
|
},
|
||||||
role: role
|
() => this.modifyPermissionRole(role)
|
||||||
},
|
|
||||||
() => this.modifyPermission(selectedRole.verbs)
|
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
findAvailableRole = (roleName: string) => {
|
findAvailableRole = (roleName: string) => {
|
||||||
const { availableRepositoryRoles } = this.props;
|
const { availableRepositoryRoles } = this.props;
|
||||||
return availableRepositoryRoles.find(
|
return availableRepositoryRoles.find(role => role.name === roleName);
|
||||||
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
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
modifyPermission = (verbs: string[]) => {
|
modifyPermissionVerbs = (verbs: string[]) => {
|
||||||
let permission = this.state.permission;
|
let permission = this.state.permission;
|
||||||
permission.verbs = verbs;
|
permission.verbs = verbs;
|
||||||
this.props.modifyPermission(
|
this.props.modifyPermission(
|
||||||
|
|||||||
@@ -739,35 +739,3 @@ export function getModifyPermissionsFailure(
|
|||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function findMatchingRoleName(
|
|
||||||
availableRoles: RepositoryRole[],
|
|
||||||
verbs: string[]
|
|
||||||
) {
|
|
||||||
if (!verbs) {
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
const matchingRole =
|
|
||||||
!!availableRoles &&
|
|
||||||
availableRoles.find(role => {
|
|
||||||
return equalVerbs(role.verbs, verbs);
|
|
||||||
});
|
|
||||||
|
|
||||||
if (matchingRole) {
|
|
||||||
return matchingRole.name;
|
|
||||||
} else {
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function equalVerbs(verbs1: string[], verbs2: string[]) {
|
|
||||||
if (!verbs1 || !verbs2) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (verbs1.length !== verbs2.length) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return verbs1.every(verb => verbs2.includes(verb));
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user