mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-09 23:15:43 +01:00
use new regex pattern for permissions
This commit is contained in:
@@ -7,6 +7,7 @@ import type {
|
||||
PermissionCollection,
|
||||
PermissionEntry
|
||||
} from "@scm-manager/ui-types";
|
||||
import * as validator from "./permissionValidation";
|
||||
|
||||
type Props = {
|
||||
t: string => string,
|
||||
@@ -18,7 +19,8 @@ type Props = {
|
||||
type State = {
|
||||
name: string,
|
||||
type: string,
|
||||
groupPermission: boolean
|
||||
groupPermission: boolean,
|
||||
valid: boolean
|
||||
};
|
||||
|
||||
class CreatePermissionForm extends React.Component<Props, State> {
|
||||
@@ -28,7 +30,8 @@ class CreatePermissionForm extends React.Component<Props, State> {
|
||||
this.state = {
|
||||
name: "",
|
||||
type: "READ",
|
||||
groupPermission: false
|
||||
groupPermission: false,
|
||||
valid: false
|
||||
};
|
||||
}
|
||||
|
||||
@@ -39,18 +42,15 @@ class CreatePermissionForm extends React.Component<Props, State> {
|
||||
return (
|
||||
<div>
|
||||
<h2 className="subtitle">
|
||||
{t("add-permission.add-permission-heading")}
|
||||
{t("permission.add-permission.add-permission-heading")}
|
||||
</h2>
|
||||
<form onSubmit={this.submit}>
|
||||
<InputField
|
||||
label={t("permission.name")}
|
||||
value={name ? name : ""}
|
||||
onChange={this.handleNameChange}
|
||||
validationError={
|
||||
this.currentPermissionIncludeName() ||
|
||||
this.state.name.startsWith("@")
|
||||
}
|
||||
errorMessage={t("add-permission.name-input-invalid")}
|
||||
validationError={!this.state.valid}
|
||||
errorMessage={t("permission.add-permission.name-input-invalid")}
|
||||
/>
|
||||
<Checkbox
|
||||
label={t("permission.group-permission")}
|
||||
@@ -63,39 +63,15 @@ class CreatePermissionForm extends React.Component<Props, State> {
|
||||
type={type ? type : "READ"}
|
||||
/>
|
||||
<SubmitButton
|
||||
label={t("add-permission.submit-button")}
|
||||
label={t("permission.add-permission.submit-button")}
|
||||
loading={loading}
|
||||
disabled={this.isValid()}
|
||||
disabled={!this.state.valid}
|
||||
/>
|
||||
</form>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
isValid = () => {
|
||||
if (
|
||||
this.state.name === null ||
|
||||
this.state.name === "" ||
|
||||
this.currentPermissionIncludeName() ||
|
||||
this.state.name.startsWith("@")
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
currentPermissionIncludeName = () => {
|
||||
for (let i = 0; i < this.props.currentPermissions.length; i++) {
|
||||
if (
|
||||
this.props.currentPermissions[i].name === this.state.name &&
|
||||
this.props.currentPermissions[i].groupPermission ==
|
||||
this.state.groupPermission
|
||||
)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
submit = e => {
|
||||
this.props.createPermission({
|
||||
name: this.state.name,
|
||||
@@ -110,7 +86,8 @@ class CreatePermissionForm extends React.Component<Props, State> {
|
||||
this.setState({
|
||||
name: "",
|
||||
type: "READ",
|
||||
groupPermission: false
|
||||
groupPermission: false,
|
||||
valid: validator.isNameValid("")
|
||||
});
|
||||
};
|
||||
|
||||
@@ -122,14 +99,24 @@ class CreatePermissionForm extends React.Component<Props, State> {
|
||||
|
||||
handleNameChange = (name: string) => {
|
||||
this.setState({
|
||||
name: name
|
||||
name: name,
|
||||
valid: validator.isPermissionValid(
|
||||
name,
|
||||
this.state.groupPermission,
|
||||
this.props.currentPermissions
|
||||
)
|
||||
});
|
||||
};
|
||||
handleGroupPermissionChange = (groupPermission: boolean) => {
|
||||
this.setState({
|
||||
groupPermission: groupPermission
|
||||
groupPermission: groupPermission,
|
||||
valid: validator.isPermissionValid(
|
||||
this.state.name,
|
||||
groupPermission,
|
||||
this.props.currentPermissions
|
||||
)
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
export default translate("permissions")(CreatePermissionForm);
|
||||
export default translate("repos")(CreatePermissionForm);
|
||||
|
||||
@@ -34,15 +34,15 @@ class DeletePermissionButton extends React.Component<Props> {
|
||||
confirmDelete = () => {
|
||||
const { t } = this.props;
|
||||
confirmAlert({
|
||||
title: t("delete-permission-button.confirm-alert.title"),
|
||||
message: t("delete-permission-button.confirm-alert.message"),
|
||||
title: t("permission.delete-permission-button.confirm-alert.title"),
|
||||
message: t("permission.delete-permission-button.confirm-alert.message"),
|
||||
buttons: [
|
||||
{
|
||||
label: t("delete-permission-button.confirm-alert.submit"),
|
||||
label: t("permission.delete-permission-button.confirm-alert.submit"),
|
||||
onClick: () => this.deletePermission()
|
||||
},
|
||||
{
|
||||
label: t("delete-permission-button.confirm-alert.cancel"),
|
||||
label: t("permission.delete-permission-button.confirm-alert.cancel"),
|
||||
onClick: () => null
|
||||
}
|
||||
]
|
||||
@@ -62,7 +62,7 @@ class DeletePermissionButton extends React.Component<Props> {
|
||||
}
|
||||
return (
|
||||
<DeleteButton
|
||||
label={t("delete-permission-button.label")}
|
||||
label={t("permission.delete-permission-button.label")}
|
||||
action={action}
|
||||
loading={loading}
|
||||
/>
|
||||
@@ -70,4 +70,4 @@ class DeletePermissionButton extends React.Component<Props> {
|
||||
}
|
||||
}
|
||||
|
||||
export default translate("permissions")(DeletePermissionButton);
|
||||
export default translate("repos")(DeletePermissionButton);
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
// @flow
|
||||
import { validation } from "@scm-manager/ui-components";
|
||||
import type {
|
||||
PermissionCollection,
|
||||
} from "@scm-manager/ui-types";
|
||||
const isNameValid = validation.isNameValid;
|
||||
|
||||
export { isNameValid };
|
||||
|
||||
export const isPermissionValid = (name: string, groupPermission: boolean, permissions: PermissionCollection) => {
|
||||
return isNameValid(name) && !currentPermissionIncludeName(name, groupPermission, permissions);
|
||||
};
|
||||
|
||||
const currentPermissionIncludeName = (name: string, groupPermission: boolean, permissions: PermissionCollection) => {
|
||||
for (let i = 0; i < permissions.length; i++) {
|
||||
if (
|
||||
permissions[i].name === name &&
|
||||
permissions[i].groupPermission == groupPermission
|
||||
)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
@@ -0,0 +1,66 @@
|
||||
//@flow
|
||||
import * as validator from "./permissionValidation";
|
||||
|
||||
describe("permission validation", () => {
|
||||
it("should return true if permission is valid and does not exist", () => {
|
||||
const permissions = [];
|
||||
const name = "PermissionName";
|
||||
const groupPermission = false;
|
||||
|
||||
expect(
|
||||
validator.isPermissionValid(name, groupPermission, permissions)
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it("should return true if permission is valid and does not exists with same group permission", () => {
|
||||
const permissions = [
|
||||
{
|
||||
name: "PermissionName",
|
||||
groupPermission: true,
|
||||
type: "READ"
|
||||
}
|
||||
];
|
||||
const name = "PermissionName";
|
||||
const groupPermission = false;
|
||||
|
||||
expect(
|
||||
validator.isPermissionValid(name, groupPermission, permissions)
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it("should return false if permission is valid but exists", () => {
|
||||
const permissions = [
|
||||
{
|
||||
name: "PermissionName",
|
||||
groupPermission: false,
|
||||
type: "READ"
|
||||
}
|
||||
];
|
||||
const name = "PermissionName";
|
||||
const groupPermission = false;
|
||||
|
||||
expect(
|
||||
validator.isPermissionValid(name, groupPermission, permissions)
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
it("should return false if permission does not exist but is invalid", () => {
|
||||
const permissions = [];
|
||||
const name = "@PermissionName";
|
||||
const groupPermission = false;
|
||||
|
||||
expect(
|
||||
validator.isPermissionValid(name, groupPermission, permissions)
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
it("should return false if permission is not valid and does not exist", () => {
|
||||
const permissions = [];
|
||||
const name = "@PermissionName";
|
||||
const groupPermission = false;
|
||||
|
||||
expect(
|
||||
validator.isPermissionValid(name, groupPermission, permissions)
|
||||
).toBe(false);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user