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
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) => {
return nameRegex.test(name);

View File

@@ -5,6 +5,7 @@ describe("test name validation", () => {
it("should return false", () => {
// invalid names taken from ValidationUtilTest.java
const invalidNames = [
"@test",
" test 123",
" test 123 ",
"test 123 ",
@@ -38,7 +39,6 @@ describe("test name validation", () => {
"test123",
"tt",
"t",
"valid_name",
"another1",
"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",
"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,
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);

View File

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

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) {
return (
<ErrorPage
title={t("permissions.error-title")}
subtitle={t("permissions.error-subtitle")}
title={t("permission.error-title")}
subtitle={t("permission.error-subtitle")}
error={error}
/>
);
@@ -198,4 +198,4 @@ const mapDispatchToProps = dispatch => {
export default connect(
mapStateToProps,
mapDispatchToProps
)(translate("permissions")(Permissions));
)(translate("repos")(Permissions));

View File

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