Merge pull request #4957 from turbo124/v5-develop

Harvest PostMark message ID
This commit is contained in:
David Bomba 2021-02-22 11:58:50 +11:00 committed by GitHub
commit 4e7b2ffab1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 242 additions and 13 deletions

View File

@ -99,21 +99,21 @@ class Handler extends ExceptionHandler
private function validException($exception) private function validException($exception)
{ {
if (strpos($exception->getMessage(), 'file_put_contents') !== false) { if (strpos($exception->getMessage(), 'file_put_contents') !== false)
return false; return false;
}
if (strpos($exception->getMessage(), 'Permission denied') !== false) { if (strpos($exception->getMessage(), 'Permission denied') !== false)
return false; return false;
}
if (strpos($exception->getMessage(), 'flock()') !== false) { if (strpos($exception->getMessage(), 'flock()') !== false)
return false; return false;
}
if (strpos($exception->getMessage(), 'expects parameter 1 to be resource') !== false) { if (strpos($exception->getMessage(), 'expects parameter 1 to be resource') !== false)
return false; return false;
}
if (strpos($exception->getMessage(), 'fwrite()') !== false)
return false;
return true; return true;
} }

View File

@ -0,0 +1,139 @@
<?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://opensource.org/licenses/AAL
*/
namespace App\Http\Controllers;
use App\Libraries\MultiDB;
use App\Libraries\OAuth\Providers\Google;
use Illuminate\Http\Request;
class ConnectedAccountController extends BaseController
{
public function __construct()
{
parent::__construct();
}
/**
* Connect an OAuth account to a regular email/password combination account
*
* @param Request $request
* @return User Refresh Feed.
*
*
* @OA\Post(
* path="/api/v1/connected_account",
* operationId="connected_account",
* tags={"connected_account"},
* summary="Connect an oauth user to an existing user",
* description="Refreshes the dataset",
* @OA\Parameter(ref="#/components/parameters/X-Api-Secret"),
* @OA\Parameter(ref="#/components/parameters/X-Api-Token"),
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Parameter(ref="#/components/parameters/include_static"),
* @OA\Parameter(ref="#/components/parameters/clear_cache"),
* @OA\Response(
* response=200,
* description="The Company User response",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
* @OA\JsonContent(ref="#/components/schemas/User"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
* ),
* @OA\Response(
* response="default",
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*/
public function index(Request $request)
{
if ($request->input('provider') == 'google') {
return $this->handleGoogleOauth();
}
return response()
->json(['message' => 'Provider not supported'], 400)
->header('X-App-Version', config('ninja.app_version'))
->header('X-Api-Version', config('ninja.minimum_client_version'));
}
private function handleGoogleOauth()
{
$user = false;
$google = new Google();
$user = $google->getTokenResponse(request()->input('id_token'));
if (is_array($user)) {
$query = [
'oauth_user_id' => $google->harvestSubField($user),
'oauth_provider_id'=> 'google',
];
/* Cannot allow duplicates! */
if ($existing_user = MultiDB::hasUser($query)) {
return response()
->json(['message' => 'User already exists in system.'], 401)
->header('X-App-Version', config('ninja.app_version'))
->header('X-Api-Version', config('ninja.minimum_client_version'));
}
}
if ($user) {
$client = new Google_Client();
$client->setClientId(config('ninja.auth.google.client_id'));
$client->setClientSecret(config('ninja.auth.google.client_secret'));
$client->setRedirectUri(config('ninja.app_url'));
$token = $client->authenticate(request()->input('server_auth_code'));
$refresh_token = '';
if (array_key_exists('refresh_token', $token)) {
$refresh_token = $token['refresh_token'];
}
$connected_account = [
'password' => '',
'email' => $google->harvestEmail($user),
'oauth_user_id' => $google->harvestSubField($user),
'oauth_user_token' => $token,
'oauth_user_refresh_token' => $refresh_token,
'oauth_provider_id' => 'google',
'email_verified_at' =>now()
];
auth()->user()->update($connected_account);
auth()->user()->email_verified_at = now();
auth()->user()->save();
//$ct = CompanyUser::whereUserId(auth()->user()->id);
return $this->listResponse(auth()->user());
}
return response()
->json(['message' => ctrans('texts.invalid_credentials')], 401)
->header('X-App-Version', config('ninja.app_version'))
->header('X-Api-Version', config('ninja.minimum_client_version'));
}
}

View File

@ -105,7 +105,7 @@ class EmailEntity implements ShouldQueue
MultiDB::setDB($this->company->db); MultiDB::setDB($this->company->db);
$nmo = new NinjaMailerObject; $nmo = new NinjaMailerObject;
$nmo->mailable = new TemplateEmail($this->email_entity_builder,$this->invitation->contact); $nmo->mailable = new TemplateEmail($this->email_entity_builder,$this->invitation->contact, $this->invitation);
$nmo->company = $this->company; $nmo->company = $this->company;
$nmo->settings = $this->settings; $nmo->settings = $this->settings;
$nmo->to_user = $this->invitation->contact; $nmo->to_user = $this->invitation->contact;

View File

@ -0,0 +1,58 @@
<?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://opensource.org/licenses/AAL
*/
namespace App\Listeners\Mail;
use App\Libraries\MultiDB;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Notification;
use Illuminate\Mail\Events\MessageSent;
class MailSentListener implements ShouldQueue
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
}
/**
* Handle the event.
*
* @param object $event
* @return void
*/
public function handle(MessageSent $event)
{
if(property_exists($event->message, 'invitation')){
MultiDB::setDb($event->message->invitation->company->db);
if($event->message->getHeaders()->get('x-pm-message-id')){
$postmark_id = $event->message->getHeaders()->get('x-pm-message-id')->getValue();
nlog($postmark_id);
$invitation = $event->message->invitation;
$invitation->message_id = $postmark_id;
$invitation->save();
}
}
}
}

View File

@ -31,6 +31,8 @@ class BaseEmailEngine implements EngineInterface
public $text; public $text;
public $invitation;
public function setFooter($footer) public function setFooter($footer)
{ {
$this->footer = $footer; $this->footer = $footer;
@ -141,4 +143,15 @@ class BaseEmailEngine implements EngineInterface
public function build() public function build()
{ {
} }
public function setInvitation($invitation)
{
$this->invitation = $invitation;
} }
public function getInvitation()
{
return $this->invitation;
}
}

View File

@ -85,7 +85,8 @@ class CreditEmailEngine extends BaseEmailEngine
->setBody($body_template) ->setBody($body_template)
->setFooter("<a href='{$this->invitation->getLink()}'>".ctrans('texts.view_credit').'</a>') ->setFooter("<a href='{$this->invitation->getLink()}'>".ctrans('texts.view_credit').'</a>')
->setViewLink($this->invitation->getLink()) ->setViewLink($this->invitation->getLink())
->setViewText(ctrans('texts.view_credit')); ->setViewText(ctrans('texts.view_credit'))
->setInvitation($this->invitation);
if ($this->client->getSetting('pdf_email_attachment') !== false) { if ($this->client->getSetting('pdf_email_attachment') !== false) {
$this->setAttachments(['path' => $this->credit->pdf_file_path(), 'name' => basename($this->credit->pdf_file_path())]); $this->setAttachments(['path' => $this->credit->pdf_file_path(), 'name' => basename($this->credit->pdf_file_path())]);

View File

@ -94,7 +94,8 @@ class InvoiceEmailEngine extends BaseEmailEngine
->setBody($body_template) ->setBody($body_template)
->setFooter("<a href='{$this->invitation->getLink()}'>".ctrans('texts.view_invoice').'</a>') ->setFooter("<a href='{$this->invitation->getLink()}'>".ctrans('texts.view_invoice').'</a>')
->setViewLink($this->invitation->getLink()) ->setViewLink($this->invitation->getLink())
->setViewText(ctrans('texts.view_invoice')); ->setViewText(ctrans('texts.view_invoice'))
->setInvitation($this->invitation);
if ($this->client->getSetting('pdf_email_attachment') !== false) { if ($this->client->getSetting('pdf_email_attachment') !== false) {
$this->setAttachments([$this->invoice->pdf_file_path()]); $this->setAttachments([$this->invoice->pdf_file_path()]);

View File

@ -85,7 +85,9 @@ class QuoteEmailEngine extends BaseEmailEngine
->setBody($body_template) ->setBody($body_template)
->setFooter("<a href='{$this->invitation->getLink()}'>".ctrans('texts.view_quote').'</a>') ->setFooter("<a href='{$this->invitation->getLink()}'>".ctrans('texts.view_quote').'</a>')
->setViewLink($this->invitation->getLink()) ->setViewLink($this->invitation->getLink())
->setViewText(ctrans('texts.view_quote')); ->setViewText(ctrans('texts.view_quote'))
->setInvitation($this->invitation);
if ($this->client->getSetting('pdf_email_attachment') !== false) { if ($this->client->getSetting('pdf_email_attachment') !== false) {
// $this->setAttachments([$this->quote->pdf_file_path()]); // $this->setAttachments([$this->quote->pdf_file_path()]);

View File

@ -29,7 +29,9 @@ class TemplateEmail extends Mailable
private $company; private $company;
public function __construct($build_email, ClientContact $contact) private $invitation;
public function __construct($build_email, ClientContact $contact, $invitation = null)
{ {
$this->build_email = $build_email; $this->build_email = $build_email;
@ -38,6 +40,8 @@ class TemplateEmail extends Mailable
$this->client = $contact->client; $this->client = $contact->client;
$this->company = $contact->company; $this->company = $contact->company;
$this->invitation = $invitation;
} }
public function build() public function build()
@ -77,6 +81,7 @@ class TemplateEmail extends Mailable
]) ])
->withSwiftMessage(function ($message) use($company){ ->withSwiftMessage(function ($message) use($company){
$message->getHeaders()->addTextHeader('Tag', $company->company_key); $message->getHeaders()->addTextHeader('Tag', $company->company_key);
$message->invitation = $this->invitation;
}); });
//conditionally attach files //conditionally attach files

View File

@ -136,6 +136,7 @@ use App\Listeners\Invoice\InvoiceRestoredActivity;
use App\Listeners\Invoice\InvoiceReversedActivity; use App\Listeners\Invoice\InvoiceReversedActivity;
use App\Listeners\Invoice\InvoiceViewedActivity; use App\Listeners\Invoice\InvoiceViewedActivity;
use App\Listeners\Invoice\UpdateInvoiceActivity; use App\Listeners\Invoice\UpdateInvoiceActivity;
use App\Listeners\Mail\MailSentListener;
use App\Listeners\Misc\InvitationViewedListener; use App\Listeners\Misc\InvitationViewedListener;
use App\Listeners\Payment\PaymentEmailFailureActivity; use App\Listeners\Payment\PaymentEmailFailureActivity;
use App\Listeners\Payment\PaymentEmailedActivity; use App\Listeners\Payment\PaymentEmailedActivity;
@ -157,6 +158,8 @@ use App\Listeners\User\RestoredUserActivity;
use App\Listeners\User\UpdateUserLastLogin; use App\Listeners\User\UpdateUserLastLogin;
use App\Listeners\User\UpdatedUserActivity; use App\Listeners\User\UpdatedUserActivity;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider; use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Illuminate\Mail\Events\MessageSending;
use Illuminate\Mail\Events\MessageSent;
class EventServiceProvider extends ServiceProvider class EventServiceProvider extends ServiceProvider
{ {
@ -166,6 +169,11 @@ class EventServiceProvider extends ServiceProvider
* @var array * @var array
*/ */
protected $listen = [ protected $listen = [
MessageSending::class =>[
],
MessageSent::class => [
MailSentListener::class,
],
UserWasCreated::class => [ UserWasCreated::class => [
CreatedUserActivity::class, CreatedUserActivity::class,
SendVerificationNotification::class, SendVerificationNotification::class,

View File

@ -36,6 +36,8 @@ Route::group(['middleware' => ['api_db', 'token_auth', 'locale'], 'prefix' => 'a
Route::put('clients/{client}/upload', 'ClientController@upload')->name('clients.upload'); Route::put('clients/{client}/upload', 'ClientController@upload')->name('clients.upload');
Route::post('clients/bulk', 'ClientController@bulk')->name('clients.bulk'); Route::post('clients/bulk', 'ClientController@bulk')->name('clients.bulk');
Route::post('connected_account', 'ConnectedAccountController@index');
Route::resource('client_statement', 'ClientStatementController@statement'); // name = (client_statement. index / create / show / update / destroy / edit Route::resource('client_statement', 'ClientStatementController@statement'); // name = (client_statement. index / create / show / update / destroy / edit
Route::post('companies/purge/{company}', 'MigrationController@purgeCompany')->middleware('password_protected'); Route::post('companies/purge/{company}', 'MigrationController@purgeCompany')->middleware('password_protected');