public function runUpdateUser(framework\Request $request) { try { $user = entities\User::getB2DBTable()->selectByID($request['user_id']); if ($user instanceof entities\User) { if (!$user->isConfirmedMemberOfScope(framework\Context::getScope())) { $this->getResponse()->setHttpStatus(400); return $this->renderJSON(array('error' => $this->getI18n()->__('This user is not a confirmed member of this scope'))); } if (!empty($request['username'])) { $testuser = entities\User::getByUsername($request['username']); if (!$testuser instanceof entities\User || $testuser->getID() == $user->getID()) { $user->setUsername($request['username']); } else { $this->getResponse()->setHttpStatus(400); return $this->renderJSON(array('error' => $this->getI18n()->__('This username is already taken'))); } } $password_changed = false; if ($request['password_action'] == 'change' && $request['new_password_1'] && $request['new_password_2']) { if ($request['new_password_1'] == $request['new_password_2']) { $user->setPassword($request['new_password_1']); $password_changed = true; } else { $this->getResponse()->setHttpStatus(400); return $this->renderJSON(array('error' => $this->getI18n()->__('Please enter the new password twice'))); } } elseif ($request['password_action'] == 'random') { $random_password = entities\User::createPassword(); $user->setPassword($random_password); $password_changed = true; } if (isset($request['realname'])) { $user->setRealname($request['realname']); } $return_options = array(); try { if ($group = entities\Group::getB2DBTable()->selectById($request['group'])) { if ($user->getGroupID() != $group->getID()) { $groups = array($user->getGroupID(), $group->getID()); $return_options['update_groups'] = array('ids' => array(), 'membercounts' => array()); } $user->setGroup($group); } } catch (\Exception $e) { throw new \Exception($this->getI18n()->__('Invalid user group')); } $existing_teams = array_keys($user->getTeams()); $new_teams = array(); $new_clients = array(); $user->clearTeams(); try { foreach ($request->getParameter('teams', array()) as $team_id => $team) { if ($team = entities\Team::getB2DBTable()->selectById($team_id)) { $new_teams[] = $team_id; $user->addToTeam($team); } } } catch (\Exception $e) { throw new \Exception($this->getI18n()->__('One or more teams were invalid')); } try { $user->clearClients(); foreach ($request->getParameter('clients', array()) as $client_id => $client) { if ($client = entities\Client::getB2DBTable()->selectById($client_id)) { $new_clients[] = $client_id; $user->addToClient($client); } } } catch (\Exception $e) { throw new \Exception($this->getI18n()->__('One or more clients were invalid')); } if (isset($request['nickname'])) { $user->setBuddyname($request['nickname']); } if (isset($request['email'])) { $user->setEmail($request['email']); } if (isset($request['homepage'])) { $user->setHomepage($request['homepage']); } if (framework\Context::getScope()->isDefault()) { $user->setActivated((bool) $request['activated']); $user->setEnabled((bool) $request['enabled']); } $user->save(); if (isset($groups)) { foreach ($groups as $group_id) { if (!$group_id) { continue; } $return_options['update_groups']['ids'][] = $group_id; $return_options['update_groups']['membercounts'][$group_id] = entities\Group::getB2DBTable()->selectById($group_id)->getNumberOfMembers(); } } if ($new_teams != $existing_teams) { $new_team_ids = array_diff($new_teams, $existing_teams); $existing_team_ids = array_diff($existing_teams, $new_teams); $teams_to_update = array_merge($new_team_ids, $existing_team_ids); $return_options['update_teams'] = array('ids' => array(), 'membercounts' => array()); foreach ($teams_to_update as $team_id) { $return_options['update_teams']['ids'][] = $team_id; $return_options['update_teams']['membercounts'][$team_id] = entities\Team::getB2DBTable()->selectById($team_id)->getNumberOfMembers(); } } $template_options = array('user' => $user); if (isset($random_password)) { $template_options['random_password'] = $random_password; } $return_options['content'] = $this->getComponentHTML('configuration/finduser_row', $template_options); $return_options['title'] = $this->getI18n()->__('User updated!'); if ($password_changed) { $return_options['message'] = $this->getI18n()->__('The password was changed'); } return $this->renderJSON($return_options); } } catch (\Exception $e) { $this->getResponse()->setHttpStatus(400); return $this->renderJSON(array('error' => $this->getI18n()->__('This user could not be updated: %message', array('%message' => $e->getMessage())))); } $this->getResponse()->setHttpStatus(400); return $this->renderJSON(array('error' => $this->getI18n()->__('This user could not be updated'))); }
/** * Return the default user group * * @return \thebuggenie\core\entities\Group */ public static function getDefaultGroup() { try { return \thebuggenie\core\entities\Group::getB2DBTable()->selectByID(self::get(self::SETTING_USER_GROUP)); } catch (\Exception $e) { return null; } }
public function getByPermissionTargetIDAndModule($permission, $target_id, $module = 'core') { $crit = $this->getCriteria(); $crit->addWhere(self::PERMISSION_TYPE, $permission); $crit->addWhere(self::TARGET_ID, $target_id); $crit->addWhere(self::MODULE, $module); $permissions = array(); if ($res = $this->doSelect($crit)) { while ($row = $res->getNextRow()) { $target = null; if ($uid = $row->get(self::UID)) { $target = \thebuggenie\core\entities\User::getB2DBTable()->selectById($uid); } if ($tid = $row->get(self::TID)) { $target = \thebuggenie\core\entities\Team::getB2DBTable()->selectById($tid); } if ($gid = $row->get(self::GID)) { $target = \thebuggenie\core\entities\Group::getB2DBTable()->selectById($gid); } if ($target instanceof \thebuggenie\core\entities\common\Identifiable) { $permissions[] = array('target' => $target, 'allowed' => (bool) $row->get(self::ALLOWED), 'user_id' => $row->get(self::UID), 'team_id' => $row->get(self::TID), 'group_id' => $row->get(self::GID)); } } } return $permissions; }