user.install 11.4 KB
Newer Older
1
2
<?php

3
4
5
6
7
/**
 * @file
 * Install, update and uninstall functions for the user module.
 */

8
/**
9
 * Implements hook_schema().
10
11
12
 */
function user_schema() {
  $schema['authmap'] = array(
13
    'description' => 'Stores distributed authentication mapping.',
14
    'fields' => array(
15
      'aid' => array(
16
        'description' => 'Primary Key: Unique authmap ID.',
17
18
19
20
21
22
23
24
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'uid' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
25
        'description' => "User's {users}.uid.",
26
27
28
29
30
31
      ),
      'authname' => array(
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => '',
32
        'description' => 'Unique authentication name.',
33
34
35
36
37
38
      ),
      'module' => array(
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => '',
39
        'description' => 'Module which is controlling the authentication.',
40
      ),
41
    ),
42
43
44
    'unique keys' => array(
      'authname' => array('authname'),
    ),
45
    'primary key' => array('aid'),
46
    'foreign keys' => array(
47
48
49
50
      'user' => array(
        'table' => 'users',
        'columns' => array('uid' => 'uid'),
      ),
51
    ),
52
53
  );

54
  $schema['role_permission'] = array(
55
    'description' => 'Stores the permissions assigned to user roles.',
56
    'fields' => array(
57
58
59
60
      'rid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
61
        'description' => 'Foreign Key: {role}.rid.',
62
      ),
63
64
      'permission' => array(
        'type' => 'varchar',
65
        'length' => 128,
66
        'not null' => TRUE,
67
        'default' => '',
68
        'description' => 'A single permission granted to the role identified by rid.',
69
      ),
70
71
72
73
74
75
76
      'module' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => "The module declaring the permission.",
      ),
77
    ),
78
    'primary key' => array('rid', 'permission'),
79
    'indexes' => array(
80
      'permission' => array('permission'),
81
    ),
82
    'foreign keys' => array(
83
84
85
86
      'role' => array(
        'table' => 'roles',
        'columns' => array('rid' => 'rid'),
      ),
87
    ),
88
89
90
  );

  $schema['role'] = array(
91
    'description' => 'Stores user roles.',
92
    'fields' => array(
93
94
95
96
      'rid' => array(
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
97
        'description' => 'Primary Key: Unique role ID.',
98
99
100
101
102
103
      ),
      'name' => array(
        'type' => 'varchar',
        'length' => 64,
        'not null' => TRUE,
        'default' => '',
104
        'description' => 'Unique role name.',
105
        'translatable' => TRUE,
106
      ),
107
108
109
110
111
112
      'weight' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'description' => 'The weight of this role in listings and the user interface.',
      ),
113
    ),
114
115
116
    'unique keys' => array(
      'name' => array('name'),
    ),
117
    'primary key' => array('rid'),
118
119
120
    'indexes' => array(
      'name_weight' => array('name', 'weight'),
    ),
121
122
  );

123
124
  // The table name here is plural, despite Drupal table naming standards,
  // because "user" is a reserved word in many databases.
125
  $schema['users'] = array(
126
    'description' => 'Stores user data.',
127
    'fields' => array(
128
      'uid' => array(
129
        'type' => 'int',
130
131
        'unsigned' => TRUE,
        'not null' => TRUE,
132
        'description' => 'Primary Key: Unique user ID.',
133
        'default' => 0,
134
135
136
137
138
139
      ),
      'name' => array(
        'type' => 'varchar',
        'length' => 60,
        'not null' => TRUE,
        'default' => '',
140
        'description' => 'Unique user name.',
141
      ),
142
143
144
145
146
147
148
      'langcode' => array(
        'type' => 'varchar',
        'length' => 12,
        'not null' => TRUE,
        'default' => '',
        'description' => "The {language}.langcode of the user's profile.",
      ),
149
150
      'pass' => array(
        'type' => 'varchar',
151
        'length' => 128,
152
153
        'not null' => TRUE,
        'default' => '',
154
        'description' => "User's password (hashed).",
155
156
157
      ),
      'mail' => array(
        'type' => 'varchar',
158
        'length' => 254,
159
160
        'not null' => FALSE,
        'default' => '',
161
        'description' => "User's e-mail address.",
162
163
164
165
166
167
      ),
      'theme' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
168
        'description' => "User's default theme.",
169
170
171
172
173
174
      ),
      'signature' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
175
        'description' => "User's signature.",
176
      ),
177
      'signature_format' => array(
178
179
        'type' => 'varchar',
        'length' => 255,
180
        'not null' => FALSE,
181
182
        'description' => 'The {filter_format}.format of the signature.',
      ),
183
184
185
186
      'created' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
187
        'description' => 'Timestamp for when user was created.',
188
189
190
191
192
      ),
      'access' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
193
        'description' => 'Timestamp for previous time user accessed the site.',
194
195
196
197
198
      ),
      'login' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
199
        'description' => "Timestamp for user's last login.",
200
201
202
203
204
205
      ),
      'status' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'size' => 'tiny',
206
        'description' => 'Whether the user is active(1) or blocked(0).',
207
208
209
      ),
      'timezone' => array(
        'type' => 'varchar',
210
        'length' => 32,
211
        'not null' => FALSE,
212
        'description' => "User's time zone.",
213
      ),
214
      'preferred_langcode' => array(
215
216
217
218
        'type' => 'varchar',
        'length' => 12,
        'not null' => TRUE,
        'default' => '',
219
        'description' => 'The {language}.langcode that the user prefers for receiving emails and viewing the site.',
220
221
      ),
      'picture' => array(
222
        'type' => 'int',
223
        'not null' => TRUE,
224
        'default' => 0,
225
        'description' => "Foreign key: {file_managed}.fid of user's picture.",
226
227
228
      ),
      'init' => array(
        'type' => 'varchar',
229
        'length' => 254,
230
231
        'not null' => FALSE,
        'default' => '',
232
        'description' => 'E-mail address used for initial account creation.',
233
234
      ),
      'data' => array(
235
        'type' => 'blob',
236
237
        'not null' => FALSE,
        'size' => 'big',
238
        'serialize' => TRUE,
239
        'description' => 'A serialized array of name value pairs that are related to the user. Any form values posted during user edit are stored and are loaded into the $user object during user_load(). Use of this field is discouraged and it will likely disappear in a future version of Drupal.',
240
      ),
241
242
    ),
    'indexes' => array(
243
      'access' => array('access'),
244
245
      'created' => array('created'),
      'mail' => array('mail'),
246
    ),
247
248
249
    'unique keys' => array(
      'name' => array('name'),
    ),
250
    'primary key' => array('uid'),
251
    'foreign keys' => array(
252
253
254
255
      'signature_format' => array(
        'table' => 'filter_format',
        'columns' => array('signature_format' => 'format'),
      ),
256
    ),
257
258
  );

259
  $schema['users_roles'] = array(
260
    'description' => 'Maps users to roles.',
261
    'fields' => array(
262
263
264
265
266
      'uid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
267
        'description' => 'Primary Key: {users}.uid for user.',
268
269
270
271
272
273
      ),
      'rid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
274
        'description' => 'Primary Key: {role}.rid for role.',
275
      ),
276
277
    ),
    'primary key' => array('uid', 'rid'),
278
279
280
    'indexes' => array(
      'rid' => array('rid'),
    ),
281
    'foreign keys' => array(
282
283
284
285
286
287
288
289
      'user' => array(
        'table' => 'users',
        'columns' => array('uid' => 'uid'),
      ),
      'role' => array(
        'table' => 'roles',
        'columns' => array('rid' => 'rid'),
      ),
290
    ),
291
292
293
294
295
  );

  return $schema;
}

296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/**
 * Implements hook_install().
 */
function user_install() {
  // Insert a row for the anonymous user.
  db_insert('users')
    ->fields(array(
      'uid' => 0,
      'name' => '',
      'mail' => '',
    ))
    ->execute();

  // We need some placeholders here as name and mail are uniques and data is
  // presumed to be a serialized array. This will be changed by the settings
  // form in the installer.
  db_insert('users')
    ->fields(array(
      'uid' => 1,
      'name' => 'placeholder-for-uid-1',
      'mail' => 'placeholder-for-uid-1',
      'created' => REQUEST_TIME,
      'status' => 1,
319
      'data' => NULL,
320
321
322
323
324
    ))
    ->execute();

  // Built-in roles.
  $rid_anonymous = db_insert('role')
325
    ->fields(array('name' => 'anonymous user', 'weight' => 0))
326
327
    ->execute();
  $rid_authenticated = db_insert('role')
328
    ->fields(array('name' => 'authenticated user', 'weight' => 1))
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
    ->execute();

  // Sanity check to ensure the anonymous and authenticated role IDs are the
  // same as the drupal defined constants. In certain situations, this will
  // not be true.
  if ($rid_anonymous != DRUPAL_ANONYMOUS_RID) {
    db_update('role')
      ->fields(array('rid' => DRUPAL_ANONYMOUS_RID))
      ->condition('rid', $rid_anonymous)
      ->execute();
  }
  if ($rid_authenticated != DRUPAL_AUTHENTICATED_RID) {
    db_update('role')
      ->fields(array('rid' => DRUPAL_AUTHENTICATED_RID))
      ->condition('rid', $rid_authenticated)
      ->execute();
  }
}
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364

/**
 * @addtogroup updates-7.x-to-8.x
 * @{
 */

/**
 * The 'Member for' extra field has moved one level up in the array.
 */
function user_update_8000() {
  $settings = field_bundle_settings('user', 'user');
  if (isset($settings['extra_fields']['display']['summary'])) {
    $settings['extra_fields']['display']['member_for'] = $settings['extra_fields']['display']['summary'];
    unset($settings['extra_fields']['display']['summary']);
    field_bundle_settings('user', 'user', $settings);
  }
}

365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/**
 * Splits {users}.language field to langcode and preferred_langcode.
 *
 * @see http://drupal.org/node/1454538
 */
function user_update_8001() {
  // The former language field is the language preference of the user. Rename
  // this to preferred_langcode in order to distinguish it from the langcode
  // field common to all entity types, used for identifying the language of the
  // entity itself.
  $preferred_langcode_field = array(
    'type' => 'varchar',
    'length' => 12,
    'not null' => TRUE,
    'default' => '',
    'description' => 'The {language}.langcode that the user prefers for receiving emails and viewing the site.',
  );
  db_change_field('users', 'language', 'preferred_langcode', $preferred_langcode_field);

  // Add the langcode field.
  $langcode_field = array(
    'type' => 'varchar',
    'length' => 12,
    'not null' => TRUE,
    'default' => '',
    'description' => "The {language}.langcode of the user's profile.",
  );
  db_add_field('users', 'langcode', $langcode_field);

  // Since distinguishing the language of the user entity from the user's
  // preferred language is a new feature in Drupal 8, assume that for updated
  // sites, existing user entities are in the user's preferred language.
  db_update('users')->expression('langcode', 'preferred_langcode')->execute();
}

400
401
402
/**
 * @} End of "addtogroup updates-7.x-to-8.x"
 */