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;
39 * Protected constructor
43 protected function __construct ($class = "") {
44 // Is the class name empty? Then this is not a specialized user class
45 if (empty($class)) $class = __CLASS__;
47 // Call parent constructor
48 parent::__construct($class);
50 // Set part description
51 $this->setObjectDescription("Generic user class");
53 // Create unique ID number
54 $this->createUniqueID();
57 $this->removeNumberFormaters();
58 $this->removeSystemArray();
62 * Creates an instance of this user class by a provided username. This
63 * factory method will check if the username is already taken and if not
64 * so it will throw an exception.
66 * @param $userName Username we need a class instance for
67 * @return $userInstance An instance of this user class
68 * @throws UsernameMissingException If the username does not exist
70 public final static function createUserByUsername ($userName) {
72 $userInstance = new User();
75 $userInstance->setUsername($userName);
77 // Check if the username exists
78 if (!$userInstance->ifUsernameExists()) {
79 // Throw an exception here
80 throw new UsernameMissingException(array($userInstance, $userName), self::EXCEPTION_USERNAME_NOT_FOUND);
83 // Return the instance
88 * Creates an instance of this user class by a provided email address. This
89 * factory method will not check if the email address is there.
91 * @param $email Email address of the user
92 * @return $userInstance An instance of this user class
94 public final static function createUserByEmail ($email) {
96 $userInstance = new User();
99 $userInstance->setEmail($email);
101 // Return the instance
102 return $userInstance;
106 * Setter for username
108 * @param $userName The username to set
111 protected final function setUsername ($userName) {
112 $this->UserName = $userName;
118 * @param $email The email to set
121 protected final function setEmail ($email) {
122 $this->userName = $email;
126 * Getter for username
128 * @return $userName The username to get
130 public final function getUsername () {
131 return $this->userName;
137 * @return $email The email to get
139 public final function getEmail () {
144 * Determines wether the username exists or not
146 * @return $exists Wether the username exists
148 public function ifUsernameExists () {
149 // By default the username does exist
152 // Get a UserDatabaseWrapper instance
153 $wrapperInstance = UserDatabaseWrapper::createUserDatabaseWrapper();
155 // Create a search criteria
156 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria');
158 // Add the username as a criteria and set limit to one entry
159 $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUsername());
160 $criteriaInstance->setLimit(1);
162 // Get a search result
163 $result = $wrapperInstance->doSelectByCriteria($criteriaInstance);
166 if (!$result->next()) {