3 * A generic class for handling users
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.ship-simu.org
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 class User extends BaseFrameworkSystem implements ManageableUser, Registerable {
26 * Username of current user
28 private $userName = "";
31 * Email of current user
36 const EXCEPTION_USERNAME_NOT_FOUND = 0xd00;
37 const EXCEPTION_USER_EMAIL_NOT_FOUND = 0xd01;
40 * Protected constructor
44 protected function __construct ($class = "") {
45 // Is the class name empty? Then this is not a specialized user class
46 if (empty($class)) $class = __CLASS__;
48 // Call parent constructor
49 parent::__construct($class);
51 // Set part description
52 $this->setObjectDescription("Generic user class");
54 // Create unique ID number
55 $this->generateUniqueId();
58 $this->removeNumberFormaters();
59 $this->removeSystemArray();
63 * Creates an instance of this user class by a provided username. This
64 * factory method will check if the username is already taken and if not
65 * so it will throw an exception.
67 * @param $userName Username we need a class instance for
68 * @return $userInstance An instance of this user class
69 * @throws UsernameMissingException If the username does not exist
71 public final static function createUserByUsername ($userName) {
73 $userInstance = new User();
76 $userInstance->setUserName($userName);
78 // Check if the username exists
79 if (!$userInstance->ifUsernameExists()) {
80 // Throw an exception here
81 throw new UsernameMissingException(array($userInstance, $userName), self::EXCEPTION_USERNAME_NOT_FOUND);
84 // Return the instance
89 * Creates an instance of this user class by a provided email address. This
90 * factory method will not check if the email address is there.
92 * @param $email Email address of the user
93 * @return $userInstance An instance of this user class
95 public final static function createUserByEmail ($email) {
97 $userInstance = new User();
100 $userInstance->setEmail($email);
102 // Return the instance
103 return $userInstance;
107 * Setter for username
109 * @param $userName The username to set
112 public final function setUserName ($userName) {
113 $this->userName = $userName;
119 * @param $email The email to set
122 protected final function setEmail ($email) {
123 $this->email = $email;
127 * Getter for username
129 * @return $userName The username to get
131 public final function getUsername () {
132 return $this->userName;
138 * @return $email The email to get
140 public final function getEmail () {
145 * Determines wether the username exists or not
147 * @return $exists Wether the username exists
149 public function ifUsernameExists () {
150 // By default the username does exist
153 // Get a UserDatabaseWrapper instance
154 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper');
156 // Create a search criteria
157 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria');
159 // Add the username as a criteria and set limit to one entry
160 $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUsername());
161 $criteriaInstance->setLimit(1);
163 // Get a search result
164 $result = $wrapperInstance->doSelectByCriteria($criteriaInstance);
167 if (!$result->next()) {
177 * Determines wether the email exists or not
179 * @return $exists Wether the email exists
181 public function ifEmailAddressExists () {
182 // By default the username does exist
185 // Get a UserDatabaseWrapper instance
186 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper');
188 // Create a search criteria
189 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria');
191 // Add the username as a criteria and set limit to one entry
192 $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_EMAIL, $this->getEmail());
193 $criteriaInstance->setLimit(1);
195 // Get a search result
196 $result = $wrapperInstance->doSelectByCriteria($criteriaInstance);
199 if (!$result->next()) {