- md5
- 22638883e1114bad64d49a3b08a6b4e3
- sha1
- d3ac425cde274732998e258876b3bcc6d15ab67e
- sha256
- bd65c3edfd2e29439e32c2a2231669a07ffec642c2926a708ecd63d515662a2a
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
<?php
/**
* @version $Id: user.php 20196 2011-01-09 02:40:25Z ian $
* @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
// No direct access.
defined('_JEXEC') or die;
jimport('joomla.application.component.modeladmin');
/**
* User model.
*
* @package Joomla.Administrator
* @subpackage com_users
* @since 1.6
*/
class UsersModelUser extends JModelAdmin
{
/**
* Returns a reference to the a Table object, always creating it.
*
* @param type $type The table type to instantiate
* @param string $prefix A prefix for the table class name. Optional.
* @param array $config Configuration array for model. Optional.
*
* @return JTable A database object
* @since 1.6
*/
public function getTable($type = 'User', $prefix = 'JTable', $config = array())
{
$table = JTable::getInstance($type, $prefix, $config);
return $table;
}
/**
* Method to get a single record.
*
* @param integer $pk The id of the primary key.
*
* @return mixed Object on success, false on failure.
* @since 1.6
*/
public function getItem($pk = null)
{
$result = parent::getItem($pk);
// Get the dispatcher and load the users plugins.
$dispatcher = JDispatcher::getInstance();
JPluginHelper::importPlugin('user');
// Trigger the data preparation event.
$results = $dispatcher->trigger('onContentPrepareData', array('com_users.user', $result));
return $result;
}
/**
* Method to get the record form.
*
* @param array $data An optional array of data for the form to interogate.
* @param boolean $loadData True if the form is to load its own data (default case), false if not.
* @return JForm A JForm object on success, false on failure
* @since 1.6
*/
public function getForm($data = array(), $loadData = true)
{
// Initialise variables.
$app = JFactory::getApplication();
// Get the form.
$form = $this->loadForm('com_users.user', 'user', array('control' => 'jform', 'load_data' => $loadData));
if (empty($form)) {
return false;
}
return $form;
}
/**
* Method to get the data that should be injected in the form.
*
* @return mixed The data for the form.
* @since 1.6
*/
protected function loadFormData()
{
// Check the session for previously entered form data.
$data = JFactory::getApplication()->getUserState('com_users.edit.user.data', array());
if (empty($data)) {
$data = $this->getItem();
}
// TODO: Maybe this can go into the parent model somehow?
// Get the dispatcher and load the users plugins.
$dispatcher = JDispatcher::getInstance();
JPluginHelper::importPlugin('user');
// Trigger the data preparation event.
$results = $dispatcher->trigger('onContentPrepareData', array('com_users.profile', $data));
// Check for errors encountered while preparing the data.
if (count($results) && in_array(false, $results, true)) {
$this->setError($dispatcher->getError());
}
return $data;
}
/**
* Override JModelAdmin::preprocessForm to ensure the correct plugin group is loaded.
*
* @param object $form A form object.
* @param mixed $data The data expected for the form.
* @param string $group The name of the plugin group to import (defaults to "content").
*
* @throws Exception if there is an error in the form event.
* @since 1.6
*/
protected function preprocessForm(JForm $form, $data, $group = 'user')
{
parent::preprocessForm($form, $data, $group);
}
/**
* Method to save the form data.
*
* @param array $data The form data.
*
* @return boolean True on success.
* @since 1.6
*/
public function save($data)
{
// Initialise variables;
$pk = (!empty($data['id'])) ? $data['id'] : (int) $this->getState('user.id');
$user = JUser::getInstance($pk);
$my = JFactory::getUser();
if ($data['block'] && $pk == $my->id && !$my->block) {
$this->setError(JText::_('COM_USERS_USERS_ERROR_CANNOT_BLOCK_SELF'));
return false;
}
// Make sure that we are not removing ourself from Super Admin group
$iAmSuperAdmin = $my->authorise('core.admin');
if ($iAmSuperAdmin && $my->get('id') == $pk) {
// Check that at least one of our new groups is Super Admin
$stillSuperAdmin = false;
$myNewGroups = $data['groups'];
foreach ($myNewGroups as $group) {
$stillSuperAdmin = ($stillSuperAdmin) ? ($stillSuperAdmin) : JAccess::checkGroup($group, 'core.admin');
}
if (!$stillSuperAdmin) {
$this->setError(JText::_('COM_USERS_USERS_ERROR_CANNOT_DEMOTE_SELF'));
return false;
}
}
// Bind the data.
if (!$user->bind($data)) {
$this->setError($user->getError());
return false;
}
// Store the data.
if (!$user->save()) {
$this->setError($user->getError());
return false;
}
$this->setState('user.id', $user->id);
return true;
}
/**
* Method to delete rows.
*
* @param array $pks An array of item ids.
*
* @return boolean Returns true on success, false on failure.
* @since 1.6
*/
public function delete(&$pks)
{
// Initialise variables.
$user = JFactory::getUser();
$table = $this->getTable();
$pks = (array) $pks;
// Check if I am a Super Admin
$iAmSuperAdmin = $user->authorise('core.admin');
// Trigger the onUserBeforeSave event.
JPluginHelper::importPlugin('user');
$dispatcher = JDispatcher::getInstance();
if (in_array($user->id, $pks)) {
$this->setError(JText::_('COM_USERS_USERS_ERROR_CANNOT_DELETE_SELF'));
return false;
}
// Iterate the items to delete each one.
foreach ($pks as $i => $pk)
{
if ($table->load($pk)) {
// Access checks.
$allow = $user->authorise('core.delete', 'com_users');
// Don't allow non-super-admin to delete a super admin
$allow = (!$iAmSuperAdmin && JAccess::check($pk, 'core.admin')) ? false : $allow;
if ($allow) {
// Get users data for the users to delete.
$user_to_delete = JFactory::getUser($pk);
// Fire the onUserBeforeDelete event.
$dispatcher->trigger('onUserBeforeDelete', array($table->getProperties()));
if (!$table->delete($pk)) {
$this->setError($table->getError());
return false;
} else {
// Trigger the onUserAfterDelete event.
$dispatcher->trigger('onUserAfterDelete', array($user_to_delete->getProperties(), true, $this->getError()));
}
}
else {
// Prune items that you can't change.
unset($pks[$i]);
JError::raiseWarning(403, JText::_('JERROR_CORE_DELETE_NOT_PERMITTED'));
}
}
else {
$this->setError($table->getError());
return false;
}
}
return true;
}
/**
* Method to block user records.
*
* @param array $pks The ids of the items to publish.
* @param int $value The value of the published state
*
* @return boolean True on success.
* @since 1.6
*/
function block(&$pks, $value = 1)
{
// Initialise variables.
$app = JFactory::getApplication();
$dispatcher = JDispatcher::getInstance();
$user = JFactory::getUser();
// Check if I am a Super Admin
$iAmSuperAdmin = $user->authorise('core.admin');
$table = $this->getTable();
$pks = (array) $pks;
JPluginHelper::importPlugin('user');
// Access checks.
foreach ($pks as $i => $pk)
{
if ($value == 1 && $pk == $user->get('id')) {
// Cannot block yourself.
unset($pks[$i]);
JError::raiseWarning(403, JText::_('COM_USERS_USERS_ERROR_CANNOT_BLOCK_SELF'));
}
else if ($table->load($pk)) {
$old = $table->getProperties();
$allow = $user->authorise('core.edit.state', 'com_users');
// Don't allow non-super-admin to delete a super admin
$allow = (!$iAmSuperAdmin && JAccess::check($pk, 'core.admin')) ? false : $allow;
// Prepare the logout options.
$options = array(
'clientid' => array(0, 1)
);
if ($allow) {
// Skip changing of same state
if ($table->block == $value) {
unset($pks[$i]);
continue;
}
$table->block = (int) $value;
// Allow an exception to be thrown.
try
{
if (!$table->check()) {
$this->setError($table->getError());
return false;
}
// Trigger the onUserBeforeSave event.
$result = $dispatcher->trigger('onUserBeforeSave', array($old, false));
if (in_array(false, $result, true)) {
// Plugin will have to raise it's own error or throw an exception.
return false;
}
// Store the table.
if (!$table->store()) {
$this->setError($table->getError());
return false;
}
// Trigger the onAftereStoreUser event
$dispatcher->trigger('onUserAfterSave', array($table->getProperties(), false, true, null));
}
catch (Exception $e)
{
$this->setError($e->getMessage());
return false;
}
// Log the user out.
if ($value) {
$app->logout($table->id, $options);
}
}
else {
// Prune items that you can't change.
unset($pks[$i]);
JError::raiseWarning(403, JText::_('JLIB_APPLICATION_ERROR_EDITSTATE_NOT_PERMITTED'));
}
}
}
return true;
}
/**
* Method to activate user records.
*
* @param array $pks The ids of the items to activate.
*
* @return boolean True on success.
* @since 1.6
*/
function activate(&$pks)
{
// Initialise variables.
$dispatcher = JDispatcher::getInstance();
$user = JFactory::getUser();
// Check if I am a Super Admin
$iAmSuperAdmin = $user->authorise('core.admin');
$table = $this->getTable();
$pks = (array) $pks;
// Access checks.
foreach ($pks as $i => $pk)
{
if ($table->load($pk)) {
$old = $table->getProperties();
$allow = $user->authorise('core.edit.state', 'com_users');
// Don't allow non-super-admin to delete a super admin
$allow = (!$iAmSuperAdmin && JAccess::check($pk, 'core.admin')) ? false : $allow;
if (empty($table->activation)) {
// Ignore activated accounts.
unset($pks[$i]);
}
else if ($allow) {
$table->block = 0;
$table->activation = '';
// Allow an exception to be thrown.
try
{
if (!$table->check()) {
$this->setError($table->getError());
return false;
}
// Trigger the onUserBeforeSave event.
$result = $dispatcher->trigger('onUserBeforeSave', array($old, false));
if (in_array(false, $result, true)) {
// Plugin will have to raise it's own error or throw an exception.
return false;
}
// Store the table.
if (!$table->store()) {
$this->setError($table->getError());
return false;
}
// Fire the onAftereStoreUser event
$dispatcher->trigger('onUserAfterSave', array($table->getProperties(), false, true, null));
}
catch (Exception $e)
{
$this->setError($e->getMessage());
return false;
}
}
else {
// Prune items that you can't change.
unset($pks[$i]);
JError::raiseWarning(403, JText::_('JLIB_APPLICATION_ERROR_EDITSTATE_NOT_PERMITTED'));
}
}
}
return true;
}
/**
* Perform batch operations
*
* @param array $config An array of variable for the batch operation
* @param array $user_ids An array of IDs on which to operate
* @since 1.6
*/
public function batch($config, $user_ids)
{
// Ensure there are selected users to operate on.
if (empty($user_ids)) {
$this->setError(JText::_('COM_USERS_USERS_NO_ITEM_SELECTED'));
return false;
}
else if (!empty($config)) {
// Only run operations if a config array is present.
// Ensure there is a valid group.
$group_id = JArrayHelper::getValue($config, 'group_id', 0, 'int');
JArrayHelper::toInteger($user_ids);
if ($group_id < 1) {
$this->setError(JText::_('COM_USERS_ERROR_INVALID_GROUP'));
return false;
}
// Get the system ACL object and set the mode to database driven.
$acl = JFactory::getACL();
$oldAclMode = $acl->setCheckMode(1);
$groupLogic = JArrayHelper::getValue($config, 'group_logic');
switch ($groupLogic)
{
case 'set':
$doDelete = 2;
$doAssign = true;
break;
case 'del':
$doDelete = true;
$doAssign = false;
break;
case 'add':
default:
$doDelete = false;
$doAssign = true;
break;
}
// Remove the users from the group(s) if requested.
if ($doDelete) {
// Purge operation, remove the users from all groups.
if ($doDelete === 2) {
$this->_db->setQuery(
'DELETE FROM `#__user_usergroup_map`' .
' WHERE `user_id` IN ('.implode(',', $user_ids).')'
);
}
else {
// Remove the users from the group.
$this->_db->setQuery(
'DELETE FROM `#__user_usergroup_map`' .
' WHERE `user_id` IN ('.implode(',', $user_ids).')' .
' AND `group_id` = '.$group_id
);
}
// Check for database errors.
if (!$this->_db->query()) {
$this->setError($this->_db->getErrorMsg());
return false;
}
}
// Assign the users to the group if requested.
if ($doAssign) {
// Build the tuples array for the assignment query.
$tuples = array();
foreach ($user_ids as $id)
{
$tuples[] = '('.$id.','.$group_id.')';
}
$this->_db->setQuery(
'INSERT IGNORE INTO `#__user_usergroup_map` (`user_id`, `group_id`)' .
' VALUES '.implode(',', $tuples)
);
// Check for database errors.
if (!$this->_db->query()) {
$this->setError($this->_db->getErrorMsg());
return false;
}
}
// Set the ACL mode back to it's previous state.
$acl->setCheckMode($oldAclMode);
}
return true;
}
/**
* Gets the available groups.
*
* @return array
* @since 1.6
*/
public function getGroups()
{
$user = JFactory::getUser();
if ($user->authorise('core.edit', 'com_users') && $user->authorise('core.manage', 'com_users'))
{
$model = JModel::getInstance('Groups', 'UsersModel', array('ignore_request' => true));
return $model->getItems();
}
else
{
return null;
}
}
/**
* Gets the groups this object is assigned to
*
* @return array
* @since 1.6
*/
public function getAssignedGroups($userId = null)
{
// Initialise variables.
$userId = (!empty($userId)) ? $userId : (int)$this->getState('user.id');
if (empty($userId)) {
$result = array();
$config = JComponentHelper::getParams('com_users');
if ($groupId = $config->get('new_usertype')) {
$result[$groupId] = null;
}
}
else {
jimport('joomla.user.helper');
$result = JUserHelper::getUserGroups($userId);
}
return $result;
}
}
No other release has this exact content. See every version of this file.