use new regex pattern for permissions

This commit is contained in:
Maren Süwer
2018-10-02 15:27:51 +02:00
parent a120b5af62
commit b7ed7e44dd
10 changed files with 153 additions and 81 deletions

View File

@@ -1,5 +1,5 @@
// @flow // @flow
const nameRegex = /^([A-z0-9.\-_@]|[^ ]([A-z0-9.\-_@ ]*[A-z0-9.\-_@]|[^\s])?)$/; const nameRegex = /^[A-Za-z0-9\.\-_][A-Za-z0-9\.\-_@]*$/;
export const isNameValid = (name: string) => { export const isNameValid = (name: string) => {
return nameRegex.test(name); return nameRegex.test(name);

View File

@@ -5,6 +5,7 @@ describe("test name validation", () => {
it("should return false", () => { it("should return false", () => {
// invalid names taken from ValidationUtilTest.java // invalid names taken from ValidationUtilTest.java
const invalidNames = [ const invalidNames = [
"@test",
" test 123", " test 123",
" test 123 ", " test 123 ",
"test 123 ", "test 123 ",
@@ -35,10 +36,9 @@ describe("test name validation", () => {
"Test123-git", "Test123-git",
"Test_user-123.git", "Test_user-123.git",
"test@scm-manager.de", "test@scm-manager.de",
"test 123", "test123",
"tt", "tt",
"t", "t",
"valid_name", "valid_name",
"another1", "another1",
"stillValid", "stillValid",

View File

@@ -1,29 +0,0 @@
{
"permissions": {
"error-title": "Error",
"error-subtitle": "Unknown permissions error"
},
"permission": {
"name": "User or Group",
"type": "Type",
"group-permission": "Group Permission"
},
"edit-permission": {
"delete-button": "Delete",
"save-button": "Save Changes"
},
"delete-permission-button": {
"label": "Delete",
"confirm-alert": {
"title": "Delete permission",
"message": "Do you really want to delete the permission?",
"submit": "Yes",
"cancel": "No"
}
},
"add-permission": {
"add-permission-heading": "Add new Permission",
"submit-button": "Submit",
"name-input-invalid": "Permission is not allowed to start with an '@' or it already exists!"
}
}

View File

@@ -43,5 +43,30 @@
"submit": "Yes", "submit": "Yes",
"cancel": "No" "cancel": "No"
} }
} },
"permission": {
"error-title": "Error",
"error-subtitle": "Unknown permissions error",
"name": "User or Group",
"type": "Type",
"group-permission": "Group Permission",
"edit-permission": {
"delete-button": "Delete",
"save-button": "Save Changes"
},
"delete-permission-button": {
"label": "Delete",
"confirm-alert": {
"title": "Delete permission",
"message": "Do you really want to delete the permission?",
"submit": "Yes",
"cancel": "No"
}
},
"add-permission": {
"add-permission-heading": "Add new Permission",
"submit-button": "Submit",
"name-input-invalid": "Permission is not allowed to be empty! If it is not empty, your input name is invalid or it already exists!"
}
}
} }

View File

@@ -7,6 +7,7 @@ import type {
PermissionCollection, PermissionCollection,
PermissionEntry PermissionEntry
} from "@scm-manager/ui-types"; } from "@scm-manager/ui-types";
import * as validator from "./permissionValidation";
type Props = { type Props = {
t: string => string, t: string => string,
@@ -18,7 +19,8 @@ type Props = {
type State = { type State = {
name: string, name: string,
type: string, type: string,
groupPermission: boolean groupPermission: boolean,
valid: boolean
}; };
class CreatePermissionForm extends React.Component<Props, State> { class CreatePermissionForm extends React.Component<Props, State> {
@@ -28,7 +30,8 @@ class CreatePermissionForm extends React.Component<Props, State> {
this.state = { this.state = {
name: "", name: "",
type: "READ", type: "READ",
groupPermission: false groupPermission: false,
valid: false
}; };
} }
@@ -39,18 +42,15 @@ class CreatePermissionForm extends React.Component<Props, State> {
return ( return (
<div> <div>
<h2 className="subtitle"> <h2 className="subtitle">
{t("add-permission.add-permission-heading")} {t("permission.add-permission.add-permission-heading")}
</h2> </h2>
<form onSubmit={this.submit}> <form onSubmit={this.submit}>
<InputField <InputField
label={t("permission.name")} label={t("permission.name")}
value={name ? name : ""} value={name ? name : ""}
onChange={this.handleNameChange} onChange={this.handleNameChange}
validationError={ validationError={!this.state.valid}
this.currentPermissionIncludeName() || errorMessage={t("permission.add-permission.name-input-invalid")}
this.state.name.startsWith("@")
}
errorMessage={t("add-permission.name-input-invalid")}
/> />
<Checkbox <Checkbox
label={t("permission.group-permission")} label={t("permission.group-permission")}
@@ -63,39 +63,15 @@ class CreatePermissionForm extends React.Component<Props, State> {
type={type ? type : "READ"} type={type ? type : "READ"}
/> />
<SubmitButton <SubmitButton
label={t("add-permission.submit-button")} label={t("permission.add-permission.submit-button")}
loading={loading} loading={loading}
disabled={this.isValid()} disabled={!this.state.valid}
/> />
</form> </form>
</div> </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 => { submit = e => {
this.props.createPermission({ this.props.createPermission({
name: this.state.name, name: this.state.name,
@@ -110,7 +86,8 @@ class CreatePermissionForm extends React.Component<Props, State> {
this.setState({ this.setState({
name: "", name: "",
type: "READ", type: "READ",
groupPermission: false groupPermission: false,
valid: validator.isNameValid("")
}); });
}; };
@@ -122,14 +99,24 @@ class CreatePermissionForm extends React.Component<Props, State> {
handleNameChange = (name: string) => { handleNameChange = (name: string) => {
this.setState({ this.setState({
name: name name: name,
valid: validator.isPermissionValid(
name,
this.state.groupPermission,
this.props.currentPermissions
)
}); });
}; };
handleGroupPermissionChange = (groupPermission: boolean) => { handleGroupPermissionChange = (groupPermission: boolean) => {
this.setState({ 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);

View File

@@ -34,15 +34,15 @@ class DeletePermissionButton extends React.Component<Props> {
confirmDelete = () => { confirmDelete = () => {
const { t } = this.props; const { t } = this.props;
confirmAlert({ confirmAlert({
title: t("delete-permission-button.confirm-alert.title"), title: t("permission.delete-permission-button.confirm-alert.title"),
message: t("delete-permission-button.confirm-alert.message"), message: t("permission.delete-permission-button.confirm-alert.message"),
buttons: [ buttons: [
{ {
label: t("delete-permission-button.confirm-alert.submit"), label: t("permission.delete-permission-button.confirm-alert.submit"),
onClick: () => this.deletePermission() onClick: () => this.deletePermission()
}, },
{ {
label: t("delete-permission-button.confirm-alert.cancel"), label: t("permission.delete-permission-button.confirm-alert.cancel"),
onClick: () => null onClick: () => null
} }
] ]
@@ -62,7 +62,7 @@ class DeletePermissionButton extends React.Component<Props> {
} }
return ( return (
<DeleteButton <DeleteButton
label={t("delete-permission-button.label")} label={t("permission.delete-permission-button.label")}
action={action} action={action}
loading={loading} loading={loading}
/> />
@@ -70,4 +70,4 @@ class DeletePermissionButton extends React.Component<Props> {
} }
} }
export default translate("permissions")(DeletePermissionButton); export default translate("repos")(DeletePermissionButton);

View File

@@ -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;
};

View File

@@ -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);
});
});

View File

@@ -92,8 +92,8 @@ class Permissions extends React.Component<Props> {
if (error) { if (error) {
return ( return (
<ErrorPage <ErrorPage
title={t("permissions.error-title")} title={t("permission.error-title")}
subtitle={t("permissions.error-subtitle")} subtitle={t("permission.error-subtitle")}
error={error} error={error}
/> />
); );
@@ -198,4 +198,4 @@ const mapDispatchToProps = dispatch => {
export default connect( export default connect(
mapStateToProps, mapStateToProps,
mapDispatchToProps mapDispatchToProps
)(translate("permissions")(Permissions)); )(translate("repos")(Permissions));

View File

@@ -173,4 +173,4 @@ const mapDispatchToProps = dispatch => {
export default connect( export default connect(
mapStateToProps, mapStateToProps,
mapDispatchToProps mapDispatchToProps
)(translate("permissions")(SinglePermission)); )(translate("repos")(SinglePermission));