]> git.mxchange.org Git - shipsimu.git/blobdiff - inc/classes/main/filter/validator/class_UserNameValidatorFilter.php
Email address confirmation now working (not in registration):
[shipsimu.git] / inc / classes / main / filter / validator / class_UserNameValidatorFilter.php
index 6f18e6b5ebd47f6e3f6fbd9f021381d1b6a31eb9..43927a7d01917e5e5683b5dfb3c359ab4a7a6066 100644 (file)
@@ -7,7 +7,7 @@
  *
  * @author             Roland Haeder <webmaster@ship-simu.org>
  * @version            0.0.0
- * @copyright  Copyright(c) 2007, 2008 Roland Haeder, this is free software
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, this is free software
  * @license            GNU GPL 3.0 or any newer version
  * @link               http://www.ship-simu.org
  *
@@ -24,9 +24,9 @@
  * You should have received a copy of the GNU General Public License
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
-class UserNameValidatorFilter extends BaseFrameworkSystem implements Filterable {
+class UserNameValidatorFilter extends BaseFilter implements Filterable {
        /**
-        * Private constructor
+        * Protected constructor
         *
         * @return      void
         */
@@ -38,11 +38,7 @@ class UserNameValidatorFilter extends BaseFrameworkSystem implements Filterable
                $this->setObjectDescription("A filter for username validation");
 
                // Create unique ID number
-               $this->createUniqueID();
-
-               // Clean up a little
-               $this->removeNumberFormaters();
-               $this->removeSystemArray();
+               $this->generateUniqueId();
        }
 
        /**
@@ -74,7 +70,7 @@ class UserNameValidatorFilter extends BaseFrameworkSystem implements Filterable
                        // Not found in form so stop the filtering process
                        $requestInstance->requestIsValid(false);
 
-                       // Set a message for the response
+                       // Add a message to the response
                        $responseInstance->addFatalMessage('username_unset');
 
                        // Abort here
@@ -83,31 +79,66 @@ class UserNameValidatorFilter extends BaseFrameworkSystem implements Filterable
                        // Empty field!
                        $requestInstance->requestIsValid(false);
 
-                       // Set a message for the response
+                       // Add a message to the response
                        $responseInstance->addFatalMessage('username_empty');
 
                        // Abort here
                        return false;
-               } elseif (!$this->ifUserNameIsValid($userName)) {
-                       // Regular expression check failed!
-                       $requestInstance->requestIsValid(false);
-
-                       // Set a message for the response
-                       $responseInstance->addFatalMessage('username_invalid');
-
-                       // Abort here
-                       return false;
-               } elseif (!$this->ifUserNameIsTaken($userName)) {
+               } elseif ($this->ifUserNameIsTaken($userName)) {
                        // Username is already taken
                        $requestInstance->requestIsValid(false);
 
-                       // Set a message for the response
+                       // Add a message to the response
                        $responseInstance->addFatalMessage('username_taken');
 
                        // Abort here
                        return false;
                }
        }
+
+       /**
+        * Check wether the username as already been taken
+        *
+        * @param       $userName               Username to check for existence
+        * @return      $alreadyTaken   Wether the username has been taken
+        */
+       private function ifUserNameIsTaken ($userName) {
+               // Default is already taken
+               $alreadyTaken = true;
+
+               // Initialize instance
+               $userInstance = null;
+
+               // Get a registry instance
+               $registry = Registry::getRegistry();
+
+               // Is the user already there?
+               if ($registry->instanceExists('user')) {
+                       // Use the instance for checking for the email
+                       $userInstance = $registry->getInstance('user');
+                       $userInstance->setUserName($userName);
+               } else {
+                       // If this instance is created then the username *does* exist
+                       try {
+                               // Get a new instance
+                               $userInstance = call_user_func_array(array($this->getConfigInstance()->readConfig('user_class'), "createUserByUsername"), array($userName));
+
+                               // Remember this user instance in our registry for later usage
+                               $registry->addInstance('user', $userInstance);
+                       } catch (UsernameMissingException $e) {
+                               // User was not found
+                       }
+               }
+
+               // Does the username exist?
+               if ((is_null($userInstance)) || (!$userInstance->ifUsernameExists())) {
+                       // This username is still available
+                       $alreadyTaken = false;
+               } // END - if
+
+               // Return the result
+               return $alreadyTaken;
+       }
 }
 
 // [EOF]