Source for file group.php

Documentation is available at group.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Administrator
  4.  * @subpackage  com_users
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7.  * @license     GNU General Public License version 2 or later; see LICENSE.txt
  8.  */
  9.  
  10. defined('_JEXEC'or die;
  11.  
  12. /**
  13.  * User group model.
  14.  *
  15.  * @package     Joomla.Administrator
  16.  * @subpackage  com_users
  17.  * @since       1.6
  18.  */
  19. class UsersModelGroup extends JModelAdmin
  20. {
  21.     /**
  22.      * @var        string    The event to trigger after saving the data.
  23.      * @since   1.6
  24.      */
  25.     protected $event_after_save = 'onUserAfterSaveGroup';
  26.  
  27.     /**
  28.      * @var        string    The event to trigger after before the data.
  29.      * @since   1.6
  30.      */
  31.     protected $event_before_save = 'onUserBeforeSaveGroup';
  32.  
  33.     /**
  34.      * Returns a reference to the a Table object, always creating it.
  35.      *
  36.      * @param   type    The table type to instantiate
  37.      * @param   string    A prefix for the table class name. Optional.
  38.      * @param   array  Configuration array for model. Optional.
  39.      * @return  JTable    A database object
  40.      * @since   1.6
  41.     */
  42.     public function getTable($type 'Usergroup'$prefix 'JTable'$config array())
  43.     {
  44.         $return JTable::getInstance($type$prefix$config);
  45.         return $return;
  46.     }
  47.  
  48.     /**
  49.      * Method to get the record form.
  50.      *
  51.      * @param   array  $data        An optional array of data for the form to interogate.
  52.      * @param   boolean    $loadData    True if the form is to load its own data (default case), false if not.
  53.      * @return  JForm    A JForm object on success, false on failure
  54.      * @since   1.6
  55.      */
  56.     public function getForm($data array()$loadData true)
  57.     {
  58.         // Get the form.
  59.         $form $this->loadForm('com_users.group''group'array('control' => 'jform''load_data' => $loadData));
  60.         if (empty($form))
  61.         {
  62.             return false;
  63.         }
  64.  
  65.         return $form;
  66.     }
  67.  
  68.     /**
  69.      * Method to get the data that should be injected in the form.
  70.      *
  71.      * @return  mixed  The data for the form.
  72.      * @since   1.6
  73.      */
  74.     protected function loadFormData()
  75.     {
  76.         // Check the session for previously entered form data.
  77.         $data JFactory::getApplication()->getUserState('com_users.edit.group.data'array());
  78.  
  79.         if (empty($data))
  80.         {
  81.             $data $this->getItem();
  82.         }
  83.  
  84.         $this->preprocessData('com_users.group'$data);
  85.  
  86.         return $data;
  87.     }
  88.  
  89.     /**
  90.      * Override preprocessForm to load the user plugin group instead of content.
  91.      *
  92.      * @param   object    form object.
  93.      * @param   mixed    The data expected for the form.
  94.      * @throws    Exception if there is an error in the form event.
  95.      * @since   1.6
  96.      */
  97.     protected function preprocessForm(JForm $form$data$groups '')
  98.     {
  99.         $obj is_array($dataJArrayHelper::toObject($data'JObject'$data;
  100.         if (isset($obj->parent_id&& $obj->parent_id == && $obj->id 0)
  101.         {
  102.             $form->setFieldAttribute('parent_id''type''hidden');
  103.             $form->setFieldAttribute('parent_id''hidden''true');
  104.         }
  105.         parent::preprocessForm($form$data'user');
  106.     }
  107.  
  108.     /**
  109.      * Method to save the form data.
  110.      *
  111.      * @param   array  The form data.
  112.      * @return  boolean  True on success.
  113.      * @since   1.6
  114.      */
  115.     public function save($data)
  116.     {
  117.         // Include the content plugins for events.
  118.         JPluginHelper::importPlugin('user');
  119.  
  120.         // Check the super admin permissions for group
  121.         // We get the parent group permissions and then check the group permissions manually
  122.         // We have to calculate the group permissions manually because we haven't saved the group yet
  123.         $parentSuperAdmin JAccess::checkGroup($data['parent_id']'core.admin');
  124.         // Get core.admin rules from the root asset
  125.         $rules JAccess::getAssetRules('root.1')->getData('core.admin');
  126.         // Get the value for the current group (will be true (allowed), false (denied), or null (inherit)
  127.         $groupSuperAdmin $rules['core.admin']->allow($data['id']);
  128.  
  129.         // We only need to change the $groupSuperAdmin if the parent is true or false. Otherwise, the value set in the rule takes effect.
  130.         if ($parentSuperAdmin === false)
  131.         {
  132.             // If parent is false (Denied), effective value will always be false
  133.             $groupSuperAdmin false;
  134.         }
  135.         elseif ($parentSuperAdmin === true)
  136.         {
  137.             // If parent is true (allowed), group is true unless explicitly set to false
  138.             $groupSuperAdmin ($groupSuperAdmin === falsefalse true;
  139.         }
  140.  
  141.         // Check for non-super admin trying to save with super admin group
  142.         $iAmSuperAdmin    JFactory::getUser()->authorise('core.admin');
  143.         if ((!$iAmSuperAdmin&& ($groupSuperAdmin))
  144.         {
  145.             try
  146.             {
  147.                 throw new Exception(JText::_('JLIB_USER_ERROR_NOT_SUPERADMIN'));
  148.             }
  149.             catch (Exception $e)
  150.             {
  151.                 $this->setError($e->getMessage());
  152.                 return false;
  153.             }
  154.         }
  155.  
  156.         // Check for super-admin changing self to be non-super-admin
  157.         // First, are we a super admin>
  158.         if ($iAmSuperAdmin)
  159.         {
  160.             // Next, are we a member of the current group?
  161.             $myGroups JAccess::getGroupsByUser(JFactory::getUser()->get('id')false);
  162.             if (in_array($data['id']$myGroups))
  163.             {
  164.                 // Now, would we have super admin permissions without the current group?
  165.                 $otherGroups array_diff($myGroupsarray($data['id']));
  166.                 $otherSuperAdmin false;
  167.                 foreach ($otherGroups as $otherGroup)
  168.                 {
  169.                     $otherSuperAdmin ($otherSuperAdmin$otherSuperAdmin JAccess::checkGroup($otherGroup'core.admin');
  170.                 }
  171.                 // If we would not otherwise have super admin permissions
  172.                 // and the current group does not have super admin permissions, throw an exception
  173.                 if ((!$otherSuperAdmin&& (!$groupSuperAdmin))
  174.                 {
  175.                     try
  176.                     {
  177.                         throw new Exception(JText::_('JLIB_USER_ERROR_CANNOT_DEMOTE_SELF'));
  178.                     }
  179.                     catch (Exception $e)
  180.                     {
  181.                         $this->setError($e->getMessage());
  182.                         return false;
  183.                     }
  184.                 }
  185.             }
  186.         }
  187.  
  188.         // Proceed with the save
  189.         return parent::save($data);
  190.     }
  191.  
  192.     /**
  193.      * Method to delete rows.
  194.      *
  195.      * @param   array  An array of item ids.
  196.      * @return  boolean  Returns true on success, false on failure.
  197.      * @since   1.6
  198.      */
  199.     public function delete(&$pks)
  200.     {
  201.         // Typecast variable.
  202.         $pks = (array) $pks;
  203.         $user    JFactory::getUser();
  204.         $groups JAccess::getGroupsByUser($user->get('id'));
  205.  
  206.         // Get a row instance.
  207.         $table $this->getTable();
  208.  
  209.         // Load plugins.
  210.         JPluginHelper::importPlugin('user');
  211.         $dispatcher JEventDispatcher::getInstance();
  212.  
  213.         // Check if I am a Super Admin
  214.         $iAmSuperAdmin    $user->authorise('core.admin');
  215.  
  216.         // do not allow to delete groups to which the current user belongs
  217.         foreach ($pks as $pk)
  218.         {
  219.             if (in_array($pk$groups))
  220.             {
  221.                 JError::raiseWarning(403JText::_('COM_USERS_DELETE_ERROR_INVALID_GROUP'));
  222.                 return false;
  223.             }
  224.         }
  225.         // Iterate the items to delete each one.
  226.         foreach ($pks as $i => $pk)
  227.         {
  228.             if ($table->load($pk))
  229.             {
  230.                 // Access checks.
  231.                 $allow $user->authorise('core.edit.state''com_users');
  232.                 // Don't allow non-super-admin to delete a super admin
  233.                 $allow (!$iAmSuperAdmin && JAccess::checkGroup($pk'core.admin')) false $allow;
  234.  
  235.                 if ($allow)
  236.                 {
  237.                     // Fire the onUserBeforeDeleteGroup event.
  238.                     $dispatcher->trigger('onUserBeforeDeleteGroup'array($table->getProperties()));
  239.  
  240.                     if (!$table->delete($pk))
  241.                     {
  242.                         $this->setError($table->getError());
  243.                         return false;
  244.                     else {
  245.                         // Trigger the onUserAfterDeleteGroup event.
  246.                         $dispatcher->trigger('onUserAfterDeleteGroup'array($table->getProperties()true$this->getError()));
  247.                     }
  248.                 else {
  249.                     // Prune items that you can't change.
  250.                     unset($pks[$i]);
  251.                     JError::raiseWarning(403JText::_('JERROR_CORE_DELETE_NOT_PERMITTED'));
  252.                 }
  253.             else {
  254.                 $this->setError($table->getError());
  255.                 return false;
  256.             }
  257.         }
  258.  
  259.         return true;
  260.     }
  261. }

Documentation generated on Tue, 19 Nov 2013 15:04:05 +0100 by phpDocumentor 1.4.3