/** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed * @throws RedirectException */ public function handle($request, Closure $next) { $response = $next($request); $source = $request->get('source', 'api_data'); $filename = $request->get('filename', 'export_' . date('Y-m-d_Hi')); if ($request->get('download') && array_key_exists($source, $response->original->getData())) { switch ($request->get('download')) { case 'json': return response()->json($response->original->getData()[$source], 200, ['Content-Disposition' => 'attachment; filename="' . $filename . '.json"']); case 'csv': $writer = Writer::createFromFileObject(new \SplTempFileObject()); $writer->setDelimiter(','); $writer->setNewline("\r\n"); $writer->setEncodingFrom("utf-8"); $headers = ['Content-Type' => 'text/csv', 'Content-Disposition' => 'attachment; filename="' . $filename . '.csv"']; $csv_headers_set = false; foreach ($response->original->getData()[$source] as $data) { if (!$csv_headers_set) { $writer->insertOne(array_keys($this->getArrayRepresentation($data))); $csv_headers_set = true; } $writer->insertOne($this->processData($this->getArrayRepresentation($data))); } return response()->make($writer, 200, $headers); default: throw RedirectException::make('/')->setError('Unrecognised type to download'); } } return $response; }
/** * Store a newly created resource in storage. * * @param Request $request * @return \Illuminate\Http\RedirectResponse * @throws RedirectException */ public function store(Request $request) { $this->validate($request, ['reference' => 'required|regex:/^[A-Za-z0-9\\-]+$/', 'installation_id' => 'required', 'name' => 'required', 'email' => 'required|max:255', 'address' => 'required']); try { $this->validateEmailAddressInput($request); $toCreate = $request->all(); $toCreate['active'] = $request->has('active') ? 1 : 0; Location::create($toCreate); } catch (\Exception $e) { $this->logError('Could not successfully create new Location' . $e->getMessage()); throw RedirectException::make('/locations/')->setError($e->getMessage()); } return $this->redirectWithSuccessMessage('locations', 'New location has been successfully created'); }
/** * @author EB * @param Request $request * @return \Illuminate\Http\RedirectResponse * @throws RedirectException */ public function changePassword(Request $request) { $user = $this->getAuthenticatedUser(); $this->validate($request, ['old_password' => 'required', 'new_password' => 'required|confirmed|different:old_password', 'new_password_confirmation' => 'required|different:old_password|same:new_password']); if (!Hash::check($request->get("old_password"), $user->getAuthPassword())) { throw RedirectException::make('account/edit')->setError('Old password must match stored password'); } try { $user->password = Hash::make($request['new_password']); $user->save(); } catch (\Exception $e) { $this->logError('AccountController: Error while trying to change password: '******'/account/edit')->setError($e->getMessage()); } return $this->redirectWithSuccessMessage('/account/edit', 'Your password has successfully been changed'); }
/** * Index * * @author MS * @param int $id * @return \Illuminate\View\View * @throws \App\Exceptions\RedirectException */ public function index($id) { $dateRange = $this->getDateRange(); try { $settlementReports = Collection::make($this->settlementGateway->getSettlementReports($this->fetchMerchantById($id)->token, $dateRange['date_from'], $dateRange['date_to'])); } catch (\Exception $e) { $this->logError('SettlementsController: failed fetching settlements' . $e->getMessage()); throw RedirectException::make('/')->setError('Problem fetching Settlements.'); } $filter = $this->getFilters(); if (!$filter->isEmpty()) { $settlementReports = $settlementReports->filter(function ($settlement_reports) use($filter) { if ($settlement_reports['provider'] == $filter['provider']) { return true; } }); } $local = []; foreach ($settlementReports as $key => $report) { $settlementReports[$key] = (object) $report; $local[$report['id']] = Application::where('ext_id', '=', $report['id'])->first(); } return View('settlements.index', ['settlement_reports' => $settlementReports, 'default_dates' => $this->getDateRange(), 'provider' => $this->fetchFilterValues($settlementReports, 'provider'), 'local' => $local]); }
/** * Remove the specified resource from storage. * * @author WN * @param int $id * @return \Illuminate\Http\RedirectResponse * @throws RedirectException */ public function destroy($id) { if ($id == $this->getAuthenticatedUser()->id) { throw RedirectException::make('/')->setError('You cannot delete yourself!'); } try { return $this->destroyModel(new User(), $id, 'user', '/users'); } catch (\Exception $e) { Log::error('Problem deleting user [' . $id . ']: ' . $e->getMessage()); throw RedirectException::make('/users/' . $id)->setError('There was a problem deleting the selected user. If this error persists, please contact afforditNOW! Support.'); } }
/** * Checks for token duplication in the database,returns true if there is no duplication detected. * * @author EA * @param $token * @param $merchantName * @return bool * @throws RedirectException */ public function validateMerchantToken($token, $merchantName) { $duplicatedTokens = Merchant::all()->where('token', $token); if (!$duplicatedTokens->isEmpty()) { $this->logError('Cannot create merchant[' . $merchantName . '] merchant:Merchant token already exist in database'); throw RedirectException::make('/merchants')->setError('Invalid merchant token '); } return true; }
/** * @author WN * @param $id * @return Location * @throws RedirectException */ protected function fetchLocation($id) { $location = $this->fetchModelByIdWithInstallationLimit(new Location(), $id, 'location', '/locations'); if (!in_array($id, $this->getAuthenticatedUser()->locations->pluck('id')->all())) { throw RedirectException::make('/')->setError('You don\'t have permission to access this Location'); } return $location; }
/** * @author EB * @param Request $request * @param Location $location * @return bool * @throws RedirectException */ private function validateApplicationRequest(Request $request, Location $location) { /** @var Application $application */ if ($application = Application::where('ext_order_reference', '=', $request->get('reference'))->where('installation_id', '=', $location->installation->id)->first()) { throw RedirectException::make('/locations/' . $location->id . '/applications/make')->setError('Unable to process the request, an application has already been created with this order reference (<a href="/installations/' . $location->installation->id . '/applications/' . $application->id . '">' . $application->ext_order_reference . '</a>)'); } return true; }
/** * Remove the specified resource from storage. * * @author WN, EB * @param int $id * @return \Illuminate\Http\RedirectResponse * @throws RedirectException */ public function destroy($id) { $role = $this->fetchRoleById($id); if ($role->name == self::SUPER_USER_NAME || $role->name == self::READ_ONLY_NAME) { throw RedirectException::make('/')->setError('Cannot delete ' . $role->name . ', it\'s a special role!'); } return $this->destroyModel(new Role(), $id, 'role', '/roles'); }
/** * Update the specified resource in storage. * * @author WN * @param int $id * @param Request $request * @return Response * @throws RedirectException */ public function update($id, Request $request) { $this->amendValidityPeriod($request); $this->validate($request, ['name' => 'required|max:255', 'active' => 'required|sometimes', 'validity' => 'required|numeric|between:7200,2592000', 'custom_logo_url' => 'url|max:255', 'email_reply_to' => 'email|max:255', 'ext_return_url' => 'url|max:255', 'ext_notification_url' => 'url|max:255', 'finance_offers' => 'required|integer']); $old = new Installation(); $old = $old->findOrFail($id); try { $request->merge(['email_configuration' => $this->getEmailConfigurationFromParams($request)]); if ($old->ext_notification_url !== $request->ext_notification_url || $old->ext_return_url !== $request->ext_return_url) { $this->installationGateway->patchInstallation($this->fetchInstallation($id)->ext_id, ['return_url' => $request->ext_return_url, 'notification_url' => $request->ext_notification_url], $this->fetchInstallation($id)->merchant->token); } } catch (\Exception $e) { throw RedirectException::make('/installations/' . $id . '/edit')->setError($e->getMessage()); } return $this->updateModel(new Installation(), $id, 'installation', '/installations', $request); }
/** * @author WN * @param string $action * @param $id * @return \Illuminate\View\View * @throws RedirectException */ private function renderConfirmationScreen($action, $id, $installation) { $application = $this->fetchApplicationById($id, $installation); if (!$this->isCancellable($application) && $action == 'cancellation' || !$this->isFulfilable($application) && $action == 'fulfilment') { Log::error('Application is not allowed to request ' . $action); throw RedirectException::make('/installations/' . $installation . '/applications/' . $id)->setError('Application is not allowed to request ' . $action); } return view('applications.' . $action, ['application' => $application]); }
/** * @author WN * @param Model $entity * @param int $merchantId * @param string $redirect * @param string $modelName * @return Model * @throws RedirectException */ protected function checkModelForMerchantLimit(Model $entity, $merchantId, $modelName, $redirect) { if (!$this->isMerchantAllowedForUser($merchantId)) { throw RedirectException::make($redirect)->setError('You are not allowed to take any action on this ' . ucwords($modelName)); } return $entity; }