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