Projet

Général

Profil

Paste
Télécharger (7,02 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ldap / ldap_feeds / ldap_feeds.module @ 59ae487e

1
<?php
2

    
3
/**
4
 * Implements hook_ctools_plugin_api().
5
 */
6
function ldap_feeds_ctools_plugin_api($owner, $api) {
7
  if ($owner == 'feeds' && $api == 'plugins') {
8
  return array('version' => 1);
9
  }
10
}
11

    
12
/**
13
 * Implements hook_feeds_plugins().
14
 */
15
function ldap_feeds_feeds_plugins() {
16

    
17
  $path = drupal_get_path('module', 'ldap_feeds');
18
  $info = array();
19

    
20
  $info['FeedsLdapQueryFetcher'] = array(
21
    'name' => 'LDAP Query Fetcher',
22
    'description' => 'Fetch content from ldap query',
23
    'handler' => array(
24
      'parent' => 'FeedsFetcher', // This is the key name, not the class name.
25
      'class' => 'FeedsLdapQueryFetcher',
26
      'file' => 'FeedsLdapQueryFetcher.inc',
27
      'path' => $path,
28
    ),
29
  );
30

    
31
  $info['FeedsDrupalUserLdapEntryFetcher'] = array(
32
    'name' => 'Drupal User LDAP Entry Fetcher',
33
    'description' => 'Fetches one entry for each LDAP authenticated user.  Fetches both LDAP entry attributes such as
34
    <code>cn, dn,</code> etc.
35
      and Drupal user data such as <code>uid, name, mail, created, status, language, </code>and <code>signature</code>.',
36
    'handler' => array(
37
      'parent' => 'FeedsFetcher', // This is the key name, not the class name.
38
      'class' => 'FeedsDrupalUserLdapEntryFetcher',
39
      'file' => 'FeedsDrupalUserLdapEntryFetcher.inc',
40
      'path' => $path,
41
    ),
42
  );
43

    
44
  $info['FeedsLdapEntryParser'] = array(
45
    'name'        => t('LDAP Entry Parser for Feeds'),
46
    'description' => t('Parse an LDAP Entry Array'),
47
    'handler'     => array(
48
      'parent' => 'FeedsParser',
49
      'class'  => 'FeedsLdapEntryParser',
50
      'file'   => 'FeedsLdapEntryParser.inc',
51
      'path'   => $path,
52
    ),
53
  );
54

    
55
  return $info;
56

    
57
}
58

    
59
/**
60
 * Implements hook_enable().
61
 *
62
 * Clear Feed's plugin cache so that this plugin shows up.
63
 */
64
function ldap_feeds_enable() {
65
  cache_clear_all('plugins:feeds:plugins', 'cache');
66
}
67

    
68
function ldap_feeds_drupal_user_attributes() {
69

    
70
  $attributes = array(
71
    'uid' => array('token' => 'drupal.uid', 'description' => 'Drupal used id.  e.g. 413'),
72
    'name' => array('token' => 'drupal.name', 'description' => 'Drupal username.  e.g. jdoe'),
73
    'mail' => array('token' => 'drupal.mail', 'description' => 'Drupal email address.  e.g. jdoe@gmail.com'),
74
    'created' => array('token' => 'drupal.created', 'description' => 'Drupal account created timestamp in unix e.g. 432432432'),
75
    'status' => array('token' => 'drupal.status', 'description' => 'Drupal user status  e.g. 1 or 0'),
76
    'language' => array('token' => 'drupal.language', 'description' => 'Drupal language.'),
77
    'signature' => array('token' => 'drupal.signature', 'description' => 'Drupal signature.  e.g. Happy Joe'),
78
    'login' => array('token' => 'drupal.login', 'description' => 'Drupal unix timestamp of last login  e.g. 1317494439'),
79
    'init' => array('token' => 'drupal.init', 'description' => 'Drupal user init  e.g. jdoe@gmail.com'),
80
  );
81
  // ldap_authentication and some other modules may want to add additional drupal user tokens
82
  // largely derived from the $user->data array, but possibly from related data such as authmaps
83
  // some use cases for alter are simply edge cases
84

    
85
  drupal_alter('ldap_feeds_drupal_user_attributes', $attributes);
86

    
87
  return $attributes;
88
}
89

    
90
/**
91
 * show some sample ldap user data to help with mapping interface
92
 */
93

    
94
function ldap_feeds_form_feeds_ui_mapping_form_alter(&$form, &$form_state, $form_id) {
95

    
96
  $importer = feeds_importer_load($form['#importer']);
97

    
98
  if ($importer->config['fetcher']['plugin_key'] == 'FeedsDrupalUserLdapEntryFetcher') {
99
    ldap_feeds_drupal_user_legend($form, $importer);
100
  }
101
  elseif ($importer->config['fetcher']['plugin_key'] == 'FeedsLdapQueryFetcher') {
102
    ldap_feeds_query_legend($form, $importer);
103
  }
104
}
105

    
106
/**
107
 * add additional data to mapping form for ldap query fetcher
108
 */
109
function ldap_feeds_query_legend(&$form, $importer) {
110

    
111
  $source = feeds_source($importer->id);
112
  $fetcher = feeds_plugin('FeedsLdapQueryFetcher', $source->importer->id);
113
 // $fetcher_result = new FeedsLdapQueryFetcher($source->config);
114
 // $records = $fetcher->getRaw(); // FeedsSource $source
115
 // foreach ($records[0] as $field_name => $value) {
116
  //  $sources[$field_name] = array(
117
  //    'name' => array('#markup' => $field_name),
118
   //   'description' => array('#markup' =>  _ldap_feeds_query_legend($records, $field_name)));
119
 // }
120
 // $form['legendset']['#description'] = t('Name column is the value that should go in the SOURCE column above.  Description column are from first few records of query in fetcher.', $tokens);
121
 // $form['legendset']['legend']['sources'] = $sources;
122

    
123
}
124

    
125
function _ldap_feeds_query_legend($records, $field_name) {
126
  $examples = array();
127
  foreach ($records as $i => $record) {
128
    $examples[] = $record[$field_name];
129
    if ($i > 5 ) {
130
      break;
131
    }
132
  }
133
  return join(', ', array_filter($examples));
134
}
135
/**
136
 * add additional data to mapping form for drupal user fetcher
137
 */
138
function ldap_feeds_drupal_user_legend(&$form, $importer) {
139

    
140
  $sources = array();
141
  $servers = ldap_servers_get_servers(NULL, 'enabled');
142
  $form['legendset']['#description'] = "";
143
  $drupal_user_attributes = $importer->config['fetcher']['config']['availableDrupalUserAttributes'];
144

    
145
  foreach ($drupal_user_attributes as $attr_name => $attr_conf) {
146
    $id = $attr_conf['token'];
147
    $sources[$id] = array('name' => array('#markup' => $id), 'description' => array('#markup' => ''));
148
  }
149

    
150
  foreach ($servers as $sid => $ldap_server) {
151
    if ($ldap_server->testingDrupalUsername) {
152
      $account = user_load_by_name($ldap_server->testingDrupalUsername);
153

    
154
      foreach ($drupal_user_attributes as $attr_name => $attr_conf) {
155
        if ($attr_name == 'count') {
156
          continue;
157
        }
158
        $id = $attr_conf['token'];
159
        if ($account) {
160
          $sources[$id] = array('name' => array('#markup' => $id), 'description' => array('#markup' => $account->{$attr_name}));
161
        }
162
      }
163

    
164
      $ldap_user = ldap_servers_get_user_ldap_data($ldap_server->testingDrupalUsername, $sid);
165
      foreach ($ldap_user['attr'] as $id => $value) {
166
        if (!is_numeric($id) && is_scalar($ldap_user['attr'][$id][0]) && $ldap_user['attr'][$id]['count'] == 1) {
167
          $sources[$id] = array('name' => array('#markup' => $id), 'description' => array('#markup' => $ldap_user['attr'][$id][0]));
168
        }
169
        elseif ($ldap_user['attr'][$id]['count'] > 1) {
170
          $item = t('MULTIVALUED ATTRIBUTE:') . join(" , \n", $ldap_user['attr'][$id]);
171
          $sources[$id] = array('name' => array('#markup' => $id), 'description' => array('#markup' => $item));
172
        }
173
      }
174
      $form['legendset']['#description'] .= t('LDAP Attributes in the source "description" column are from testing ldap user (%testing_user) on the server %sid, which is configured in
175
        the ldap server form.', array('%sid' => $sid, '%testing_user' => $ldap_server->testingDrupalUsername));
176
    }
177
    else {
178
      foreach (array('dn' => 'distinguished name', 'cn' => 'cname') as $id => $value) {
179
        $sources[$id] = array('name' => array('#markup' => $id), 'description' => array('#markup' => $value));
180
      }
181
    }
182
  }
183
  $form['legendset']['legend']['sources'] = $sources;
184

    
185
}