Source for file user.php
Documentation is available at user.php
* @package Joomla.Platform
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
* @package Joomla.Platform
* Associative array of group ids => group ids for the user
* @param JDatabaseDriver $db Database driver object.
* Method to load a user, user groups, and any other necessary data
* from the database so that it can be bound to the user object.
* @param integer $userId An optional user id.
* @param boolean $reset False if row not found or on error
* (internal error state set in that case).
* @return boolean True on success, false on failure.
public function load($userId =
null, $reset =
true)
// Check for a valid id to load.
$query =
$this->_db->getQuery(true)
->from($this->_db->quoteName('#__users'))
->where($this->_db->quoteName('id') .
' = ' . (int)
$userId);
$this->_db->setQuery($query);
$data = (array)
$this->_db->loadAssoc();
// Convert e-mail from punycode
// Bind the data to the table.
$return =
$this->bind($data);
->select($this->_db->quoteName('g.id'))
->select($this->_db->quoteName('g.title'))
->from($this->_db->quoteName('#__usergroups') .
' AS g')
->join('INNER', $this->_db->quoteName('#__user_usergroup_map') .
' AS m ON m.group_id = g.id')
->where($this->_db->quoteName('m.user_id') .
' = ' . (int)
$userId);
$this->_db->setQuery($query);
// Add the groups to the user data.
$this->groups =
$this->_db->loadAssocList('id', 'id');
* Method to bind the user, user groups, and any other necessary data.
* @param array $array The data to bind.
* @param mixed $ignore An array or space separated list of fields to ignore.
* @return boolean True on success, false on failure.
public function bind($array, $ignore =
'')
$registry->loadArray($array['params']);
$array['params'] = (string)
$registry;
// Attempt to bind the data.
$return =
parent::bind($array, $ignore);
// Load the real group data based on the bound ids.
if ($return &&
!empty($this->groups))
// Get the titles for the user groups.
$query =
$this->_db->getQuery(true)
->select($this->_db->quoteName('id'))
->select($this->_db->quoteName('title'))
->from($this->_db->quoteName('#__usergroups'))
->where($this->_db->quoteName('id') .
' = ' .
implode(' OR ' .
$this->_db->quoteName('id') .
' = ', $this->groups));
$this->_db->setQuery($query);
// Set the titles for the user groups.
$this->groups =
$this->_db->loadAssocList('id', 'id');
* Validation and filtering
* @return boolean True if satisfactory
// Set user id to null istead of 0, if needed
// Validate user information
if (trim($this->name) ==
'')
$this->setError(JText::_('JLIB_DATABASE_ERROR_PLEASE_ENTER_YOUR_NAME'));
if (trim($this->username) ==
'')
$this->setError(JText::_('JLIB_DATABASE_ERROR_PLEASE_ENTER_A_USER_NAME'));
// Convert e-mail to punycode for storage
// Set the registration timestamp
if (empty($this->registerDate) ||
$this->registerDate ==
$this->_db->getNullDate())
// Set the lastvisitDate timestamp
if (empty($this->lastvisitDate))
$this->lastvisitDate =
$this->_db->getNullDate();
// Check for existing username
$query =
$this->_db->getQuery(true)
->select($this->_db->quoteName('id'))
->from($this->_db->quoteName('#__users'))
->where($this->_db->quoteName('username') .
' = ' .
$this->_db->quote($this->username))
->where($this->_db->quoteName('id') .
' != ' . (int)
$this->id);
$this->_db->setQuery($query);
$xid = (int)
$this->_db->loadResult();
if ($xid &&
$xid != (int)
$this->id)
// Check for existing email
->select($this->_db->quoteName('id'))
->from($this->_db->quoteName('#__users'))
->where($this->_db->quoteName('email') .
' = ' .
$this->_db->quote($this->email))
->where($this->_db->quoteName('id') .
' != ' . (int)
$this->id);
$this->_db->setQuery($query);
$xid = (int)
$this->_db->loadResult();
if ($xid &&
$xid != (int)
$this->id)
// Check for root_user != username
$rootUser =
$config->get('root_user');
->select($this->_db->quoteName('id'))
->from($this->_db->quoteName('#__users'))
->where($this->_db->quoteName('username') .
' = ' .
$this->_db->quote($rootUser));
$this->_db->setQuery($query);
$xid = (int)
$this->_db->loadResult();
if ($rootUser ==
$this->username &&
(!$xid ||
$xid &&
$xid != (int)
$this->id)
||
$xid &&
$xid == (int)
$this->id &&
$rootUser !=
$this->username)
$this->setError(JText::_('JLIB_DATABASE_ERROR_USERNAME_CANNOT_CHANGE'));
* Method to store a row in the database from the JTable instance properties.
* If a primary key value is set the row with that primary key value will be
* updated with the instance property values. If no primary key value is set
* a new row will be inserted into the database with the properties from the
* @param boolean $updateNulls True to update fields even if they are null.
* @return boolean True on success.
* @link http://docs.joomla.org/JTable/store
public function store($updateNulls =
false)
// Get the table key and key value.
// TODO: This is a dumb way to handle the groups.
// Store groups locally so as to not update directly.
// Insert or update the object based on presence of a key value.
// Already have a table key, update the row.
$this->_db->updateObject($this->_tbl, $this, $this->_tbl_key, $updateNulls);
// Don't have a table key, insert the row.
// Reset groups to the local object.
// Store the group data if the user data was saved.
// Delete the old user group maps.
$query =
$this->_db->getQuery(true)
->delete($this->_db->quoteName('#__user_usergroup_map'))
->where($this->_db->quoteName('user_id') .
' = ' . (int)
$this->id);
$this->_db->setQuery($query);
// Set the new user group maps.
->insert($this->_db->quoteName('#__user_usergroup_map'))
->columns(array($this->_db->quoteName('user_id'), $this->_db->quoteName('group_id')));
// Have to break this up into individual queries for cross-database support.
foreach ($this->groups as $group)
->values($this->id .
', ' .
$group);
$this->_db->setQuery($query);
// If a user is blocked, delete the cookie login rows
if ($this->block == (int)
1)
->delete($this->_db->quoteName('#__user_keys'))
->where($this->_db->quoteName('user_id') .
' = ' .
$this->_db->quote($this->username));
$this->_db->setQuery($query);
* Method to delete a user, user groups, and any other necessary data from the database.
* @param integer $userId An optional user id.
* @return boolean True on success, false on failure.
public function delete($userId =
null)
// Set the primary key to delete.
$this->$k = (int)
$userId;
$query =
$this->_db->getQuery(true)
->delete($this->_db->quoteName($this->_tbl))
->where($this->_db->quoteName($this->_tbl_key) .
' = ' . (int)
$this->$k);
$this->_db->setQuery($query);
// Delete the user group maps.
->delete($this->_db->quoteName('#__user_usergroup_map'))
->where($this->_db->quoteName('user_id') .
' = ' . (int)
$this->$k);
$this->_db->setQuery($query);
->delete($this->_db->quoteName('#__messages_cfg'))
->where($this->_db->quoteName('user_id') .
' = ' . (int)
$this->$k);
$this->_db->setQuery($query);
->delete($this->_db->quoteName('#__messages'))
->where($this->_db->quoteName('user_id_to') .
' = ' . (int)
$this->$k);
$this->_db->setQuery($query);
->delete($this->_db->quoteName('#__user_keys'))
->where($this->_db->quoteName('user_id') .
' = ' .
$this->_db->quote($this->username));
$this->_db->setQuery($query);
* Updates last visit time of user
* @param integer $timeStamp The timestamp, defaults to 'now'.
* @param integer $userId The user id (optional).
* @return boolean False if an error occurs
public function setLastVisit($timeStamp =
null, $userId =
null)
jexit('No userid in setLastVisit');
// If no timestamp value is passed to function, than current time is used.
// Update the database row for the user.
$query =
$db->getQuery(true)
->update($db->quoteName($this->_tbl))
->set($db->quoteName('lastvisitDate') .
'=' .
$db->quote($date->toSql()))
->where($db->quoteName('id') .
'=' . (int)
$userId);
Documentation generated on Tue, 19 Nov 2013 15:16:24 +0100 by phpDocumentor 1.4.3