3 namespace CoreFramework\User\Login;
5 // Import framework stuff
6 use CoreFramework\Registry\Registerable;
9 * A generic class for handling users
11 * @author Roland Haeder <webmaster@shipsimu.org>
13 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
14 * @license GNU GPL 3.0 or any newer version
15 * @link http://www.shipsimu.org
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 class Member extends BaseUser implements ManageableMember, Registerable {
32 * Protected constructor
36 protected function __construct () {
37 // Call parent constructor
38 parent::__construct(__CLASS__);
42 * Creates an instance of this user class by a provided username. This
43 * factory method will check if username is already taken and if not so it
44 * will throw an exception.
46 * @param $userName Username we need a class instance for
47 * @return $userInstance An instance of this user class
48 * @throws UsernameMissingException If the username does not exist
49 * @throws UnexpectedGuestAccountException If the user status is 'guest'
51 public static final function createMemberByUsername ($userName) {
53 $userInstance = new Member();
56 $userInstance->setUserName($userName);
58 // Check if username exists
59 if ($userInstance->ifUsernameExists() === FALSE) {
60 // Throw an exception here
61 throw new UsernameMissingException(array($userInstance, $userName), self::EXCEPTION_USERNAME_NOT_FOUND);
62 } elseif ($userInstance->isGuest() === TRUE) {
63 // User should not be a guest here
64 throw new UnexpectedGuestAccountException(array($userInstance, $userName), self::EXCEPTION_USER_IS_GUEST);
67 // Return the instance
72 * Creates an instance of this user class by a provided email address. This
73 * factory method will not check if email address is there.
75 * @param $email Email address of the user
76 * @return $userInstance An instance of this user class
78 public static final function createMemberByEmail ($email) {
80 $userInstance = new Member();
83 $userInstance->setEmail($email);
85 // Return the instance
90 * Creates a user by a given request instance
92 * @param $requestInstance An instance of a Requestable class
93 * @return $userInstance An instance of this user class
94 * @todo Add more ways over creating user classes
96 public static final function createMemberByRequest (Requestable $requestInstance) {
97 // Determine if by email or username
98 if (!is_null($requestInstance->getRequestElement('username'))) {
100 $userInstance = self::createMemberByUserName($requestInstance->getRequestElement('username'));
101 } elseif (!is_null($requestInstance->getRequestElement('email'))) {
103 $userInstance = self::createMemberByEmail($requestInstance->getRequestElement('email'));
106 $userInstance = new Member();
107 $userInstance->debugBackTrace('More ways of creating user classes are needed here.');
111 // Return the prepared instance
112 return $userInstance;
116 * Updates the last activity timestamp and last performed action in the
117 * database result. You should call flushPendingUpdates() to flush these updates
118 * to the database layer.
120 * @param $requestInstance A requestable class instance
123 public function updateLastActivity (Requestable $requestInstance) {
125 $lastAction = $requestInstance->getRequestElement('action');
127 // If there is no action use the default on
128 if (is_null($lastAction)) {
129 $lastAction = $this->getConfigInstance()->getConfigEntry('login_default_action');
132 // Get a critieria instance
133 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
135 // Add search criteria
136 $searchInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
137 $searchInstance->setLimit(1);
139 // Now get another criteria
140 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
142 // And add our both entries
143 $updateInstance->addCriteria('last_activity', date('Y-m-d H:i:s', time()));
144 $updateInstance->addCriteria('last_action', $lastAction);
146 // Add the search criteria for searching for the right entry
147 $updateInstance->setSearchInstance($searchInstance);
149 // Set wrapper class name
150 $updateInstance->setWrapperConfigEntry('user_db_wrapper_class');
152 // Remember the update in database result
153 $this->getResultInstance()->add2UpdateQueue($updateInstance);