Continued:
[core.git] / inc / main / classes / filter / validator / class_UserNameValidatorFilter.php
1 <?php
2 // Own namespace
3 namespace CoreFramework\Filter\Validator\Username;
4
5 /**
6  * A concrete filter for validating the username. This filter may intercept the
7  * filter chain if no username is given or if supplied username has an invalid
8  * form. It could also intercept our filter chain if username is already taken.
9  *
10  * @author              Roland Haeder <webmaster@shipsimu.org>
11  * @version             0.0.0
12  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
13  * @license             GNU GPL 3.0 or any newer version
14  * @link                http://www.shipsimu.org
15  *
16  * This program is free software: you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation, either version 3 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program. If not, see <http://www.gnu.org/licenses/>.
28  */
29 class UserNameValidatorFilter extends BaseFilter implements Filterable {
30         /**
31          * Protected constructor
32          *
33          * @return      void
34          */
35         protected function __construct () {
36                 // Call parent constructor
37                 parent::__construct(__CLASS__);
38         }
39
40         /**
41          * Creates an instance of this filter class
42          *
43          * @return      $filterInstance                 An instance of this filter class
44          */
45         public static final function createUserNameValidatorFilter () {
46                 // Get a new instance
47                 $filterInstance = new UserNameValidatorFilter();
48
49                 // Return the instance
50                 return $filterInstance;
51         }
52
53         /**
54          * Executes the filter with given request and response objects
55          *
56          * @param       $requestInstance        An instance of a class with an Requestable interface
57          * @param       $responseInstance       An instance of a class with an Responseable interface
58          * @return      void
59          * @throws      FilterChainException    If this filter fails to operate
60          */
61         public function execute (Requestable $requestInstance, Responseable $responseInstance) {
62                 // Get username from request
63                 $userName = $requestInstance->getRequestElement('username');
64
65                 // Is the username set?
66                 if (is_null($userName)) {
67                         // Not found in form so stop the filtering process
68                         $requestInstance->requestIsValid(FALSE);
69
70                         // Add a message to the response
71                         $responseInstance->addFatalMessage('username_unset');
72
73                         // Abort here
74                         throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
75                 } elseif (empty($userName)) {
76                         // Empty field!
77                         $requestInstance->requestIsValid(FALSE);
78
79                         // Add a message to the response
80                         $responseInstance->addFatalMessage('username_empty');
81
82                         // Abort here
83                         throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
84                 } elseif ($this->ifUserNameIsTaken($userName)) {
85                         // Username is already taken
86                         $requestInstance->requestIsValid(FALSE);
87
88                         // Add a message to the response
89                         $responseInstance->addFatalMessage('username_taken');
90
91                         // Abort here
92                         throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
93                 }
94         }
95
96         /**
97          * Check whether the username as already been taken
98          *
99          * @param       $userName               Username to check for existence
100          * @return      $alreadyTaken   Whether the username has been taken
101          */
102         private function ifUserNameIsTaken ($userName) {
103                 // Default is already taken
104                 $alreadyTaken = TRUE;
105
106                 // Initialize instance
107                 $userInstance = NULL;
108
109                 // Get a registry instance
110                 $registry = Registry::getRegistry();
111
112                 // Is the user already there?
113                 if ($registry->instanceExists('user')) {
114                         // Use the instance for checking for the email
115                         $userInstance = $registry->getInstance('user');
116                         $userInstance->setUserName($userName);
117                 } else {
118                         // If this instance is created then the username *does* exist
119                         try {
120                                 // Get a new instance
121                                 $userInstance = call_user_func_array(array($this->getConfigInstance()->getConfigEntry('user_class'), 'createMemberByUsername'), array($userName));
122
123                                 // Remember this user instance in our registry for later usage
124                                 $registry->addInstance('user', $userInstance);
125                         } catch (UsernameMissingException $e) {
126                                 // User was not found
127                         }
128                 }
129
130                 // Does the username exist?
131                 if ((is_null($userInstance)) || ($userInstance->ifUsernameExists() === FALSE)) {
132                         // This username is still available
133                         $alreadyTaken = FALSE;
134                 } // END - if
135
136                 // Return the result
137                 return $alreadyTaken;
138         }
139
140 }