| [ Index ] |
PHP Cross Reference of Joomla 2.5.4 DE |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * @package Joomla.Administrator 4 * @subpackage com_users 5 * 6 * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved. 7 * @license GNU General Public License version 2 or later; see LICENSE.txt 8 */ 9 10 // No direct access. 11 defined('_JEXEC') or die; 12 13 jimport('joomla.application.component.modeladmin'); 14 15 /** 16 * User model. 17 * 18 * @package Joomla.Administrator 19 * @subpackage com_users 20 * @since 1.6 21 */ 22 class UsersModelUser extends JModelAdmin 23 { 24 /** 25 * Returns a reference to the a Table object, always creating it. 26 * 27 * @param string $type The table type to instantiate 28 * @param string $prefix A prefix for the table class name. Optional. 29 * @param array $config Configuration array for model. Optional. 30 * 31 * @return JTable A database object 32 * 33 * @since 1.6 34 */ 35 public function getTable($type = 'User', $prefix = 'JTable', $config = array()) 36 { 37 $table = JTable::getInstance($type, $prefix, $config); 38 39 return $table; 40 } 41 42 /** 43 * Method to get a single record. 44 * 45 * @param integer $pk The id of the primary key. 46 * 47 * @return mixed Object on success, false on failure. 48 * 49 * @since 1.6 50 */ 51 public function getItem($pk = null) 52 { 53 $result = parent::getItem($pk); 54 55 // Get the dispatcher and load the users plugins. 56 $dispatcher = JDispatcher::getInstance(); 57 JPluginHelper::importPlugin('user'); 58 59 // Trigger the data preparation event. 60 $results = $dispatcher->trigger('onContentPrepareData', array('com_users.user', $result)); 61 62 return $result; 63 } 64 65 /** 66 * Method to get the record form. 67 * 68 * @param array $data An optional array of data for the form to interogate. 69 * @param boolean $loadData True if the form is to load its own data (default case), false if not. 70 * 71 * @return mixed A JForm object on success, false on failure 72 * 73 * @since 1.6 74 */ 75 public function getForm($data = array(), $loadData = true) 76 { 77 // Initialise variables. 78 $app = JFactory::getApplication(); 79 80 // Get the form. 81 $form = $this->loadForm('com_users.user', 'user', array('control' => 'jform', 'load_data' => $loadData)); 82 if (empty($form)) 83 { 84 return false; 85 } 86 87 return $form; 88 } 89 90 /** 91 * Method to get the data that should be injected in the form. 92 * 93 * @return mixed The data for the form. 94 * 95 * @since 1.6 96 */ 97 protected function loadFormData() 98 { 99 // Check the session for previously entered form data. 100 $data = JFactory::getApplication()->getUserState('com_users.edit.user.data', array()); 101 102 if (empty($data)) 103 { 104 $data = $this->getItem(); 105 } 106 107 // TODO: Maybe this can go into the parent model somehow? 108 // Get the dispatcher and load the users plugins. 109 $dispatcher = JDispatcher::getInstance(); 110 JPluginHelper::importPlugin('user'); 111 112 // Trigger the data preparation event. 113 $results = $dispatcher->trigger('onContentPrepareData', array('com_users.profile', $data)); 114 115 // Check for errors encountered while preparing the data. 116 if (count($results) && in_array(false, $results, true)) 117 { 118 $this->setError($dispatcher->getError()); 119 } 120 121 return $data; 122 } 123 124 /** 125 * Override JModelAdmin::preprocessForm to ensure the correct plugin group is loaded. 126 * 127 * @param JForm $form A JForm object. 128 * @param mixed $data The data expected for the form. 129 * @param string $group The name of the plugin group to import (defaults to "content"). 130 * 131 * @return void 132 * 133 * @since 1.6 134 * @throws Exception if there is an error in the form event. 135 */ 136 protected function preprocessForm(JForm $form, $data, $group = 'user') 137 { 138 parent::preprocessForm($form, $data, $group); 139 } 140 141 /** 142 * Method to save the form data. 143 * 144 * @param array $data The form data. 145 * 146 * @return boolean True on success. 147 * 148 * @since 1.6 149 */ 150 public function save($data) 151 { 152 // Initialise variables; 153 $pk = (!empty($data['id'])) ? $data['id'] : (int) $this->getState('user.id'); 154 $user = JUser::getInstance($pk); 155 156 $my = JFactory::getUser(); 157 158 if ($data['block'] && $pk == $my->id && !$my->block) 159 { 160 $this->setError(JText::_('COM_USERS_USERS_ERROR_CANNOT_BLOCK_SELF')); 161 return false; 162 } 163 164 // Make sure that we are not removing ourself from Super Admin group 165 $iAmSuperAdmin = $my->authorise('core.admin'); 166 if ($iAmSuperAdmin && $my->get('id') == $pk) 167 { 168 // Check that at least one of our new groups is Super Admin 169 $stillSuperAdmin = false; 170 $myNewGroups = $data['groups']; 171 foreach ($myNewGroups as $group) 172 { 173 $stillSuperAdmin = ($stillSuperAdmin) ? ($stillSuperAdmin) : JAccess::checkGroup($group, 'core.admin'); 174 } 175 if (!$stillSuperAdmin) 176 { 177 $this->setError(JText::_('COM_USERS_USERS_ERROR_CANNOT_DEMOTE_SELF')); 178 return false; 179 } 180 } 181 182 // Bind the data. 183 if (!$user->bind($data)) 184 { 185 $this->setError($user->getError()); 186 return false; 187 } 188 189 // Store the data. 190 if (!$user->save()) 191 { 192 $this->setError($user->getError()); 193 return false; 194 } 195 196 $this->setState('user.id', $user->id); 197 198 return true; 199 } 200 201 /** 202 * Method to delete rows. 203 * 204 * @param array &$pks An array of item ids. 205 * 206 * @return boolean Returns true on success, false on failure. 207 * 208 * @since 1.6 209 */ 210 public function delete(&$pks) 211 { 212 // Initialise variables. 213 $user = JFactory::getUser(); 214 $table = $this->getTable(); 215 $pks = (array) $pks; 216 217 // Check if I am a Super Admin 218 $iAmSuperAdmin = $user->authorise('core.admin'); 219 220 // Trigger the onUserBeforeSave event. 221 JPluginHelper::importPlugin('user'); 222 $dispatcher = JDispatcher::getInstance(); 223 224 if (in_array($user->id, $pks)) 225 { 226 $this->setError(JText::_('COM_USERS_USERS_ERROR_CANNOT_DELETE_SELF')); 227 return false; 228 } 229 230 // Iterate the items to delete each one. 231 foreach ($pks as $i => $pk) 232 { 233 if ($table->load($pk)) 234 { 235 // Access checks. 236 $allow = $user->authorise('core.delete', 'com_users'); 237 // Don't allow non-super-admin to delete a super admin 238 $allow = (!$iAmSuperAdmin && JAccess::check($pk, 'core.admin')) ? false : $allow; 239 240 if ($allow) 241 { 242 // Get users data for the users to delete. 243 $user_to_delete = JFactory::getUser($pk); 244 245 // Fire the onUserBeforeDelete event. 246 $dispatcher->trigger('onUserBeforeDelete', array($table->getProperties())); 247 248 if (!$table->delete($pk)) 249 { 250 $this->setError($table->getError()); 251 return false; 252 } 253 else 254 { 255 // Trigger the onUserAfterDelete event. 256 $dispatcher->trigger('onUserAfterDelete', array($user_to_delete->getProperties(), true, $this->getError())); 257 } 258 } 259 else 260 { 261 // Prune items that you can't change. 262 unset($pks[$i]); 263 JError::raiseWarning(403, JText::_('JERROR_CORE_DELETE_NOT_PERMITTED')); 264 } 265 } 266 else 267 { 268 $this->setError($table->getError()); 269 return false; 270 } 271 } 272 273 return true; 274 } 275 276 /** 277 * Method to block user records. 278 * 279 * @param array &$pks The ids of the items to publish. 280 * @param integer $value The value of the published state 281 * 282 * @return boolean True on success. 283 * 284 * @since 1.6 285 */ 286 function block(&$pks, $value = 1) 287 { 288 // Initialise variables. 289 $app = JFactory::getApplication(); 290 $dispatcher = JDispatcher::getInstance(); 291 $user = JFactory::getUser(); 292 // Check if I am a Super Admin 293 $iAmSuperAdmin = $user->authorise('core.admin'); 294 $table = $this->getTable(); 295 $pks = (array) $pks; 296 297 JPluginHelper::importPlugin('user'); 298 299 // Access checks. 300 foreach ($pks as $i => $pk) 301 { 302 if ($value == 1 && $pk == $user->get('id')) 303 { 304 // Cannot block yourself. 305 unset($pks[$i]); 306 JError::raiseWarning(403, JText::_('COM_USERS_USERS_ERROR_CANNOT_BLOCK_SELF')); 307 308 } 309 elseif ($table->load($pk)) 310 { 311 $old = $table->getProperties(); 312 $allow = $user->authorise('core.edit.state', 'com_users'); 313 // Don't allow non-super-admin to delete a super admin 314 $allow = (!$iAmSuperAdmin && JAccess::check($pk, 'core.admin')) ? false : $allow; 315 316 // Prepare the logout options. 317 $options = array( 318 'clientid' => array(0, 1) 319 ); 320 321 if ($allow) 322 { 323 // Skip changing of same state 324 if ($table->block == $value) 325 { 326 unset($pks[$i]); 327 continue; 328 } 329 330 $table->block = (int) $value; 331 332 // Allow an exception to be thrown. 333 try 334 { 335 if (!$table->check()) 336 { 337 $this->setError($table->getError()); 338 return false; 339 } 340 341 // Trigger the onUserBeforeSave event. 342 $result = $dispatcher->trigger('onUserBeforeSave', array($old, false, $table->getProperties())); 343 if (in_array(false, $result, true)) 344 { 345 // Plugin will have to raise it's own error or throw an exception. 346 return false; 347 } 348 349 // Store the table. 350 if (!$table->store()) 351 { 352 $this->setError($table->getError()); 353 return false; 354 } 355 356 // Trigger the onAftereStoreUser event 357 $dispatcher->trigger('onUserAfterSave', array($table->getProperties(), false, true, null)); 358 } 359 catch (Exception $e) 360 { 361 $this->setError($e->getMessage()); 362 363 return false; 364 } 365 366 // Log the user out. 367 if ($value) 368 { 369 $app->logout($table->id, $options); 370 } 371 } 372 else 373 { 374 // Prune items that you can't change. 375 unset($pks[$i]); 376 JError::raiseWarning(403, JText::_('JLIB_APPLICATION_ERROR_EDITSTATE_NOT_PERMITTED')); 377 } 378 } 379 } 380 381 return true; 382 } 383 384 /** 385 * Method to activate user records. 386 * 387 * @param array &$pks The ids of the items to activate. 388 * 389 * @return boolean True on success. 390 * 391 * @since 1.6 392 */ 393 function activate(&$pks) 394 { 395 // Initialise variables. 396 $dispatcher = JDispatcher::getInstance(); 397 $user = JFactory::getUser(); 398 // Check if I am a Super Admin 399 $iAmSuperAdmin = $user->authorise('core.admin'); 400 $table = $this->getTable(); 401 $pks = (array) $pks; 402 403 JPluginHelper::importPlugin('user'); 404 405 // Access checks. 406 foreach ($pks as $i => $pk) 407 { 408 if ($table->load($pk)) 409 { 410 $old = $table->getProperties(); 411 $allow = $user->authorise('core.edit.state', 'com_users'); 412 // Don't allow non-super-admin to delete a super admin 413 $allow = (!$iAmSuperAdmin && JAccess::check($pk, 'core.admin')) ? false : $allow; 414 415 if (empty($table->activation)) 416 { 417 // Ignore activated accounts. 418 unset($pks[$i]); 419 } 420 elseif ($allow) 421 { 422 $table->block = 0; 423 $table->activation = ''; 424 425 // Allow an exception to be thrown. 426 try 427 { 428 if (!$table->check()) 429 { 430 $this->setError($table->getError()); 431 return false; 432 } 433 434 // Trigger the onUserBeforeSave event. 435 $result = $dispatcher->trigger('onUserBeforeSave', array($old, false, $table->getProperties())); 436 if (in_array(false, $result, true)) 437 { 438 // Plugin will have to raise it's own error or throw an exception. 439 return false; 440 } 441 442 // Store the table. 443 if (!$table->store()) 444 { 445 $this->setError($table->getError()); 446 return false; 447 } 448 449 // Fire the onAftereStoreUser event 450 $dispatcher->trigger('onUserAfterSave', array($table->getProperties(), false, true, null)); 451 } 452 catch (Exception $e) 453 { 454 $this->setError($e->getMessage()); 455 456 return false; 457 } 458 } 459 else 460 { 461 // Prune items that you can't change. 462 unset($pks[$i]); 463 JError::raiseWarning(403, JText::_('JLIB_APPLICATION_ERROR_EDITSTATE_NOT_PERMITTED')); 464 } 465 } 466 } 467 468 return true; 469 } 470 471 /** 472 * Method to perform batch operations on an item or a set of items. 473 * 474 * @param array $commands An array of commands to perform. 475 * @param array $pks An array of item ids. 476 * @param array $contexts An array of item contexts. 477 * 478 * @return boolean Returns true on success, false on failure. 479 * 480 * @since 2.5 481 */ 482 public function batch($commands, $pks, $contexts) 483 { 484 // Sanitize user ids. 485 $pks = array_unique($pks); 486 JArrayHelper::toInteger($pks); 487 488 // Remove any values of zero. 489 if (array_search(0, $pks, true)) 490 { 491 unset($pks[array_search(0, $pks, true)]); 492 } 493 494 if (empty($pks)) 495 { 496 $this->setError(JText::_('COM_USERS_USERS_NO_ITEM_SELECTED')); 497 return false; 498 } 499 500 $done = false; 501 502 if (!empty($commands['group_id'])) 503 { 504 $cmd = JArrayHelper::getValue($commands, 'group_action', 'add'); 505 506 if (!$this->batchUser((int) $commands['group_id'], $pks, $cmd)) 507 { 508 return false; 509 } 510 $done = true; 511 } 512 513 if (!$done) 514 { 515 $this->setError(JText::_('JLIB_APPLICATION_ERROR_INSUFFICIENT_BATCH_INFORMATION')); 516 return false; 517 } 518 519 // Clear the cache 520 $this->cleanCache(); 521 522 return true; 523 } 524 525 /** 526 * Perform batch operations 527 * 528 * @param integer $group_id The group ID which assignments are being edited 529 * @param array $user_ids An array of user IDs on which to operate 530 * @param string $action The action to perform 531 * 532 * @return boolean True on success, false on failure 533 * 534 * @since 1.6 535 */ 536 public function batchUser($group_id, $user_ids, $action) 537 { 538 // Get the DB object 539 $db = $this->getDbo(); 540 541 JArrayHelper::toInteger($user_ids); 542 543 if ($group_id < 1) 544 { 545 $this->setError(JText::_('COM_USERS_ERROR_INVALID_GROUP')); 546 return false; 547 } 548 549 switch ($action) 550 { 551 // Sets users to a selected group 552 case 'set': 553 $doDelete = 'all'; 554 $doAssign = true; 555 break; 556 557 // Remove users from a selected group 558 case 'del': 559 $doDelete = 'group'; 560 break; 561 562 // Add users to a selected group 563 case 'add': 564 default: 565 $doAssign = true; 566 break; 567 } 568 569 // Remove the users from the group if requested. 570 if (isset($doDelete)) 571 { 572 $query = $db->getQuery(true); 573 574 // Remove users from the group 575 $query->delete($db->quoteName('#__user_usergroup_map')); 576 $query->where($db->quoteName('user_id') . ' IN (' . implode(',', $user_ids) . ')'); 577 578 // Only remove users from selected group 579 if ($doDelete == 'group') 580 { 581 $query->where($db->quoteName('group_id') . ' = ' . (int) $group_id); 582 } 583 584 $db->setQuery($query); 585 586 // Check for database errors. 587 if (!$db->query()) 588 { 589 $this->setError($db->getErrorMsg()); 590 return false; 591 } 592 } 593 594 // Assign the users to the group if requested. 595 if (isset($doAssign)) 596 { 597 $query = $db->getQuery(true); 598 599 // First, we need to check if the user is already assigned to a group 600 $query->select($db->quoteName('user_id')); 601 $query->from($db->quoteName('#__user_usergroup_map')); 602 $query->where($db->quoteName('group_id') . ' = ' . (int) $group_id); 603 $db->setQuery($query); 604 $users = $db->loadColumn(); 605 606 // Build the values clause for the assignment query. 607 $query->clear(); 608 $groups = false; 609 foreach ($user_ids as $id) 610 { 611 if (!in_array($id, $users)) 612 { 613 $query->values($id . ',' . $group_id); 614 $groups = true; 615 } 616 } 617 618 // If we have no users to process, throw an error to notify the user 619 if (!$groups) 620 { 621 $this->setError(JText::_('COM_USERS_ERROR_NO_ADDITIONS')); 622 return false; 623 } 624 625 $query->insert($db->quoteName('#__user_usergroup_map')); 626 $query->columns(array($db->quoteName('user_id'), $db->quoteName('group_id'))); 627 $db->setQuery($query); 628 629 // Check for database errors. 630 if (!$db->query()) 631 { 632 $this->setError($db->getErrorMsg()); 633 return false; 634 } 635 } 636 637 return true; 638 } 639 640 /** 641 * Gets the available groups. 642 * 643 * @return array An array of groups 644 * 645 * @since 1.6 646 */ 647 public function getGroups() 648 { 649 $user = JFactory::getUser(); 650 if ($user->authorise('core.edit', 'com_users') && $user->authorise('core.manage', 'com_users')) 651 { 652 $model = JModel::getInstance('Groups', 'UsersModel', array('ignore_request' => true)); 653 return $model->getItems(); 654 } 655 else 656 { 657 return null; 658 } 659 } 660 661 /** 662 * Gets the groups this object is assigned to 663 * 664 * @param integer $userId The user ID to retrieve the groups for 665 * 666 * @return array An array of assigned groups 667 * 668 * @since 1.6 669 */ 670 public function getAssignedGroups($userId = null) 671 { 672 // Initialise variables. 673 $userId = (!empty($userId)) ? $userId : (int)$this->getState('user.id'); 674 675 if (empty($userId)) 676 { 677 $result = array(); 678 $config = JComponentHelper::getParams('com_users'); 679 if ($groupId = $config->get('new_usertype')) 680 { 681 $result[] = $groupId; 682 } 683 } 684 else 685 { 686 jimport('joomla.user.helper'); 687 $result = JUserHelper::getUserGroups($userId); 688 } 689 690 return $result; 691 } 692 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Tue Apr 3 11:40:28 2012 | Cross-referenced by PHPXref 0.7.1 |