240 lines
6.8 KiB
PHP
240 lines
6.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\ExternalService;
|
|
use App\Facade\Theme;
|
|
use App\Facade\UserConfig;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\StoreServiceRequest;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\App;
|
|
use Illuminate\Support\Facades\View;
|
|
|
|
class ServiceController extends Controller
|
|
{
|
|
/**
|
|
* List of fields that must be encrypted before being saved.
|
|
*
|
|
* @var string[]
|
|
*/
|
|
private $fieldsToEncrypt;
|
|
|
|
/**
|
|
* List of fields that depend on the service_type being configured.
|
|
*
|
|
* @var string[]
|
|
*/
|
|
private $serviceTypeDependentFields;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->middleware('auth');
|
|
View::share('is_admin', true);
|
|
|
|
$this->serviceTypeDependentFields = ['app_id', 'app_secret'];
|
|
$this->fieldsToEncrypt = ['app_id', 'app_secret'];
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function create()
|
|
{
|
|
$this->authorizeAccessToAdminPanel('admin:manage-services');
|
|
|
|
return Theme::render('admin.create_service', [
|
|
'service' => new ExternalService(),
|
|
'serviceTypes' => $this->serviceTypeList()
|
|
]);
|
|
}
|
|
|
|
public function delete(Request $request, $id)
|
|
{
|
|
$this->authorizeAccessToAdminPanel('admin:manage-users');
|
|
|
|
$service = ExternalService::where('id', intval($id))->first();
|
|
if (is_null($service))
|
|
{
|
|
App::abort(404);
|
|
}
|
|
|
|
if ($this->isServiceInUse($service))
|
|
{
|
|
$request->session()->flash('warning', trans('admin.cannot_delete_service_in_use'));
|
|
return redirect(route('services.index'));
|
|
}
|
|
|
|
return Theme::render('admin.delete_service', ['service' => $service]);
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param int $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function destroy(Request $request, $id)
|
|
{
|
|
$this->authorizeAccessToAdminPanel('admin:manage-services');
|
|
|
|
$service = ExternalService::where('id', intval($id))->first();
|
|
if (is_null($service))
|
|
{
|
|
App::abort(404);
|
|
}
|
|
|
|
if ($this->isServiceInUse($service))
|
|
{
|
|
$request->session()->flash('warning', trans('admin.cannot_delete_service_in_use'));
|
|
return redirect(route('services.index'));
|
|
}
|
|
|
|
try
|
|
{
|
|
$service->delete();
|
|
$request->session()->flash('success', trans('admin.service_deletion_successful', [
|
|
'name' => $service->name
|
|
]));
|
|
}
|
|
catch (\Exception $ex)
|
|
{
|
|
$request->session()->flash('error', trans('admin.service_deletion_failed', [
|
|
'error_message' => $ex->getMessage(),
|
|
'name' => $service->name
|
|
]));
|
|
}
|
|
|
|
return redirect(route('services.index'));
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*
|
|
* @param int $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function edit(Request $request, $id)
|
|
{
|
|
$this->authorizeAccessToAdminPanel('admin:manage-services');
|
|
|
|
$service = ExternalService::where('id', intval($id))->first();
|
|
if (is_null($service))
|
|
{
|
|
App::abort(404);
|
|
}
|
|
|
|
// Decrypt the fields that are stored as encrypted in the DB
|
|
foreach ($this->fieldsToEncrypt as $field)
|
|
{
|
|
if (!empty($service->$field))
|
|
{
|
|
$service->$field = decrypt($service->$field);
|
|
}
|
|
}
|
|
|
|
return Theme::render('admin.edit_service', [
|
|
'service' => $service,
|
|
'serviceTypes' => $this->serviceTypeList()
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Display a listing of the resource.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function index(Request $request)
|
|
{
|
|
$this->authorizeAccessToAdminPanel('admin:manage-services');
|
|
|
|
$services = ExternalService::orderBy('name')
|
|
->paginate(UserConfig::get('items_per_page'));
|
|
|
|
return Theme::render('admin.list_services', [
|
|
'error' => $request->session()->get('error'),
|
|
'services' => $services,
|
|
'success' => $request->session()->get('success'),
|
|
'warning' => $request->session()->get('warning')
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function store(StoreServiceRequest $request)
|
|
{
|
|
$this->authorizeAccessToAdminPanel('admin:manage-services');
|
|
|
|
$service = new ExternalService($request->only(['name', 'service_type']));
|
|
|
|
foreach ($this->serviceTypeDependentFields as $field)
|
|
{
|
|
if ($request->has($field))
|
|
{
|
|
$service->$field = in_array($field, $this->fieldsToEncrypt)
|
|
? encrypt($request->get($field))
|
|
: $request->get($field);
|
|
}
|
|
}
|
|
|
|
$service->save();
|
|
|
|
return redirect(route('services.index'));
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @param int $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function update(StoreServiceRequest $request, $id)
|
|
{
|
|
$this->authorizeAccessToAdminPanel('admin:manage-services');
|
|
|
|
$service = ExternalService::where('id', intval($id))->first();
|
|
if (is_null($service))
|
|
{
|
|
App::abort(404);
|
|
}
|
|
|
|
$service->fill($request->only(['name', 'service_type']));
|
|
|
|
foreach ($this->serviceTypeDependentFields as $field)
|
|
{
|
|
if ($request->has($field))
|
|
{
|
|
$service->$field = in_array($field, $this->fieldsToEncrypt)
|
|
? encrypt($request->get($field))
|
|
: $request->get($field);
|
|
}
|
|
}
|
|
|
|
$service->save();
|
|
|
|
return redirect(route('services.index'));
|
|
}
|
|
|
|
private function isServiceInUse(ExternalService $service)
|
|
{
|
|
// TODO check if the service is in use anywhere else and prevent it being deleted if so
|
|
return false;
|
|
}
|
|
|
|
private function serviceTypeList()
|
|
{
|
|
return [
|
|
ExternalService::DROPBOX => trans(sprintf('services.%s', ExternalService::DROPBOX)),
|
|
ExternalService::FACEBOOK => trans(sprintf('services.%s', ExternalService::FACEBOOK)),
|
|
ExternalService::GOOGLE => trans(sprintf('services.%s', ExternalService::GOOGLE)),
|
|
ExternalService::TWITTER => trans(sprintf('services.%s', ExternalService::TWITTER))
|
|
];
|
|
}
|
|
} |