diff --git a/src/applications/people/query/PhabricatorPeopleLogQuery.php b/src/applications/people/query/PhabricatorPeopleLogQuery.php index 9bcdc53f49..e6e2506bee 100644 --- a/src/applications/people/query/PhabricatorPeopleLogQuery.php +++ b/src/applications/people/query/PhabricatorPeopleLogQuery.php @@ -1,113 +1,104 @@ actorPHIDs = $actor_phids; return $this; } public function withUserPHIDs(array $user_phids) { $this->userPHIDs = $user_phids; return $this; } public function withRelatedPHIDs(array $related_phids) { $this->relatedPHIDs = $related_phids; return $this; } public function withSessionKeys(array $session_keys) { $this->sessionKeys = $session_keys; return $this; } public function withActions(array $actions) { $this->actions = $actions; return $this; } public function withRemoteAddressPrefix($remote_address_prefix) { $this->remoteAddressPrefix = $remote_address_prefix; return $this; } + public function newResultObject() { + return new PhabricatorUserLog(); + } + protected function loadPage() { - $table = new PhabricatorUserLog(); - $conn_r = $table->establishConnection('r'); - - $data = queryfx_all( - $conn_r, - 'SELECT * FROM %T %Q %Q %Q', - $table->getTableName(), - $this->buildWhereClause($conn_r), - $this->buildOrderClause($conn_r), - $this->buildLimitClause($conn_r)); - - return $table->loadAllFromArray($data); + return $this->loadStandardPage($this->newResultObject()); } - protected function buildWhereClause(AphrontDatabaseConnection $conn_r) { - $where = array(); + protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) { + $where = parent::buildWhereClauseParts($conn); if ($this->actorPHIDs !== null) { $where[] = qsprintf( - $conn_r, + $conn, 'actorPHID IN (%Ls)', $this->actorPHIDs); } if ($this->userPHIDs !== null) { $where[] = qsprintf( - $conn_r, + $conn, 'userPHID IN (%Ls)', $this->userPHIDs); } if ($this->relatedPHIDs !== null) { $where[] = qsprintf( - $conn_r, - 'actorPHID IN (%Ls) OR userPHID IN (%Ls)', + $conn, + '(actorPHID IN (%Ls) OR userPHID IN (%Ls))', $this->relatedPHIDs, $this->relatedPHIDs); } if ($this->sessionKeys !== null) { $where[] = qsprintf( - $conn_r, + $conn, 'session IN (%Ls)', $this->sessionKeys); } if ($this->actions !== null) { $where[] = qsprintf( - $conn_r, + $conn, 'action IN (%Ls)', $this->actions); } if ($this->remoteAddressPrefix !== null) { $where[] = qsprintf( - $conn_r, + $conn, 'remoteAddr LIKE %>', $this->remoteAddressPrefix); } - $where[] = $this->buildPagingClause($conn_r); - - return $this->formatWhereClause($where); + return $where; } public function getQueryApplicationClass() { return 'PhabricatorPeopleApplication'; } } diff --git a/src/applications/people/query/PhabricatorPeopleLogSearchEngine.php b/src/applications/people/query/PhabricatorPeopleLogSearchEngine.php index 851ef113d0..aea576693f 100644 --- a/src/applications/people/query/PhabricatorPeopleLogSearchEngine.php +++ b/src/applications/people/query/PhabricatorPeopleLogSearchEngine.php @@ -1,194 +1,131 @@ setParameter( - 'userPHIDs', - $this->readUsersFromRequest($request, 'users')); - - $saved->setParameter( - 'actorPHIDs', - $this->readUsersFromRequest($request, 'actors')); - - $saved->setParameter( - 'actions', - $this->readListFromRequest($request, 'actions')); - - $saved->setParameter( - 'ip', - $request->getStr('ip')); - - $saved->setParameter( - 'sessions', - $this->readListFromRequest($request, 'sessions')); - - return $saved; - } - - public function buildQueryFromSavedQuery(PhabricatorSavedQuery $saved) { - $query = id(new PhabricatorPeopleLogQuery()); + public function newQuery() { + $query = new PhabricatorPeopleLogQuery(); // NOTE: If the viewer isn't an administrator, always restrict the query to // related records. This echoes the policy logic of these logs. This is // mostly a performance optimization, to prevent us from having to pull // large numbers of logs that the user will not be able to see and filter // them in-process. $viewer = $this->requireViewer(); if (!$viewer->getIsAdmin()) { $query->withRelatedPHIDs(array($viewer->getPHID())); } - $actor_phids = $saved->getParameter('actorPHIDs', array()); - if ($actor_phids) { - $query->withActorPHIDs($actor_phids); + return $query; + } + + protected function buildQueryFromParameters(array $map) { + $query = $this->newQuery(); + + if ($map['userPHIDs']) { + $query->withUserPHIDs($map['userPHIDs']); } - $user_phids = $saved->getParameter('userPHIDs', array()); - if ($user_phids) { - $query->withUserPHIDs($user_phids); + if ($map['actorPHIDs']) { + $query->withActorPHIDs($map['actorPHIDs']); } - $actions = $saved->getParameter('actions', array()); - if ($actions) { - $query->withActions($actions); + if ($map['actions']) { + $query->withActions($map['actions']); } - $remote_prefix = $saved->getParameter('ip'); - if (strlen($remote_prefix)) { - $query->withRemoteAddressprefix($remote_prefix); + if (strlen($map['ip'])) { + $query->withRemoteAddressPrefix($map['ip']); } - $sessions = $saved->getParameter('sessions', array()); - if ($sessions) { - $query->withSessionKeys($sessions); + if ($map['sessions']) { + $query->withSessionKeys($map['sessions']); } return $query; } - public function buildSearchForm( - AphrontFormView $form, - PhabricatorSavedQuery $saved) { - - $actor_phids = $saved->getParameter('actorPHIDs', array()); - $user_phids = $saved->getParameter('userPHIDs', array()); - - $actions = $saved->getParameter('actions', array()); - $remote_prefix = $saved->getParameter('ip'); - $sessions = $saved->getParameter('sessions', array()); - - $actions = array_fuse($actions); - $action_control = id(new AphrontFormCheckboxControl()) - ->setLabel(pht('Actions')); - $action_types = PhabricatorUserLog::getActionTypeMap(); - foreach ($action_types as $type => $label) { - $action_control->addCheckbox( - 'actions[]', - $type, - $label, - isset($actions[$label])); - } - - $form - ->appendControl( - id(new AphrontFormTokenizerControl()) - ->setDatasource(new PhabricatorPeopleDatasource()) - ->setName('actors') - ->setLabel(pht('Actors')) - ->setValue($actor_phids)) - ->appendControl( - id(new AphrontFormTokenizerControl()) - ->setDatasource(new PhabricatorPeopleDatasource()) - ->setName('users') - ->setLabel(pht('Users')) - ->setValue($user_phids)) - ->appendChild($action_control) - ->appendChild( - id(new AphrontFormTextControl()) - ->setLabel(pht('Filter IP')) - ->setName('ip') - ->setValue($remote_prefix)) - ->appendChild( - id(new AphrontFormTextControl()) - ->setLabel(pht('Sessions')) - ->setName('sessions') - ->setValue(implode(', ', $sessions))); - + protected function buildCustomSearchFields() { + return array( + id(new PhabricatorUsersSearchField()) + ->setKey('userPHIDs') + ->setAliases(array('users', 'user', 'userPHID')) + ->setLabel(pht('Users')) + ->setDescription(pht('Search for activity affecting specific users.')), + id(new PhabricatorUsersSearchField()) + ->setKey('actorPHIDs') + ->setAliases(array('actors', 'actor', 'actorPHID')) + ->setLabel(pht('Actors')) + ->setDescription(pht('Search for activity by specific users.')), + id(new PhabricatorSearchCheckboxesField()) + ->setKey('actions') + ->setLabel(pht('Actions')) + ->setDescription(pht('Search for particular types of activity.')) + ->setOptions(PhabricatorUserLog::getActionTypeMap()), + id(new PhabricatorSearchTextField()) + ->setKey('ip') + ->setLabel(pht('Filter IP')) + ->setDescription(pht('Search for actions by remote address.')), + id(new PhabricatorSearchStringListField()) + ->setKey('sessions') + ->setLabel(pht('Sessions')) + ->setDescription(pht('Search for activity in particular sessions.')), + ); } protected function getURI($path) { return '/people/logs/'.$path; } protected function getBuiltinQueryNames() { $names = array( 'all' => pht('All'), ); return $names; } public function buildSavedQueryFromBuiltin($query_key) { $query = $this->newSavedQuery(); $query->setQueryKey($query_key); switch ($query_key) { case 'all': return $query; } return parent::buildSavedQueryFromBuiltin($query_key); } - protected function getRequiredHandlePHIDsForResultList( - array $logs, - PhabricatorSavedQuery $query) { - - $phids = array(); - foreach ($logs as $log) { - $phids[$log->getActorPHID()] = true; - $phids[$log->getUserPHID()] = true; - } - - return array_keys($phids); - } - protected function renderResultList( array $logs, PhabricatorSavedQuery $query, array $handles) { assert_instances_of($logs, 'PhabricatorUserLog'); $viewer = $this->requireViewer(); $table = id(new PhabricatorUserLogView()) ->setUser($viewer) - ->setLogs($logs) - ->setHandles($handles); + ->setLogs($logs); if ($viewer->getIsAdmin()) { $table->setSearchBaseURI($this->getApplicationURI('logs/')); } - $result = new PhabricatorApplicationSearchResultView(); - $result->setTable($table); - - return $result; + return id(new PhabricatorApplicationSearchResultView()) + ->setTable($table); } } diff --git a/src/applications/people/view/PhabricatorUserLogView.php b/src/applications/people/view/PhabricatorUserLogView.php index d648b248f7..309540c611 100644 --- a/src/applications/people/view/PhabricatorUserLogView.php +++ b/src/applications/people/view/PhabricatorUserLogView.php @@ -1,88 +1,87 @@ searchBaseURI = $search_base_uri; return $this; } public function setLogs(array $logs) { assert_instances_of($logs, 'PhabricatorUserLog'); $this->logs = $logs; return $this; } - public function setHandles(array $handles) { - assert_instances_of($handles, 'PhabricatorObjectHandle'); - $this->handles = $handles; - return $this; - } - public function render() { $logs = $this->logs; - $handles = $this->handles; $viewer = $this->getUser(); + $phids = array(); + foreach ($logs as $log) { + $phids[] = $log->getActorPHID(); + $phids[] = $log->getUserPHID(); + } + $handles = $viewer->loadHandles($phids); + $action_map = PhabricatorUserLog::getActionTypeMap(); $base_uri = $this->searchBaseURI; $rows = array(); foreach ($logs as $log) { $ip = $log->getRemoteAddr(); $session = substr($log->getSession(), 0, 6); if ($base_uri) { $ip = phutil_tag( 'a', array( 'href' => $base_uri.'?ip='.$ip.'#R', ), $ip); } $action = $log->getAction(); $action_name = idx($action_map, $action, $action); $rows[] = array( phabricator_date($log->getDateCreated(), $viewer), phabricator_time($log->getDateCreated(), $viewer), $action_name, $log->getActorPHID() ? $handles[$log->getActorPHID()]->getName() : null, $username = $handles[$log->getUserPHID()]->renderLink(), $ip, $session, ); } $table = new AphrontTableView($rows); $table->setHeaders( array( pht('Date'), pht('Time'), pht('Action'), pht('Actor'), pht('User'), pht('IP'), pht('Session'), )); $table->setColumnClasses( array( '', 'right', 'wide', '', '', '', 'n', )); return $table; } } diff --git a/src/applications/settings/panel/PhabricatorActivitySettingsPanel.php b/src/applications/settings/panel/PhabricatorActivitySettingsPanel.php index 50f951d661..eeeca27663 100644 --- a/src/applications/settings/panel/PhabricatorActivitySettingsPanel.php +++ b/src/applications/settings/panel/PhabricatorActivitySettingsPanel.php @@ -1,62 +1,61 @@ getUser(); $user = $this->getUser(); $pager = id(new AphrontCursorPagerView()) ->readFromRequest($request); $logs = id(new PhabricatorPeopleLogQuery()) ->setViewer($viewer) ->withRelatedPHIDs(array($user->getPHID())) ->executeWithCursorPager($pager); $phids = array(); foreach ($logs as $log) { $phids[] = $log->getUserPHID(); $phids[] = $log->getActorPHID(); } if ($phids) { $handles = id(new PhabricatorHandleQuery()) ->setViewer($viewer) ->withPHIDs($phids) ->execute(); } else { $handles = array(); } $table = id(new PhabricatorUserLogView()) ->setUser($viewer) - ->setLogs($logs) - ->setHandles($handles); + ->setLogs($logs); $panel = $this->newBox(pht('Account Activity Logs'), $table); $pager_box = id(new PHUIBoxView()) ->addMargin(PHUI::MARGIN_LARGE) ->appendChild($pager); return array($panel, $pager_box); } public function isManagementPanel() { return true; } }