1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-11 13:42:49 +01:00
invoiceninja/app/Http/Controllers/ClientPortal/InvitationController.php

114 lines
3.8 KiB
PHP
Raw Normal View History

2019-09-23 07:59:01 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
2019-09-23 07:59:01 +02:00
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
2019-09-23 07:59:01 +02:00
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Http\Controllers\ClientPortal;
2020-08-12 04:02:21 +02:00
use App\Events\Credit\CreditWasViewed;
2020-07-17 03:18:40 +02:00
use App\Events\Invoice\InvoiceWasViewed;
use App\Events\Misc\InvitationWasViewed;
2020-08-12 04:02:21 +02:00
use App\Events\Quote\QuoteWasViewed;
2019-09-23 07:59:01 +02:00
use App\Http\Controllers\Controller;
2020-07-08 14:02:16 +02:00
use App\Utils\Ninja;
2019-09-23 07:59:01 +02:00
use App\Utils\Traits\MakesDates;
use App\Utils\Traits\MakesHash;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
2020-10-09 03:59:59 +02:00
use Illuminate\Support\Str;
2019-09-23 07:59:01 +02:00
/**
* Class InvitationController.
2019-09-23 07:59:01 +02:00
*/
class InvitationController extends Controller
{
use MakesHash;
use MakesDates;
public function router(string $entity, string $invitation_key)
{
Auth::logout();
return $this->genericRouter($entity, $invitation_key);
}
public function recurringRouter(string $invitation_key)
{
return $this->genericRouter('recurring_invoice', $invitation_key);
}
private function genericRouter(string $entity, string $invitation_key)
2019-09-23 07:59:01 +02:00
{
$key = $entity.'_id';
2020-11-11 01:13:39 +01:00
$entity_obj = 'App\Models\\'.ucfirst(Str::camel($entity)).'Invitation';
2019-09-23 07:59:01 +02:00
$invitation = $entity_obj::whereRaw('BINARY `key`= ?', [$invitation_key])
->with('contact.client')
->firstOrFail();
2019-09-23 07:59:01 +02:00
/* Return early if we have the correct client_hash embedded */
2020-11-25 15:19:52 +01:00
if (request()->has('client_hash') && request()->input('client_hash') == $invitation->contact->client->client_hash) {
auth()->guard('contact')->login($invitation->contact, true);
2020-11-25 15:19:52 +01:00
} elseif ((bool) $invitation->contact->client->getSetting('enable_client_portal_password') !== false) {
//If no contact password is set - this will cause a 401 error - instead redirect to the client.login route
$this->middleware('auth:contact');
return redirect()->route('client.login');
2020-11-25 15:19:52 +01:00
} else {
auth()->guard('contact')->login($invitation->contact, true);
}
if (auth()->guard('contact') && ! request()->has('silent') && ! $invitation->viewed_date) {
$invitation->markViewed();
2020-07-17 03:18:40 +02:00
event(new InvitationWasViewed($invitation->{$entity}, $invitation, $invitation->{$entity}->company, Ninja::eventVars()));
$this->fireEntityViewedEvent($invitation, $entity);
}
return redirect()->route('client.'.$entity.'.show', [$entity => $this->encodePrimaryKey($invitation->{$key})]);
}
2020-07-17 03:18:40 +02:00
private function fireEntityViewedEvent($invitation, $entity_string)
{
switch ($entity_string) {
case 'invoice':
$invitation->invoice->service()->markSent()->save();
2020-07-17 03:18:40 +02:00
event(new InvoiceWasViewed($invitation, $invitation->company, Ninja::eventVars()));
break;
2020-08-12 04:02:21 +02:00
case 'quote':
$invitation->quote->service()->markSent()->save();
2020-08-12 04:02:21 +02:00
event(new QuoteWasViewed($invitation, $invitation->company, Ninja::eventVars()));
break;
case 'credit':
$invitation->credit->service()->markSent()->save();
2020-08-12 04:02:21 +02:00
event(new CreditWasViewed($invitation, $invitation->company, Ninja::eventVars()));
break;
2020-07-17 03:18:40 +02:00
default:
// code...
2020-07-17 03:18:40 +02:00
break;
}
}
public function routerForDownload(string $entity, string $invitation_key)
{
return redirect('client/'.$entity.'/'.$invitation_key.'/download_pdf');
2019-09-23 07:59:01 +02:00
}
public function routerForIframe(string $entity, string $client_hash, string $invitation_key)
{
}
2019-09-23 07:59:01 +02:00
}