Projet

Général

Profil

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

root / drupal7 / sites / all / modules / cas / cas.install @ e9f59589

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
    'primary key' => array('aid'),
61
    'foreign keys' => array(
62
      'user' => array(
63
        'table' => 'users',
64
        'columns' => array('uid' => 'uid'),
65
      ),
66
    ),
67
  );
68

    
69
  return $schema;
70
}
71

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

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

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

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

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

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

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

    
175
  db_create_table('cas_login_data', $schema['cas_login_data']);
176
}
177

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

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

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

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

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

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

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

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

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

    
275
    $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.');
276
  }
277

    
278
  variable_del('cas_authmap');
279
  variable_del('cas_hijack_user');
280
  return $message;
281
}
282

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

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

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

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

    
324
  return array();
325
}
326

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

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

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

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