2020-04-19 10:54:07 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
|
2020-04-30 08:38:37 +01:00
|
|
|
use App\Configuration;
|
2020-04-20 22:33:42 +01:00
|
|
|
use App\ExternalService;
|
|
|
|
use App\Facade\Theme;
|
|
|
|
use App\Facade\UserConfig;
|
2020-04-19 10:54:07 +01:00
|
|
|
use App\Http\Controllers\Controller;
|
2020-04-20 22:33:42 +01:00
|
|
|
use App\Http\Requests\StoreServiceRequest;
|
2020-04-21 08:40:56 +01:00
|
|
|
use App\Services\DropboxService;
|
|
|
|
use App\Storage;
|
2020-04-20 22:33:42 +01:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Facades\App;
|
|
|
|
use Illuminate\Support\Facades\View;
|
2020-04-19 10:54:07 +01:00
|
|
|
|
|
|
|
class ServiceController extends Controller
|
|
|
|
{
|
2020-04-20 22:33:42 +01:00
|
|
|
/**
|
|
|
|
* List of fields that must be encrypted before being saved.
|
|
|
|
*
|
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
private $fieldsToEncrypt;
|
2020-04-19 10:54:07 +01:00
|
|
|
|
2020-04-20 22:33:42 +01:00
|
|
|
/**
|
|
|
|
* 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'];
|
|
|
|
}
|
|
|
|
|
2020-04-21 08:40:56 +01:00
|
|
|
public function authoriseDropbox(Request $request)
|
|
|
|
{
|
|
|
|
$this->authorizeAccessToAdminPanel('admin:manage-storage');
|
|
|
|
|
|
|
|
if (!$request->has('state') && !$request->has('code'))
|
|
|
|
{
|
|
|
|
// TODO flash an error
|
|
|
|
return redirect('storages.index');
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
$storageID = decrypt($request->get('state'));
|
|
|
|
|
|
|
|
$storage = Storage::where('id', intval($storageID))->first();
|
|
|
|
if (is_null($storage))
|
|
|
|
{
|
|
|
|
// TODO flash an error
|
|
|
|
return redirect('storages.index');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_null($storage->externalService))
|
|
|
|
{
|
|
|
|
// TODO flash an error
|
|
|
|
return redirect('storages.index');
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ($storage->externalService->service_type)
|
|
|
|
{
|
|
|
|
case ExternalService::DROPBOX:
|
|
|
|
$dropbox = new DropboxService();
|
|
|
|
$dropbox->handleAuthenticationResponse($request, $storage);
|
|
|
|
// TODO flash a success message
|
|
|
|
return redirect(route('storage.index'));
|
|
|
|
|
|
|
|
default:
|
|
|
|
// TODO flash an error
|
|
|
|
return redirect('storages.index');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (\Exception $ex)
|
|
|
|
{
|
|
|
|
// TODO flash an error
|
|
|
|
return redirect('storages.index');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-20 22:33:42 +01:00
|
|
|
/**
|
|
|
|
* Show the form for creating a new resource.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
2020-04-29 22:19:21 +01:00
|
|
|
public function create(Request $request)
|
2020-04-20 22:33:42 +01:00
|
|
|
{
|
|
|
|
$this->authorizeAccessToAdminPanel('admin:manage-services');
|
|
|
|
|
2020-04-29 22:19:21 +01:00
|
|
|
$serviceTypes = $this->serviceTypeList();
|
|
|
|
$selectedServiceType = old('service_type', $request->get('service_type'));
|
|
|
|
|
|
|
|
if (!array_key_exists($selectedServiceType, $serviceTypes))
|
|
|
|
{
|
|
|
|
$selectedServiceType = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$returnTo = old('return_to', $request->get('return_to'));
|
|
|
|
if (!array_key_exists($returnTo, $this->validReturnLocations()))
|
|
|
|
{
|
|
|
|
$returnTo = '';
|
|
|
|
}
|
|
|
|
|
2020-04-20 22:33:42 +01:00
|
|
|
return Theme::render('admin.create_service', [
|
2020-04-21 08:40:56 +01:00
|
|
|
'callbackUrls' => $this->callbackList(),
|
2020-04-29 22:19:21 +01:00
|
|
|
'returnTo' => $returnTo,
|
|
|
|
'selectedServiceType' => $selectedServiceType,
|
2020-04-20 22:33:42 +01:00
|
|
|
'service' => new ExternalService(),
|
2020-04-29 22:19:21 +01:00
|
|
|
'serviceTypes' => $serviceTypes
|
2020-04-20 22:33:42 +01:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
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', [
|
2020-04-21 08:40:56 +01:00
|
|
|
'callbackUrls' => $this->callbackList(),
|
2020-04-20 22:33:42 +01:00
|
|
|
'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();
|
|
|
|
|
2020-04-29 22:19:21 +01:00
|
|
|
$returnToLocations = $this->validReturnLocations();
|
|
|
|
$returnTo = $request->get('return_to');
|
|
|
|
|
|
|
|
if (array_key_exists($returnTo, $returnToLocations))
|
|
|
|
{
|
|
|
|
return redirect($returnToLocations[$returnTo]);
|
|
|
|
}
|
|
|
|
|
2020-04-20 22:33:42 +01:00
|
|
|
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'));
|
|
|
|
}
|
|
|
|
|
2020-04-21 08:40:56 +01:00
|
|
|
private function callbackList()
|
|
|
|
{
|
|
|
|
$dropboxService = new DropboxService();
|
|
|
|
|
|
|
|
return [
|
2020-04-29 22:19:21 +01:00
|
|
|
ExternalService::DROPBOX => $dropboxService->callbackUrl(),
|
|
|
|
ExternalService::FACEBOOK => route('login_callback.facebook'),
|
|
|
|
ExternalService::GOOGLE => route('login_callback.google'),
|
|
|
|
ExternalService::TWITTER => route('login_callback.twitter')
|
2020-04-21 08:40:56 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-04-20 22:33:42 +01:00
|
|
|
private function isServiceInUse(ExternalService $service)
|
|
|
|
{
|
2020-04-30 08:38:37 +01:00
|
|
|
switch ($service->service_type)
|
|
|
|
{
|
|
|
|
case ExternalService::FACEBOOK:
|
|
|
|
// Cannot delete Facebook service if it's set as the login provider
|
|
|
|
$facebookConfig = Configuration::where('key', 'facebook_external_service_id')->first();
|
|
|
|
return !is_null($facebookConfig) && intval($facebookConfig->value) == $service->id;
|
|
|
|
|
|
|
|
case ExternalService::GOOGLE:
|
|
|
|
// Cannot delete Google service if it's set as the login provider
|
|
|
|
$googleConfig = Configuration::where('key', 'google_external_service_id')->first();
|
|
|
|
return !is_null($googleConfig) && intval($googleConfig->value) == $service->id;
|
|
|
|
|
|
|
|
case ExternalService::DROPBOX:
|
|
|
|
return Storage::where('external_service_id', $service->id)->count() > 0;
|
|
|
|
|
|
|
|
case ExternalService::TWITTER:
|
|
|
|
// Cannot delete Twitter service if it's set as the login provider
|
|
|
|
$twitterConfig = Configuration::where('key', 'twitter_external_service_id')->first();
|
|
|
|
return !is_null($twitterConfig) && intval($twitterConfig->value) == $service->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2020-04-20 22:33:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
|
|
|
];
|
|
|
|
}
|
2020-04-29 22:19:21 +01:00
|
|
|
|
|
|
|
private function validReturnLocations()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'settings' => route('admin.settings')
|
|
|
|
];
|
|
|
|
}
|
2020-04-19 10:54:07 +01:00
|
|
|
}
|