1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-26 16:52:41 +01:00

Upgrade user account activity logs to modern construction

Summary: Depends on D18965. Ref T13049. Move this Query and SearchEngine to be a little more modern, to prepare for Export support.

Test Plan:
  - Used all the query fields, viewed activity logs via People and Settings.
  - I'm not sure the "Session" query is used/useful and may remove it before I'm done here, but I just left it in place for now.

Reviewers: amckinley

Reviewed By: amckinley

Maniphest Tasks: T13049

Differential Revision: https://secure.phabricator.com/D18966
This commit is contained in:
epriestley 2018-01-30 06:09:35 -08:00
parent b27fd05eef
commit 91108cf838
4 changed files with 71 additions and 145 deletions

View file

@ -40,70 +40,61 @@ final class PhabricatorPeopleLogQuery
return $this; return $this;
} }
protected function loadPage() { public function newResultObject() {
$table = new PhabricatorUserLog(); return 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);
} }
protected function buildWhereClause(AphrontDatabaseConnection $conn_r) { protected function loadPage() {
$where = array(); return $this->loadStandardPage($this->newResultObject());
}
protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
$where = parent::buildWhereClauseParts($conn);
if ($this->actorPHIDs !== null) { if ($this->actorPHIDs !== null) {
$where[] = qsprintf( $where[] = qsprintf(
$conn_r, $conn,
'actorPHID IN (%Ls)', 'actorPHID IN (%Ls)',
$this->actorPHIDs); $this->actorPHIDs);
} }
if ($this->userPHIDs !== null) { if ($this->userPHIDs !== null) {
$where[] = qsprintf( $where[] = qsprintf(
$conn_r, $conn,
'userPHID IN (%Ls)', 'userPHID IN (%Ls)',
$this->userPHIDs); $this->userPHIDs);
} }
if ($this->relatedPHIDs !== null) { if ($this->relatedPHIDs !== null) {
$where[] = qsprintf( $where[] = qsprintf(
$conn_r, $conn,
'actorPHID IN (%Ls) OR userPHID IN (%Ls)', '(actorPHID IN (%Ls) OR userPHID IN (%Ls))',
$this->relatedPHIDs, $this->relatedPHIDs,
$this->relatedPHIDs); $this->relatedPHIDs);
} }
if ($this->sessionKeys !== null) { if ($this->sessionKeys !== null) {
$where[] = qsprintf( $where[] = qsprintf(
$conn_r, $conn,
'session IN (%Ls)', 'session IN (%Ls)',
$this->sessionKeys); $this->sessionKeys);
} }
if ($this->actions !== null) { if ($this->actions !== null) {
$where[] = qsprintf( $where[] = qsprintf(
$conn_r, $conn,
'action IN (%Ls)', 'action IN (%Ls)',
$this->actions); $this->actions);
} }
if ($this->remoteAddressPrefix !== null) { if ($this->remoteAddressPrefix !== null) {
$where[] = qsprintf( $where[] = qsprintf(
$conn_r, $conn,
'remoteAddr LIKE %>', 'remoteAddr LIKE %>',
$this->remoteAddressPrefix); $this->remoteAddressPrefix);
} }
$where[] = $this->buildPagingClause($conn_r); return $where;
return $this->formatWhereClause($where);
} }
public function getQueryApplicationClass() { public function getQueryApplicationClass() {

View file

@ -15,34 +15,8 @@ final class PhabricatorPeopleLogSearchEngine
return 500; return 500;
} }
public function buildSavedQueryFromRequest(AphrontRequest $request) { public function newQuery() {
$saved = new PhabricatorSavedQuery(); $query = new PhabricatorPeopleLogQuery();
$saved->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());
// NOTE: If the viewer isn't an administrator, always restrict the query to // 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 // related records. This echoes the policy logic of these logs. This is
@ -54,82 +28,61 @@ final class PhabricatorPeopleLogSearchEngine
$query->withRelatedPHIDs(array($viewer->getPHID())); $query->withRelatedPHIDs(array($viewer->getPHID()));
} }
$actor_phids = $saved->getParameter('actorPHIDs', array()); return $query;
if ($actor_phids) { }
$query->withActorPHIDs($actor_phids);
protected function buildQueryFromParameters(array $map) {
$query = $this->newQuery();
if ($map['userPHIDs']) {
$query->withUserPHIDs($map['userPHIDs']);
} }
$user_phids = $saved->getParameter('userPHIDs', array()); if ($map['actorPHIDs']) {
if ($user_phids) { $query->withActorPHIDs($map['actorPHIDs']);
$query->withUserPHIDs($user_phids);
} }
$actions = $saved->getParameter('actions', array()); if ($map['actions']) {
if ($actions) { $query->withActions($map['actions']);
$query->withActions($actions);
} }
$remote_prefix = $saved->getParameter('ip'); if (strlen($map['ip'])) {
if (strlen($remote_prefix)) { $query->withRemoteAddressPrefix($map['ip']);
$query->withRemoteAddressprefix($remote_prefix);
} }
$sessions = $saved->getParameter('sessions', array()); if ($map['sessions']) {
if ($sessions) { $query->withSessionKeys($map['sessions']);
$query->withSessionKeys($sessions);
} }
return $query; return $query;
} }
public function buildSearchForm( protected function buildCustomSearchFields() {
AphrontFormView $form, return array(
PhabricatorSavedQuery $saved) { id(new PhabricatorUsersSearchField())
->setKey('userPHIDs')
$actor_phids = $saved->getParameter('actorPHIDs', array()); ->setAliases(array('users', 'user', 'userPHID'))
$user_phids = $saved->getParameter('userPHIDs', array()); ->setLabel(pht('Users'))
->setDescription(pht('Search for activity affecting specific users.')),
$actions = $saved->getParameter('actions', array()); id(new PhabricatorUsersSearchField())
$remote_prefix = $saved->getParameter('ip'); ->setKey('actorPHIDs')
$sessions = $saved->getParameter('sessions', array()); ->setAliases(array('actors', 'actor', 'actorPHID'))
->setLabel(pht('Actors'))
$actions = array_fuse($actions); ->setDescription(pht('Search for activity by specific users.')),
$action_control = id(new AphrontFormCheckboxControl()) id(new PhabricatorSearchCheckboxesField())
->setLabel(pht('Actions')); ->setKey('actions')
$action_types = PhabricatorUserLog::getActionTypeMap(); ->setLabel(pht('Actions'))
foreach ($action_types as $type => $label) { ->setDescription(pht('Search for particular types of activity.'))
$action_control->addCheckbox( ->setOptions(PhabricatorUserLog::getActionTypeMap()),
'actions[]', id(new PhabricatorSearchTextField())
$type, ->setKey('ip')
$label, ->setLabel(pht('Filter IP'))
isset($actions[$label])); ->setDescription(pht('Search for actions by remote address.')),
} id(new PhabricatorSearchStringListField())
->setKey('sessions')
$form ->setLabel(pht('Sessions'))
->appendControl( ->setDescription(pht('Search for activity in particular sessions.')),
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 getURI($path) { protected function getURI($path) {
@ -156,19 +109,6 @@ final class PhabricatorPeopleLogSearchEngine
return parent::buildSavedQueryFromBuiltin($query_key); 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( protected function renderResultList(
array $logs, array $logs,
PhabricatorSavedQuery $query, PhabricatorSavedQuery $query,
@ -179,16 +119,13 @@ final class PhabricatorPeopleLogSearchEngine
$table = id(new PhabricatorUserLogView()) $table = id(new PhabricatorUserLogView())
->setUser($viewer) ->setUser($viewer)
->setLogs($logs) ->setLogs($logs);
->setHandles($handles);
if ($viewer->getIsAdmin()) { if ($viewer->getIsAdmin()) {
$table->setSearchBaseURI($this->getApplicationURI('logs/')); $table->setSearchBaseURI($this->getApplicationURI('logs/'));
} }
$result = new PhabricatorApplicationSearchResultView(); return id(new PhabricatorApplicationSearchResultView())
$result->setTable($table); ->setTable($table);
return $result;
} }
} }

View file

@ -3,7 +3,6 @@
final class PhabricatorUserLogView extends AphrontView { final class PhabricatorUserLogView extends AphrontView {
private $logs; private $logs;
private $handles;
private $searchBaseURI; private $searchBaseURI;
public function setSearchBaseURI($search_base_uri) { public function setSearchBaseURI($search_base_uri) {
@ -17,17 +16,17 @@ final class PhabricatorUserLogView extends AphrontView {
return $this; return $this;
} }
public function setHandles(array $handles) {
assert_instances_of($handles, 'PhabricatorObjectHandle');
$this->handles = $handles;
return $this;
}
public function render() { public function render() {
$logs = $this->logs; $logs = $this->logs;
$handles = $this->handles;
$viewer = $this->getUser(); $viewer = $this->getUser();
$phids = array();
foreach ($logs as $log) {
$phids[] = $log->getActorPHID();
$phids[] = $log->getUserPHID();
}
$handles = $viewer->loadHandles($phids);
$action_map = PhabricatorUserLog::getActionTypeMap(); $action_map = PhabricatorUserLog::getActionTypeMap();
$base_uri = $this->searchBaseURI; $base_uri = $this->searchBaseURI;

View file

@ -43,8 +43,7 @@ final class PhabricatorActivitySettingsPanel extends PhabricatorSettingsPanel {
$table = id(new PhabricatorUserLogView()) $table = id(new PhabricatorUserLogView())
->setUser($viewer) ->setUser($viewer)
->setLogs($logs) ->setLogs($logs);
->setHandles($handles);
$panel = $this->newBox(pht('Account Activity Logs'), $table); $panel = $this->newBox(pht('Account Activity Logs'), $table);