Skip to content

Commit

Permalink
SHIBUI-2092
Browse files Browse the repository at this point in the history
  • Loading branch information
chasegawa committed Sep 14, 2021
1 parent de5685e commit 580b0cf
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,5 @@ void doshibui_1740_migration() {
userService.save(user); // this will ensure group is set as the default user group
}
});

// SHIBUI-1743: Adding regex expression to groups
groupService.findAll().forEach(g -> {
g.setValidationRegex(Group.DEFAULT_REGEX);
try {
groupService.updateGroup(g);
}
catch (Exception e) {
// Shouldn't happen
e.printStackTrace();
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class Group implements Owner {
private String resourceId = UUID.randomUUID().toString();

@Column(name = "validation_regex")
private String validationRegex = DEFAULT_REGEX;
private String validationRegex;

/**
* Define a Group object based on the user
Expand All @@ -58,7 +58,6 @@ public Group(User user) {
resourceId = user.getUsername();
name = user.getUsername();
description = "default user-group";
validationRegex = DEFAULT_REGEX;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ public boolean doesStringMatchGroupPattern(String groupId, String uri) {
Group group = find(groupId);

String regExp = group.getValidationRegex();
if (StringUtils.isEmpty(regExp)) {
return true;
}

engine.put("str", uri);
try {
engine.eval("var rgx=" + regExp);
Expand Down Expand Up @@ -123,11 +127,10 @@ public Group updateGroup(Group group) throws EntityNotFoundException, InvalidGro
}

/**
* If the regex is missing, go with the default "anything goes" regex, otherwise validate that the pattern is valid to use.
* If the regex is blank simply return
*/
private void validateGroupRegex(Group group) throws InvalidGroupRegexException {
if (StringUtils.isEmpty(group.getValidationRegex())) {
group.setValidationRegex(Group.DEFAULT_REGEX);
return;
}
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,12 @@ class UsersControllerIntegrationTests extends AbstractBaseDataJpaTest {
.andExpect(jsonPath("\$.[1].emailAddress").value("peter@institution.edu"))
.andExpect(jsonPath("\$.[1].role").value("ROLE_USER"))
.andExpect(jsonPath("\$.[1].groupId").value("nonadmin"))
.andExpect(jsonPath("\$.[1].userGroups.[0].validationRegex").value(Group.DEFAULT_REGEX))
.andExpect(jsonPath("\$.[1].userGroups.[0].validationRegex").value(null))
.andExpect(jsonPath("\$.[2].username").value("none"))
.andExpect(jsonPath("\$.[2].emailAddress").value("badboy@institution.edu"))
.andExpect(jsonPath("\$.[2].role").value("ROLE_NONE"))
.andExpect(jsonPath("\$.[2].groupId").value("none"))
.andExpect(jsonPath("\$.[2].userGroups.[0].validationRegex").value(Group.DEFAULT_REGEX))
.andExpect(jsonPath("\$.[2].userGroups.[0].validationRegex").value(null))
.andExpect(jsonPath("\$.[3].username").value("anonymousUser"))
.andExpect(jsonPath("\$.[3].emailAddress").value("anon@institution.edu"))
.andExpect(jsonPath("\$.[3].role").value("ROLE_ADMIN"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class GroupServiceTests extends AbstractBaseDataJpaTest {
}

then:
g.getValidationRegex() == Group.DEFAULT_REGEX
g.getValidationRegex() == null

when:
g.setValidationRegex("/\\w\\b\\w/")
Expand Down

0 comments on commit 580b0cf

Please sign in to comment.