Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/people/query/PhabricatorPeopleLogSearchEngine.php
12256 views
1
<?php
2
3
final class PhabricatorPeopleLogSearchEngine
4
extends PhabricatorApplicationSearchEngine {
5
6
public function getResultTypeDescription() {
7
return pht('Account Activity');
8
}
9
10
public function getApplicationClassName() {
11
return 'PhabricatorPeopleApplication';
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
protected function renderResultList(
128
array $logs,
129
PhabricatorSavedQuery $query,
130
array $handles) {
131
assert_instances_of($logs, 'PhabricatorUserLog');
132
133
$viewer = $this->requireViewer();
134
135
$table = id(new PhabricatorUserLogView())
136
->setUser($viewer)
137
->setLogs($logs);
138
139
if ($viewer->getIsAdmin()) {
140
$table->setSearchBaseURI($this->getApplicationURI('logs/'));
141
}
142
143
return id(new PhabricatorApplicationSearchResultView())
144
->setTable($table);
145
}
146
147
protected function newExportFields() {
148
$viewer = $this->requireViewer();
149
150
$fields = array(
151
$fields[] = id(new PhabricatorPHIDExportField())
152
->setKey('actorPHID')
153
->setLabel(pht('Actor PHID')),
154
$fields[] = id(new PhabricatorStringExportField())
155
->setKey('actor')
156
->setLabel(pht('Actor')),
157
$fields[] = id(new PhabricatorPHIDExportField())
158
->setKey('userPHID')
159
->setLabel(pht('User PHID')),
160
$fields[] = id(new PhabricatorStringExportField())
161
->setKey('user')
162
->setLabel(pht('User')),
163
$fields[] = id(new PhabricatorStringExportField())
164
->setKey('action')
165
->setLabel(pht('Action')),
166
$fields[] = id(new PhabricatorStringExportField())
167
->setKey('actionName')
168
->setLabel(pht('Action Name')),
169
$fields[] = id(new PhabricatorStringExportField())
170
->setKey('session')
171
->setLabel(pht('Session')),
172
$fields[] = id(new PhabricatorStringExportField())
173
->setKey('old')
174
->setLabel(pht('Old Value')),
175
$fields[] = id(new PhabricatorStringExportField())
176
->setKey('new')
177
->setLabel(pht('New Value')),
178
);
179
180
if ($viewer->getIsAdmin()) {
181
$fields[] = id(new PhabricatorStringExportField())
182
->setKey('remoteAddress')
183
->setLabel(pht('Remote Address'));
184
}
185
186
return $fields;
187
}
188
189
protected function newExportData(array $logs) {
190
$viewer = $this->requireViewer();
191
192
193
$phids = array();
194
foreach ($logs as $log) {
195
$phids[] = $log->getUserPHID();
196
$phids[] = $log->getActorPHID();
197
}
198
$handles = $viewer->loadHandles($phids);
199
200
$types = PhabricatorUserLogType::getAllLogTypes();
201
$types = mpull($types, 'getLogTypeName', 'getLogTypeKey');
202
203
$export = array();
204
foreach ($logs as $log) {
205
206
$user_phid = $log->getUserPHID();
207
if ($user_phid) {
208
$user_name = $handles[$user_phid]->getName();
209
} else {
210
$user_name = null;
211
}
212
213
$actor_phid = $log->getActorPHID();
214
if ($actor_phid) {
215
$actor_name = $handles[$actor_phid]->getName();
216
} else {
217
$actor_name = null;
218
}
219
220
$action = $log->getAction();
221
$action_name = idx($types, $action, pht('Unknown ("%s")', $action));
222
223
$map = array(
224
'actorPHID' => $actor_phid,
225
'actor' => $actor_name,
226
'userPHID' => $user_phid,
227
'user' => $user_name,
228
'action' => $action,
229
'actionName' => $action_name,
230
'session' => substr($log->getSession(), 0, 6),
231
'old' => $log->getOldValue(),
232
'new' => $log->getNewValue(),
233
);
234
235
if ($viewer->getIsAdmin()) {
236
$map['remoteAddress'] = $log->getRemoteAddr();
237
}
238
239
$export[] = $map;
240
}
241
242
return $export;
243
}
244
245
}
246
247