Projet

Général

Profil

Paste
Télécharger (6,38 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ldap / ldap_feeds / ldap_feeds.module @ 91af538d

1
<?php
2

    
3
/**
4
 * @file
5
 */
6

    
7
/**
8
 * Implements hook_ctools_plugin_api().
9
 */
10
function ldap_feeds_ctools_plugin_api($owner, $api) {
11
  if ($owner == 'feeds' && $api == 'plugins') {
12
    return ['version' => 1];
13
  }
14
}
15

    
16
/**
17
 * Implements hook_feeds_plugins().
18
 */
19
function ldap_feeds_feeds_plugins() {
20

    
21
  $path = drupal_get_path('module', 'ldap_feeds');
22
  $info = [];
23

    
24
  $info['FeedsLdapQueryFetcher'] = [
25
    'name' => 'LDAP Query Fetcher',
26
    'description' => 'Fetch content from ldap query',
27
    'handler' => [
28
       // This is the key name, not the class name.
29
      'parent' => 'FeedsFetcher',
30
      'class' => 'FeedsLdapQueryFetcher',
31
      'file' => 'FeedsLdapQueryFetcher.inc',
32
      'path' => $path,
33
    ],
34
  ];
35

    
36
  $info['FeedsDrupalUserLdapEntryFetcher'] = [
37
    'name' => 'Drupal User LDAP Entry Fetcher',
38
    'description' => 'Fetches one entry for each LDAP authenticated user.  Fetches both LDAP entry attributes such as
39
    <code>cn, dn,</code> etc.
40
      and Drupal user data such as <code>uid, name, mail, created, status, language, </code>and <code>signature</code>.
41
      Make sure to add dn in your LDAP query for this parser, if you use custom attributes.',
42
    'handler' => [
43
      // This is the key name, not the class name.
44
      'parent' => 'FeedsFetcher',
45
      'class' => 'FeedsDrupalUserLdapEntryFetcher',
46
      'file' => 'FeedsDrupalUserLdapEntryFetcher.inc',
47
      'path' => $path,
48
    ],
49
  ];
50

    
51
  $info['FeedsLdapEntryParser'] = [
52
    'name'        => t('LDAP Entry Parser for Feeds'),
53
    'description' => t('Parse an LDAP Entry Array'),
54
    'handler'     => [
55
      'parent' => 'FeedsParser',
56
      'class'  => 'FeedsLdapEntryParser',
57
      'file'   => 'FeedsLdapEntryParser.inc',
58
      'path'   => $path,
59
    ],
60
  ];
61

    
62
  return $info;
63

    
64
}
65

    
66
/**
67
 * Implements hook_enable().
68
 *
69
 * Clear Feed's plugin cache so that this plugin shows up.
70
 */
71
function ldap_feeds_enable() {
72
  cache_clear_all('plugins:feeds:plugins', 'cache');
73
}
74

    
75
/**
76
 *
77
 */
78
function ldap_feeds_drupal_user_attributes() {
79

    
80
  $attributes = [
81
    'uid' => ['token' => 'drupal.uid', 'description' => 'Drupal used id.  e.g. 413'],
82
    'name' => ['token' => 'drupal.name', 'description' => 'Drupal username.  e.g. jdoe'],
83
    'mail' => ['token' => 'drupal.mail', 'description' => 'Drupal email address.  e.g. jdoe@gmail.com'],
84
    'created' => ['token' => 'drupal.created', 'description' => 'Drupal account created timestamp in unix e.g. 432432432'],
85
    'status' => ['token' => 'drupal.status', 'description' => 'Drupal user status  e.g. 1 or 0'],
86
    'language' => ['token' => 'drupal.language', 'description' => 'Drupal language.'],
87
    'signature' => ['token' => 'drupal.signature', 'description' => 'Drupal signature.  e.g. Happy Joe'],
88
    'login' => ['token' => 'drupal.login', 'description' => 'Drupal unix timestamp of last login  e.g. 1317494439'],
89
    'init' => ['token' => 'drupal.init', 'description' => 'Drupal user init  e.g. jdoe@gmail.com'],
90
  ];
91
  // ldap_authentication and some other modules may want to add additional drupal user tokens
92
  // largely derived from the $user->data array, but possibly from related data such as authmaps
93
  // some use cases for alter are simply edge cases.
94
  drupal_alter('ldap_feeds_drupal_user_attributes', $attributes);
95

    
96
  return $attributes;
97
}
98

    
99
/**
100
 * Show some sample ldap user data to help with mapping interface.
101
 */
102
function ldap_feeds_form_feeds_ui_mapping_form_alter(&$form, &$form_state, $form_id) {
103

    
104
  $importer = feeds_importer_load($form['#importer']);
105

    
106
  if ($importer->config['fetcher']['plugin_key'] == 'FeedsDrupalUserLdapEntryFetcher') {
107
    ldap_feeds_drupal_user_legend($form, $importer);
108
  }
109
  elseif ($importer->config['fetcher']['plugin_key'] == 'FeedsLdapQueryFetcher') {
110
    ldap_feeds_query_legend($form, $importer);
111
  }
112
}
113

    
114
/**
115
 * Add additional data to mapping form for ldap query fetcher.
116
 */
117
function ldap_feeds_query_legend(&$form, $importer) {
118

    
119
  $source = feeds_source($importer->id);
120
  $fetcher = feeds_plugin('FeedsLdapQueryFetcher', $source->importer->id);
121
}
122

    
123
/**
124
 *
125
 */
126
function _ldap_feeds_query_legend($records, $field_name) {
127
  $examples = [];
128
  foreach ($records as $i => $record) {
129
    $examples[] = $record[$field_name];
130
    if ($i > 5) {
131
      break;
132
    }
133
  }
134
  return join(', ', array_filter($examples));
135
}
136

    
137
/**
138
 * Add additional data to mapping form for drupal user fetcher.
139
 */
140
function ldap_feeds_drupal_user_legend(&$form, $importer) {
141

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

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

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

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

    
166
      $ldap_user = ldap_servers_get_user_ldap_data($ldap_server->testingDrupalUsername, $sid);
167
      foreach ($ldap_user['attr'] as $id => $value) {
168
        if (!is_numeric($id) && is_scalar($ldap_user['attr'][$id][0]) && $ldap_user['attr'][$id]['count'] == 1) {
169
          $sources[$id] = ['name' => ['#markup' => $id], 'description' => ['#markup' => $ldap_user['attr'][$id][0]]];
170
        }
171
        elseif ($ldap_user['attr'][$id]['count'] > 1) {
172
          $item = t('MULTIVALUED ATTRIBUTE:') . join(" , \n", $ldap_user['attr'][$id]);
173
          $sources[$id] = ['name' => ['#markup' => $id], 'description' => ['#markup' => $item]];
174
        }
175
      }
176
      $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
177
        the ldap server form.', ['%sid' => $sid, '%testing_user' => $ldap_server->testingDrupalUsername]);
178
    }
179
    else {
180
      foreach (['dn' => 'distinguished name', 'cn' => 'cname'] as $id => $value) {
181
        $sources[$id] = ['name' => ['#markup' => $id], 'description' => ['#markup' => $value]];
182
      }
183
    }
184
  }
185
  $form['legendset']['legend']['sources'] = $sources;
186

    
187
}