forked from ProcessMaker/processmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDownloadSecurityLog.php
More file actions
274 lines (235 loc) · 6.93 KB
/
DownloadSecurityLog.php
File metadata and controls
274 lines (235 loc) · 6.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<?php
namespace ProcessMaker\Jobs;
use Exception;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use ProcessMaker\Events\SecurityLogDownloadFailed;
use ProcessMaker\Events\SecurityLogDownloadJobCompleted;
use ProcessMaker\Models\Media;
use ProcessMaker\Models\SecurityLog;
use ProcessMaker\Models\User;
use Ramsey\Uuid\Uuid;
class DownloadSecurityLog implements ShouldQueue
{
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use SerializesModels;
private User $user;
private string $format;
private ?int $userId;
public const CSV_SEPARATOR = ',';
public const EXPIRATION_HOURS = 24;
public const FORMAT_CSV = 'csv';
public const FORMAT_XML = 'xml';
/**
* @param User $user
* @param string $format xml|csv
* @param int|null $userId
*/
public function __construct(User $user, string $format, int $userId = null)
{
$this->user = $user;
$this->format = $format;
$this->userId = $userId;
}
/**
* Execute the job.
*
* @return void
* @throw Exception
*/
public function handle()
{
// Check if the S3 is ready to use
if (!Media::s3IsReady()) {
event(new SecurityLogDownloadFailed($this->user, false, __('You seem to be missing access to this feature. Please contact ProcessMaker Support.
')));
return;
}
try {
// Get the temp filename
$filename = $this->createTemporaryFilename();
// Get the date of expiration
$expires = $this->getExpires();
// Export the file and get the URL
$url = $this->export($filename, $expires);
$message = __('Click on the link to download the log file. This link will be available until ' . $expires->toString());
// Call the event
event(new SecurityLogDownloadJobCompleted($this->user, true, $message, $url));
} catch (Exception $e) {
$message = __('It was not possible to connect AWS S3 service. Please contact your Customer Success Manager to use it.');
event(new SecurityLogDownloadFailed($this->user, false, $e->getMessage()));
}
}
/**
* Get expires time
*
* @return Carbon time
*/
protected function getExpires()
{
return now()->addHours(static::EXPIRATION_HOURS);
}
/**
* Create a temp file
*
* @return string
*/
protected function createTemporaryFilename()
{
$uuid = Uuid::uuid4()->toString() . Str::random(8);
$s3Uri = empty(config('app.security_log_s3_uri')) ? 'security-logs' : config('app.security_log_s3_uri');
return $s3Uri . '/' . $uuid . '.' . $this->format;
}
/**
* Export the file and get the URL
*
* @param string $filename
* @param Carbon $expires
*
* @return URL
*/
protected function export(string $filename, Carbon $expires)
{
// Get a disk manager for S3
$disk = Storage::disk('s3');
// Create a stream
$stream = fopen('php://temp', 'w+');
// Write the content
$stream = $this->writeContent($stream);
// Rewind the stream
rewind($stream);
// Save the stream to S3
$disk->put($filename, stream_get_contents($stream), [
'ACL' => 'private', // private|public-read,
'Expires' => $expires->toString(),
]);
// Close the stream
fclose($stream);
// Save temporary Url
$url = $disk->temporaryUrl(
$filename,
$expires,
[
'ResponseContentType' => 'application/octet-stream',
'ResponseContentDisposition' => 'attachment; filename=' . $filename,
]
);
return $url;
}
/**
* Generate the content according to the format
*
* @return string
*/
protected function writeContent($stream)
{
$query = DB::table('security_logs');
// Check the filter per user
if ($this->userId) {
$query->where('user_id', $this->userId);
}
// Initial tags for XML
$this->initialTagsXML($this->format === static::FORMAT_XML, $stream);
// Use a cursor to iterate over the table data
$query->orderBy('id')->cursor()->each(function ($record) use ($stream) {
// Convert each record to an array and write it to the stream
$stream = $this->format === static::FORMAT_CSV ? $this->toCSV($stream, (array) $record) : $this->toXML($stream, (array) $record);
});
// End tags for XML
$this->endTagsXML($this->format === static::FORMAT_XML, $stream);
return $stream;
}
/**
* Write the CSV line
*
* @param string $stream
* @param array $record
*
* @return string
*/
protected function toCSV($stream, array $record)
{
fputcsv($stream, (array) $record, static::CSV_SEPARATOR);
return $stream;
}
/**
* Write the XML node
*
* @param string $stream
* @param array $record
*
* @return string
*/
protected function toXML($stream, array $record)
{
$content = $this->getXmlNode((array) $record);
fwrite($stream, $content);
return $stream;
}
/**
* Write the initial tags to XML
*
* @param bool $write
* @param string $stream
*
* @return void
*/
protected function initialTagsXML($write, $stream)
{
if ($write) {
$contentXml = '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL;
$contentXml .= '<securityLogs>';
fwrite($stream, $contentXml);
}
}
/**
* Write the end tags to XML
*
* @param bool $write
* @param string $stream
*
* @return void
*/
protected function endTagsXML($write, $stream)
{
if ($write) {
$contentXml = PHP_EOL . '</securityLogs>';
fwrite($stream, $contentXml);
}
}
/**
* Get XML node
*
* @param array $item
*
* @return string
*/
protected function getXmlNode(array $item)
{
$tab = "\t";
$content = PHP_EOL . $tab . '<securityLog>';
foreach ($item as $key => $value) {
if (is_object($value)) {
$value = json_encode($value);
}
$content .= sprintf(
'%s<%s>%s</%s>',
PHP_EOL . $tab,
$key,
$value,
$key
);
}
$content .= PHP_EOL . $tab . '</securityLog>';
return $content;
}
}