3 * A generic class for handling users
5 * @author Roland Haeder <webmaster@shipsimu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.shipsimu.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 Member extends BaseUser implements ManageableMember, Registerable {
26 * Protected constructor
30 protected function __construct () {
31 // Call parent constructor
32 parent::__construct(__CLASS__);
36 * Creates an instance of this user class by a provided username. This
37 * factory method will check if username is already taken and if not so it
38 * will throw an exception.
40 * @param $userName Username we need a class instance for
41 * @return $userInstance An instance of this user class
42 * @throws UsernameMissingException If the username does not exist
43 * @throws UnexpectedGuestAccountException If the user status is 'guest'
45 public static final function createMemberByUsername ($userName) {
47 $userInstance = new Member();
50 $userInstance->setUserName($userName);
52 // Check if username exists
53 if ($userInstance->ifUsernameExists() === FALSE) {
54 // Throw an exception here
55 throw new UsernameMissingException(array($userInstance, $userName), self::EXCEPTION_USERNAME_NOT_FOUND);
56 } elseif ($userInstance->isGuest() === TRUE) {
57 // User should not be a guest here
58 throw new UnexpectedGuestAccountException(array($userInstance, $userName), self::EXCEPTION_USER_IS_GUEST);
61 // Return the instance
66 * Creates an instance of this user class by a provided email address. This
67 * factory method will not check if email address is there.
69 * @param $email Email address of the user
70 * @return $userInstance An instance of this user class
72 public static final function createMemberByEmail ($email) {
74 $userInstance = new Member();
77 $userInstance->setEmail($email);
79 // Return the instance
84 * Creates a user by a given request instance
86 * @param $requestInstance An instance of a Requestable class
87 * @return $userInstance An instance of this user class
88 * @todo Add more ways over creating user classes
90 public static final function createMemberByRequest (Requestable $requestInstance) {
91 // Determine if by email or username
92 if (!is_null($requestInstance->getRequestElement('username'))) {
94 $userInstance = self::createMemberByUserName($requestInstance->getRequestElement('username'));
95 } elseif (!is_null($requestInstance->getRequestElement('email'))) {
97 $userInstance = self::createMemberByEmail($requestInstance->getRequestElement('email'));
100 $userInstance = new Member();
101 $userInstance->debugBackTrace('More ways of creating user classes are needed here.');
105 // Return the prepared instance
106 return $userInstance;
110 * Updates the last activity timestamp and last performed action in the
111 * database result. You should call flushPendingUpdates() to flush these updates
112 * to the database layer.
114 * @param $requestInstance A requestable class instance
117 public function updateLastActivity (Requestable $requestInstance) {
119 $lastAction = $requestInstance->getRequestElement('action');
121 // If there is no action use the default on
122 if (is_null($lastAction)) {
123 $lastAction = $this->getConfigInstance()->getConfigEntry('login_default_action');
126 // Get a critieria instance
127 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
129 // Add search criteria
130 $searchInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
131 $searchInstance->setLimit(1);
133 // Now get another criteria
134 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
136 // And add our both entries
137 $updateInstance->addCriteria('last_activity', date('Y-m-d H:i:s', time()));
138 $updateInstance->addCriteria('last_action', $lastAction);
140 // Add the search criteria for searching for the right entry
141 $updateInstance->setSearchInstance($searchInstance);
143 // Set wrapper class name
144 $updateInstance->setWrapperConfigEntry('user_db_wrapper_class');
146 // Remember the update in database result
147 $this->getResultInstance()->add2UpdateQueue($updateInstance);