Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX Resolve deduping problem with group codes. #10333

Conversation

GuySartorelli
Copy link
Member

Group code deduping was introduced with #10113 but it had the unexpected side effect of changing a group code if the same code was set a second time on an existing group. Reproduction steps are in the parent issue.

As discussed in the parent issue I have moved the deduping logic to onBeforeWrite() - this makes it more robust as we know at write time that the code is unique. We could otherwise get into a situation where we are creating two new groups at the same time and they both end up with the same code.

There was also some validation added in that PR to throw a validation error if the code was duplicated - but because of the code deduping the validation error could never be thrown. I have removed that validation logic.

Parent issue

@GuySartorelli GuySartorelli force-pushed the pulls/4.10/fix-group-code-dedupe branch from 306bdfa to 8e9956b Compare May 24, 2022 23:51
@GuySartorelli GuySartorelli linked an issue May 24, 2022 that may be closed by this pull request
3 tasks
src/Security/Group.php Outdated Show resolved Hide resolved
@@ -522,16 +513,6 @@ public function validate()
->map('Code', 'Title')
->toArray();

if (isset($currentGroups[$this->Code])) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm just wondering if it's the correct approach to delete validation and just increment groups?

Is the reason why we are deleting the validation is because it's just never worked? Or it used to work and we accidentally broke it with a recent PR?

If it never worked then it makes sense to delete this code block (or at least move it to a private method for now link it back up in a future major). Otherwise wouldn't the correct fix here be to fix the broken validation rather than incrementing duplicate groups?

(I can see how enforcing validation when programatically setting a group would be difficult)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete validation and just increment groups

That's what's already happening. This validation code doesn't get reached at all because the code is already incremented in setCode() (prior to this PR).

Is the reason why we are deleting the validation is because it's just never worked? Or it used to work and we accidentally broke it with a recent PR?

Yes, we're deleting it because it never worked. It was added at the same time as the code in setCode() which removed deduping. Now that I'm moving the deduping logic to onBeforeWrite() the validation would kick in if I kept it here, and the deduping wouldn't happen, which would be a breaking change.
Based on the comments in the issue and in the original PR, it sounds like the idea was always to add deduping - there's no clear reason why this validation code was added in the first place but it just wasn't being called in any case.

@GuySartorelli GuySartorelli force-pushed the pulls/4.10/fix-group-code-dedupe branch from 8e9956b to 8fe54da Compare May 26, 2022 22:52
Copy link
Member

@emteknetnz emteknetnz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens in the scenario of this issue?

You can currently create a new Group in the Security section of the CMS and leave the Title blank or fill it with only whitespace.

#10113

src/Security/Group.php Outdated Show resolved Hide resolved
src/Security/Group.php Show resolved Hide resolved
Copy link
Member

@emteknetnz emteknetnz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens in the scenario of this issue?

You can currently create a new Group in the Security section of the CMS and leave the Title blank or fill it with only whitespace.

#10113

@GuySartorelli
Copy link
Member Author

The validation of group title that was put in place with that PR is still there. Both before and after the changes I'm proposing the following will be true:

  • You can not create a group with an empty title via the security admin - a validation error is thrown.
  • You can create a group with a name that is only whitespace.

Also remove dead validation code.
@GuySartorelli GuySartorelli force-pushed the pulls/4.10/fix-group-code-dedupe branch from 8fe54da to e0c4f01 Compare May 26, 2022 23:19
@emteknetnz emteknetnz dismissed their stale review May 26, 2022 23:22

Questions answered

Copy link
Member

@emteknetnz emteknetnz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, makes sense.

Looking back at the issue that proceeded this bug issue, I'm thinking we may need to broaden the scope here as we have a different set of rules for UI vs programatically. Perhaps we need to:
a) Add the same incrementing logic to Title if title is not blank
b) Add behat test to ensure that blank title throws validation warning in UI
c) Add unit test to ensure that blank Title is allowed when group is created programmatically

It may make sense to split this off handling duplicate Title as a separate issue? What do you think?

@GuySartorelli
Copy link
Member Author

GuySartorelli commented May 26, 2022

I think the title stuff is out of scope for this bugfix (This PR is about the Code specifically), but I can definitely see where you're coming from and agree we should add those tests. I've created #10336 for that and added it to the backlog.

Note that as far as deduping the title is concerned: There was validation added in that same PR in validate() which throws a validation error on duplicate titles. Because the PR didn't add any deduping of titles, that validation does take effect (unlike the Code validation).

Copy link
Member

@emteknetnz emteknetnz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK seems good

The unit test covers the programatically created group.

I did a front-end test to check the UI Title validation works, I then created a group and immediatly changed the Title, then created a new group with the original Title (so that it would have a duplicate Code as the first group), in the database it suffixed the new group code with -2

@emteknetnz emteknetnz merged commit 825dd4b into silverstripe:4.10 May 27, 2022
@emteknetnz emteknetnz deleted the pulls/4.10/fix-group-code-dedupe branch May 27, 2022 00:22
@emteknetnz
Copy link
Member

@GuySartorelli You should release a new patch version for 4.10.* and merge up to 4.11 (don't release as it's in beta, we can release 4.11.1 shortly after stable), and also merge-up to 4

@GuySartorelli
Copy link
Member Author

Tagged and merged up

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unexpected Group.Code value change in v4.10
2 participants