Beispiel #1
0
 /**
  * Perform batch operations
  *
  * @param   integer  $group_id  The group ID which assignments are being edited
  * @param   array    $user_ids  An array of user IDs on which to operate
  * @param   string   $action    The action to perform
  *
  * @return  boolean  True on success, false on failure
  *
  * @since   1.6
  */
 public function batchUser($group_id, $user_ids, $action)
 {
     JArrayHelper::toInteger($user_ids);
     // Check if I am a Super Admin
     $iAmSuperAdmin = JFactory::getUser()->authorise('core.admin');
     // Non-super super user cannot work with super-admin user.
     if (!$iAmSuperAdmin && JUserHelper::checkSuperUserInUsers($user_ids)) {
         $this->setError(JText::_('COM_USERS_ERROR_CANNOT_BATCH_SUPERUSER'));
         return false;
     }
     // Non-super admin cannot work with super-admin group.
     if (!$iAmSuperAdmin && JAccess::checkGroup($group_id, 'core.admin') || $group_id < 1) {
         $this->setError(JText::_('COM_USERS_ERROR_INVALID_GROUP'));
         return false;
     }
     // Get the DB object
     $db = $this->getDbo();
     switch ($action) {
         // Sets users to a selected group
         case 'set':
             $doDelete = 'all';
             $doAssign = true;
             break;
             // Remove users from a selected group
         // Remove users from a selected group
         case 'del':
             $doDelete = 'group';
             break;
             // Add users to a selected group
         // Add users to a selected group
         case 'add':
         default:
             $doAssign = true;
             break;
     }
     // Remove the users from the group if requested.
     if (isset($doDelete)) {
         $query = $db->getQuery(true);
         // Remove users from the group
         $query->delete($db->quoteName('#__user_usergroup_map'))->where($db->quoteName('user_id') . ' IN (' . implode(',', $user_ids) . ')');
         // Only remove users from selected group
         if ($doDelete == 'group') {
             $query->where($db->quoteName('group_id') . ' = ' . (int) $group_id);
         }
         $db->setQuery($query);
         try {
             $db->execute();
         } catch (RuntimeException $e) {
             $this->setError($e->getMessage());
             return false;
         }
     }
     // Assign the users to the group if requested.
     if (isset($doAssign)) {
         $query = $db->getQuery(true);
         // First, we need to check if the user is already assigned to a group
         $query->select($db->quoteName('user_id'))->from($db->quoteName('#__user_usergroup_map'))->where($db->quoteName('group_id') . ' = ' . (int) $group_id);
         $db->setQuery($query);
         $users = $db->loadColumn();
         // Build the values clause for the assignment query.
         $query->clear();
         $groups = false;
         foreach ($user_ids as $id) {
             if (!in_array($id, $users)) {
                 $query->values($id . ',' . $group_id);
                 $groups = true;
             }
         }
         // If we have no users to process, throw an error to notify the user
         if (!$groups) {
             $this->setError(JText::_('COM_USERS_ERROR_NO_ADDITIONS'));
             return false;
         }
         $query->insert($db->quoteName('#__user_usergroup_map'))->columns(array($db->quoteName('user_id'), $db->quoteName('group_id')));
         $db->setQuery($query);
         try {
             $db->execute();
         } catch (RuntimeException $e) {
             $this->setError($e->getMessage());
             return false;
         }
     }
     return true;
 }