2016-09-24 09:34:08 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
|
|
|
|
use App\Facade\Theme;
|
|
|
|
use App\Facade\UserConfig;
|
|
|
|
use App\Storage;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
|
|
|
use App\Http\Requests;
|
|
|
|
use App\Http\Controllers\Controller;
|
2016-09-28 20:13:18 +01:00
|
|
|
use Illuminate\Support\Facades\App;
|
2016-10-05 16:31:37 +01:00
|
|
|
use Illuminate\Support\Facades\View;
|
2016-09-24 09:34:08 +01:00
|
|
|
|
|
|
|
class StorageController extends Controller
|
|
|
|
{
|
2016-11-03 13:33:35 +00:00
|
|
|
/**
|
|
|
|
* @var mixed
|
|
|
|
*/
|
|
|
|
private $encryptedFields;
|
|
|
|
|
2016-09-24 09:34:08 +01:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->middleware('auth');
|
2016-10-05 16:31:37 +01:00
|
|
|
View::share('is_admin', true);
|
2016-11-03 13:33:35 +00:00
|
|
|
|
|
|
|
$this->encryptedFields = ['password', 'access_key', 'secret_key'];
|
2016-09-24 09:34:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display a listing of the resource.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
2016-09-28 20:32:19 +01:00
|
|
|
public function index(Request $request)
|
2016-09-24 09:34:08 +01:00
|
|
|
{
|
2017-04-15 09:58:33 +01:00
|
|
|
$this->authorizeAccessToAdminPanel('admin:manage-storage');
|
2016-09-24 09:34:08 +01:00
|
|
|
|
|
|
|
$storageLocations = Storage::orderBy('name')
|
|
|
|
->paginate(UserConfig::get('items_per_page'));
|
|
|
|
|
|
|
|
return Theme::render('admin.list_storage', [
|
2016-09-28 20:32:19 +01:00
|
|
|
'error' => $request->session()->get('error'),
|
2016-10-27 11:36:37 +01:00
|
|
|
'storageLocations' => $storageLocations,
|
|
|
|
'warning' => $request->session()->get('warning'),
|
2016-09-24 09:34:08 +01:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for creating a new resource.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
2016-09-28 20:32:19 +01:00
|
|
|
public function create(Request $request)
|
2016-09-24 09:34:08 +01:00
|
|
|
{
|
2017-04-15 09:58:33 +01:00
|
|
|
$this->authorizeAccessToAdminPanel('admin:manage-storage');
|
2016-09-24 09:34:08 +01:00
|
|
|
|
|
|
|
$filesystemDefaultLocation = sprintf('%s/storage/app/albums', dirname(dirname(dirname(dirname(__DIR__)))));
|
|
|
|
|
|
|
|
return Theme::render('admin.create_storage', [
|
|
|
|
'album_sources' => UserConfig::albumSources(),
|
2016-09-28 20:32:19 +01:00
|
|
|
'filesystem_default_location' => $filesystemDefaultLocation,
|
|
|
|
'info' => $request->session()->get('info')
|
2016-09-24 09:34:08 +01:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a newly created resource in storage.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function store(Requests\StoreStorageRequest $request)
|
|
|
|
{
|
2017-04-15 09:58:33 +01:00
|
|
|
$this->authorizeAccessToAdminPanel('admin:manage-storage');
|
2016-09-24 09:34:08 +01:00
|
|
|
|
|
|
|
$storage = new Storage();
|
2016-10-28 04:54:38 +01:00
|
|
|
$storage->fill($request->only([
|
|
|
|
'name',
|
|
|
|
'source',
|
|
|
|
'location',
|
|
|
|
'auth_url',
|
|
|
|
'tenant_name',
|
|
|
|
'username',
|
|
|
|
'password',
|
|
|
|
'service_name',
|
|
|
|
'service_region',
|
2016-10-28 12:59:36 +01:00
|
|
|
'container_name',
|
2016-11-03 13:33:35 +00:00
|
|
|
'cdn_url',
|
|
|
|
'access_key',
|
|
|
|
'secret_key'
|
2016-10-28 04:54:38 +01:00
|
|
|
]));
|
2016-10-27 11:36:37 +01:00
|
|
|
$storage->is_active = true;
|
2016-09-24 09:34:08 +01:00
|
|
|
$storage->is_default = (strtolower($request->get('is_default')) == 'on');
|
2016-10-27 11:36:37 +01:00
|
|
|
$storage->is_internal = false;
|
2016-10-28 04:54:38 +01:00
|
|
|
|
|
|
|
if ($storage->source != 'LocalFilesystemSource' && isset($storage->location))
|
|
|
|
{
|
|
|
|
unset($storage->location);
|
|
|
|
}
|
|
|
|
|
2016-11-03 13:33:35 +00:00
|
|
|
foreach ($this->encryptedFields as $field)
|
2016-10-28 04:54:38 +01:00
|
|
|
{
|
2016-11-03 13:33:35 +00:00
|
|
|
if (isset($storage->$field) && !empty($storage->$field))
|
|
|
|
{
|
|
|
|
$storage->$field = encrypt($storage->$field);
|
|
|
|
}
|
2016-10-28 04:54:38 +01:00
|
|
|
}
|
|
|
|
|
2016-09-24 09:34:08 +01:00
|
|
|
$storage->save();
|
|
|
|
|
|
|
|
if ($storage->is_default)
|
|
|
|
{
|
2016-09-28 20:13:18 +01:00
|
|
|
$this->unsetIsDefaultFromOthers($storage);
|
2016-09-24 09:34:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return redirect(route('storage.index'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display the specified resource.
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
//public function show($id)
|
|
|
|
//{
|
|
|
|
//
|
|
|
|
//}
|
|
|
|
|
2016-09-28 20:32:19 +01:00
|
|
|
/**
|
|
|
|
* Show the form for deleting the specified resource.
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function delete(Request $request, $id)
|
|
|
|
{
|
2017-04-15 09:58:33 +01:00
|
|
|
$this->authorizeAccessToAdminPanel('admin:manage-storage');
|
2016-09-28 20:32:19 +01:00
|
|
|
|
|
|
|
$storage = Storage::where('id', intval($id))->first();
|
|
|
|
if (is_null($storage))
|
|
|
|
{
|
|
|
|
App::abort(404);
|
|
|
|
}
|
|
|
|
|
2016-10-27 11:36:37 +01:00
|
|
|
if ($storage->is_internal)
|
|
|
|
{
|
|
|
|
// Can't delete the default storage location
|
|
|
|
$request->session()->flash('warning', trans('admin.delete_storage_internal'));
|
|
|
|
return redirect(route('storage.index'));
|
|
|
|
}
|
|
|
|
|
2016-09-28 20:32:19 +01:00
|
|
|
if ($storage->albums()->count() > 0)
|
|
|
|
{
|
|
|
|
// Can't delete storage location while albums exist
|
2016-10-27 11:36:37 +01:00
|
|
|
$request->session()->flash('error', trans('admin.delete_storage_existing_albums'));
|
2016-09-28 20:32:19 +01:00
|
|
|
return redirect(route('storage.index'));
|
|
|
|
}
|
|
|
|
|
|
|
|
return Theme::render('admin.delete_storage', ['storage' => $storage]);
|
|
|
|
}
|
|
|
|
|
2016-09-24 09:34:08 +01:00
|
|
|
/**
|
|
|
|
* Show the form for editing the specified resource.
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
2017-03-25 08:26:35 +00:00
|
|
|
public function edit(Request $request, $id)
|
2016-09-24 09:34:08 +01:00
|
|
|
{
|
2017-04-15 09:58:33 +01:00
|
|
|
$this->authorizeAccessToAdminPanel('admin:manage-storage');
|
2016-09-28 20:13:18 +01:00
|
|
|
|
2017-03-25 08:26:35 +00:00
|
|
|
/** @var Storage $storage */
|
2016-09-28 20:13:18 +01:00
|
|
|
$storage = Storage::where('id', intval($id))->first();
|
|
|
|
if (is_null($storage))
|
|
|
|
{
|
|
|
|
App::abort(404);
|
|
|
|
}
|
|
|
|
|
2016-11-03 13:33:35 +00:00
|
|
|
foreach ($this->encryptedFields as $field)
|
2016-10-28 12:59:36 +01:00
|
|
|
{
|
2016-11-03 13:33:35 +00:00
|
|
|
if (isset($storage->$field) && !empty($storage->$field))
|
|
|
|
{
|
|
|
|
$storage->$field = decrypt($storage->$field);
|
|
|
|
}
|
2016-10-28 12:59:36 +01:00
|
|
|
}
|
|
|
|
|
2017-03-25 08:26:35 +00:00
|
|
|
if (!$request->session()->has('_old_input'))
|
|
|
|
{
|
|
|
|
$request->session()->flash('_old_input', $storage->toArray());
|
|
|
|
}
|
|
|
|
|
2016-09-28 20:13:18 +01:00
|
|
|
return Theme::render('admin.edit_storage', ['storage' => $storage]);
|
2016-09-24 09:34:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the specified resource in storage.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param int $id
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
2016-09-28 20:13:18 +01:00
|
|
|
public function update(Requests\StoreStorageRequest $request, $id)
|
2016-09-24 09:34:08 +01:00
|
|
|
{
|
2017-04-15 09:58:33 +01:00
|
|
|
$this->authorizeAccessToAdminPanel('admin:manage-storage');
|
2016-09-28 20:13:18 +01:00
|
|
|
|
|
|
|
$storage = Storage::where('id', intval($id))->first();
|
|
|
|
if (is_null($storage))
|
|
|
|
{
|
|
|
|
App::abort(404);
|
|
|
|
}
|
|
|
|
|
2016-10-28 12:59:36 +01:00
|
|
|
$storage->fill($request->only([
|
|
|
|
'name',
|
|
|
|
'auth_url',
|
|
|
|
'tenant_name',
|
|
|
|
'username',
|
|
|
|
'password',
|
|
|
|
'service_name',
|
|
|
|
'service_region',
|
|
|
|
'container_name',
|
2016-11-03 13:33:35 +00:00
|
|
|
'cdn_url',
|
|
|
|
'access_key',
|
|
|
|
'secret_key'
|
2016-10-28 12:59:36 +01:00
|
|
|
]));
|
2016-10-27 11:36:37 +01:00
|
|
|
$storage->is_active = (strtolower($request->get('is_active')) == 'on');
|
2016-09-28 20:13:18 +01:00
|
|
|
$storage->is_default = (strtolower($request->get('is_default')) == 'on');
|
2016-10-27 11:36:37 +01:00
|
|
|
|
|
|
|
if ($storage->is_default && !$storage->is_active)
|
|
|
|
{
|
|
|
|
$storage->is_default = false;
|
|
|
|
}
|
|
|
|
|
2016-11-03 13:33:35 +00:00
|
|
|
foreach ($this->encryptedFields as $field)
|
2016-10-28 12:59:36 +01:00
|
|
|
{
|
2016-11-03 13:33:35 +00:00
|
|
|
if (isset($storage->$field) && !empty($storage->$field))
|
|
|
|
{
|
|
|
|
$storage->$field = encrypt($storage->$field);
|
|
|
|
}
|
2016-10-28 12:59:36 +01:00
|
|
|
}
|
|
|
|
|
2016-09-28 20:13:18 +01:00
|
|
|
$storage->save();
|
|
|
|
|
|
|
|
if ($storage->is_default)
|
|
|
|
{
|
|
|
|
$this->unsetIsDefaultFromOthers($storage);
|
|
|
|
}
|
|
|
|
|
|
|
|
return redirect(route('storage.index'));
|
2016-09-24 09:34:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the specified resource from storage.
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
2016-09-28 20:32:19 +01:00
|
|
|
public function destroy(Request $request, $id)
|
2016-09-24 09:34:08 +01:00
|
|
|
{
|
2017-04-15 09:58:33 +01:00
|
|
|
$this->authorizeAccessToAdminPanel('admin:manage-storage');
|
2016-09-28 20:32:19 +01:00
|
|
|
|
|
|
|
$storage = Storage::where('id', intval($id))->first();
|
|
|
|
if (is_null($storage))
|
|
|
|
{
|
|
|
|
App::abort(404);
|
|
|
|
}
|
|
|
|
|
2016-10-27 11:36:37 +01:00
|
|
|
if ($storage->is_internal)
|
|
|
|
{
|
|
|
|
// Can't delete the default storage location
|
|
|
|
$request->session()->flash('warning', trans('admin.delete_storage_internal'));
|
|
|
|
return redirect(route('storage.index'));
|
|
|
|
}
|
|
|
|
|
2016-09-28 20:32:19 +01:00
|
|
|
if ($storage->albums()->count() > 0)
|
|
|
|
{
|
|
|
|
// Can't delete storage location while albums exist
|
|
|
|
$request->session()->flash('error', trans('admin.delete_storage_existing_albums'));
|
|
|
|
return redirect(route('storage.index'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$storage->delete();
|
|
|
|
|
|
|
|
return redirect(route('storage.index'));
|
2016-09-24 09:34:08 +01:00
|
|
|
}
|
2016-09-28 20:13:18 +01:00
|
|
|
|
|
|
|
private function unsetIsDefaultFromOthers(Storage $storage)
|
|
|
|
{
|
|
|
|
// If this storage is flagged as default, remove all others
|
|
|
|
foreach (Storage::all() as $otherStorage)
|
|
|
|
{
|
|
|
|
if ($otherStorage->id == $storage->id)
|
|
|
|
{
|
|
|
|
// Ignore the one just created
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$otherStorage->is_default = false;
|
|
|
|
$otherStorage->save();
|
|
|
|
}
|
|
|
|
}
|
2016-09-24 09:34:08 +01:00
|
|
|
}
|