/** * 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.'); } }
/** * Load data by a given user name. * * @param string $userName The user name * * @return TableUsers * * @since 1.0 */ public function loadByUserName($userName) { $db = $this->database; $table = new TableUsers($db); $table->loadByUserName($userName); if (!$table->id) { // Register a new user $date = new Date(); $this->registerDate = $date->format($db->getDateFormat()); $table->save($this); } $this->id = $table->id; $this->params->loadString($table->params); $this->loadAccessGroups(); return $this; }