2020-04-16 23:19:21 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\ClientPortal;
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
2020-09-06 11:38:10 +02:00
|
|
|
use App\Models\Invoice;
|
2020-04-16 23:19:21 +02:00
|
|
|
use App\Utils\Traits\MakesHash;
|
|
|
|
use Illuminate\Support\Facades\Hash;
|
|
|
|
|
|
|
|
class EntityViewController extends Controller
|
|
|
|
{
|
|
|
|
use MakesHash;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Available options for viewing.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $entity_types = ['invoice', 'quote'];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the entity outside client portal.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
|
|
|
*/
|
|
|
|
public function index(string $entity_type, string $invitation_key)
|
|
|
|
{
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! in_array($entity_type, $this->entity_types)) {
|
2020-04-16 23:19:21 +02:00
|
|
|
abort(404);
|
|
|
|
}
|
|
|
|
|
|
|
|
$invitation_entity = sprintf('App\\Models\\%sInvitation', ucfirst($entity_type));
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$key = $entity_type.'_id';
|
2020-04-16 23:19:21 +02:00
|
|
|
|
2020-09-07 06:49:57 +02:00
|
|
|
$invitation = $invitation_entity::whereRaw('BINARY `key`= ?', [$invitation_key])
|
|
|
|
->with('contact.client')
|
|
|
|
->firstOrFail();
|
2020-04-16 23:19:21 +02:00
|
|
|
|
|
|
|
$contact = $invitation->contact;
|
2020-09-07 06:49:57 +02:00
|
|
|
$client = $contact->client;
|
|
|
|
$entity = $invitation->{$entity_type};
|
2020-04-16 23:19:21 +02:00
|
|
|
|
|
|
|
if (is_null($contact->password) || empty($contact->password)) {
|
|
|
|
return redirect("/client/password/reset?email={$contact->email}");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((bool) $invitation->contact->client->getSetting('enable_client_portal_password') !== false) {
|
|
|
|
session()->flash("{$entity_type}_VIEW_{$entity->hashed_id}", true);
|
|
|
|
}
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! session("{$entity_type}_VIEW_{$entity->hashed_id}")) {
|
2020-04-16 23:19:21 +02:00
|
|
|
return redirect()->route('client.entity_view.password', compact('entity_type', 'invitation_key'));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->render('view_entity.index', [
|
|
|
|
'root' => 'themes',
|
|
|
|
'entity' => $entity,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for entering password.
|
|
|
|
*
|
|
|
|
* @param string $entity_type
|
|
|
|
* @param string $invitation_key
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
|
|
|
*/
|
|
|
|
public function password(string $entity_type, string $invitation_key)
|
|
|
|
{
|
|
|
|
return $this->render('view_entity.password', [
|
|
|
|
'root' => 'themes',
|
|
|
|
'entity_type' => $entity_type,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**`
|
|
|
|
* Handle the password check.
|
|
|
|
*
|
|
|
|
* @param string $entity_type
|
|
|
|
* @param string $invitation_key
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse|mixed
|
|
|
|
*/
|
|
|
|
public function handlePassword(string $entity_type, string $invitation_key)
|
|
|
|
{
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! in_array($entity_type, $this->entity_types)) {
|
2020-04-16 23:19:21 +02:00
|
|
|
abort(404);
|
|
|
|
}
|
|
|
|
|
|
|
|
$invitation_entity = sprintf('App\\Models\\%sInvitation', ucfirst($entity_type));
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$key = $entity_type.'_id';
|
2020-04-16 23:19:21 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$invitation = $invitation_entity::whereRaw('BINARY `key`= ?', [$invitation_key])->firstOrFail();
|
2020-04-16 23:19:21 +02:00
|
|
|
|
|
|
|
$contact = $invitation->contact;
|
|
|
|
|
|
|
|
$check = Hash::check(request()->password, $contact->password);
|
|
|
|
|
|
|
|
$entity_class = sprintf('App\\Models\\%s', ucfirst($entity_type));
|
|
|
|
|
|
|
|
$entity = $entity_class::findOrFail($invitation->{$key});
|
|
|
|
|
|
|
|
if ($check) {
|
|
|
|
session()->flash("{$entity_type}_VIEW_{$entity->hashed_id}", true);
|
|
|
|
|
|
|
|
return redirect()->route('client.entity_view', compact('entity_type', 'invitation_key'));
|
|
|
|
}
|
|
|
|
|
|
|
|
session()->flash('PASSWORD_FAILED', true);
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-04-16 23:19:21 +02:00
|
|
|
return back();
|
|
|
|
}
|
|
|
|
}
|