-
Notifications
You must be signed in to change notification settings - Fork 3
/
versioncontrol.pages.inc
542 lines (479 loc) · 18.9 KB
/
versioncontrol.pages.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
<?php
// $Id$
/**
* @file
* Version Control API - An interface to version control systems
* whose functionality is provided by pluggable back-end modules.
*
* This file contains the user interface for non-admin tasks.
*
* Copyright 2006, 2007 Derek Wright ("dww" , http://drupal.org/user/46549)
* Copyright 2007, 2009 by Jakob Petsovits ("jpetso", http://drupal.org/user/56020)
*/
// The account registration form has a special demo mode to show the admin how
// account registration will look like in the general case, mainly in order to
// demonstrate how the various registration messages are being used.
define('VERSIONCONTROL_REGISTER_DEMO', -1);
/**
* Form callback for "versioncontrol/register[/$register_uid[/$register_at_repo_id]]":
* Provide an indirection that leads to an account registration form.
*/
function versioncontrol_account_register_page($register_uid = FALSE, $register_at_repo_id = FALSE) {
global $user;
if ($user->uid == 0 || !versioncontrol_user_access()) {
$presets = _versioncontrol_get_string_presets();
return variable_get(
'versioncontrol_registration_message_unauthorized',
$presets['versioncontrol_registration_message_unauthorized']
);
}
if ($register_uid == 'demo') {
$register_uid = VERSIONCONTROL_REGISTER_DEMO;
}
else {
$register_uid = is_numeric($register_uid) ? $register_uid : $user->uid;
}
$admin_access = versioncontrol_admin_access();
if (!$admin_access && $register_uid != $user->uid) {
drupal_access_denied();
return;
}
$accounts = versioncontrol_get_accounts(array('uids' => array($register_uid)), TRUE);
$repositories = versioncontrol_get_repositories();
// Construct the '#options' array.
$account_usernames = array();
foreach ($accounts as $uid => $usernames_by_repository) {
foreach ($usernames_by_repository as $repo_id => $username) {
$account_usernames[$repo_id][] = $username;
}
}
if ($register_uid == VERSIONCONTROL_REGISTER_DEMO) {
$account_usernames = array();
}
$repository_names = array();
foreach ($repositories as $repo_id => $repository) {
if (isset($account_usernames[$repo_id])
&& versioncontrol_is_account_authorized($repository, $register_uid)) {
// We only want repositories in the list of repositories that are open
// for registrations where no (authorized) account exists yet.
continue;
}
if (!isset($first_repo_id)) {
$first_repo_id = $repo_id;
}
$repository_names[$repo_id] = check_plain($repository['name']);
}
// Filter (and possibly change the caption of) the list of repositories to
// select. The admin has all the permissions and gets the uncensored list.
if (!$admin_access) {
foreach (module_implements('versioncontrol_alter_repository_selection') as $module) {
$function = $module .'_versioncontrol_alter_repository_selection';
$function($repository_names, $repositories);
}
}
// If there's just one repository on the site, redirect directly to this one.
if (count($repository_names) == 1) {
$only_repo_id = $first_repo_id;
}
else if (count($repositories) == 1) {
$only_repo_id = reset(array_keys($repositories));
}
if ($register_uid == VERSIONCONTROL_REGISTER_DEMO) {
unset($only_repo_id);
}
if (is_numeric($register_at_repo_id) || isset($only_repo_id)) {
// If there is only one repository left to register, it doesn't matter
// whether or not the URL contains a repository (and which one), we always
// redirect to the remaining possible one. Otherwise, we try to register
// at the given repository.
$repo_id = isset($only_repo_id) ? $only_repo_id : $register_at_repo_id;
if (isset($account_usernames[$repo_id])) {
drupal_set_message(t('You already have a registered account.'));
drupal_goto('user/'. $user->uid .'/edit/versioncontrol/'. $repo_id
.'/'. reset($account_usernames[$repo_id]));
// drupal_goto() calls exit() so script execution ends right here
}
else if (isset($repository_names[$repo_id])) {
return drupal_get_form('versioncontrol_account_edit_form',
$register_uid, $repositories[$repo_id], VERSIONCONTROL_FORM_CREATE
);
}
}
return drupal_get_form('versioncontrol_account_register_form',
$register_uid, $repository_names, $first_repo_id
);
}
/**
* Implementation of hook_versioncontrol_alter_repository_selection():
* Alter the list of repositories that are available for user registration
* and editing. This hook is called for all users except those with
* "administer version control systems" permissions.
*
* @param $repository_names
* The list of repository names as it is shown in the select box
* at 'versioncontrol/register'. Array keys are the repository ids,
* and array elements are the captions in the select box.
* There's two things that can be done with this array:
* - Change (amend) the caption, in order to provide more information
* for the user. (E.g. note that an application is necessary.)
* - Unset any number of array elements. If you do so, the user will not
* be able to register a new account for this repository.
* @param $repositories
* A list of repositories (with the repository ids as array keys) that
* includes at least all of the repositories that correspond to the
* repository ids of the @p $repository_names array.
*/
function versioncontrol_versioncontrol_alter_repository_selection(&$repository_names, $repositories) {
foreach ($repository_names as $repo_id => $caption) {
if ($repositories[$repo_id]['authorization_method'] == 'versioncontrol_admin') {
unset($repository_names[$repo_id]);
}
}
}
/**
* The actual form for "versioncontrol/register[/$register_uid]".
*/
function versioncontrol_account_register_form(&$form_state, $register_uid, $repository_names, $first_repo_id) {
$form = array();
if (empty($repository_names)) {
drupal_set_title(t('No more registrations possible'));
$form['no_registration'] = array(
'#type' => 'markup',
'#value' => t('You already have an account for all the repositories where you can register. Go to your !user-account-page to configure repository account settings.',
array('!user-account-page' => l(t('user account page'), 'user/'. $register_uid .'/edit/versioncontrol'))
),
'#prefix' => '<p>',
'#suffix' => '</p>',
);
return $form;
}
$form['#id'] = 'vcs-account-indirection-form';
$message = variable_get('versioncontrol_registration_message_authorized', FALSE);
if ($message == FALSE) {
$presets = _versioncontrol_get_string_presets();
$message = $presets['versioncontrol_registration_message_authorized'];
}
if (!empty($message)) {
$form['overview'] = array(
'#type' => 'fieldset',
'#title' => t('Overview'),
'#weight' => -100,
);
$form['overview']['overview'] = array(
'#type' => 'markup',
'#value' => $message,
);
}
$form['#uid'] = $register_uid;
$form['repository'] = array(
'#type' => 'fieldset',
'#title' => t('Select repository'),
'#weight' => 10,
);
$form['repository']['repo_id'] = array(
'#type' => 'select',
'#title' => t('Create user account in'),
'#options' => $repository_names,
'#default_value' => $first_repo_id,
);
if ($register_uid != VERSIONCONTROL_REGISTER_DEMO) {
$form['repository']['submit'] = array(
'#type' => 'submit',
'#value' => t('Create account'),
'#weight' => 100,
);
}
return $form;
}
/**
* Submit handler for the indirection form.
* Surprisingly, all it does is redirect to the appropriate registration form.
*/
function versioncontrol_account_register_form_submit($form, &$form_state) {
$form_state['redirect'] = 'versioncontrol/register/'. $form['#uid'] .'/'. $form_state['values']['repo_id'];
}
/**
* Form callback for 'user/%versioncontrol_user_accounts/edit/versioncontrol':
* Display a list of the given user's VCS accounts, or directly return the form
* array from versioncontrol_account_edit_form() if only a single account
* exists for that user (or if the account has been given in the URL).
*/
function versioncontrol_account_page($accounts, $url_repo_id = NULL, $url_username = NULL) {
$selected_usernames = array();
foreach ($accounts as $only_uid => $usernames_by_repository) {
// The caller (menu system) ensures that there is only one uid.
$uid = $only_uid;
foreach ($usernames_by_repository as $repo_id => $username) {
if (isset($url_repo_id) && $repo_id != $url_repo_id) {
unset($accounts[$uid][$repo_id]);
continue; // disregard repositories that don't match the URL constraints
}
$usernames = array($username);
foreach ($usernames as $key => $username) {
if (isset($url_username) && $username != $url_username) {
unset($accounts[$uid][$repo_id]);
continue; // disregard usernames that don't match the URL constraints
}
$any_repo_id = $repo_id;
$selected_usernames[] = $username;
}
}
}
if (empty($selected_usernames)) {
drupal_not_found();
return;
}
elseif (count($selected_usernames) == 1) {
$repository = versioncontrol_get_repository($any_repo_id);
return drupal_get_form('versioncontrol_account_edit_form',
$uid, $repository, reset($selected_usernames)
);
}
else {
return drupal_get_form('versioncontrol_account_list_form', $accounts);
}
}
/**
* Form callback for "admin/project/versioncontrol-accounts" and
* (in certain cases) "user/%versioncontrol_user_accounts/edit/versioncontrol":
* Show a list of VCS accounts to the admin or the user.
*
* @param $accounts
* The list of accounts to show, in versioncontrol_get_accounts() format.
*/
function versioncontrol_account_list_form(&$form_state, $accounts) {
$form = array();
$form['#id'] = 'versioncontrol-account-list-form';
$repositories = versioncontrol_get_repositories(array(
'repo_ids' => array_keys(reset($accounts)),
));
$header = array(t('Repository'), t('Username'), '');
$rows = array();
foreach ($accounts as $uid => $usernames_by_repository) {
foreach ($usernames_by_repository as $repo_id => $username) {
if (!isset($repositories[$repo_id])) { // happens if the backend isn't loaded
continue;
}
$usernames = array($username);
$backend = versioncontrol_get_backend($repositories[$repo_id]);
foreach ($usernames as $username) {
$formatted_username = theme('versioncontrol_account_username',
$uid, $username, $repositories[$repo_id],
array('prefer_drupal_username' => FALSE)
);
$repo_name = module_exists('commitlog')
? theme('commitlog_repository', $repositories[$repo_id])
: check_plain($repositories[$repo_id]['name']);
$rows[] = array(
$repo_name,
$formatted_username,
l(t('Edit @vcs account', array('@vcs' => $backend['name'])),
'user/'. $uid .'/edit/versioncontrol/'. $repo_id .'/'. drupal_urlencode($username)
),
);
}
}
}
$form['accounts'] = array(
'#type' => 'markup',
'#value' => theme('table', $header, $rows, array('style' => 'width: 50%;')),
);
return $form;
}
/**
* Form callback for (in certain cases) "versioncontrol/register"
* and "user/%versioncontrol_user_accounts/edit/versioncontrol":
* Provide a form to register or edit a VCS account.
*
* @param $uid
* The uid of the Drupal user whose account is being edited or registered.
* @param $repository
* The repository of the added/edited account.
* @param $vcs_username
* The user's VCS-specific username for the repository,
* or VERSIONCONTROL_FORM_CREATE if a new VCS account should be registered.
*/
function versioncontrol_account_edit_form(&$form_state, $uid, $repository, $vcs_username) {
$form = array();
$create_account = ($vcs_username === VERSIONCONTROL_FORM_CREATE);
$backends = versioncontrol_get_backends();
$vcs_name = $backends[$repository['vcs']]['name'];
$user = ($uid === VERSIONCONTROL_REGISTER_DEMO) ? FALSE : user_load($uid);
if (!($user || ($uid === VERSIONCONTROL_REGISTER_DEMO && $create_account))) {
drupal_not_found(); // cannot edit/register accounts for non-existing users
return array();
}
// Set an appropriate page title.
if ($create_account) {
drupal_set_title(t(
'Create user account in @repository',
array('@repository' => $repository['name'])
));
}
elseif ($user) {
drupal_set_title(check_plain($user->name));
}
$form['#id'] = 'versioncontrol-account-form';
$form['#repository'] = $repository;
$form['#vcs'] = $repository['vcs'];
$form['#vcs_name'] = $vcs_name;
$form['#uid'] = $uid;
$form['#original_username'] = $vcs_username;
$form['#validate'] = array('versioncontrol_account_edit_form_validate');
$form['#submit'] = array('versioncontrol_account_edit_form_submit');
if ($create_account) {
$registration_message = isset($repository['data']['versioncontrol']['registration_message'])
? $repository['data']['versioncontrol']['registration_message']
: '';
if (!empty($registration_message)) {
$form['overview'] = array(
'#type' => 'fieldset',
'#title' => t('Overview'),
'#weight' => -100,
);
$form['overview']['overview'] = array(
'#type' => 'markup',
'#value' => $registration_message,
);
}
}
$form['account'] = array(
'#type' => 'fieldset',
'#title' => t('@vcs account settings', array('@vcs' => $vcs_name)),
'#weight' => 0,
);
$admin_access = versioncontrol_admin_access();
if ($create_account || $admin_access) {
if ($create_account) {
// Have a nice default value for the new VCS username.
$vcs_username = versioncontrol_account_username_suggestion($repository, $user);
}
if ($admin_access) { // the admin version
$description = t('The @vcs username associated with the account of !user. This field is used to link commit messages to user accounts.', array('@vcs' => $vcs_name, '!user' => theme_username($user)));
}
else { // the account creation version
$description = t('Choose a username to access the @vcs repository with. @vcs usernames should be lowercase. Spaces or other special characters are not allowed.', array('@vcs' => $vcs_name));
}
$form['account']['account_name'] = array(
'#type' => 'textfield',
'#title' => t('@vcs username', array('@vcs' => $vcs_name)),
'#description' => $description,
'#default_value' => $vcs_username,
'#weight' => 0,
'#size' => 30,
'#maxlength' => 64,
);
}
else {
$form['account_name'] = array(
'#type' => 'value',
'#value' => $vcs_username,
);
$form['account']['account_name_display'] = array(
'#type' => 'item',
'#title' => t('@vcs username', array('@vcs' => $vcs_name)),
'#description' => t('Your @vcs username. This field can only be edited by administrators and is used to link your @vcs messages to your user account.', array('@vcs' => $vcs_name)),
'#value' => $vcs_username,
'#weight' => 0,
);
}
if ($uid !== VERSIONCONTROL_REGISTER_DEMO) {
$form['submit'] = array(
'#type' => 'submit',
'#value' => $create_account
? t('Create @vcs account', array('@vcs' => $vcs_name))
: t('Update @vcs account', array('@vcs' => $vcs_name)),
'#weight' => 100,
);
}
return $form;
}
/**
* Validate the edit/register user account form submission before it is submitted.
*/
function versioncontrol_account_edit_form_validate($form, &$form_state) {
if (!isset($form_state['values']['account_name'])) {
return;
}
$uid = $form['#uid'];
$username = trim($form_state['values']['account_name']);
$repository = $form['#repository'];
$vcs_name = $form['#vcs_name'];
if (!isset($repository)) { // admin deletes repo while user fills in form
form_set_error('account',
t('The repository has been deleted.')
);
return;
}
if (empty($username)) {
form_set_error('account_name',
t('The @vcs username may not be empty.', array('@vcs' => $vcs_name))
);
}
else {
// Check for username validity - done by the backend, but with a fallback
// for alphanum-only characters.
if (!versioncontrol_is_account_username_valid($repository, $username)) {
form_set_error('account_name',
t('The specified @vcs username is invalid.', array('@vcs' => $vcs_name))
);
}
// The username is passed by reference and might have changed. That's a bit
// uncomfortable as a caller API, but more convenient for the backends.
// (Plus it makes sense anyways since we have trimmed the username too.)
$form_state['values']['account_name'] = $username;
// Check for duplicates.
$existing_uid = versioncontrol_get_account_uid_for_username($repository['repo_id'], $username, TRUE);
if ($existing_uid && $uid != $existing_uid) {
if ($existing_user = user_load(array('uid' => $existing_uid))) {
$existing_username = theme('username', $existing_user);
}
else {
$existing_username = t('user #!id', array('!id' => $existing_uid));
}
form_set_error('account_name',
t('The specified @vcs username is already in use by !existing-user.',
array('@vcs' => $vcs_name, '!existing-user' => $existing_username))
);
}
}
}
/**
* Add or update the user account when the edit/register form is submitted.
*/
function versioncontrol_account_edit_form_submit($form, &$form_state) {
// Reconstruct the user data from the $form_state that was passed.
$uid = $form['#uid'];
$username = $form_state['values']['account_name'];
$repository = $form['#repository'];
$vcs_name = $form['#vcs_name'];
$vcs_specific = NULL;
// Let other modules provide additional account data.
$additional_data = array();
foreach (module_implements('versioncontrol_account_submit') as $module) {
$function = $module .'_versioncontrol_account_submit';
$function($additional_data, $form, $form_state);
}
if (empty($form['#original_username'])) {
versioncontrol_insert_account($repository, $uid, $username, $additional_data);
$message = drupal_set_message(t(
'The @vcs account %username has been registered.',
array('@vcs' => $vcs_name, '%username' => $username)
));
}
else {
versioncontrol_update_account($repository, $uid, $username, $additional_data);
// Regular users cannot change the username, and will probably get
// a message for each of the other actions that hook into the form.
if (versioncontrol_admin_access()) {
$message = drupal_set_message(t(
'The @vcs account %username has been updated successfully.',
array('@vcs' => $vcs_name, '%username' => $username)
));
}
}
$form_state['redirect'] = versioncontrol_admin_access()
? 'admin/project/versioncontrol-accounts'
: 'user/'. $uid .'/edit/versioncontrol/'. $repository['repo_id']
.'/'. drupal_urlencode($username);
}