middleware('auth'); View::share('is_admin', true); $this->serviceTypeDependentFields = ['app_id', 'app_secret']; $this->fieldsToEncrypt = ['app_id', 'app_secret']; } 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'); } } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create(Request $request) { $this->authorizeAccessToAdminPanel('admin:manage-services'); $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 = ''; } return Theme::render('admin.create_service', [ 'callbackUrls' => $this->callbackList(), 'returnTo' => $returnTo, 'selectedServiceType' => $selectedServiceType, 'service' => new ExternalService(), 'serviceTypes' => $serviceTypes ]); } 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', [ 'callbackUrls' => $this->callbackList(), '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(); $returnToLocations = $this->validReturnLocations(); $returnTo = $request->get('return_to'); if (array_key_exists($returnTo, $returnToLocations)) { return redirect($returnToLocations[$returnTo]); } 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 callbackList() { $dropboxService = new DropboxService(); return [ ExternalService::DROPBOX => $dropboxService->callbackUrl(), ExternalService::FACEBOOK => route('login_callback.facebook'), ExternalService::GOOGLE => route('login_callback.google'), ExternalService::TWITTER => route('login_callback.twitter') ]; } 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)) ]; } private function validReturnLocations() { return [ 'settings' => route('admin.settings') ]; } }