Commit 153dd32c authored by Caleb Crawley's avatar Caleb Crawley Committed by Neil Drumm
Browse files

Issue #3265726 by crawleyhost: Allow git.drupalcode.org username renaming

parent 3dd8aaae
Loading
Loading
Loading
Loading
+0 −24
Original line number Diff line number Diff line
@@ -238,30 +238,6 @@ function drupalorg_git_gateway_get_total_accounts() {
  return db_query('SELECT count(distinct(uid)) FROM {users_roles} WHERE rid IN (:rid, :rid2)', array(':rid' => DRUPALORG_GIT_GATEWAY_RID, ':rid2' => DRUPALORG_GIT_GATEWAY_VETTED_RID))->fetchField();
}

/**
 * Create a suggested username based on the provided name value, ensuring
 * no conflicts with existing git usernames.
 *
 * @param string $name
 */
function drupalorg_git_gateway_suggest_git_username($name) {
  $base_suggestion = preg_replace('/[^A-Za-z0-9\._-]/', '', $name);
  $len = strlen($base_suggestion);

  $result = db_query("SELECT git_username FROM {users} WHERE git_username LIKE (':name')", array(':name' => $name));
  $matches = array();
  foreach ($result as $record) {
    $matches[] = $record->git_username;
  }

  // Ensure no duplicates, and add an incrementing counter if one is found.
  $i = 0;
  while (array_search($base_suggestion, $matches) !== FALSE) {
    $base_suggestion = substr($base_suggestion, 0, $len) . ++$i;
  }
  return $base_suggestion;
}

/**
 * Implements hook_versioncontrol_project_auth_data_alter().
 *
+70 −136
Original line number Diff line number Diff line
@@ -31,15 +31,9 @@ function drupalorg_git_gateway_user_page($account) {
 * Form callback, agreement for Git access.
 */
function drupalorg_git_gateway_user_form($form, &$form_state) {
  $form = array();
  $account = $form_state['account'];
  $form['#submit'][] = 'drupalorg_git_gateway_user_form_submit';

  // Fork off to the confirm form for the username before anything else.
  if (!empty($form_state['storage']['chosen_username'])) {
    return drupalorg_git_gateway_confirm_username($form, $form_state, $account);
  }

  $form['#validate'] = array('drupalorg_git_gateway_user_form_base_validate');
  $form['submit'] = array(
    '#type' => 'submit',
@@ -62,29 +56,15 @@ function drupalorg_git_gateway_user_form($form, &$form_state) {
    array_unshift($form['#submit'], 'drupalorg_git_gateway_admin_user_form_submit');
  }

  // If everything's set in the expected state, change the button text to make
  // clear what submitting the form does, and show configuration helper text.
  // If everything’s set in the expected state, show configuration helper text.
  if (!empty($account->git_consent) && !empty($account->git_username)) {
    $form['submit']['#value'] = t('Update Git access agreement');
    $form['git_config'] = _drupalorg_git_gateway_user_config($account);
  }

  if (!isset($account->git_username)) {
  $form['username'] = _drupalorg_git_gateway_username_set($account);
    if ($show_messages) {
      drupal_set_message(t('You will not be able to use Git until you have selected a Git username. A suggestion has been provided for you, based on your username. <strong>Note that once chosen, your Git username cannot be changed.</strong>'), 'warning');
  if ($show_messages && empty($account->git_username)) {
    drupal_set_message(t('You will not be able to use Git until you have selected a Git username.'), 'warning');
  }
  }
  else {
    $form['access'] = [
      '#weight' => -20,
      'username' => [
        '#type' => 'item',
        '#title' => t('Your Git username'),
        '#markup' => $account->git_username,
        '#description' => t('You can use this as your HTTPS username when authenticating with Git to Drupal.org. Using SSH is recommended and the username is always <code>git</code>. Drupal.org uses this username to generate your personalized sandbox URIs. <a href="!gitauth">More about Drupal.org Git authentication</a>', ['!gitauth' => url('gitauth')]),
      ],
    ];

  // If GitLab is present, add HTTPS password management.
  try {
    if (user_access('gitlab sync account', $account) && variable_get('versioncontrol_gitlab_key') && class_exists('Gitlab\Client') && ($gitlab_user = versioncontrol_gitlab_get_user($account))) {
@@ -119,7 +99,6 @@ function drupalorg_git_gateway_user_form($form, &$form_state) {
  catch (Exception $e) {
    watchdog('drupalorg_git_gateway', 'Exception in GitLab token UI for user %user error: %msg', ['%user' => $account->uid . ' - ' . $account->git_username, '%msg' => $e->getMessage()], WATCHDOG_WARNING);
  }
  }

  $form['git_tos'] = _drupalorg_git_gateway_user_tos($account);
  if (empty($account->git_consent)) {
@@ -165,24 +144,12 @@ function drupalorg_git_gateway_admin_user_form($form, &$form_state) {
    '#collapsed' => FALSE,
    '#weight' => -20,
  );
  if (!empty($account->git_username)) {
    $form['user_info']['username'] = array(
  $form['user_info']['username'] = [
    '#type' => 'item',
      '#title' => t('Git username'),
      '#disabled' => TRUE,
      '#markup' => $account->git_username,
    '#title' => t('Current Git username'),
    '#markup' => $account->git_username ?: t('(Has not yet chosen a Git username)'),
    '#weight' => -20,
    );
  }
  else {
    $form['user_info']['username'] = array(
      '#type' => 'item',
      '#title' => t('Git username'),
      '#disabled' => TRUE,
      '#markup' => t('(User has not yet chosen a Git username)'),
      '#weight' => -20,
    );
  }
  ];

  $form['user_info']['consent'] = array(
    '#type' => 'item',
@@ -253,23 +220,39 @@ function _drupalorg_git_gateway_user_tos($account) {
}

function _drupalorg_git_gateway_username_set($account) {
  $form = array(
  $form = [
    'username_container' => [],
  ];
  if (!empty($account->git_username)) {
    $form['access'] = [
      'username' => [
        '#type' => 'item',
        '#title' => t('Your Git username'),
        '#markup' => $account->git_username,
        '#description' => t('You can use this as your HTTPS username when authenticating with Git to Drupal.org. Using SSH is recommended and the username is always <code>git</code>. Drupal.org uses this username to generate your personalized sandbox URIs. <a href="!gitauth">More about Drupal.org Git authentication</a>', ['!gitauth' => url('gitauth')]),
      ],
      '#weight' => -1,
    ];
    $form['username_container'] = [
      '#type' => 'fieldset',
    '#title' => t('Git username'),
    '#collapsible' => FALSE,
    '#collapsed' => FALSE,
    '#weight' => -20,
  );

  $form['#description'] = t('Choose a username to use for Git. This can be used when authenticating to Drupal.org with Git, and Drupal.org will use it to generate your personalized sandbox URIs. <strong>Once chosen, your Git username cannot be changed.</strong>');
  $form['git_username'] = array(
      '#title' => t('Change Git username'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      'git_username' => [],
      'submit' => [
        '#type' => 'submit',
        '#value' => t('Save'),
      ],
    ];
  }
  $form['username_container']['git_username'] = [
    '#type' => 'textfield',
    '#title' => t('Desired Git username'),
    '#title' => t('Requested Git username'),
    '#required' => TRUE,
    '#maxlength' => 64,
    '#default_value' => drupalorg_git_gateway_suggest_git_username($account->name),
    '#default_value' => $account->git_username ?: $account->name,
    '#description' => t('Can contain A-Z letters, numbers, periods, underscores, and dashes. Can not start with a dash; or end with a dash, dot, <code>.git</code>, or <code>.atom</code>.'),
  );
  ];
  return $form;
}

@@ -282,7 +265,10 @@ function _drupalorg_git_gateway_user_config($account) {
    '#weight' => 15,
  );

  $user_config = t('You will not be credited in commit statistics or listings unless you set the Git email address to either <a href="!emailadmin">an address associated with your account</a> or the anonymized email provided below. Read <a href="!handbook">the handbook</a> for a full discussion of identifying yourself to Git. We recommend:', (array('!handbook' => url('node/1022156'), '!emailadmin' => url("user/$account->uid/edit/email-addresses")))) . "\n";
  $user_config = t('Your commits may not be credited in commit statistics or listings unless you set the Git email address to either <a href="!emailadmin">an address associated with your account</a> or the anonymized email provided below. Read <a href="!handbook">the handbook</a> for a full discussion of identifying yourself to Git. We recommend:', [
    '!handbook' => url('node/1022156'),
    '!emailadmin' => url('user/' . $account->uid . '/edit/email-addresses'),
  ]);
  $wrapper = entity_metadata_wrapper('user', $account);
  $first_name = $wrapper->field_first_name->value();
  $last_name = $wrapper->field_last_name->value();
@@ -351,37 +337,6 @@ Your Git access on Drupal.org has been disabled because it is in violation of th
  return $form;
}

/**
 * Form callback, confirm Git username choice.
 *
 * @see drupalorg_git_gateway_user_form()
 */
function drupalorg_git_gateway_confirm_username($form, &$form_state, $account) {
  // Copy out the original settings so we have them when actually submitting.
  $form_state['storage']['base_form_values'] = $form_state['values'];

  $form['confirm_username'] = array(
    '#type' => 'item',
    '#title' => t('Are you sure you want "%username" for your Git username?', array('%username' => $form_state['storage']['chosen_username'])),
    '#description' => t('Once chosen, your Git username can <em>never</em> be changed!'),
  );

  $form['confirm'] = array(
    '#type' => 'submit',
    '#value' => t('Confirm'),
  );
  $form['cancel'] = array(
    '#type' => 'submit',
    '#value' => t('Cancel'),
    // '#button_type' => 'cancel',
    '#executes_submit_callback' => FALSE,
  );

  $form['#validate'] = array('drupalorg_git_gateway_confirm_username_validate');

  return $form;
}

/**
 * Form validate callback for drupalorg_git_gateway_user_form().
 */
@@ -418,27 +373,6 @@ function drupalorg_git_gateway_user_form_base_validate($form, &$form_state) {
    elseif (preg_match('/\.([^.]*)$/', $form_state['values']['git_username'], $match) && in_array($match[1], ['html', 'xhtml', 'text', 'txt', 'js', 'css', 'ics', 'csv', 'vcf', 'vtt', 'png', 'jpeg', 'jpg', 'jpe', 'pjpeg', 'gif', 'bmp', 'tiff', 'tif', 'svg', 'mpeg', 'mpg', 'mpe', 'mp3', 'mp1', 'mp2', 'ogg', 'oga', 'spx', 'opus', 'm4a', 'mpg4', 'aac', 'webm', 'mp4', 'm4v', 'otf', 'ttf', 'woff', 'woff2', 'xml', 'rss', 'atom', 'yaml', 'yml', 'multipart_form', 'url_encoded_form', 'pdf', 'zip', 'gzip', 'gz', 'diff', 'patch', 'markdown', 'md', 'mov', 'ogv', 'json', 'ico'])) {
      form_set_error('git_username', t('The requested username is reserved, please choose a different Git username.'));
    }
    elseif (empty($form_state['account']->git_username)) {
      $form_state['rebuild'] = TRUE;
      $form_state['storage']['chosen_username'] = $form_state['values']['git_username'];
    }
  }
}

/**
 * Form validate callback for drupalorg_git_gateway_confirm_username().
 */
function drupalorg_git_gateway_confirm_username_validate($form, &$form_state) {
  // Copy the stored base form values into place for the main submit handler.
  $form_state['values'] += $form_state['storage']['base_form_values'];
  // Confirm or cancel, form processing should complete; ensure that happens.
  unset($form_state['storage']);
  $form_state['rebuild'] = FALSE;
  $form_state['submitted'] = TRUE;

  if (empty($form_state['clicked_button']) || $form_state['clicked_button']['#value'] != t('Confirm')) {
    // Unset the git username so it isn't processed.
    unset($form_state['values']['git_username']);
  }
}