Hi, I am a new to Drupal and I have been working on an internal website developed for my company. I have a php cron script that regularly runs against my Active Directory server to check for any changes to users and their department assignments.

I have created a CCK field, field_objectguid, to store the unique object GUID that is assigned to every AD objects in my Department type node. This is done so that in case a department/user changes its name, the system can detect it and update the existing one instead of creating it.

Here is the code I have for the department type nodes:

foreach (ldap_get_entries($ds, $result) as $k => $v) {
	$group = node_load(array('field_objectguid' => bin2hex($v["objectguid"][0]), 'type' => DEPARTMENT_NODE_TYPE)); // Error on this line
	if($group && $group->title != $v["description"][0]){
		$group->title = $v["description"][0];
		$group->og_description = $v["description"][0];
		node_save($group);
	}
}

The problem is node_load() can't use CCK fields since its code only queries the core node fields. I have looked around for solutions but haven't found an official fix to this kind of problem. Is there any other way to approach this?

Comments

wolfdenoir’s picture

Component: content.module » CCK in core
Assigned: wolfdenoir » Unassigned
nevets’s picture

You need to know the table field_objectguid is in and do something like

$nid = db_result(db_query("SELECT nid FROM {table_with_field_objectguid} WHERE field_objectguid = '%d'", bin2hex($v["objectguid"][0])));  // The %d assumes the field is an integer
if ( $nid ) {
  $group = node_load($nid);
   ...
}
wolfdenoir’s picture

Hey thanks for the advice nevets! Although I kind of come up with my own workaround.

Instead of querying the nodes specifically using the CCK field, I pulled the entire list of department nodes and iterated through them, querying the corresponding AD object each time with the field_objectguid stored in the node, which is made available AFTER the node_load():

$depts = db_query("SELECT n.nid FROM node n WHERE n.type = '". DEPARTMENT_NODE_TYPE. "'");
while($obj = db_fetch_object($depts)){
  $dept = node_load($obj->nid);
  if($dept){
    $guid = $dept->field_objectguid[0]['value'];

    // escape every hex values
    for($i = 0; $i < strlen($guid); $i = $i + 3){
      $guid = substr_replace($guid, '\\', $i, 0);
    }

    $filter = "(objectguid=$guid)";
    $result = ldap_search($ds, 'DC=my_company,DC=com', $filter);

    $v = ldap_get_attributes($ds, ldap_first_entry($ds, $result));
    if(isset($v["description"]) && $dept->title != $v["description"][0]){
      $dept->title = $v["description"][0];
      $dept->og_description = $v["description"][0];
      node_save($dept);
    }
  }
}

The code could probably be further refined. It's not the most elegant solution, but it works for my case. :)