2018-07-24 19:35:26 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Users;
|
|
|
|
|
2023-09-07 12:22:14 -07:00
|
|
|
use App\Helpers\StorageHelper;
|
2018-07-24 19:35:26 -07:00
|
|
|
use App\Http\Controllers\Controller;
|
2024-02-07 07:21:48 -08:00
|
|
|
use App\Http\Requests\UploadFileRequest;
|
2018-07-24 19:35:26 -07:00
|
|
|
use App\Models\Actionlog;
|
|
|
|
use App\Models\User;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
use Illuminate\Support\Facades\Input;
|
|
|
|
use Illuminate\Support\Facades\Response;
|
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
2021-10-06 12:26:45 -07:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
2018-07-24 19:35:26 -07:00
|
|
|
|
|
|
|
class UserFilesController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Return JSON response with a list of user details for the getIndex() view.
|
|
|
|
*
|
2024-02-07 07:21:48 -08:00
|
|
|
* @param UploadFileRequest $request
|
2018-07-24 19:35:26 -07:00
|
|
|
* @param int $userId
|
|
|
|
* @return string JSON
|
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
2024-02-07 07:21:48 -08:00
|
|
|
*@author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v1.6]
|
2018-07-24 19:35:26 -07:00
|
|
|
*/
|
2024-02-07 07:21:48 -08:00
|
|
|
public function store(UploadFileRequest $request, $userId = null)
|
2018-07-24 19:35:26 -07:00
|
|
|
{
|
|
|
|
$user = User::find($userId);
|
2021-06-10 13:15:52 -07:00
|
|
|
$destinationPath = config('app.private_uploads').'/users';
|
2018-07-24 19:35:26 -07:00
|
|
|
|
|
|
|
if (isset($user->id)) {
|
|
|
|
$this->authorize('update', $user);
|
|
|
|
|
|
|
|
$logActions = [];
|
|
|
|
$files = $request->file('file');
|
2021-05-27 13:48:13 -07:00
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
if (is_null($files)) {
|
2021-05-27 13:48:13 -07:00
|
|
|
return redirect()->back()->with('error', trans('admin/users/message.upload.nofiles'));
|
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
foreach ($files as $file) {
|
2024-02-07 07:21:48 -08:00
|
|
|
$file_name = $request->handleFile('private_uploads/users/', 'user-'.$user->id, $file);
|
2021-10-06 12:26:45 -07:00
|
|
|
|
2018-07-24 19:35:26 -07:00
|
|
|
//Log the uploaded file to the log
|
|
|
|
$logAction = new Actionlog();
|
|
|
|
$logAction->item_id = $user->id;
|
|
|
|
$logAction->item_type = User::class;
|
|
|
|
$logAction->user_id = Auth::id();
|
2019-12-11 11:09:54 -08:00
|
|
|
$logAction->note = $request->input('notes');
|
2018-07-24 19:35:26 -07:00
|
|
|
$logAction->target_id = null;
|
|
|
|
$logAction->created_at = date("Y-m-d H:i:s");
|
2021-10-06 12:26:45 -07:00
|
|
|
$logAction->filename = $file_name;
|
2018-07-24 19:35:26 -07:00
|
|
|
$logAction->action_type = 'uploaded';
|
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
if (! $logAction->save()) {
|
|
|
|
return JsonResponse::create(['error' => 'Failed validation: '.print_r($logAction->getErrors(), true)], 500);
|
2018-07-24 19:35:26 -07:00
|
|
|
}
|
|
|
|
$logActions[] = $logAction;
|
|
|
|
}
|
2021-01-27 12:01:42 -08:00
|
|
|
// dd($logActions);
|
|
|
|
return redirect()->back()->with('success', trans('admin/users/message.upload.success'));
|
2018-07-24 19:35:26 -07:00
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
return redirect()->back()->with('error', trans('admin/users/message.upload.nofiles'));
|
2018-07-24 19:35:26 -07:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete file
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v1.6]
|
|
|
|
* @param int $userId
|
|
|
|
* @param int $fileId
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
|
|
|
*/
|
|
|
|
public function destroy($userId = null, $fileId = null)
|
|
|
|
{
|
|
|
|
$user = User::find($userId);
|
|
|
|
$destinationPath = config('app.private_uploads').'/users';
|
|
|
|
|
|
|
|
if (isset($user->id)) {
|
|
|
|
$this->authorize('update', $user);
|
|
|
|
$log = Actionlog::find($fileId);
|
2021-06-10 13:15:52 -07:00
|
|
|
$full_filename = $destinationPath.'/'.$log->filename;
|
2018-07-24 19:35:26 -07:00
|
|
|
if (file_exists($full_filename)) {
|
2021-06-10 13:15:52 -07:00
|
|
|
unlink($destinationPath.'/'.$log->filename);
|
2018-07-24 19:35:26 -07:00
|
|
|
}
|
|
|
|
$log->delete();
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2018-07-24 19:35:26 -07:00
|
|
|
return redirect()->back()->with('success', trans('admin/users/message.deletefile.success'));
|
|
|
|
}
|
|
|
|
// Prepare the error message
|
|
|
|
$error = trans('admin/users/message.user_not_found', ['id' => $userId]);
|
|
|
|
// Redirect to the licence management page
|
|
|
|
return redirect()->route('users.index')->with('error', $error);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display/download the uploaded file
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v1.6]
|
|
|
|
* @param int $userId
|
|
|
|
* @param int $fileId
|
|
|
|
* @return mixed
|
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
|
|
|
*/
|
|
|
|
public function show($userId = null, $fileId = null)
|
|
|
|
{
|
2023-10-09 08:43:14 -07:00
|
|
|
|
|
|
|
if (empty($fileId)) {
|
|
|
|
return redirect()->route('users.show')->with('error', 'Invalid file request');
|
|
|
|
}
|
|
|
|
|
2018-07-24 19:35:26 -07:00
|
|
|
$user = User::find($userId);
|
|
|
|
|
|
|
|
// the license is valid
|
|
|
|
if (isset($user->id)) {
|
2023-09-05 10:28:01 -07:00
|
|
|
|
2018-07-24 19:35:26 -07:00
|
|
|
$this->authorize('view', $user);
|
|
|
|
|
2023-10-09 08:43:14 -07:00
|
|
|
if ($log = Actionlog::whereNotNull('filename')->where('item_id', $user->id)->find($fileId)) {
|
2023-10-09 07:17:03 -07:00
|
|
|
|
|
|
|
// Display the file inline
|
|
|
|
if (request('inline') == 'true') {
|
|
|
|
$headers = [
|
|
|
|
'Content-Disposition' => 'inline',
|
|
|
|
];
|
|
|
|
return Storage::download('private_uploads/users/'.$log->filename, $log->filename, $headers);
|
|
|
|
}
|
2023-09-05 10:28:01 -07:00
|
|
|
|
2023-10-09 07:17:03 -07:00
|
|
|
return Storage::download('private_uploads/users/'.$log->filename);
|
2023-09-05 10:28:01 -07:00
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2023-10-09 07:17:03 -07:00
|
|
|
return redirect()->route('users.index')->with('error', trans('admin/users/message.log_record_not_found'));
|
2018-07-24 19:35:26 -07:00
|
|
|
}
|
|
|
|
|
2023-09-05 10:28:01 -07:00
|
|
|
// Redirect to the user management page if the user doesn't exist
|
|
|
|
return redirect()->route('users.index')->with('error', trans('admin/users/message.user_not_found', ['id' => $userId]));
|
2018-07-24 19:35:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|