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

Catch and log nonexistent groups instead of a fatal error #114

Merged
merged 1 commit into from
Dec 27, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 20 additions & 8 deletions CRM/Contactlayout/BAO/ContactLayout.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static function getLayout($cid, $uid = NULL) {
$get->addClause('OR', $subClauses);

foreach ($get->execute() as $layout) {
if (self::userIsIn($uid, $layout['groups'])) {
if (self::checkGroupFilter($uid, $layout)) {
self::loadBlocks($layout, $contact['contact_type']);
return $layout;
}
Expand All @@ -48,22 +48,34 @@ public static function getLayout($cid, $uid = NULL) {
* Check if the user matches the group filter for a layout
*
* @param int $uid
* @param array|null $groups
* @param array $layout
*
* @return bool
*/
private static function userIsIn($uid, $groups) {
private static function checkGroupFilter($uid, $layout) {
// If no group filter, any user matches
if (!$groups) {
if (empty($layout['groups'])) {
return TRUE;
}
// Convert group names to ids, and verify groups exist
$groupIds = (array) civicrm_api4('Group', 'get', [
'where' => [['name', 'IN', $layout['groups']]],
], ['name' => 'id']);

// In case groups used by this layout have been deleted
if (count($groupIds) < count($layout['groups'])) {
Civi::log()->warning(sprintf('ContactLayout "%s" cannot filter on nonexistent group "%s".',
$layout['label'],
implode('" and "', array_diff($layout['groups'], array_keys($groupIds)))
));
}
if (!$groupIds) {
// Can't filter if the groups don't exist.
return TRUE;
}
$groupIds = array_map(function($groupName) {
return CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $groupName, 'id', 'name');
}, $groups);
return (bool) \Civi\Api4\Contact::get(FALSE)
->addSelect('id')
->addWhere('id', '=', $uid)
// TODO: Change this back to ('groups:name', 'IN', $groups) when fixed upstream
->addWhere('groups', 'IN', $groupIds)
->execute()->count();
}
Expand Down