This issue is very similar to the final patch in #1242156: illegal username character. I'd like to see us using the first initial and last name of a customer name to create a username for use during anonymous checkout completion / account creation instead of the e-mail address (or at least as an alternative). View the patch there to see how I added this computed property to order entities; the patch for customer profiles will require the same integration. We should consider what to do about username collision, too, in the event that two anonymous customers with the same name create an account on the site. It should be intelligent enough to allow the same username for orders using the same user account e-mail address.

I'm not sure if we should make this new token the default for Commerce 1.x, as it would change the default account creation behavior for all existing sites, but that probably isn't a huge deal. Sites wanting to continue using the e-mail address can be instructed to use the old token.

Comments

alyssaengleson@aimclear.com’s picture

Component: Customer » Order
alyssaengleson@aimclear.com’s picture

Category: feature » support
alyssaengleson@aimclear.com’s picture

Component: Order » Customer
Category: support » feature
jacknely’s picture

Any developments with this?

I really need a solution/workaround quick

marcin.wosinek’s picture

Status: Active » Needs review
StatusFileSize
new2.11 KB

I've started working on patch.

Right now it adds new property "auto-username" which contains all but last initials, and last name. It works for both name inputs (Single line and First name, Last name).

We should consider what to do about username collision

commerce_order_get_properties, just before returning is correct place to put logic for this?

It should be intelligent enough to allow the same username for orders using the same user account e-mail address.

Just before creating new user we check if hers email is already registered, so it seems to me that we don't need to care about it.

Other thing is that we should fallback to use email-based username if there is no name in customer address.

joshmiller’s picture

Status: Needs review » Needs work

Patch would not apply. Here's my thought: If we're going to generate an automatic username, we have a *lot* to think about. This long list of valid usernames that are somewhat strange:

  // Username validation.
  function testUsernames() {
    $test_cases = array( // '<username>' => array('<description>', 'assert<testName>'),
      'foo'                    => array('Valid username', 'assertNull'),
      'FOO'                    => array('Valid username', 'assertNull'),
      'Foo O\'Bar'             => array('Valid username', 'assertNull'),
      'foo@bar'                => array('Valid username', 'assertNull'),
      'foo@example.com'        => array('Valid username', 'assertNull'),
      'foo@-example.com'       => array('Valid username', 'assertNull'), // invalid domains are allowed in usernames
      'þòøÇߪř€'               => array('Valid username', 'assertNull'),
      'ᚠᛇᚻ᛫ᛒᛦᚦ'                => array('Valid UTF8 username', 'assertNull'), // runes
    );
}

And while core has rules about what can be in a username, it doesn't actually convert any names. This makes converting given names, Commerce's problem.

/**
 * Verify the syntax of the given name.
 */
function user_validate_name($name) {
  if (!$name) {
    return t('You must enter a username.');
  }
  if (substr($name, 0, 1) == ' ') {
    return t('The username cannot begin with a space.');
  }
  if (substr($name, -1) == ' ') {
    return t('The username cannot end with a space.');
  }
  if (strpos($name, '  ') !== FALSE) {
    return t('The username cannot contain multiple spaces in a row.');
  }
  if (preg_match('/[^\x{80}-\x{F7} a-z0-9@_.\'-]/i', $name)) {
    return t('The username contains an illegal character.');
  }
  if (preg_match('/[\x{80}-\x{A0}' .         // Non-printable ISO-8859-1 + NBSP
                  '\x{AD}' .                // Soft-hyphen
                  '\x{2000}-\x{200F}' .     // Various space characters
                  '\x{2028}-\x{202F}' .     // Bidirectional text overrides
                  '\x{205F}-\x{206F}' .     // Various text hinting characters
                  '\x{FEFF}' .              // Byte order mark
                  '\x{FF01}-\x{FF60}' .     // Full-width latin
                  '\x{FFF9}-\x{FFFD}' .     // Replacement characters
                  '\x{0}-\x{1F}]/u',        // NULL byte and control characters
                  $name)) {
    return t('The username contains an illegal character.');
  }
  if (drupal_strlen($name) > USERNAME_MAX_LENGTH) {
    return t('The username %name is too long: it must be %max characters or less.', array('%name' => $name, '%max' => USERNAME_MAX_LENGTH));
  }
}

Additionally, if we add one small feature like this, someone is liable to dislike whatever we choose as the way the username is generated. So, I would like to see this token have a configuration screen that creates a pattern that we can use to generate the actual username (ugh!).

Or ... people could just use http://drupal.org/project/auto_username module and be done with it?

Josh

joshmiller’s picture

Issue summary: View changes

Adding clarification on the checkout completion rule update.