CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • Autocomplete fields -- ACL cache database error
Pages: [1]

Author Topic: Autocomplete fields -- ACL cache database error  (Read 2138 times)

Matt2000

  • I post frequently
  • ***
  • Posts: 288
  • Karma: 27
    • http://www.ninjitsuweb.com
Autocomplete fields -- ACL cache database error
June 23, 2010, 02:10:21 pm
I'm working on making auto-complete fields work for anonymous users.

I've used hook_civicrm_xmlMenu() to modify the permission for civicrm/ajax/contactlist, and I've created an ACL to allow anonymous users to view the contacts that should be permitted.

But it still doesn't work, and when I view /civicrm/ajax/contactlist?reset=1&context=customfield&id=26 I see:

Code: [Select]
Sorry. A non-recoverable error has occurred.
DB Error: constraint violation

Database Error Code: Cannot add or update a child row: a foreign key constraint fails (`dev_civicrm/civicrm_acl_contact_cache`, CONSTRAINT `FK_civicrm_acl_contact_cache_user_id` FOREIGN KEY (`user_id`) REFERENCES `civicrm_contact` (`id`) ON DELETE CASCADE), 1452

Return to home page.

Error Details:

Array
(
    [callback] => Array
        (
            [0] => CRM_Core_Error
            [1] => handle
        )

    [code] => -3
    [message] => DB Error: constraint violation
    [mode] => 16
    [debug_info] => REPLACE INTO civicrm_acl_contact_cache ( user_id, contact_id, operation ) VALUES ( 0, 5, 'View' ),( 0, 8, 'View' ),( 0, 10, 'View' ),( 0, 12, 'View' ),( 0, 14, 'View' ),( 0, 16, 'View' ),( 0, 18, 'View' ),( 0, 20, 'View' ),( 0, 22, 'View' ),( 0, 24, 'View' ),( 0, 26, 'View' ),( 0, 28, 'View' ),( 0, 31, 'View' ),( 0, 33, 'View' ),( 0, 35, 'View' ),( 0, 37, 'View' ),( 0, 43, 'View' ),( 0, 45, 'View' ),( 0, 46, 'View' ),( 0, 48, 'View' ),( 0, 51, 'View' ),( 0, 53, 'View' ),( 0, 55, 'View' ),( 0, 58, 'View' ),( 0, 75, 'View' ),( 0, 76, 'View' ),( 0, 77, 'View' ),( 0, 78, 'View' ),( 0, 79, 'View' ),( 0, 80, 'View' ),( 0, 81, 'View' ); [nativecode=1452 ** Cannot add or update a child row: a foreign key constraint fails (`dev_civicrm/civicrm_acl_contact_cache`, CONSTRAINT `FK_civicrm_acl_contact_cache_user_id` FOREIGN KEY (`user_id`) REFERENCES `civicrm_contact` (`id`) ON DELETE CASCADE)]
    [type] => DB_Error
    [user_info] => REPLACE INTO civicrm_acl_contact_cache ( user_id, contact_id, operation ) VALUES ( 0, 5, 'View' ),( 0, 8, 'View' ),( 0, 10, 'View' ),( 0, 12, 'View' ),( 0, 14, 'View' ),( 0, 16, 'View' ),( 0, 18, 'View' ),( 0, 20, 'View' ),( 0, 22, 'View' ),( 0, 24, 'View' ),( 0, 26, 'View' ),( 0, 28, 'View' ),( 0, 31, 'View' ),( 0, 33, 'View' ),( 0, 35, 'View' ),( 0, 37, 'View' ),( 0, 43, 'View' ),( 0, 45, 'View' ),( 0, 46, 'View' ),( 0, 48, 'View' ),( 0, 51, 'View' ),( 0, 53, 'View' ),( 0, 55, 'View' ),( 0, 58, 'View' ),( 0, 75, 'View' ),( 0, 76, 'View' ),( 0, 77, 'View' ),( 0, 78, 'View' ),( 0, 79, 'View' ),( 0, 80, 'View' ),( 0, 81, 'View' ); [nativecode=1452 ** Cannot add or update a child row: a foreign key constraint fails (`dev_civicrm/civicrm_acl_contact_cache`, CONSTRAINT `FK_civicrm_acl_contact_cache_user_id` FOREIGN KEY (`user_id`) REFERENCES `civicrm_contact` (`id`) ON DELETE CASCADE)]
    [to_string] => [db_error: message="DB Error: constraint violation" code=-3 mode=callback callback=CRM_Core_Error::handle prefix="" info="REPLACE INTO civicrm_acl_contact_cache ( user_id, contact_id, operation ) VALUES ( 0, 5, 'View' ),( 0, 8, 'View' ),( 0, 10, 'View' ),( 0, 12, 'View' ),( 0, 14, 'View' ),( 0, 16, 'View' ),( 0, 18, 'View' ),( 0, 20, 'View' ),( 0, 22, 'View' ),( 0, 24, 'View' ),( 0, 26, 'View' ),( 0, 28, 'View' ),( 0, 31, 'View' ),( 0, 33, 'View' ),( 0, 35, 'View' ),( 0, 37, 'View' ),( 0, 43, 'View' ),( 0, 45, 'View' ),( 0, 46, 'View' ),( 0, 48, 'View' ),( 0, 51, 'View' ),( 0, 53, 'View' ),( 0, 55, 'View' ),( 0, 58, 'View' ),( 0, 75, 'View' ),( 0, 76, 'View' ),( 0, 77, 'View' ),( 0, 78, 'View' ),( 0, 79, 'View' ),( 0, 80, 'View' ),( 0, 81, 'View' ); [nativecode=1452 ** Cannot add or update a child row: a foreign key constraint fails (`dev_civicrm/civicrm_acl_contact_cache`, CONSTRAINT `FK_civicrm_acl_contact_cache_user_id` FOREIGN KEY (`user_id`) REFERENCES `civicrm_contact` (`id`) ON DELETE CASCADE)]"]
)

Any insight would be much appreciated.[/code]
« Last Edit: June 23, 2010, 02:29:01 pm by Matt2000 »
Drupal/CiviCRM micro-blogging http://twitter.com/matt2000

Ninjitsu Web Development http://www.NinjitsuWeb.com/

Matt2000

  • I post frequently
  • ***
  • Posts: 288
  • Karma: 27
    • http://www.ninjitsuweb.com
Re: Autocomplete fields -- ACL cache database error
June 23, 2010, 03:11:23 pm
Turns out that I could just delete my ACL, since I'm also using hook_civicrm_contactListQuery().

The bug is probably in CRM_Contact_BAO_Contact_Permission::cacheClause() but I didn't dig deep enough to find it.

Also, it seems to me that CRM_Contact_Page_AJAX::getContactList() could be much more efficient or useful, since at the moment, it's building up an ACL query clause that probably gets thrown away when the hook is used.

There are two possible improvements

(1) Be more efficient, by looking for hook implementations at the top of the function, and skipping all the rest of the query building if we get back a complete query, and/or

(2) Pass all of the query clauses ($from, $aclFrom, $additionalFrom, $whereClause, $limit) on to the hook, so that developers can re-use or only change relevant portions. This could be done in a backward compatible way by passing both $query, and an array containing the individual query clauses.
Drupal/CiviCRM micro-blogging http://twitter.com/matt2000

Ninjitsu Web Development http://www.NinjitsuWeb.com/

Kurund Jalmi

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4169
  • Karma: 128
    • CiviCRM
  • CiviCRM version: 4.x, future
  • CMS version: Drupal 7, Joomla 3.x
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Autocomplete fields -- ACL cache database error
June 25, 2010, 06:35:01 am
Currently anonymous user cannot access Contact Reference, hence it might be broken if you try it using hooks etc. It would be good if you can file an issue with proposed solution and if possible patch.

Main reason for not exposing Contact Reference for anonymous user is security, you will be exposing your entire db to anonymous user.

Kurund
Found this reply helpful? Support CiviCRM

Matt2000

  • I post frequently
  • ***
  • Posts: 288
  • Karma: 27
    • http://www.ninjitsuweb.com
Re: Autocomplete fields -- ACL cache database error
June 25, 2010, 12:02:45 pm
I understand the privacy concerns of auto-complete. But since we're using hook_civicrm_contactListQuery, we're actually only exposing the data returned by that query. Which is why the ACL work done in CRM_Contact_Page_AJAX::getContactList() is unnecessary when the hook exists.

What I'm suggesting is that getContactList() could be refactored to allow the developer to decide whether or not to employ ACLs, but that would required fixing this ACL caching bug, and that's the part that's beyond my familiarity with the code.
Drupal/CiviCRM micro-blogging http://twitter.com/matt2000

Ninjitsu Web Development http://www.NinjitsuWeb.com/

Kurund Jalmi

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4169
  • Karma: 128
    • CiviCRM
  • CiviCRM version: 4.x, future
  • CMS version: Drupal 7, Joomla 3.x
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Autocomplete fields -- ACL cache database error
June 28, 2010, 02:15:54 am
Quote
What I'm suggesting is that getContactList() could be refactored to allow the developer to decide whether or not to employ ACLs, but that would required fixing this ACL caching bug, and that's the part that's beyond my familiarity with the code.

File an issue for this.

Kurund
Found this reply helpful? Support CiviCRM

integritec

  • I’m new here
  • *
  • Posts: 2
  • Karma: 0
  • CiviCRM version: 3.3.2
  • CMS version: Drupal 6.2
  • MySQL version: 5.1.47 Community
  • PHP version: 5.2.14
Re: Autocomplete fields -- ACL cache database error
January 19, 2011, 02:28:18 am
Quote from: Matt2000 on June 25, 2010, 12:02:45 pm
... since we're using hook_civicrm_contactListQuery, we're actually only exposing the data returned by that query. Which is why the ACL work done in CRM_Contact_Page_AJAX::getContactList() is unnecessary when the hook exists...

@Matt, I'm looking to do a similar thing (part of user signup involves choosing membership of a valid organisation sub-type). I'm researching hooks at the moment, but wondering if you could speed this process up by letting me (and others in my position) know where you'd put the hook code - and possibly posting a sample?

I appreciate this is an old thread, so this is a bit of a long shot - but it would speed up my research hugely.

Kind regards,

Al

integritec

  • I’m new here
  • *
  • Posts: 2
  • Karma: 0
  • CiviCRM version: 3.3.2
  • CMS version: Drupal 6.2
  • MySQL version: 5.1.47 Community
  • PHP version: 5.2.14
AOQ
January 19, 2011, 03:27:54 am
Answered own question. For anyone else finding this thread in a search, procedure is as follows (relatively new to Drupal & Civi so let me know if this is not the right way).
Using "churchlookup" as module name - replace as necessary:

- Create module to host hook code:
-- directory [civisite] -> sites -> all -> modules -> churchlookup
-- file churchlookup.info

name = ChurchLookup Module
description = Allows anonymous users to look up Churches in CiviCRM
dependencies[] = civicrm
package = CiviCRM
core = 6.x
version = 1.0

-- file churchlookup.module
<?php
   function churchlookup_civicrm_contactListQuery( &$query, $name, $context, $id ) {
      if ($context == 'customfield' && $id = 7) { // 7 == "Church" field
         $query = "
SELECT c.sort_name as data, c.id
FROM civicrm_contact c, civicrm_group_contact cg
WHERE c.sort_name LIKE '$name%'
AND   cg.group_id IN ( 6 ) /* 6 is the 'Churches' group */
AND   cg.contact_id = c.id
AND   cg.status = 'Added'
ORDER BY c.sort_name;
         ";
      }
   }
?>
Then enable the module in Drupal administration

(apologies for lack of code tagging - the forum kept telling me I "couldn't post external links"...)

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • Autocomplete fields -- ACL cache database error

This forum was archived on 2017-11-26.