Skip to content
Snippets Groups Projects
Commit c2e711fc authored by JohnBarclay's avatar JohnBarclay
Browse files

feeds alpha code and issue #1167010 fix

parent 341219cc
No related branches found
Tags 7.x-1.0-beta1
No related merge requests found
......@@ -263,7 +263,8 @@ function _ldap_authentication_user_login_authenticate_validate(&$form_state) {
$authentication_result = LDAP_AUTHENTICATION_RESULT_FAIL_FIND;
continue; // next server, please
}
$watchdog_tokens['%dn'] = $ldap_user['dn'];
$watchdog_tokens['%mail'] = $ldap_user['mail'];
/**
* check allowed and excluded list and php for allowed users
*/
......@@ -334,7 +335,9 @@ function _ldap_authentication_user_login_authenticate_validate(&$form_state) {
* username does not exist but email does. Since user_external_login_register does not deal with
* mail attribute and the email conflict error needs to be caught beforehand, need to throw error here
*/
watchdog('ldap_authentication', 'LDAP user with DN %dn has email address (%mail) conflict with a drupal user %name', array('%name' => $account_with_same_email->name, '%mail' => $ldap_user['mail']) , WATCHDOG_ERROR);
$watchdog_tokens['%duplicate_name'] = $account_with_same_email->name;
watchdog('ldap_authentication', 'LDAP user with DN %dn has email address
(%mail) conflict with a drupal user %duplicate_name', $watchdog_tokens, WATCHDOG_ERROR);
drupal_set_message(t('Another user already exists in the system with the same email address. You should contact the system administrator in order to solve this conflict.'), 'error');
return FALSE;
......@@ -361,7 +364,8 @@ function _ldap_authentication_user_login_authenticate_validate(&$form_state) {
else { // account already exists
if ($ldap_authentication_authmap == FALSE) { // LDAP_authen.AC.disallow.ldap.drupal
if ($auth_conf->loginConflictResolve == LDAP_AUTHENTICATION_CONFLICT_LOG) {
watchdog('ldap_authentication', 'LDAP user with DN %dn has a naming conflict with a local drupal user %name', array('%dn' => $ldap_user['dn'], '%name' => $account->name), WATCHDOG_ERROR);
$watchdog_tokens['%conflict_name'] = $account_with_same_email->name;
watchdog('ldap_authentication', 'LDAP user with DN %dn has a naming conflict with a local drupal user %conflict_name', $watchdog_tokens, WATCHDOG_ERROR);
drupal_set_message(t('Another user already exists in the system with the same login name. You should contact the system administrator in order to solve this conflict.'), 'error');
return FALSE;
}
......
......@@ -386,9 +386,9 @@ function _ldap_authorization_ldap_authorization_maps_alter(&$user, &$user_ldap_e
$derive_from_entry_authorizations = array();
if ($consumer_conf->deriveFromEntry) {
foreach ($consumer_conf->deriveFromEntryEntries as $branch) {
$entries = $ldap_server->search($consumer_conf->deriveFromEntryAttr . '=' . $user_ldap_entry['dn'], $branch, array('cn'));
$entries = $ldap_server->search($branch, $consumer_conf->deriveFromEntryAttr . '=' . $user_ldap_entry['dn'], array('cn'));
if (empty($entries) || $entries['count'] == 0) {
$entries = $ldap_server->search($consumer_conf->deriveFromEntryAttr . '=' . $user->name, $branch, array('cn'));
$entries = $ldap_server->search($branch, $consumer_conf->deriveFromEntryAttr . '=' . $user->name, array('cn'));
}
foreach ($entries as $entry) {
if (isset($entry['cn'])) {
......
......@@ -6,73 +6,61 @@
* Provides the Parser for an ldap entry array.
*/
/**
* Base class for the HTML and XML parsers.
*/
class FeedsLdapEntryParser extends FeedsParser {
public $ldap_result;
/**
/**
* Implements FeedsParser::parse().
*/
public function parse(FeedsSource $source, FeedsFetcherResult $fetcher_result) {
$this->source_config = $source->getConfigFor($this);
if (empty($this->source_config)) {
$this->source_config = $this->config;
}
$this->parserResult = new FeedsParserResult();
$mappings = $source->importer->processor->config['mappings'];
$this->mappings = $this->filterMappings($mappings);
$fetcher_config = $source->getConfigFor($source->importer->fetcher);
$this->mappings = $mappings;
// Set link.
$fetcher_config = $source->getConfigFor($source->importer->fetcher);
$this->parserResult->link = $fetcher_config['source'];
$this->ldap_result = $fetcher_result->ldap_result;
$result = new FeedsParserResult();
/**
* $this->parserResult should be in the following form:
*
* 0 => array('count' => 1, 'dn' => 'ou=content editors,ou=groups,dc=ad,dc=myuniveristy,dc=edu'),
* 1 => array('count' => 1, 'dn' => 'ou=content approvers,ou=groups,dc=ad,dc=myuniveristy,dc=edu'),
* 'count' => 2,
**/
$items_num = count($fetcher_result->ldap_result); // $this->parserResult['count'];
$items_num = $this->parserResult['count'];
for ($i = 0; $i < $items_num; $i++) {
for ($i = 0; $i < $items_num - 1; $i++) {
$item = array();
$data = $this->parserResult[$i];
$item['guid'] = $data['dn'];
foreach ($data as $attr_name => $attr_values) {
if ($attr_name == 'count') {
continue;
}
if (is_scalar($attr_values)) {
$item[$attr_name] = $attr_values;
$item[$attr_name . '.1'] = $attr_values;
}
else {
$item[$attr_name] = $attr_values[0];
for ($j = 0; $j < $data['count']; $j++) {
$item[$data[$j] . '.' . $j] = $attr_values[$j];
}
$data = $fetcher_result->ldap_result[$i];
$parsedItem = $variables = array();
foreach ($this->source_config['sources'] as $source => $query) {
if (isset($data[$query])) {
if (!is_array($data[$query])) {
$variables['$' . $this->mappings[$source]] = $data[$query];
}
else {
$variables['$' . $this->mappings[$source]] = '';
}
$parsedItem[$source] = $data[$query];
}
$parsedItem['guid'] = $data['dn'];
}
$item['raw'] = $data;
$this->parserResult->items[] = $item;
$this->parserResult->items[] = $parsedItem;
}
// Release parser.
unset($parser);
return $this->parserResult;
return $result;
}
/**
* Source form.
*/
public function sourceForm($source_config) {
$form = array();
dpm('sourceForm, this:'); dpm($this);
$mappings_ = feeds_importer($this->id)->processor->config['mappings'];
$mappings = feeds_importer($this->id)->processor->config['mappings'];
if (empty($source_config)) {
$source_config = $this->config;
}
......@@ -80,20 +68,6 @@ class FeedsLdapEntryParser extends FeedsParser {
// Add extensions that might get importerd.
$fetcher = feeds_importer($this->id)->fetcher;
$uniques = $mappings = array();
foreach ($mappings_ as $mapping) {
if (strpos($mapping['source'], 'ldapentryparser:') === 0) {
$mappings[$mapping['source']] = $mapping['target'];
if ($mapping['unique']) {
$uniques[] = $mapping['target'];
}
}
}
if (empty($mappings)) {
$form['error_message']['#markup'] = '<div class="help">' . t('FeedsLdapEntryParser: No mappings are defined.') . '</div><br>';
return $form;
}
$variables = array();
foreach ($mappings as $source => $target) {
$form['sources'][$source] = array(
......@@ -117,42 +91,27 @@ class FeedsLdapEntryParser extends FeedsParser {
* Override parent::sourceFormValidate().
*/
public function configFormValidate(&$values) {
dpm('configFormValidate'); dpm($values);
$this->setConfig(array('sources' => $values));
$this->save();
}
/**
* Override parent::getMappingSources().
*/
public function getMappingSources() {
$mappings = $this->filterMappings(feeds_importer($this->id)->processor->config['mappings']);
$next = 0;
if (!empty($mappings)) {
$mapping_keys = array_keys($mappings);
$last_mapping = end($mapping_keys);
$next = explode(':', $last_mapping);
$next = $next[1] + 1;
$attributes = feeds_importer($this->id)->fetcher->config['attributes'];
$attributes = explode(',', $attributes);
foreach ($attributes as $i => $attribute) {
$attribute = trim($attribute);
$attribute = trim($attribute, '"');
$sources[$attribute] = array(
'name' => $attribute . t(' LDAP Attribute'),
'description' => t('populate this field with %attr attribute data.', array('%attr' => $attribute)),
);
}
return array(
'ldapentryparser:' . $next => array(
'name' => t('LDAP Entry Attribute'),
'description' => t('Allows you to configure an LDAP Entry attribute value that will populate this field.'),
),
) + parent::getMappingSources();
}
/**
* Filters mappings, returning the ones that belong to us.
*/
protected function filterMappings($mappings) {
$our_mappings = array();
foreach ($mappings as $mapping) {
if (strpos($mapping['source'], 'ldapentryparser:') === 0) {
$our_mappings[$mapping['source']] = $mapping['target'];
}
}
return $our_mappings;
$sources = $sources + parent::getMappingSources();
return $sources;
}
/**
......@@ -166,13 +125,12 @@ class FeedsLdapEntryParser extends FeedsParser {
return array();
}
/**
* Define defaults.
*/
public function configDefaults() {
return array(
'sources' => array(),
);
return array();
}
......
......@@ -11,23 +11,24 @@
* Result of FeedsLdapQueryFetcher::fetch().
*/
class FeedsLdapQueryFetcherResult extends FeedsFetcherResult {
protected $basedn = array(); // should default to servers basedn
protected $sid;
protected $filter;
protected $attributes = array();
protected $sizelimit = LDAP_FEEDS_FETCHER_SIZELIMIT;
protected $LdapServer;
public $basedn = array(); // should default to servers basedn
public $sid;
public $filter;
public $attributes = array();
public $sizelimit = LDAP_FEEDS_FETCHER_SIZELIMIT;
public $LdapServer;
public $ldap_result;
/**
* Constructor.
*/
public function __construct($source_config) {
// @todo, get $query from $source_config
dpm('source_config');
dpm($source_config);
$this->sid = isset($query['sid']) ? $query['sid'] : FALSE;
$this->filter = isset($query['filter']) ? $query['filter'] : FALSE ;
$this->attributes = isset($query['attributes']) ? $query['attributes'] : array();
$this->sid = isset($source_config['sid']) ? $source_config['sid'] : FALSE;
$this->filter = isset($source_config['filter']) ? $source_config['filter'] : FALSE ;
$this->attributes = isset($source_config['attributes']) ? $source_config['attributes'] : array();
$this->sizelimit = isset($source_config['sid']) ? $source_config['sizelimit'] : 0;
if ($this->attributes) {
$temp = $this->attributes;
$this->attributes = array();
......@@ -39,11 +40,10 @@ class FeedsLdapQueryFetcherResult extends FeedsFetcherResult {
}
}
$this->basedn = (isset($query['basedn'])) ? $query['basedn'] : FALSE;
$this->basedn = (isset($source_config['basedn'])) ? $source_config['basedn'] : FALSE;
if ($this->basedn) {
$this->basedn = explode("\n", $this->basedn);
}
// dpm($this);
parent::__construct('');
}
......@@ -51,18 +51,14 @@ class FeedsLdapQueryFetcherResult extends FeedsFetcherResult {
* Overrides parent::getRaw();
*/
public function getRaw() {
// dpm('getRaw'); dpm((array)$this);
$this->LdapServer = ldap_servers_get_servers($this->sid, 'all', TRUE);
$tokens = array('!sid' => $this->sid, '!server_admin' => '<a href="{LDAP_SERVERS_INDEX_BASE_PATH}">LDAP Servers Admin</a>');
$this->LdapServer = ldap_servers_get_servers($this->sid, 'enabled', TRUE, TRUE);
$tokens = array('!sid' => $this->LdapServer->sid, '!server_admin' => '<a href="' . LDAP_SERVERS_INDEX_BASE_PATH . '">LDAP Servers Admin</a>');
//@todo should the validation of constructor parameters be validated here or in constructor.
// gives more room to alter/correct object if validated here rather than constructor
if (! $this->LdapServer) {
throw new Exception(t('LDAP Server !sid not found, please check ldap server admin page: !server_admin', $tokens));
if (! $this->LdapServer || !is_object($this->LdapServer) ) {
throw new Exception(t('LDAP Server !sid not found, please check ldap server admin page: !server_admin', $tokens));
}
if (!$this->LdapServer->status) {
if ((bool)$this->LdapServer->status == FALSE) {
throw new Exception(t('LDAP Server !sid not enabled, please check ldap server admin page: !server_admin', $tokens));
}
......@@ -79,14 +75,34 @@ class FeedsLdapQueryFetcherResult extends FeedsFetcherResult {
$tokens['errorMsg'] = $this->LdapServer->errorMsg();
throw new Exception(t('LDAP Server !sid failed to connect, with error message: !errorMsg', $tokens));
}
$bind = $this->LdapServer->bind();
if ($bind != LDAP_SUCCESS) {
$tokens['errorMsg'] = $this->LdapServer->errorMsg();
throw new Exception(t('LDAP Server !sid failed to bind, with error message: !errorMsg', $tokens));
}
$results = array();
foreach ($this->basedn as $i => $basedn) {
$result = $this->LdapServer->search($this->filter, $basedn, NULL, $this->sizelimit); // , $this->attributes
$result = $this->LdapServer->search($basedn, $this->filter, $this->attributes, 0, $this->sizelimit); // , $this->attributes
$results = array_merge($results, $result);
}
$results['count'] = count($results); // just keep with the ldap search results format, even if redundant
return $results; // @todo ? should sanitizeRaw be applied to this array? $this->sanitizeRaw($results);
return $this->sanitizeRaw($results); // @todo ? should sanitizeRaw be applied to this array? $this->sanitizeRaw($results);
}
/**
* Override parent::configDefaults().
*/
public function configDefaults() {
return array(
'sid' => NULL,
'basedn' => NULL,
'attributes' => '"cn", "dn", "mail"',
'filter' => NULL,
'sizelimit' => 100,
);
}
}
......@@ -99,8 +115,7 @@ class FeedsLdapQueryFetcher extends FeedsFetcher {
* Implements FeedsFetcher::fetch().
*/
public function fetch(FeedsSource $source) {
$source_config = $source->getConfigFor($this);
$result = new FeedsLdapQueryFetcherResult($source_config);
$result = new FeedsLdapQueryFetcherResult($this->config);
return $result;
}
......@@ -111,19 +126,6 @@ class FeedsLdapQueryFetcher extends FeedsFetcher {
}
/**
* Override parent::configDefaults().
*/
public function configDefaults() {
return array(
'sid' => NULL,
'basedn' => NULL,
'filter' => NULL,
'attributes' => NULL,
'sizelimit' => $this->sizelimit,
);
}
/**
* Override parent::configForm().
*/
......@@ -202,11 +204,4 @@ class FeedsLdapQueryFetcher extends FeedsFetcher {
}
/**
* @todo, what about sourceForm, sourceFormValidate, sourceSave, sourceDelete?
* If all the configuration data for source is in the config form, are these needed.
*/
}
This is a development version of an feeds fetcher and feeds parser for ldap.
The plan is to have 2 fetchers:
- FeedsLdapQueryFetcher for fetching generic ldap queries, configured by admins
- FeedsLdapDrupalUserFetcher for fetching ldap user entries associated with drupal users.
And 1 parser:
- FeedsLdapEntryParser that converts ldap entries array returned from ldap_search() to standard feed parser result format.
It is quite broken. I tried to piece together parts of xml and html fetchers and xpath parser, but a lot of cutting and pasting and trial and error has made a mess of it. If someone takes over on this, maybe best to start over again.
name = LDAP Feeds Parser
description = "Parse an LDAP object for Feeds Module. Used to automate content creation based on ldap queries"
description = "DOES NOT WORK. Included for development purposes only. Parse an LDAP object for Feeds Module. Used to automate content creation based on ldap queries"
package = "Lightweight Directory Access Protocol"
dependencies[] = feeds
dependencies[] = ldap_servers
......
......@@ -12,7 +12,7 @@ function ldap_feeds_feeds_plugins() {
$info['FeedsLdapQueryFetcher'] = array(
'name' => 'LDAP Query Fetcher',
'description' => 'Fetch content from ldap query',
'description' => 'DOES NOT WORK. Included for development purposes only. Fetch content from ldap query',
'handler' => array(
'parent' => 'FeedsFetcher', // This is the key name, not the class name.
'class' => 'FeedsLdapQueryFetcher',
......@@ -23,7 +23,7 @@ function ldap_feeds_feeds_plugins() {
$info['FeedsLdapEntryParser'] = array(
'name' => t('LDAP Entry Parser for Feeds'),
'description' => t('Parse an LDAP Entry Array'),
'description' => t('DOES NOT WORK. Included for development purposes only. Parse an LDAP Entry Array'),
'handler' => array(
'parent' => 'FeedsParser',
'class' => 'FeedsLdapEntryParser',
......
See: http://drupal.org/node/622700
# abstract class FeedsPlugin: base class for all plugins
- FeedsFetcher - A plugin responsible for downloading, loading or receiving a feed
- FeedsParser - A plugin responsible for bringing a fetched feed into a normalized format for processors
- FeedsProcessor (don't need to implement) - A plugin that "does stuff" with a parsed feed.
- FeedsSource: holds a source (i. e. a URL or a file path). A FeedsSource object is being passed into FeedsImporter when importing from that source. A FeedsSource can be tied to a specific node or not.
FeedsFetcher
<?php
// $Id: LdapServerTest.class.inc,v 1.4.2.1 2011/02/08 06:01:00 johnbarclay Exp $
/**
* @file
* test configurations for LdapServerTest.class.php
* file name should be of form LdapServerTestData.<sid>.inc
* where sid is the server id data is used for.
*
*/
$test_data = array();
/**
* $test_data['properties'] are all the initial properties of the instantiated LdapServerTest object
*/
$test_data['server']['properties'] = array(
'sid' => 'ldapfeeds',
'name' => 'Test LDAP Server 1 for LDAP Authorization' ,
'inDatabase' => TRUE,
'status' => 1,
'type' => 'ad',
'address' => 'ad.myuniveristy.edu',
'port' => 389,
'tls' => FALSE,
'bind_method' => LDAP_SERVERS_BIND_METHOD_SERVICE_ACCT,
'basedn' => array(
'ou=campus accounts,dc=ad,dc=myuniveristy,dc=edu',
'ou=education,dc=ad,dc=myuniveristy,dc=edu',
'ou=guest accounts,dc=ad,dc=myuniveristy,dc=edu',
),
'binddn' => 'cn=service-account,dc=ad,dc=myuniveristy,dc=edu',
'bindpw' => 'goodpwd',
'user_dn_expression' => 'user_dn_expression',
'user_attr' => 'sAMAccountName',
'mail_attr' => 'mail',
'ldapToDrupalUserPhp' => NULL,
'testingDrupalUsername' => 'jdoe'
);
/**
*
* method responses are stored in array $test_data['methodResponses']
* where keys are:
* <method_name>
* parameter1,
* parameter2,
* ...
*
* and value is the response test ldap server is expected to return. values
* can be scalar, array, object, etc, depending on what the method being mimicked
* is expected to return
*/
$test_data['server']['methodResponses']['connect'] = LDAP_SUCCESS;
$test_data['server']['search_results']['objectclass=user']['ou=campus accounts,dc=ad,dc=myuniveristy,dc=edu'] = array(
0 => array(
'count' => 4,
'dn' => 'cn=jkool,ou=campus accounts,dc=ad,dc=myuniveristy,dc=edu',
'mail' => 'jkool@myuniversity.edu',
'cn' => 'jkool',
'sn' => 'kool',
),
1 => array(
'count' => 4,
'dn' => 'cn=bkool,ou=campus accounts,dc=ad,dc=myuniveristy,dc=edu',
'mail' => 'bkool@myuniversity.edu',
'cn' => 'bkool',
'sn' => 'kool',
),
2 => array(
'count' => 4,
'dn' => 'cn=rkool,ou=campus accounts,dc=ad,dc=myuniveristy,dc=edu',
'mail' => 'rkool@myuniversity.edu',
'cn' => 'rkool',
'sn' => 'kool',
),
'count' => 3,
);
$test_data['server']['search_results']['member=cn=jkool,ou=guest accounts,dc=ad,dc=myuniveristy,dc=edu']['ou=groups,dc=ad,dc=myuniveristy,dc=edu'] = array(
0 => array('count' => 1, 'dn' => 'ou=content editors,ou=groups,dc=ad,dc=myuniveristy,dc=edu'),
'count' => 1,
);
/**
* fake user data array below 'attr' should mimick ldap user result data
*/
$test_data['server']['users']['cn=jdoe,ou=campus accounts,dc=ad,dc=myuniveristy,dc=edu']['attr'] = array(
'dn' => 'cn=jdoe,ou=campus accounts,dc=ad,dc=myuniveristy,dc=edu',
'mail' => array( 0 => 'jdoe@myuniversity.edu', 'count' => 1),
'sAMAccountName' => array( 0 => 'jdoe', 'count' => 1),
'password' => array( 0 => 'goodpwd', 'count' => 1),
);
$test_data['server']['users']['cn=jkool,ou=guest accounts,dc=ad,dc=myuniveristy,dc=edu']['attr'] = array(
'dn' => 'cn=jkool,ou=guest accounts,dc=ad,dc=myuniveristy,dc=edu',
'mail' => array( 0 => 'jkool@guests.myuniversity.edu', 'count' => 1),
'sAMAccountName' => array( 0 => 'jkool', 'count' => 1),
'password' => array( 0 => 'goodpwd', 'count' => 1),
'memberOf' => array( 0 => 'cn=sysadmins,ou=it,dc=ad,dc=myuniveristy,dc=edu', 'count' => 1),
);
$test_data['server']['users']['cn=unkool,ou=lost,dc=ad,dc=myuniveristy,dc=edu']['attr'] = array(
'dn' => 'cn=unkool,ou=lost,dc=ad,dc=myuniveristy,dc=edu',
'mail' => array( 0 => 'unkool@nowhere.myuniversity.edu', 'count' => 1),
'sAMAccountName' => array( 0 => 'jkool', 'count' => 1),
'password' => array( 0 => 'goodpwd', 'count' => 1),
'memberOf' => array( 0 => 'cn=unknown_people,ou=nowhere,dc=ad,dc=myuniveristy,dc=edu', 'count' => 1),
);
$test_data['server']['users']['cn=verykool,ou=special guests,ou=guest accounts,dc=ad,dc=myuniveristy,dc=edu']['attr'] = array(
'dn' => 'cn=verykool,ou=special guests,ou=guest accounts,dc=ad,dc=myuniveristy,dc=edu',
'mail' => array( 0 => 'verykool@myuniversity.edu', 'count' => 1),
'sAMAccountName' => array( 0 => 'verykool', 'count' => 1),
'password' => array( 0 => 'goodpwd', 'count' => 1),
'meMBErof' => array(
0 => 'cn=sysadmins,ou=it,dc=ad,dc=myuniveristy,dc=edu',
1 => 'CN=NETadmins,ou=it,dc=ad,dc=myuniveristy,dc=edu',
'count' => 2,
),
);
/**
* test users should include service account if one is being used
*/
$test_data['server']['users']['cn=service-account,dc=ad,dc=myuniveristy,dc=edu']['attr'] = array(
'dn' => 'cn=service-account,dc=ad,dc=myuniveristy,dc=edu',
'mail' => array( 0 => 'service-account@myuniversity.edu', 'count' => 1),
'sAMAccountName' => array( 0 =>'service-account', 'count' => 1),
'memberOf' => array(
0 => 'CN=service_accounts,OU=ServiceAccountGroups,DC=ad,DC=myuniveristy,DC=edu',
'count' => 2,
),
'password' => array( 0 => 'goodpwd', 'count' => 1),
);
<?php
// $Id: feeds_fetcher_file.test,v 1.1.2.3 2010/10/27 22:33:32 alexb Exp $
/**
* @file
* File fetcher tests.
*/
require_once(drupal_get_path('module','feeds') . '/tests/feeds.test.inc');
class LdapFeedsTestCase extends FeedsWebTestCase {
/**
* Describe this test.
*/
public function getInfo() {
return array(
'name' => t('Ldap query fetcher'),
'description' => t('Tests for Ldap query fetcher.'),
'group' => t('LDAP'),
);
}
public $testFunctions;
function setUp() {
parent::setUp(array('ldap_servers')); // don't need any real servers, configured, just ldap_servers code base
variable_set('ldap_simpletest', 1);
}
function tearDown(){
parent::tearDown();
variable_del('ldap_simpletest');
}
/**
* prepTestData create fake ldap server configuration.
*
* @param string $testid the name of the test. used to determine which configuration file to include
* @return object consumer configuration object (class = LdapAuthorizationConsumerConfAdmin)
*
*/
function prepTestData($testid) {
$this->testFunctions = new LdapTestFunctions();
// create fake ldap server configuration instance
include(drupal_get_path('module','ldap_feeds') . '/tests/LdapServerTestData.inc');
$this->testFunctions->prepTestServers('ldapfeeds', $test_data['server']);
}
function removeTestData($testid) {
$this->testFunctions->removeTestServers('ldapfeeds');
return $consumer_conf_admin->delete();
}
/**
* This test should create a complete importer
*/
public function testLdapQueryFetcherAndParser() {
$test_id = 'LdapQueryFetcherAndParser';
$conf_id = 'LdapQueryFetcherAndParser';
$consumer_conf_admin = $this->prepTestData($conf_id);
// Set up an importer.
$this->createImporterConfiguration('Node import', 'node');
// 1. create importer (Basic Settings at admin/structure/feeds/edit/node/settings)
$basic_settings = array(
'name' => 'ldap_test_importer',
'description' => 'ldap_test_importer',
'content_type' => '',
);
$this->drupalPost('admin/structure/feeds/edit/node/settings', $basic_settings, 'Save');
// 2. setup fetcher
$this->setPlugin('node', 'FeedsLdapQueryFetcher');
$fetcher_conf = array(
'sid' => 'ldapfeeds',
'basedn' => 'ou=campus accounts,dc=ad,dc=myuniveristy,dc=edu',
'filter' => 'objectclass=user',
'attributes' => '"dn","mail","cn","sn"',
'sizelimit' => 5
);
$this->drupalPost('admin/structure/feeds/edit/node/settings/FeedsLdapQueryFetcher', $fetcher_conf, 'Save');
// 3. set parser.
$this->setPlugin('node', 'FeedsLdapEntryParser');
// no settings for parser.
// 4. set processor
$this->setPlugin('node', 'FeedsNodeProcessor');
//@todo need to have field_sn and field_mail in this content type.
$bundle = $this->createContentType(NULL, array(
'field_sn' => 'text',
'field_mail' => 'text',
));
$mappings = array(
'0' => array(
'source' => 'dn',
'target' => 'title',
),
'1' => array(
'source' => 'cn',
'target' => 'body',
),
'2' => array(
'source' => 'sn',
'target' => 'field_sn',
),
'3' => array(
'source' => 'mail',
'target' => 'field_mail',
),
);
$this->addMappings('node', $mappings);
//@todo what is path to import without feed node?
$edit = array();
$this->drupalPost('import/test_ldap', $edit, t('Import'));
$this->assertText('Created 3 nodes');
$query = new EntityFieldQuery;
$entities = $query
->entityCondition('entity_type', 'node')
->entityCondition('bundle', $bundle)
->fieldOrderBy('field_mail', 'value', 'ASC')
->execute();
$nodes = entity_load('node', array_keys($entities['node']));
/**
*
0 => array(
'count' => 4,
'dn' => 'cn=bkool,ou=campus accounts,dc=ad,dc=myuniveristy,dc=edu',
'mail' => 'bkool@myuniversity.edu',
'cn' => 'bkool',
'sn' => 'kool',
),
* 1 => array(
'count' => 4,
'dn' => 'cn=jkool,ou=campus accounts,dc=ad,dc=myuniveristy,dc=edu',
'mail' => 'jkool@myuniversity.edu',
'cn' => 'jkool',
'sn' => 'kool',
),
2 => array(
'count' => 4,
'dn' => 'cn=rkool,ou=campus accounts,dc=ad,dc=myuniveristy,dc=edu',
'mail' => 'rkool@myuniversity.edu',
'cn' => 'rkool',
'sn' => 'kool',
),
**/
$delete_result = $this->removeTestData($conf_id);
}
}
......@@ -23,13 +23,13 @@ class LdapProvisionConf {
'provisionCron',
'provisionCronLast',
);
function __construct() {
$this->load();
}
function load() {
function load() {
if ($saved = variable_get("ldap_provision_conf", FALSE)) {
$this->inDatabase = TRUE;
foreach ($this->saveable as $property) {
......@@ -37,7 +37,7 @@ class LdapProvisionConf {
$this->{$property} = $saved[$property];
}
}
}
}
else {
$this->inDatabase = FALSE;
}
......@@ -51,14 +51,14 @@ class LdapProvisionConf {
/**
* Provides last time that the provision cron job was executed
*/
*/
function get_last_cron() {
return $this->provisionCronLast;
}
/**
* Saves current time as last time provision cron job was executed
*/
*/
function update_cron() {
$this->provisionCronLast = time();
}
......@@ -111,10 +111,10 @@ class LdapProvisionConf {
$attributes[] = 'dn';
$filter = $this->get_filter($ldap_server);
$basedn = ''; // need to modify this to get the base dn from the server
// searches each basedn for this server configuration
foreach($ldap_server->basedn as $index => $base) {
$accounts[$sid][$index] = $ldap_server->search($filter, $base, $attributes);
$accounts[$sid][$index] = $ldap_server->search($base, $filter, $attributes);
}
}
return $accounts;
......
......@@ -212,35 +212,30 @@ class LdapServer {
}
/**
* Preform an LDAP search.
* Perform an LDAP search. Must be connected and bound first.
*
* @peram string $filter
* The search filter.
* @peram strign $basedn
* The search base. If NULL, we use $this->basedn
* @peram array $attributes
* List of desired attributes. If omitted, we only return "dn".
* @param params same as ldap_search() params except $link_identifier is omitted.
*
* @return
* An array of matching entries->attributes, or FALSE if the search is
* empty.
*/
function search($filter, $basedn = NULL, $attributes = array(), $max = 0) {
if ($basedn == NULL) {
function search($base_dn = NULL, $filter, $attributes = array(), $attrsonly = 0, $sizelimit = 0, $timelimit = 0, $deref = LDAP_DEREF_NEVER) {
if ($base_dn == NULL) {
if (count($this->basedn) == 1) {
$basedn = $this->basedn[0];
$base_dn = $this->basedn[0];
}
else {
return FALSE;
}
}
$result = @ldap_search($this->connection, $basedn, $filter, $attributes , 0, $max);
$result = @ldap_search($this->connection, $base_dn, $filter, $attributes, $attrsonly, $sizelimit, $timelimit, $deref);
if ($result && ldap_count_entries($this->connection, $result)) {
return ldap_get_entries($this->connection, $result);
$entries = ldap_get_entries($this->connection, $result);
return $entries;
} elseif ($this->ldapErrorNumber()) {
$watchdog_tokens = array('%basedn' => $basedn, '%filter' => $filter,
$watchdog_tokens = array('%basedn' => $base_dn, '%filter' => $filter,
'%attributes' => print_r($attributes, TRUE), '%errmsg' => $this->errorMsg('ldap'),
'%errno' => $this->ldapErrorNumber());
watchdog('ldap', "LDAP ldap_search error. basedn: %basedn, filter: %filter, attributes:
......@@ -252,7 +247,6 @@ class LdapServer {
}
/**
* Queries LDAP server for the user.
*
......@@ -269,7 +263,7 @@ class LdapServer {
$filter = $this->user_attr . '=' . $drupal_user_name;
$result = $this->search($filter, $basedn);
$result = $this->search($basedn, $filter);
if (!$result || !isset($result['count']) || !$result['count']) continue;
// Must find exactly one user for authentication to.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment