2018-08-28 16:28:56 +02:00
|
|
|
// @flow
|
|
|
|
|
import React from "react";
|
2018-11-21 18:08:21 +01:00
|
|
|
import { translate } from "react-i18next";
|
|
|
|
|
import { SubmitButton } from "@scm-manager/ui-components";
|
2018-08-30 11:33:40 +02:00
|
|
|
import TypeSelector from "./TypeSelector";
|
2018-11-21 18:08:21 +01:00
|
|
|
import type {
|
|
|
|
|
PermissionCollection,
|
|
|
|
|
PermissionCreateEntry
|
|
|
|
|
} from "@scm-manager/ui-types";
|
2018-10-02 15:27:51 +02:00
|
|
|
import * as validator from "./permissionValidation";
|
2018-11-21 18:08:21 +01:00
|
|
|
import Autocomplete from "../../../containers/Autocomplete";
|
|
|
|
|
import type { SelectValue } from "../../../containers/Autocomplete";
|
2018-08-28 16:28:56 +02:00
|
|
|
|
|
|
|
|
type Props = {
|
2018-08-30 11:33:40 +02:00
|
|
|
t: string => string,
|
2018-10-18 15:20:30 +02:00
|
|
|
createPermission: (permission: PermissionCreateEntry) => void,
|
2018-09-03 15:17:57 +02:00
|
|
|
loading: boolean,
|
2018-11-21 18:08:21 +01:00
|
|
|
currentPermissions: PermissionCollection,
|
|
|
|
|
groupAutoCompleteLink: string,
|
|
|
|
|
userAutoCompleteLink: string
|
2018-08-28 16:28:56 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type State = {
|
2018-08-30 11:33:40 +02:00
|
|
|
name: string,
|
|
|
|
|
type: string,
|
2018-10-02 15:27:51 +02:00
|
|
|
groupPermission: boolean,
|
2018-11-21 18:08:21 +01:00
|
|
|
valid: boolean,
|
|
|
|
|
value?: SelectValue
|
2018-08-28 16:28:56 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class CreatePermissionForm extends React.Component<Props, State> {
|
|
|
|
|
constructor(props: Props) {
|
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
|
|
this.state = {
|
2018-08-30 11:33:40 +02:00
|
|
|
name: "",
|
2018-09-25 08:42:37 +02:00
|
|
|
type: "READ",
|
2018-10-02 15:27:51 +02:00
|
|
|
groupPermission: false,
|
2018-11-21 18:08:21 +01:00
|
|
|
valid: true,
|
|
|
|
|
value: undefined
|
2018-08-28 16:28:56 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-21 18:08:21 +01:00
|
|
|
permissionScopeChanged = event => {
|
|
|
|
|
const groupPermission = event.target.value === "GROUP_PERMISSION";
|
|
|
|
|
this.setState({
|
|
|
|
|
groupPermission: groupPermission,
|
|
|
|
|
valid: validator.isPermissionValid(
|
|
|
|
|
this.state.name,
|
|
|
|
|
groupPermission,
|
|
|
|
|
this.props.currentPermissions
|
|
|
|
|
)
|
|
|
|
|
});
|
|
|
|
|
this.setState({ ...this.state, groupPermission });
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
loadUserAutocompletion = (inputValue: string) => {
|
|
|
|
|
const url = this.props.userAutoCompleteLink + "?q=";
|
|
|
|
|
return fetch(url + inputValue)
|
|
|
|
|
.then(response => response.json())
|
|
|
|
|
.then(json => {
|
|
|
|
|
return json.map(element => {
|
|
|
|
|
return {
|
|
|
|
|
value: element,
|
|
|
|
|
label: `${element.displayName} (${element.id})`
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
loadGroupAutocompletion = (inputValue: string) => {
|
|
|
|
|
const url = this.props.groupAutoCompleteLink + "?q=";
|
|
|
|
|
return fetch(url + inputValue)
|
|
|
|
|
.then(response => response.json())
|
|
|
|
|
.then(json => {
|
|
|
|
|
return json.map(element => {
|
|
|
|
|
return {
|
|
|
|
|
value: element,
|
|
|
|
|
label: `${element.displayName} (${element.id})`
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
renderAutocompletionField = () => {
|
2018-11-22 10:55:21 +01:00
|
|
|
const { t } = this.props;
|
2018-11-21 18:08:21 +01:00
|
|
|
if (this.state.groupPermission) {
|
|
|
|
|
return (
|
|
|
|
|
<Autocomplete
|
|
|
|
|
loadSuggestions={this.loadGroupAutocompletion}
|
|
|
|
|
valueSelected={this.groupOrUserSelected}
|
|
|
|
|
value={this.state.value}
|
2018-11-22 10:55:21 +01:00
|
|
|
label={t("permission.group")}
|
|
|
|
|
noOptionsMessage={t("permission.autocomplete.no-group-options")}
|
|
|
|
|
loadingMessage={t("permission.autocomplete.loading")}
|
|
|
|
|
placeholder={t("permission.autocomplete.group-placeholder")}
|
2018-11-21 18:08:21 +01:00
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return (
|
|
|
|
|
<Autocomplete
|
|
|
|
|
loadSuggestions={this.loadUserAutocompletion}
|
|
|
|
|
valueSelected={this.groupOrUserSelected}
|
|
|
|
|
value={this.state.value}
|
2018-11-22 10:55:21 +01:00
|
|
|
label={t("permission.user")}
|
|
|
|
|
noOptionsMessage={t("permission.autocomplete.no-user-options")}
|
|
|
|
|
loadingMessage={t("permission.autocomplete.loading")}
|
|
|
|
|
placeholder={t("permission.autocomplete.user-placeholder")}
|
2018-11-21 18:08:21 +01:00
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
groupOrUserSelected = (value: SelectValue) => {
|
|
|
|
|
this.setState({
|
|
|
|
|
value,
|
|
|
|
|
name: value.value.id,
|
|
|
|
|
valid: validator.isPermissionValid(
|
|
|
|
|
value.value.id,
|
|
|
|
|
this.state.groupPermission,
|
|
|
|
|
this.props.currentPermissions
|
|
|
|
|
)
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2018-08-28 16:28:56 +02:00
|
|
|
render() {
|
2018-08-30 12:11:31 +02:00
|
|
|
const { t, loading } = this.props;
|
2018-11-21 18:08:21 +01:00
|
|
|
|
2018-11-22 10:18:53 +01:00
|
|
|
const { type } = this.state;
|
2018-08-30 11:33:40 +02:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div>
|
|
|
|
|
<h2 className="subtitle">
|
2018-10-02 15:27:51 +02:00
|
|
|
{t("permission.add-permission.add-permission-heading")}
|
2018-08-30 11:33:40 +02:00
|
|
|
</h2>
|
2018-09-11 16:17:11 +02:00
|
|
|
<form onSubmit={this.submit}>
|
2018-11-21 18:08:21 +01:00
|
|
|
<div className="control">
|
|
|
|
|
<label className="radio">
|
|
|
|
|
<input
|
|
|
|
|
type="radio"
|
|
|
|
|
name="permission_scope"
|
|
|
|
|
checked={!this.state.groupPermission}
|
|
|
|
|
value="USER_PERMISSION"
|
|
|
|
|
onChange={this.permissionScopeChanged}
|
|
|
|
|
/>
|
2018-11-22 10:18:53 +01:00
|
|
|
{t("permission.user-permission")}
|
2018-11-21 18:08:21 +01:00
|
|
|
</label>
|
|
|
|
|
<label className="radio">
|
|
|
|
|
<input
|
|
|
|
|
type="radio"
|
|
|
|
|
name="permission_scope"
|
|
|
|
|
value="GROUP_PERMISSION"
|
|
|
|
|
checked={this.state.groupPermission}
|
|
|
|
|
onChange={this.permissionScopeChanged}
|
|
|
|
|
/>
|
2018-11-22 10:18:53 +01:00
|
|
|
{t("permission.group-permission")}
|
2018-11-21 18:08:21 +01:00
|
|
|
</label>
|
|
|
|
|
</div>
|
|
|
|
|
{this.renderAutocompletionField()}
|
|
|
|
|
|
2018-09-11 16:17:11 +02:00
|
|
|
<TypeSelector
|
|
|
|
|
label={t("permission.type")}
|
2018-10-15 13:13:37 +02:00
|
|
|
helpText={t("permission.help.typeHelpText")}
|
2018-09-11 16:17:11 +02:00
|
|
|
handleTypeChange={this.handleTypeChange}
|
|
|
|
|
type={type ? type : "READ"}
|
|
|
|
|
/>
|
|
|
|
|
<SubmitButton
|
2018-10-02 15:27:51 +02:00
|
|
|
label={t("permission.add-permission.submit-button")}
|
2018-09-11 16:17:11 +02:00
|
|
|
loading={loading}
|
2018-10-18 15:20:30 +02:00
|
|
|
disabled={!this.state.valid || this.state.name === ""}
|
2018-09-11 16:17:11 +02:00
|
|
|
/>
|
|
|
|
|
</form>
|
2018-08-30 11:33:40 +02:00
|
|
|
</div>
|
|
|
|
|
);
|
2018-08-28 16:28:56 +02:00
|
|
|
}
|
2018-08-30 11:33:40 +02:00
|
|
|
|
2018-09-13 09:59:15 +02:00
|
|
|
submit = e => {
|
2018-08-30 11:33:40 +02:00
|
|
|
this.props.createPermission({
|
|
|
|
|
name: this.state.name,
|
|
|
|
|
type: this.state.type,
|
|
|
|
|
groupPermission: this.state.groupPermission
|
|
|
|
|
});
|
2018-09-11 11:17:11 +02:00
|
|
|
this.removeState();
|
2018-09-13 09:59:15 +02:00
|
|
|
e.preventDefault();
|
2018-09-11 11:17:11 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
removeState = () => {
|
|
|
|
|
this.setState({
|
|
|
|
|
name: "",
|
2018-09-25 08:49:24 +02:00
|
|
|
type: "READ",
|
2018-10-02 15:27:51 +02:00
|
|
|
groupPermission: false,
|
2018-10-02 15:30:23 +02:00
|
|
|
valid: true
|
2018-09-11 11:17:11 +02:00
|
|
|
});
|
2018-08-30 11:33:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
handleTypeChange = (type: string) => {
|
|
|
|
|
this.setState({
|
|
|
|
|
type: type
|
|
|
|
|
});
|
|
|
|
|
};
|
2018-08-28 16:28:56 +02:00
|
|
|
}
|
|
|
|
|
|
2018-10-02 15:27:51 +02:00
|
|
|
export default translate("repos")(CreatePermissionForm);
|