* 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
*/
$this->setObjectDescription("A filter for username validation");
// Create unique ID number
- $this->createUniqueID();
-
- // Clean up a little
- $this->removeNumberFormaters();
- $this->removeSystemArray();
+ $this->generateUniqueId();
}
/**
* @return void
*/
public function execute (Requestable $requestInstance, Responseable $responseInstance) {
- // Implement this!
- $this->partialStub("Please implement this method.");
+ // Get username from request
+ $userName = $requestInstance->getRequestElement('username');
+
+ // Is the username set?
+ if (is_null($userName)) {
+ // Not found in form so stop the filtering process
+ $requestInstance->requestIsValid(false);
+
+ // Add a message to the response
+ $responseInstance->addFatalMessage('username_unset');
+
+ // Abort here
+ return false;
+ } elseif (empty($userName)) {
+ // Empty field!
+ $requestInstance->requestIsValid(false);
+
+ // Add a message to the response
+ $responseInstance->addFatalMessage('username_empty');
+
+ // Abort here
+ return false;
+ } elseif ($this->ifUserNameIsTaken($userName)) {
+ // Username is already taken
+ $requestInstance->requestIsValid(false);
+
+ // 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;
+ }
+
+ // Return the result
+ return $alreadyTaken;
}
}