Path: blob/master/src/applications/people/query/PhabricatorPeopleLogSearchEngine.php
12256 views
<?php12final class PhabricatorPeopleLogSearchEngine3extends PhabricatorApplicationSearchEngine {45public function getResultTypeDescription() {6return pht('Account Activity');7}89public function getApplicationClassName() {10return 'PhabricatorPeopleApplication';11}1213public function getPageSize(PhabricatorSavedQuery $saved) {14return 500;15}1617public function newQuery() {18$query = new PhabricatorPeopleLogQuery();1920// NOTE: If the viewer isn't an administrator, always restrict the query to21// related records. This echoes the policy logic of these logs. This is22// mostly a performance optimization, to prevent us from having to pull23// large numbers of logs that the user will not be able to see and filter24// them in-process.25$viewer = $this->requireViewer();26if (!$viewer->getIsAdmin()) {27$query->withRelatedPHIDs(array($viewer->getPHID()));28}2930return $query;31}3233protected function buildQueryFromParameters(array $map) {34$query = $this->newQuery();3536if ($map['userPHIDs']) {37$query->withUserPHIDs($map['userPHIDs']);38}3940if ($map['actorPHIDs']) {41$query->withActorPHIDs($map['actorPHIDs']);42}4344if ($map['actions']) {45$query->withActions($map['actions']);46}4748if (strlen($map['ip'])) {49$query->withRemoteAddressPrefix($map['ip']);50}5152if ($map['sessions']) {53$query->withSessionKeys($map['sessions']);54}5556if ($map['createdStart'] || $map['createdEnd']) {57$query->withDateCreatedBetween(58$map['createdStart'],59$map['createdEnd']);60}6162return $query;63}6465protected function buildCustomSearchFields() {66$types = PhabricatorUserLogType::getAllLogTypes();67$types = mpull($types, 'getLogTypeName', 'getLogTypeKey');6869return array(70id(new PhabricatorUsersSearchField())71->setKey('userPHIDs')72->setAliases(array('users', 'user', 'userPHID'))73->setLabel(pht('Users'))74->setDescription(pht('Search for activity affecting specific users.')),75id(new PhabricatorUsersSearchField())76->setKey('actorPHIDs')77->setAliases(array('actors', 'actor', 'actorPHID'))78->setLabel(pht('Actors'))79->setDescription(pht('Search for activity by specific users.')),80id(new PhabricatorSearchDatasourceField())81->setKey('actions')82->setLabel(pht('Actions'))83->setDescription(pht('Search for particular types of activity.'))84->setDatasource(new PhabricatorUserLogTypeDatasource()),85id(new PhabricatorSearchTextField())86->setKey('ip')87->setLabel(pht('Filter IP'))88->setDescription(pht('Search for actions by remote address.')),89id(new PhabricatorSearchStringListField())90->setKey('sessions')91->setLabel(pht('Sessions'))92->setDescription(pht('Search for activity in particular sessions.')),93id(new PhabricatorSearchDateField())94->setLabel(pht('Created After'))95->setKey('createdStart'),96id(new PhabricatorSearchDateField())97->setLabel(pht('Created Before'))98->setKey('createdEnd'),99);100}101102protected function getURI($path) {103return '/people/logs/'.$path;104}105106protected function getBuiltinQueryNames() {107$names = array(108'all' => pht('All'),109);110111return $names;112}113114public function buildSavedQueryFromBuiltin($query_key) {115$query = $this->newSavedQuery();116$query->setQueryKey($query_key);117118switch ($query_key) {119case 'all':120return $query;121}122123return parent::buildSavedQueryFromBuiltin($query_key);124}125126protected function renderResultList(127array $logs,128PhabricatorSavedQuery $query,129array $handles) {130assert_instances_of($logs, 'PhabricatorUserLog');131132$viewer = $this->requireViewer();133134$table = id(new PhabricatorUserLogView())135->setUser($viewer)136->setLogs($logs);137138if ($viewer->getIsAdmin()) {139$table->setSearchBaseURI($this->getApplicationURI('logs/'));140}141142return id(new PhabricatorApplicationSearchResultView())143->setTable($table);144}145146protected function newExportFields() {147$viewer = $this->requireViewer();148149$fields = array(150$fields[] = id(new PhabricatorPHIDExportField())151->setKey('actorPHID')152->setLabel(pht('Actor PHID')),153$fields[] = id(new PhabricatorStringExportField())154->setKey('actor')155->setLabel(pht('Actor')),156$fields[] = id(new PhabricatorPHIDExportField())157->setKey('userPHID')158->setLabel(pht('User PHID')),159$fields[] = id(new PhabricatorStringExportField())160->setKey('user')161->setLabel(pht('User')),162$fields[] = id(new PhabricatorStringExportField())163->setKey('action')164->setLabel(pht('Action')),165$fields[] = id(new PhabricatorStringExportField())166->setKey('actionName')167->setLabel(pht('Action Name')),168$fields[] = id(new PhabricatorStringExportField())169->setKey('session')170->setLabel(pht('Session')),171$fields[] = id(new PhabricatorStringExportField())172->setKey('old')173->setLabel(pht('Old Value')),174$fields[] = id(new PhabricatorStringExportField())175->setKey('new')176->setLabel(pht('New Value')),177);178179if ($viewer->getIsAdmin()) {180$fields[] = id(new PhabricatorStringExportField())181->setKey('remoteAddress')182->setLabel(pht('Remote Address'));183}184185return $fields;186}187188protected function newExportData(array $logs) {189$viewer = $this->requireViewer();190191192$phids = array();193foreach ($logs as $log) {194$phids[] = $log->getUserPHID();195$phids[] = $log->getActorPHID();196}197$handles = $viewer->loadHandles($phids);198199$types = PhabricatorUserLogType::getAllLogTypes();200$types = mpull($types, 'getLogTypeName', 'getLogTypeKey');201202$export = array();203foreach ($logs as $log) {204205$user_phid = $log->getUserPHID();206if ($user_phid) {207$user_name = $handles[$user_phid]->getName();208} else {209$user_name = null;210}211212$actor_phid = $log->getActorPHID();213if ($actor_phid) {214$actor_name = $handles[$actor_phid]->getName();215} else {216$actor_name = null;217}218219$action = $log->getAction();220$action_name = idx($types, $action, pht('Unknown ("%s")', $action));221222$map = array(223'actorPHID' => $actor_phid,224'actor' => $actor_name,225'userPHID' => $user_phid,226'user' => $user_name,227'action' => $action,228'actionName' => $action_name,229'session' => substr($log->getSession(), 0, 6),230'old' => $log->getOldValue(),231'new' => $log->getNewValue(),232);233234if ($viewer->getIsAdmin()) {235$map['remoteAddress'] = $log->getRemoteAddr();236}237238$export[] = $map;239}240241return $export;242}243244}245246247