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

CRM-19773 - Call hook_civicrm_selectWhereClause when checking event permissions #9544

Merged
merged 1 commit into from
Apr 19, 2017

Conversation

colemanw
Copy link
Member

@colemanw colemanw commented Dec 15, 2016

* @return array
*/
public static function getLocationEvents() {
$events = array();
$ret = array(
Copy link
Contributor

Choose a reason for hiding this comment

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

So cool ...

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep, that's 3 levels of joins to get the state_province name :)

if ($userID = $session->get('userID')) {
$createdEvents = array_keys(CRM_Event_PseudoConstant::event(NULL, TRUE, "created_id={$userID}"));
}
$result = civicrm_api3('Event', 'get', array(
Copy link
Contributor

Choose a reason for hiding this comment

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

Wouldn't keeping $allEvents = CRM_Event_PseudoConstant::event(NULL, TRUE); be more efficient than an API call?

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 api call invokes hook_civicrm_selectWhereClause and the pseudoconstant lookup does not (I did add it there, but because of the $all flag being set to true it skips the hook). The pseudoconstant function is deprecated anyway.

@@ -482,7 +482,7 @@ public function whereClause(&$params, $sortBy = TRUE, $force) {
}
else {
$curDate = date('YmdHis');
$clauses[5] = "(end_date >= {$curDate} OR end_date IS NULL)";
$clauses[] = "(end_date >= {$curDate} OR end_date IS NULL)";
}
}
else {
Copy link
Contributor

Choose a reason for hiding this comment

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

So if I understand correctly we do not need to filter in the Manage Events page since the filtering takes place in CRM_Event_BAO_Event::checkPermissions() ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Right.

@colemanw
Copy link
Member Author

@civicrm-builder retest this please

@eileenmcnaughton
Copy link
Contributor

test this please

@eileenmcnaughton
Copy link
Contributor

It would be good to get a test on this

Copy link
Contributor

@jitendrapurohit jitendrapurohit left a comment

Choose a reason for hiding this comment

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

Tested, works fine. Also added a test #10125. Not sure why the build isn't passing here.

@colemanw
Copy link
Member Author

colemanw commented Apr 8, 2017

@civicrm-builder test this please

$result = civicrm_api3('Event', 'get', array(
'check_permissions' => 1,
'return' => 'title',
'created_id' => 'user_contact_id',
Copy link
Contributor

Choose a reason for hiding this comment

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

Build shows a break at this point - maybe all failing test needs $this->createLoggedInUser(); in them to validate 'created_id' => 'user_contact_id' param?

@eileenmcnaughton
Copy link
Contributor

I don't know why the test is failing but once you get it to pass it's good to merge based on @jitendrapurohit's review & my reading of the code - adding merge-on-pass

@eileenmcnaughton
Copy link
Contributor

@colemanw this has been reviewed AND @jitendrapurohit added a unit test! - but can't merge as tests are failing

I think the trick is to allow 'user_contact_id' to be converted to NULL & rely on whether it is mandatory or not to enforce the id - in the case of the failing tests it is not

  if (!empty($fieldValue) || $fieldValue === '0' || $fieldValue === 0) {
    // if value = 'user_contact_id' (or similar), replace value with contact id
    if (!is_numeric($fieldValue) && is_scalar($fieldValue)) {
      $realContactId = _civicrm_api3_resolve_contactID($fieldValue);
      if ('unknown-user' === $realContactId) {
        throw new API_Exception("\"$fieldName\" \"{$fieldValue}\" cannot be resolved to a contact ID", 2002, array('error_field' => $fieldName, "type" => "integer"));
      }
      elseif (is_numeric($realContactId)) {
        $fieldValue = $realContactId;
      }
    }
    if (!empty($fieldInfo['pseudoconstant']) || !empty($fieldInfo['options'])) {
      _civicrm_api3_api_match_pseudoconstant($fieldValue, $entity, $fieldName, $fieldInfo, $op);
    }

    // After swapping options, ensure we have an integer(s)
    foreach ((array) ($fieldValue) as $value) {
      if ($value && !is_numeric($value) && $value !== 'null' && !is_array($value)) {
        throw new API_Exception("$fieldName is not a valid integer", 2001, array('error_field' => $fieldName, "type" => "integer"));
      }
    }

@eileenmcnaughton
Copy link
Contributor

from function _civicrm_api3_validate_integer(&$params, $fieldName, &$fieldInfo, $entity) {

@eileenmcnaughton
Copy link
Contributor

@colemanw needs rebasing but will hopefully pass now

@colemanw colemanw merged commit 11243f3 into civicrm:master Apr 19, 2017
@colemanw colemanw deleted the CRM-19773 branch April 19, 2017 17:48
@eileenmcnaughton
Copy link
Contributor

@colemanw this appears to have caused a regression due to default limit of 25 on the api calls

@mfb
Copy link
Contributor

mfb commented May 10, 2017

Yes, it's fixed if I add 'options' => array('limit' => 0) to these API calls. See also https://civicrm.stackexchange.com/questions/18516/events-missing-after-upgrade-to-4-7-19

@colemanw
Copy link
Member Author

I've opened a PR - #10327

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.

6 participants