Projet

Général

Profil

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

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

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_first');
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_first_login');
112
  variable_del('cas_hijack_user');
113
  variable_del('cas_ldap_email_attribute');
114
  variable_del('cas_logout_redirect');
115
  variable_del('cas_signout');
116
  variable_del('cas_useldap');
117
  variable_del('cas_useldap_groups');
118
  variable_del('cas_verify');
119
}
120

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
323
  return array();
324
}
325

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

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