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 »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Changes made via API looks like made by the modified contacts
Pages: [1]

Author Topic: Changes made via API looks like made by the modified contacts  (Read 871 times)

capo

  • I post occasionally
  • **
  • Posts: 108
  • Karma: 5
Changes made via API looks like made by the modified contacts
October 07, 2013, 09:21:40 am
If I update a contact record using the API, it looks like the contact updated the data itself. The corresponding record at the log table, has the contact id as the "modified_id", who is supossed to be the author of the change.

The ID of the user related with the provided api_key, is being verified but then ignored:

CRM/Utils/REST.php (line 292, 4.5.alpha1)
Code: [Select]
    // Check and see if a valid secret API key is provided.
    $api_key = CRM_Utils_Request::retrieve('api_key', 'String', $store, FALSE, NULL, 'REQUEST');
    if (!$api_key || strtolower($api_key) == 'null') {
      return self::error("FATAL: mandatory param 'api_key' (user key) missing");
    }
    $valid_user = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $api_key, 'id', 'api_key');

After that line, CiviCRM validates that $valid_user isn't empty. If it is, it raises an authentication error. Otherwise, it continues but the value stored at the local variable $valid_user (wich is the id of the contact with the given API key) is no longer used.

When the log record is created, CiviCRM runs:

CRM/Core/BAO/Log.php (line 86, 4.5.alpha1)
Code: [Select]
    if (!$userID) {
      $session = CRM_Core_Session::singleton();
      $userID = $session->get('userID');
    }

    if (!$userID) {
      $userID = $contactID;
    }

    if (!$userID) {
      return;
    }

Before the assignation of $contactID as the author of the changes, I've added this:

Code: [Select]
    if (!$userID) {
      $api_key = CRM_Utils_Request::retrieve('api_key', 'String', $store, FALSE, NULL, 'REQUEST');

      if ($api_key && strtolower($api_key) != 'null') {
        $userID = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $api_key, 'id', 'api_key');
      }
    }

So now, changes look like made by my "API User", insted of the contact itself.

Is it a intentional behaviour? Or should I send a pull request with the patch?
« Last Edit: October 07, 2013, 09:33:49 am by capo »

capo

  • I post occasionally
  • **
  • Posts: 108
  • Karma: 5
Re: Changes made via API looks like made by the modified contacts
October 07, 2013, 09:28:14 am
Would also be interesting to have the view of an expert about the fact that I'm querying a request parameter at a BAO level.

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: Changes made via API looks like made by the modified contacts
October 07, 2013, 09:40:15 am
I am bit unclear on few things, let's discuss in person :)

Kurund
Found this reply helpful? Support CiviCRM

capo

  • I post occasionally
  • **
  • Posts: 108
  • Karma: 5
Re: Changes made via API looks like made by the modified contacts
October 07, 2013, 09:51:58 am
After conversation with Kurund, we created an issue (#CRM-13549) and I'll send a pull request.

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Changes made via API looks like made by the modified contacts
October 07, 2013, 01:31:27 pm
awesome
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Changes made via API looks like made by the modified contacts

This forum was archived on 2017-11-26.