Exemplo n.º 1
0
 /**
  * Prepare the response.
  *
  * @return  void
  *
  * @since   1.0
  * @throws  \Exception
  */
 protected function prepareResponse()
 {
     if (false == $this->getContainer()->get('app')->getUser()->check('manage')) {
         throw new \Exception('You are not authorized');
     }
     $input = $this->getContainer()->get('app')->input;
     $db = $this->getContainer()->get('db');
     $user = $input->getCmd('user');
     $groupId = $input->getInt('group_id');
     $assign = $input->getInt('assign');
     if (!$groupId) {
         throw new \Exception('Missing group id');
     }
     $tableUsers = new TableUsers($this->getContainer()->get('db'));
     $tableUsers->loadByUserName($user);
     if (!$tableUsers->id) {
         throw new \Exception('User not found');
     }
     $check = $db->setQuery($db->getQuery(true)->from($db->quoteName('#__user_accessgroup_map', 'm'))->select('COUNT(*)')->where($db->quoteName('group_id') . ' = ' . (int) $groupId)->where($db->quoteName('user_id') . ' = ' . (int) $tableUsers->id))->loadResult();
     if ($assign) {
         if ($check) {
             throw new \Exception('The user is already assigned to this group.');
         }
         $this->assign($tableUsers->id, $groupId);
         $this->response->data->message = g11n3t('The user has been assigned.');
     } else {
         if (!$check) {
             throw new \Exception('The user is not assigned to this group.');
         }
         $this->unAssign($tableUsers->id, $groupId);
         $this->response->data->message = g11n3t('The user has been unassigned.');
     }
 }
Exemplo n.º 2
0
 /**
  * Method to load a User object by user id number.
  *
  * @param   mixed  $identifier  The user id of the user to load.
  *
  * @return  $this  Method allows chaining
  *
  * @since   1.0
  * @throws  \RuntimeException
  */
 protected function load($identifier)
 {
     // Create the user table object
     $table = new TableUsers($this->database);
     // Load the User object based on the user id or throw a warning.
     if (!$table->load($identifier)) {
         throw new \RuntimeException('Unable to load the user with id: ' . $identifier);
     }
     // Assuming all is well at this point let's bind the data
     foreach ($table->getFields() as $key => $value) {
         if (isset($this->{$key}) && $key != 'params') {
             $this->{$key} = $table->{$key};
         }
     }
     $this->params->loadString($table->params);
     $this->loadAccessGroups();
     return $this;
 }