1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-20 08:21:34 +02:00

Allow login using contact_key or client_hash

This commit is contained in:
David Bomba 2020-09-30 08:42:55 +10:00
parent 9b546e66ac
commit 8756a89885
11 changed files with 61 additions and 9 deletions

View File

@ -589,7 +589,7 @@ class CompanySettings extends BaseSettings
'$credit.po_number',
'$credit.date',
'$credit.balance',
'$credit.amount',
'$credit.total',
],
'product_columns' => [
'$product.product_key',

View File

@ -336,7 +336,7 @@ class Designer
'$credit.po_number' => '<span class="flex justify-between items-center">$credit.po_number_label<span></span><span>$credit.po_number</span></span>',
'$credit.date' => '<span class="flex justify-between items-center">$credit.date_label<span></span><span>$credit.date</span></span>',
'$credit.balance' => '<span class="flex justify-between items-center">$credit.balance_label<span></span><span>$credit.balance</span></span>',
'$credit.amount' => '<span class="flex justify-between items-center">$credit.amount_label<span></span><span>$credit.amount</span></span>',
'$credit.total' => '<span class="flex justify-between items-center">$credit.total_label<span></span><span>$credit.total</span></span>',
'$credit.partial_due' => '<span class="flex justify-between items-center">$credit.partial_due_label<span></span><span>$credit.partial_due</span></span>',
'$credit.custom1' => '<span class="flex justify-between items-center">$credit.custom1_label<span></span><span>$credit.custom1</span></span>',
'$credit.custom2' => '<span class="flex justify-between items-center">$credit.custom2_label<span></span><span>$credit.custom2</span></span>',

View File

@ -12,16 +12,21 @@
namespace App\Http\Middleware;
use App\Libraries\MultiDB;
use App\Models\Client;
use App\Models\ClientContact;
use App\Models\CompanyToken;
use Closure;
use Auth;
use Closure;
class ContactKeyLogin
{
/**
* Handle an incoming request.
*
* Sets a contact LOGGED IN if an appropriate client_hash is provided as a query parameter
* OR
* If the contact_key is provided in the route
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
@ -47,6 +52,25 @@ class ContactKeyLogin
return redirect()->to('client/dashboard');
}
}
else if($request->has('client_hash') && config('ninja.db.multi_db_enabled')){
if (MultiDB::findAndSetDbByClientHash($request->input('client_hash'))) {
$client = Client::where('client_hash', $request->input('client_hash'))->first();
Auth::guard('contact')->login($client->primary_contact()->first(), true);
return redirect()->to('client/dashboard');
}
}
else if($request->has('client_hash')){
if($client = Client::where('client_hash', $request->input('client_hash'))->first()){
Auth::guard('contact')->login($client->primary_contact()->first(), true);
return redirect()->to('client/dashboard');
}
}
return $next($request);

View File

@ -100,7 +100,7 @@ class StoreRecurringInvoiceRequest extends Request
if(isset($input['auto_bill']))
$input['auto_bill_enabled'] = $this->setAutoBillFlag($input['auto_bill']);
else{
$client = Client::find($this->decodePrimaryKey($input['client_id']));
$client = Client::find($input['client_id']);
$input['auto_bill'] = $client->getSetting('auto_bill');
}

View File

@ -11,6 +11,7 @@
namespace App\Libraries;
use App\Models\Client;
use App\Models\ClientContact;
use App\Models\Company;
use App\Models\CompanyToken;
@ -200,7 +201,6 @@ class MultiDB
foreach (self::$dbs as $db) {
if ($client_contact = ClientContact::on($db)->where('contact_key', $contact_key)->first()) {
self::setDb($client_contact->company->db);
return true;
}
}
@ -208,6 +208,17 @@ class MultiDB
return false;
}
public static function findAndSetDbByClientHash($client_hash) :bool
{
foreach (self::$dbs as $db) {
if ($client = Client::on($db)->where('client_hash', $client_hash)->first()) {
self::setDb($client->company->db);
return true;
}
}
return false;
}
public static function findAndSetDbByDomain($subdomain) :bool
{

View File

@ -198,4 +198,20 @@ class ClientContact extends Authenticatable implements HasLocalePreference
return asset('images/svg/user.svg');
}
/**
* Provides a convenience login click for contacts to bypass the
* contact authentication layer
*
* @return string URL
*/
public function getLoginLink()
{
$domain = isset($this->company->portal_domain) ?: $this->company->domain();
return $domain . 'client/key_login/' . $this->contact_key;
}
}

View File

@ -426,7 +426,7 @@ class Company extends BaseModel
public function domain()
{
if (Ninja::isNinja()) {
return $this->subdomain.config('ninja.app_domain');
return $this->subdomain . config('ninja.app_domain');
}
return config('ninja.app_url');

View File

@ -47,6 +47,7 @@ class ClientContactTransformer extends EntityTransformer
'send_email' => (bool) $contact->send_email,
'last_login' => (int) $contact->last_login,
'password' => empty($contact->password) ? '' : '**********',
'link' => $contact->getLoginLink(),
];
}
}

View File

@ -162,7 +162,7 @@ class HtmlEngine
$data['$quote.amount'] = ['value' => Number::formatMoney($this->entity_calc->getTotal(), $this->client) ?: '&nbsp;', 'label' => ctrans('texts.quote_total')];
$data['$credit.total'] = ['value' => Number::formatMoney($this->entity_calc->getTotal(), $this->client) ?: '&nbsp;', 'label' => ctrans('texts.credit_total')];
$data['$credit.number'] = ['value' => $this->entity->number ?: '&nbsp;', 'label' => ctrans('texts.credit_number')];
$data['$credit.amount'] = &$data['$credit.total'];
$data['$credit.total'] = &$data['$credit.total'];
$data['$credit.po_number'] = &$data['$invoice.po_number'];
$data['$credit.date'] = ['value' => $this->entity->date, 'label' => ctrans('texts.credit_date')];
$data['$balance'] = ['value' => Number::formatMoney($this->entity_calc->getBalance(), $this->client) ?: '&nbsp;', 'label' => ctrans('texts.balance')];

View File

@ -253,7 +253,7 @@ trait MakesInvoiceValues
$data['$quote.amount'] = ['value' => Number::formatMoney($calc->getTotal(), $this->client) ?: '&nbsp;', 'label' => ctrans('texts.quote_total')];
$data['$credit.total'] = ['value' => Number::formatMoney($calc->getTotal(), $this->client) ?: '&nbsp;', 'label' => ctrans('texts.credit_total')];
$data['$credit.number'] = ['value' => $this->number ?: '&nbsp;', 'label' => ctrans('texts.credit_number')];
$data['$credit.amount'] = &$data['$credit.total'];
$data['$credit.total'] = &$data['$credit.total'];
$data['$credit.po_number'] = &$data['$invoice.po_number'];
$data['$credit.date'] = ['value' => $this->date, 'label' => ctrans('texts.credit_date')];
$data['$balance'] = ['value' => Number::formatMoney($calc->getBalance(), $this->client) ?: '&nbsp;', 'label' => ctrans('texts.balance')];

View File

@ -96,7 +96,7 @@ trait MakesTemplateData
$data['$quote_total'] = ['value' => '$100.00', 'label' => ctrans('texts.quote_total')];
$data['$quote.amount'] = &$data['$quote_total'];
$data['$credit_total'] = ['value' => '$100.00', 'label' => ctrans('texts.credit_total')];
$data['$credit.amount'] = &$data['$credit_total'];
$data['$credit.total'] = &$data['$credit_total'];
$data['$balance'] = ['value' => '$100.00', 'label' => ctrans('texts.balance')];
$data['$invoice.balance'] = &$data['$balance'];
$data['$taxes'] = ['value' => '$10.00', 'label' => ctrans('texts.taxes')];