3 namespace CoreFramework\User\Login;
5 // Import framework stuff
6 use CoreFramework\Factory\ObjectFactory;
7 use CoreFramework\Registry\Registerable;
10 * A generic class for handling users
12 * @author Roland Haeder <webmaster@shipsimu.org>
14 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
15 * @license GNU GPL 3.0 or any newer version
16 * @link http://www.shipsimu.org
18 * This program is free software: you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation, either version 3 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 class Member extends BaseUser implements ManageableMember, Registerable {
33 * Protected constructor
37 protected function __construct () {
38 // Call parent constructor
39 parent::__construct(__CLASS__);
43 * Creates an instance of this user class by a provided username. This
44 * factory method will check if username is already taken and if not so it
45 * will throw an exception.
47 * @param $userName Username we need a class instance for
48 * @return $userInstance An instance of this user class
49 * @throws UsernameMissingException If the username does not exist
50 * @throws UnexpectedGuestAccountException If the user status is 'guest'
52 public static final function createMemberByUsername ($userName) {
54 $userInstance = new Member();
57 $userInstance->setUserName($userName);
59 // Check if username exists
60 if ($userInstance->ifUsernameExists() === FALSE) {
61 // Throw an exception here
62 throw new UsernameMissingException(array($userInstance, $userName), self::EXCEPTION_USERNAME_NOT_FOUND);
63 } elseif ($userInstance->isGuest() === TRUE) {
64 // User should not be a guest here
65 throw new UnexpectedGuestAccountException(array($userInstance, $userName), self::EXCEPTION_USER_IS_GUEST);
68 // Return the instance
73 * Creates an instance of this user class by a provided email address. This
74 * factory method will not check if email address is there.
76 * @param $email Email address of the user
77 * @return $userInstance An instance of this user class
79 public static final function createMemberByEmail ($email) {
81 $userInstance = new Member();
84 $userInstance->setEmail($email);
86 // Return the instance
91 * Creates a user by a given request instance
93 * @param $requestInstance An instance of a Requestable class
94 * @return $userInstance An instance of this user class
95 * @todo Add more ways over creating user classes
97 public static final function createMemberByRequest (Requestable $requestInstance) {
98 // Determine if by email or username
99 if (!is_null($requestInstance->getRequestElement('username'))) {
101 $userInstance = self::createMemberByUserName($requestInstance->getRequestElement('username'));
102 } elseif (!is_null($requestInstance->getRequestElement('email'))) {
104 $userInstance = self::createMemberByEmail($requestInstance->getRequestElement('email'));
107 $userInstance = new Member();
108 $userInstance->debugBackTrace('More ways of creating user classes are needed here.');
112 // Return the prepared instance
113 return $userInstance;
117 * Updates the last activity timestamp and last performed action in the
118 * database result. You should call flushPendingUpdates() to flush these updates
119 * to the database layer.
121 * @param $requestInstance A requestable class instance
124 public function updateLastActivity (Requestable $requestInstance) {
126 $lastAction = $requestInstance->getRequestElement('action');
128 // If there is no action use the default on
129 if (is_null($lastAction)) {
130 $lastAction = $this->getConfigInstance()->getConfigEntry('login_default_action');
133 // Get a critieria instance
134 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
136 // Add search criteria
137 $searchInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
138 $searchInstance->setLimit(1);
140 // Now get another criteria
141 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
143 // And add our both entries
144 $updateInstance->addCriteria('last_activity', date('Y-m-d H:i:s', time()));
145 $updateInstance->addCriteria('last_action', $lastAction);
147 // Add the search criteria for searching for the right entry
148 $updateInstance->setSearchInstance($searchInstance);
150 // Set wrapper class name
151 $updateInstance->setWrapperConfigEntry('user_db_wrapper_class');
153 // Remember the update in database result
154 $this->getResultInstance()->add2UpdateQueue($updateInstance);