@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.)
hq.recaptime.dev/wiki/Phorge
phorge
phabricator
1<?php
2
3final class PhabricatorPeopleLogSearchEngine
4 extends PhabricatorApplicationSearchEngine {
5
6 public function getResultTypeDescription() {
7 return pht('Account Activity');
8 }
9
10 public function getApplicationClassName() {
11 return PhabricatorPeopleApplication::class;
12 }
13
14 public function getPageSize(PhabricatorSavedQuery $saved) {
15 return 500;
16 }
17
18 public function newQuery() {
19 $query = new PhabricatorPeopleLogQuery();
20
21 // NOTE: If the viewer isn't an administrator, always restrict the query to
22 // related records. This echoes the policy logic of these logs. This is
23 // mostly a performance optimization, to prevent us from having to pull
24 // large numbers of logs that the user will not be able to see and filter
25 // them in-process.
26 $viewer = $this->requireViewer();
27 if (!$viewer->getIsAdmin()) {
28 $query->withRelatedPHIDs(array($viewer->getPHID()));
29 }
30
31 return $query;
32 }
33
34 protected function buildQueryFromParameters(array $map) {
35 $query = $this->newQuery();
36
37 if ($map['userPHIDs']) {
38 $query->withUserPHIDs($map['userPHIDs']);
39 }
40
41 if ($map['actorPHIDs']) {
42 $query->withActorPHIDs($map['actorPHIDs']);
43 }
44
45 if ($map['actions']) {
46 $query->withActions($map['actions']);
47 }
48
49 if (strlen($map['ip'])) {
50 $query->withRemoteAddressPrefix($map['ip']);
51 }
52
53 if ($map['sessions']) {
54 $query->withSessionKeys($map['sessions']);
55 }
56
57 if ($map['createdStart'] || $map['createdEnd']) {
58 $query->withDateCreatedBetween(
59 $map['createdStart'],
60 $map['createdEnd']);
61 }
62
63 return $query;
64 }
65
66 protected function buildCustomSearchFields() {
67 $types = PhabricatorUserLogType::getAllLogTypes();
68 $types = mpull($types, 'getLogTypeName', 'getLogTypeKey');
69
70 return array(
71 id(new PhabricatorUsersSearchField())
72 ->setKey('userPHIDs')
73 ->setAliases(array('users', 'user', 'userPHID'))
74 ->setLabel(pht('Users'))
75 ->setDescription(pht('Search for activity affecting specific users.')),
76 id(new PhabricatorUsersSearchField())
77 ->setKey('actorPHIDs')
78 ->setAliases(array('actors', 'actor', 'actorPHID'))
79 ->setLabel(pht('Actors'))
80 ->setDescription(pht('Search for activity by specific users.')),
81 id(new PhabricatorSearchDatasourceField())
82 ->setKey('actions')
83 ->setLabel(pht('Actions'))
84 ->setDescription(pht('Search for particular types of activity.'))
85 ->setDatasource(new PhabricatorUserLogTypeDatasource()),
86 id(new PhabricatorSearchTextField())
87 ->setKey('ip')
88 ->setLabel(pht('Filter IP'))
89 ->setDescription(pht('Search for actions by remote address.')),
90 id(new PhabricatorSearchStringListField())
91 ->setKey('sessions')
92 ->setLabel(pht('Sessions'))
93 ->setDescription(pht('Search for activity in particular sessions.')),
94 id(new PhabricatorSearchDateField())
95 ->setLabel(pht('Created After'))
96 ->setKey('createdStart'),
97 id(new PhabricatorSearchDateField())
98 ->setLabel(pht('Created Before'))
99 ->setKey('createdEnd'),
100 );
101 }
102
103 protected function getURI($path) {
104 return '/people/logs/'.$path;
105 }
106
107 protected function getBuiltinQueryNames() {
108 $names = array(
109 'all' => pht('All'),
110 );
111
112 return $names;
113 }
114
115 public function buildSavedQueryFromBuiltin($query_key) {
116 $query = $this->newSavedQuery();
117 $query->setQueryKey($query_key);
118
119 switch ($query_key) {
120 case 'all':
121 return $query;
122 }
123
124 return parent::buildSavedQueryFromBuiltin($query_key);
125 }
126
127 /**
128 * @param array<PhabricatorUserLog> $logs
129 * @param PhabricatorSavedQuery $query
130 * @param array<PhabricatorObjectHandle> $handles
131 */
132 protected function renderResultList(
133 array $logs,
134 PhabricatorSavedQuery $query,
135 array $handles) {
136 assert_instances_of($logs, PhabricatorUserLog::class);
137
138 $viewer = $this->requireViewer();
139
140 $table = id(new PhabricatorUserLogView())
141 ->setUser($viewer)
142 ->setLogs($logs);
143
144 if ($viewer->getIsAdmin()) {
145 $table->setSearchBaseURI($this->getApplicationURI('logs/'));
146 }
147
148 return id(new PhabricatorApplicationSearchResultView())
149 ->setTable($table);
150 }
151
152 protected function newExportFields() {
153 $viewer = $this->requireViewer();
154
155 $fields = array(
156 $fields[] = id(new PhabricatorPHIDExportField())
157 ->setKey('actorPHID')
158 ->setLabel(pht('Actor PHID')),
159 $fields[] = id(new PhabricatorStringExportField())
160 ->setKey('actor')
161 ->setLabel(pht('Actor')),
162 $fields[] = id(new PhabricatorPHIDExportField())
163 ->setKey('userPHID')
164 ->setLabel(pht('User PHID')),
165 $fields[] = id(new PhabricatorStringExportField())
166 ->setKey('user')
167 ->setLabel(pht('User')),
168 $fields[] = id(new PhabricatorStringExportField())
169 ->setKey('action')
170 ->setLabel(pht('Action')),
171 $fields[] = id(new PhabricatorStringExportField())
172 ->setKey('actionName')
173 ->setLabel(pht('Action Name')),
174 $fields[] = id(new PhabricatorStringExportField())
175 ->setKey('session')
176 ->setLabel(pht('Session')),
177 $fields[] = id(new PhabricatorStringExportField())
178 ->setKey('old')
179 ->setLabel(pht('Old Value')),
180 $fields[] = id(new PhabricatorStringExportField())
181 ->setKey('new')
182 ->setLabel(pht('New Value')),
183 );
184
185 if ($viewer->getIsAdmin()) {
186 $fields[] = id(new PhabricatorStringExportField())
187 ->setKey('remoteAddress')
188 ->setLabel(pht('Remote Address'));
189 }
190
191 return $fields;
192 }
193
194 protected function newExportData(array $logs) {
195 $viewer = $this->requireViewer();
196
197
198 $phids = array();
199 foreach ($logs as $log) {
200 $phids[] = $log->getUserPHID();
201 $phids[] = $log->getActorPHID();
202 }
203 $handles = $viewer->loadHandles($phids);
204
205 $types = PhabricatorUserLogType::getAllLogTypes();
206 $types = mpull($types, 'getLogTypeName', 'getLogTypeKey');
207
208 $export = array();
209 foreach ($logs as $log) {
210
211 $user_phid = $log->getUserPHID();
212 if ($user_phid) {
213 $user_name = $handles[$user_phid]->getName();
214 } else {
215 $user_name = null;
216 }
217
218 $actor_phid = $log->getActorPHID();
219 if ($actor_phid) {
220 $actor_name = $handles[$actor_phid]->getName();
221 } else {
222 $actor_name = null;
223 }
224
225 $action = $log->getAction();
226 $action_name = idx($types, $action, pht('Unknown ("%s")', $action));
227
228 $map = array(
229 'actorPHID' => $actor_phid,
230 'actor' => $actor_name,
231 'userPHID' => $user_phid,
232 'user' => $user_name,
233 'action' => $action,
234 'actionName' => $action_name,
235 'session' => substr($log->getSession(), 0, 6),
236 'old' => $log->getOldValue(),
237 'new' => $log->getNewValue(),
238 );
239
240 if ($viewer->getIsAdmin()) {
241 $map['remoteAddress'] = $log->getRemoteAddr();
242 }
243
244 $export[] = $map;
245 }
246
247 return $export;
248 }
249
250}