Projet

Général

Profil

Paste
Télécharger (10,9 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / cas / cas.install @ 4f315dab

1
<?php
2

    
3
/**
4
 * @file
5
 * Installation hooks for the CAS module.
6
 */
7

    
8
/**
9
 * Implements hook_schema().
10
 */
11
function cas_schema() {
12
  $schema = array();
13

    
14
  $schema['cas_login_data'] = array(
15
    'description' => 'Stores CAS session information.',
16
    'fields' => array(
17
      'cas_session_id' => array(
18
        'description' => 'CAS session ID',
19
        'type' => 'varchar',
20
        'length' => 255,
21
        'not null' => TRUE,
22
        'default' => '',
23
      ),
24
      'uid' => array(
25
        'description' => 'The {users}.uid associated with the CAS session.',
26
        'type' => 'int',
27
        'unsigned' => TRUE,
28
        'not null' => TRUE,
29
      ),
30
    ),
31
    'primary key' => array('cas_session_id'),
32
  );
33

    
34
  $schema['cas_user'] = array(
35
    'description' => 'Stores CAS authentication mapping.',
36
    'fields' => array(
37
      'aid' => array(
38
        'description' => 'Primary Key: Unique authmap ID.',
39
        'type' => 'serial',
40
        'unsigned' => TRUE,
41
        'not null' => TRUE,
42
      ),
43
      'uid' => array(
44
        'type' => 'int',
45
        'not null' => TRUE,
46
        'default' => 0,
47
        'description' => "User's {users}.uid.",
48
      ),
49
      'cas_name' => array(
50
        'type' => 'varchar',
51
        'length' => 128,
52
        'not null' => TRUE,
53
        'default' => '',
54
        'description' => 'Unique authentication name.',
55
      ),
56
    ),
57
    'unique keys' => array(
58
      'cas_name' => array('cas_name'),
59
    ),
60

    
61
    'indexes' => array('cas_user' => array('uid')),
62
    'primary key' => array('aid'),
63
    'foreign keys' => array(
64
      'user' => array(
65
        'table' => 'users',
66
        'columns' => array('uid' => 'uid'),
67
      ),
68
    ),
69
  );
70

    
71
  return $schema;
72
}
73

    
74
/**
75
 * Implements hook_uninstall().
76
 */
77
function cas_uninstall() {
78
  // Delete variables.
79
  variable_del('cas_access');
80
  variable_del('cas_allow_rememberme');
81
  variable_del('cas_authmap');
82
  variable_del('cas_auto_assigned_role');
83
  variable_del('cas_cert');
84
  variable_del('cas_changePasswordURL');
85
  variable_del('cas_check_frequency');
86
  variable_del('cas_debugfile');
87
  variable_del('cas_domain');
88
  variable_del('cas_exclude');
89
  variable_del('cas_first_login_destination');
90
  variable_del('cas_hide_email');
91
  variable_del('cas_hide_password');
92
  variable_del('cas_library_dir');
93
  variable_del('cas_login_drupal_invite');
94
  variable_del('cas_login_form');
95
  variable_del('cas_login_invite');
96
  variable_del('cas_login_message');
97
  variable_del('cas_login_redir_message');
98
  variable_del('cas_logout_destination');
99
  variable_del('cas_pages');
100
  variable_del('cas_pgtformat');
101
  variable_del('cas_pgtpath');
102
  variable_del('cas_port');
103
  variable_del('cas_proxy');
104
  variable_del('cas_proxy_list');
105
  variable_del('cas_registerURL');
106
  variable_del('cas_server');
107
  variable_del('cas_uri');
108
  variable_del('cas_user_register');
109
  variable_del('cas_version');
110

    
111
  // And old (un-used) variables.
112
  variable_del('cas_cert_verify');
113
  variable_del('cas_check_first');
114
  variable_del('cas_first_login');
115
  variable_del('cas_hijack_user');
116
  variable_del('cas_ldap_email_attribute');
117
  variable_del('cas_logout_redirect');
118
  variable_del('cas_signout');
119
  variable_del('cas_useldap');
120
  variable_del('cas_useldap_groups');
121
  variable_del('cas_verify');
122
}
123

    
124
/**
125
 * Implements hook_requirements().
126
 */
127
function cas_requirements($phase) {
128
  $requirements = array();
129
  $t = get_t();
130

    
131
  if ($phase == 'runtime') {
132
    $phpcas_url = 'https://wiki.jasig.org/display/CASC/phpCAS';
133

    
134
    $requirements['phpcas']['title'] = $t('phpCAS');
135
    // Okay to call functions from cas.module since we are in the runtime
136
    // phase. We hide errors here in case phpcas could not be loaded.
137
    if ($version = @cas_phpcas_load()) {
138
      $requirements['phpcas']['value'] = $version;
139
      $requirements['phpcas']['severity'] = REQUIREMENT_INFO;
140
      $requirements['phpcas']['description'] = $t('Please check periodically for <a href="@phpcas_url">security updates</a> to phpCAS.', array('@phpcas_url' => $phpcas_url));
141
    }
142
    else {
143
      $requirements['phpcas']['value'] = $t('Not found');
144
      $requirements['phpcas']['severity'] = REQUIREMENT_ERROR;
145
      $requirements['phpcas']['description'] = $t('phpCAS could not be loaded. Please <a href="@phpcas_url">download phpCAS</a> and <a href="@cas_url">configure its location</a>.', array('@phpcas_url' => $phpcas_url, '@cas_url' => url('admin/config/people/cas')));
146
    }
147
  }
148
  return $requirements;
149
}
150

    
151
/**
152
 * Creates CAS login data table for Single-Sign-Out.
153
 */
154
function cas_update_1() {
155
  $schema = array();
156

    
157
  $schema['cas_login_data'] = array(
158
    'description' => 'Stores CAS session information.',
159
    'fields' => array(
160
      'cas_session_id' => array(
161
        'description' => 'CAS session ID',
162
        'type' => 'varchar',
163
        'length' => 255,
164
        'not null' => TRUE,
165
        'default' => '',
166
      ),
167
      'uid' => array(
168
        'description' => 'The {users}.uid associated with the CAS session.',
169
        'type' => 'int',
170
        'unsigned' => TRUE,
171
        'not null' => TRUE,
172
      ),
173
    ),
174
    'primary key' => array('cas_session_id'),
175
  );
176

    
177
  db_create_table('cas_login_data', $schema['cas_login_data']);
178
}
179

    
180
/**
181
 * Depreciate "Verify the server using PEM cerificate" option.
182
 */
183
function cas_update_6300() {
184
  if (variable_get('cas_cert_verify', 'none') == 'verify') {
185
    variable_set('cas_cert_verify', 'none');
186
  }
187
}
188

    
189
/**
190
 * Migrate authmap entries to new {cas_user} table.
191
 */
192
function cas_update_6301() {
193
  $schema = array();
194

    
195
  $schema['cas_user'] = array(
196
    'description' => 'Stores CAS authentication mapping.',
197
    'fields' => array(
198
      'aid' => array(
199
        'description' => 'Primary Key: Unique CAS authentication mapping ID.',
200
        'type' => 'serial',
201
        'unsigned' => TRUE,
202
        'not null' => TRUE,
203
      ),
204
      'uid' => array(
205
        'type' => 'int',
206
        'not null' => TRUE,
207
        'default' => 0,
208
        'description' => "User's {users}.uid.",
209
      ),
210
      'cas_name' => array(
211
        'type' => 'varchar',
212
        'length' => 128,
213
        'not null' => TRUE,
214
        'default' => '',
215
        'description' => 'Unique CAS username.',
216
      ),
217
    ),
218
    'unique keys' => array(
219
      'cas_name' => array('cas_name'),
220
    ),
221
    'primary key' => array('aid'),
222
    'foreign keys' => array(
223
      'user' => array(
224
        'table' => 'users',
225
        'columns' => array('uid' => 'uid'),
226
      ),
227
    ),
228
  );
229

    
230
  // Create {cas_user} table.
231
  db_create_table('cas_user', $schema['cas_user']);
232

    
233
  // Migrate entries from {authmap} to {cas_user}.
234
  $query = db_select('authmap', 'a')
235
    ->condition('module', 'cas')
236
    ->condition('uid', 0, '<>');
237
  $query->addField('a', 'uid');
238
  $query->addField('a', 'authname', 'cas_name');
239
  db_insert('cas_user')
240
    ->from($query)
241
    ->execute();
242

    
243
  // Remove old entries in {authmap}.
244
  db_delete('authmap')
245
    ->condition('module', 'cas')
246
    ->execute();
247
}
248

    
249
/**
250
 * Remove 'hijack user' and 'Drupal is CAS user repository' options.
251
 */
252
function cas_update_6302() {
253
  $message = NULL;
254
  $t = get_t();
255

    
256
  if (variable_get('cas_authmap', 0) || variable_get('cas_hijack_user', 0)) {
257
    // Create a mapping in {cas_user} for each current Drupal user.
258
    // The code below generates SQL equivalent to:
259
    //   INSERT INTO cas_user (uid, cas_name)
260
    //   SELECT u.uid AS uid, u.name as cas_name
261
    //   FROM users u
262
    //   WHERE uid <> 0 AND NOT EXISTS (SELECT cas_name FROM cas_user c WHERE c.cas_name = u.name);
263

    
264
    $query = db_select('users', 'u');
265
    $query->addField('u', 'uid', 'uid');
266
    $query->addField('u', 'name', 'cas_name');
267
    $query->condition('uid', 0, '<>');
268
    $query->notExists(
269
      db_select('cas_user', 'c')
270
      ->fields('c', array('cas_name'))
271
      ->where('c.cas_name = u.name')
272
    );
273
    db_insert('cas_user')
274
      ->from($query)
275
      ->execute();
276

    
277
    $message = $t('Users have been automatically assigned their CAS username. For more information, see "Associating CAS usernames with Drupal users" in the CAS module README.txt.');
278
  }
279

    
280
  variable_del('cas_authmap');
281
  variable_del('cas_hijack_user');
282
  return $message;
283
}
284

    
285
/**
286
 * Remove unnecessary CAS settings.
287
 */
288
function cas_update_6303() {
289
  // We have removed the cas_first_login option, and instead verify that
290
  // cas_first_login_destination is non-empty. To preserve functionality,
291
  // we need to update the destination to '<front>' if previously the option
292
  // was selected but the destination was empty.
293
  if (variable_get('cas_first_login', FALSE)) {
294
    if (variable_get('cas_first_login_destination', '') == '') {
295
      variable_set('cas_first_login_destination', '<front>');
296
    }
297
  }
298
  else {
299
    variable_set('cas_first_login_destination', '');
300
  }
301
  variable_del('cas_first_login');
302

    
303
  // Similarly for the cas_logout_redirect and cas_logout_destination
304
  // variables.
305
  if (variable_get('cas_logout_redirect', FALSE)) {
306
    if (variable_get('cas_logout_destination', '') == '') {
307
      variable_set('cas_logout_destination', '<front>');
308
    }
309
  }
310
  else {
311
    variable_set('cas_logout_destination', '');
312
  }
313
  variable_del('cas_logout_redirect');
314

    
315
  // If the Certicate Authority is not being verified, ensure that the
316
  // certificate field is empty.
317
  if (variable_get('cas_cert_verify', 'none') == 'none') {
318
    variable_set('cas_cert', '');
319
  }
320
  variable_del('cas_cert_verify');
321

    
322
  // Also remove the variable controlling CAS Single Sign-Out which is now
323
  // always enabled.
324
  variable_del('cas_signout');
325

    
326
  return array();
327
}
328

    
329
/**
330
 * Add destination parameter to CAS Login / CAS Logout menu links.
331
 */
332
function cas_update_6304() {
333
  // Load and save each link to 'cas' or 'caslogout' so that the 'alter' option
334
  // is enabled. This allows us to append the destination parameter to the
335
  // links at runtime. Since the menu items 'cas' and 'caslogout' are not
336
  // functional without the destination parameter, we do this for all menu
337
  // links, even custom defined ones (i.e., those with module = 'menu').
338
  $result = db_query("SELECT mlid FROM {menu_links} WHERE link_path IN (:link_path)", array(':link_path' => array('cas', 'caslogout')));
339
  foreach ($result as $record) {
340
    $link = menu_link_load($record->mlid);
341
    menu_link_save($link);
342
  }
343
}
344

    
345
/**
346
 * Transform numeric block deltas to string block deltas.
347
 */
348
function cas_update_7000(&$sandbox) {
349
  $renamed_deltas = array(
350
    'cas' => array('0' => 'login'),
351
  );
352
  $moved_deltas = array();
353
  update_fix_d7_block_deltas($sandbox, $renamed_deltas, $moved_deltas);
354
}
355

    
356
/**
357
 * Use variable 'cas_check_frequency' instead of 'cas_gateway'.
358
 */
359
function cas_update_7101() {
360
  if (variable_get('cas_check_first', NULL) === NULL) {
361
    // The old variable was not set, nothing to do.
362
    return;
363
  }
364

    
365
  if (variable_get('cas_check_first', FALSE)) {
366
    // Check once, but not again until login.
367
    variable_set('cas_check_frequency', -1);
368
  }
369
  else {
370
    // Check never.
371
    variable_set('cas_check_frequency', -2);
372
  }
373
  variable_del('cas_check_first');
374
}
375

    
376
/**
377
 * Add index on cas_user.uid
378
 */
379
function cas_update_7102() {
380
  if (!db_index_exists('cas_user', 'cas_user')) {
381
    db_add_index('cas_user', 'cas_user', array('uid'));
382
  }
383
}