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

Ensure custom group name does not conflict with existing field #20694

Merged
merged 1 commit into from
Jun 25, 2021

Conversation

colemanw
Copy link
Member

Overview

Adds validation when creating a new custom group to prevent name conflicts with existing fields.

Technical Details

In APIv4, you access custom fields like myCustomGroup.my_field. That works as long as the custom group isn't named something like "contact_type" or "activity_type_id" which would lead to very confusing api calls with unpredictable results.

Comments

Validates according to the entity being extended.

@civibot
Copy link

civibot bot commented Jun 23, 2021

(Standard links)

@civibot civibot bot added the master label Jun 23, 2021
}
else {
$group->name = CRM_Utils_String::munge($group->title, '_', 64);
$group->name = CRM_Utils_String::munge($group->title);
Copy link
Contributor

Choose a reason for hiding this comment

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

Just noting the default max length is 63 so this will work with the current table definition

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes this PR changes it from 64 down to 63 (which happens to be the default for that function) to accommodate the potential extra character inserted during validation.

}

self::validateCustomGroupName($group);
Copy link
Contributor

Choose a reason for hiding this comment

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

@colemanw should this only be run on creation?

Copy link
Member Author

Choose a reason for hiding this comment

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

It is.

Copy link
Contributor

Choose a reason for hiding this comment

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

right I see that now

if ($extendsDAO) {
$fields = array_column($extendsDAO::fields(), 'name');
if (in_array($group->name, $fields)) {
$group->name .= '0';
Copy link
Contributor

Choose a reason for hiding this comment

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

@colemanw what happens if you try to create a 2nd ActivityType Custom Group? i.e. one already has a 0 on it would that be an issue (this is probably quite the edge case but just thinking)

Copy link
Member Author

Choose a reason for hiding this comment

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

The current behavior is that if you try to create a custom group with a duplicate name (or for that matter, a duplicate title) then you'll get a DB error due to the unique key constraints.

This PR doesn't alter that.

@seamuslee001
Copy link
Contributor

This seems fine to me merging

@seamuslee001 seamuslee001 merged commit 53cb0b2 into civicrm:master Jun 25, 2021
@seamuslee001 seamuslee001 deleted the validateCustomGroupName branch June 25, 2021 07:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants