1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 05:02:36 +01:00

GMail credentials notification

This commit is contained in:
David Bomba 2022-03-24 08:34:52 +11:00
parent a41468816e
commit 3e8c1b7910
6 changed files with 210 additions and 9 deletions

View File

@ -210,15 +210,8 @@ class NinjaMailerJob implements ShouldQueue
$user = $user->fresh();
}
//17-01-2022 - ensure we have a token otherwise we fail gracefully to default sending engine
// if(strlen($user->oauth_user_token) == 0){
// $this->nmo->settings->email_sending_method = 'default';
// return $this->setMailDriver();
// }
$google->getClient()->setAccessToken(json_encode($user->oauth_user_token));
//need to slow down gmail requests otherwise we hit 429's
sleep(rand(2,6));
}
catch(\Exception $e) {
@ -227,6 +220,16 @@ class NinjaMailerJob implements ShouldQueue
return $this->setMailDriver();
}
/**
* If the user doesn't have a valid token, notify them
*/
if(!$user->oauth_user_token) {
$this->company->account->gmailCredentialNotification();
return;
}
/*
* Now that our token is refreshed and valid we can boot the
* mail driver at runtime and also set the token which will persist

View File

@ -0,0 +1,64 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Mail\Ninja;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\App;
class GmailTokenInvalid extends Mailable
{
public $company;
public $settings;
public $logo;
public $title;
public $body;
public $whitelabel;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct($company)
{
$this->company = $company;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
App::setLocale($this->company->getLocale());
$this->settings = $this->company->settings;
$this->logo = $this->company->present()->logo();
$this->title = ctrans('texts.gmail_credentials_invalid_subject');
$this->body = ctrans('texts.gmail_credentials_invalid_body');
$this->whitelabel = $this->company->account->isPaid();
$this->replyTo('contact@invoiceninja.com', 'Contact');
return $this->from(config('mail.from.address'), config('mail.from.name'))
->subject(ctrans('texts.gmail_credentials_invalid_subject'))
->view('email.admin.email_quota_exceeded');
}
}

View File

@ -14,8 +14,10 @@ namespace App\Models;
use App\Jobs\Mail\NinjaMailerJob;
use App\Jobs\Mail\NinjaMailerObject;
use App\Mail\Ninja\EmailQuotaExceeded;
use App\Mail\Ninja\GmailTokenInvalid;
use App\Models\Presenters\AccountPresenter;
use App\Notifications\Ninja\EmailQuotaNotification;
use App\Notifications\Ninja\GmailCredentialNotification;
use App\Utils\Ninja;
use App\Utils\Traits\MakesHash;
use Carbon\Carbon;
@ -424,4 +426,43 @@ class Account extends BaseModel
return false;
}
public function gmailCredentialNotification() :bool
{
if(is_null(Cache::get($this->key)))
return false;
try {
if(is_null(Cache::get("gmail_credentials_notified:{$this->key}"))) {
App::forgetInstance('translator');
$t = app('translator');
$t->replace(Ninja::transformTranslations($this->companies()->first()->settings));
$nmo = new NinjaMailerObject;
$nmo->mailable = new GmailTokenInvalid($this->companies()->first());
$nmo->company = $this->companies()->first();
$nmo->settings = $this->companies()->first()->settings;
$nmo->to_user = $this->companies()->first()->owner();
NinjaMailerJob::dispatch($nmo);
Cache::put("gmail_credentials_notified:{$this->key}", true, 60 * 24);
if(config('ninja.notification.slack'))
$this->companies()->first()->notification(new GmailCredentialNotification($this))->ninja();
}
return true;
}
catch(\Exception $e){
\Sentry\captureMessage("I encountered an error with sending with gmail for account {$this->key}");
}
return false;
}
}

View File

@ -0,0 +1,88 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Notifications\Ninja;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Messages\SlackMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class GmailCredentialNotification extends Notification
{
/**
* Create a new notification instance.
*
* @return void
*/
protected $account;
public function __construct($account)
{
$this->account = $account;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['slack'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return MailMessage
*/
public function toMail($notifiable)
{
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
public function toSlack($notifiable)
{
$content = "GMail credentials invalid for Account {$this->account->key} \n";
$owner = $this->account->companies()->first()->owner();
$content .= "Owner {$owner->present()->name() } | {$owner->email}";
return (new SlackMessage)
->success()
->from(ctrans('texts.notification_bot'))
->image('https://app.invoiceninja.com/favicon.png')
->content($content);
}
}

View File

@ -29,12 +29,16 @@ class GetInvoicePdf extends AbstractService
public function run()
{
if (! $this->contact) {
$this->contact = $this->invoice->client->primary_contact()->first() ?: $this->invoice->client->contacts()->first();
}
$invitation = $this->invoice->invitations->where('client_contact_id', $this->contact->id)->first();
if(!$invitation)
$invitation = $this->invoice->invitations->first();
$path = $this->invoice->client->invoice_filepath($invitation);
$file_path = $path.$this->invoice->numberFormatter().'.pdf';
@ -48,8 +52,7 @@ class GetInvoicePdf extends AbstractService
$file_path = CreateEntityPdf::dispatchNow($invitation);
}
// return Storage::disk($disk)->path($file_path);
//
return $file_path;
}
}

View File

@ -4570,6 +4570,8 @@ $LANG = array(
'credits_backup_subject' => 'Your credits are ready for download',
'document_download_subject' => 'Your documents are ready for download',
'reminder_message' => 'Reminder for invoice :number for :balance',
'gmail_credentials_invalid_subject' => 'Send with GMail invalid credentials',
'gmail_credentials_invalid_body' => 'Your GMail credentials are not correct, please log into the administrator portal and navigate to Settings > User Details and disconnect and reconnect your GMail account. We will send you this notification daily until this issue is resolved',
);
return $LANG;