From c63291c0e173a802a92476e3ef451219fa9e20c1 Mon Sep 17 00:00:00 2001
From: Marco Villegas <marvil07@gmail.com>
Date: Mon, 29 Nov 2010 01:57:47 -0500
Subject: [PATCH 1/2] Move account class non-object-dependent non-crud methods to backend class.

- move VersioncontrolAccount::isUsernameValid() to VersioncontrolBackend class
- move VersioncontrolAccount::usernameSuggestion() to VersioncontrolBackend class
---
 includes/VersioncontrolAccount.php          |   31 ---------------------------
 includes/VersioncontrolBackend.php          |   14 ++++++++++++
 versioncontrol.pages.inc                    |    4 +-
 versioncontrol_fakevcs/includes/classes.inc |   16 +++++++-------
 4 files changed, 24 insertions(+), 41 deletions(-)

diff --git includes/VersioncontrolAccount.php includes/VersioncontrolAccount.php
index 8ff93da..05d8e1e 100644
--- includes/VersioncontrolAccount.php
+++ includes/VersioncontrolAccount.php
@@ -31,37 +31,6 @@ abstract class VersioncontrolAccount extends VersioncontrolEntity {
   public $repo_id;
 
   /**
-   * Return the most accurate guess on what the VCS username for a Drupal user
-   * might look like in the repository's account.
-   *
-   * @param $user
-   *  The Drupal user who wants to register an account.
-   */
-  public function usernameSuggestion($user) {
-    return strtr(drupal_strtolower($user->name),
-      array(' ' => '', '@' => '', '.' => '', '-' => '', '_' => '', '.' => '')
-    );
-  }
-
-  /**
-   * Determine if the account repository allows a username to exist.
-   *
-   * @param $username
-   *  The username to check. It is passed by reference so if the username is
-   *  valid but needs minor adaptions (such as cutting away unneeded parts) then
-   *  it the backend can modify it before returning the result.
-   *
-   * @return
-   *   TRUE if the username is valid, FALSE if not.
-   */
-  public function isUsernameValid(&$username) {
-    if (!preg_match('/^[a-zA-Z0-9]+$/', $username)) {
-      return FALSE;
-    }
-    return TRUE;
-  }
-
-  /**
    * Update a VCS user account in the database, and call the necessary
    * module hooks. The account repository and uid must stay the same values as
    * the one given on account creation, whereas vcs_username and
diff --git includes/VersioncontrolBackend.php includes/VersioncontrolBackend.php
index bab4718..592144c 100644
--- includes/VersioncontrolBackend.php
+++ includes/VersioncontrolBackend.php
@@ -217,4 +217,18 @@ abstract class VersioncontrolBackend {
   public function formatRevisionIdentifier($revision, $format = 'full') {
     return $revision;
   }
+
+  /**
+   * Return the most accurate guess on what the VCS username for a Drupal user
+   * might look like in the repository's account.
+   *
+   * @param $user
+   *  The Drupal user who wants to register an account.
+   */
+  public function usernameSuggestion($user) {
+    return strtr(drupal_strtolower($user->name),
+      array(' ' => '', '@' => '', '.' => '', '-' => '', '_' => '', '.' => '')
+    );
+  }
+
 }
diff --git versioncontrol.pages.inc versioncontrol.pages.inc
index 1307d2c..28b3caa 100644
--- versioncontrol.pages.inc
+++ versioncontrol.pages.inc
@@ -397,7 +397,7 @@ function versioncontrol_account_edit_form(&$form_state, $uid, $repository, $vcs_
       );
       $vcs_account = $repository->getBackend()->buildEntity('account', $data);
       // Have a nice default value for the new VCS username.
-      $vcs_username = $vcs_account->usernameSuggestion($user);
+      $vcs_username = $repository->getBackend()->usernameSuggestion($user);
     }
 
     if ($admin_access) { // the admin version
@@ -476,7 +476,7 @@ function versioncontrol_account_edit_form_validate($form, &$form_state) {
       'repository' => $repository,
     );
     $vcs_account = $repository->getBackend()->buildEntity('account', $data);
-    if (!$vcs_account->isUsernameValid($username)) {
+    if (!$repository->getBackend()->isUsernameValid($username)) {
       form_set_error('account_name',
         t('The specified @vcs username is invalid.', array('@vcs' => $vcs_name))
       );
diff --git versioncontrol_fakevcs/includes/classes.inc versioncontrol_fakevcs/includes/classes.inc
index 1e32fed..441941e 100644
--- versioncontrol_fakevcs/includes/classes.inc
+++ versioncontrol_fakevcs/includes/classes.inc
@@ -34,6 +34,14 @@ class VersioncontrolFakeBackend extends VersioncontrolBackend {
     );
   }
 
+  /**
+   * Overwrite
+   */
+  function isUsernameValid(&$username) {
+    // Continuing the email-style username example from above.
+    return valid_email_address($username);
+  }
+
 }
 
 class VersioncontrolFakeRepository extends VersioncontrolRepository implements VersioncontrolRepositoryImportExport, VersioncontrolRepositoryGetItem {
@@ -149,14 +157,6 @@ class VersioncontrolFakeAccount extends VersioncontrolAccount {
   /**
    * Overwrite
    */
-  function isUsernameValid(&$username) {
-    // Continuing the email-style username example from above.
-    return valid_email_address($username);
-  }
-
-  /**
-   * Overwrite
-   */
   protected function _insert($additional_data) {
     $fakevcs_specific = $additional_data['fakevcs_specific'];
     if (!isset($fakevcs_specific) || !isset($fakevcs_specific['password'])) {
-- 
1.7.2.3

