From 84a64773a60b8030104b83b425d27d801e045427 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Sat, 16 May 2020 20:26:16 +1000 Subject: [PATCH] Adjustments for OAuth (#3707) * Fixes for null values in custom values * Refactor mailing * Working on send emails from GMail API * Fixes for tests * Test for GMail * Adjustments for storing oauth token --- app/Http/Controllers/Auth/LoginController.php | 8 ++++---- app/Http/Controllers/EmailController.php | 4 ++++ app/Http/Requests/Invoice/UpdateInvoiceRequest.php | 3 ++- app/Jobs/Mail/BaseMailerJob.php | 11 ++++++----- app/Jobs/Mail/EntitySentEmail.php | 2 ++ app/Libraries/Google/Google.php | 8 ++------ app/Mail/Admin/EntitySentObject.php | 11 +++++++---- app/Models/User.php | 10 +++++----- routes/api.php | 2 +- tests/Feature/PreviewTest.php | 3 +-- 10 files changed, 34 insertions(+), 28 deletions(-) diff --git a/app/Http/Controllers/Auth/LoginController.php b/app/Http/Controllers/Auth/LoginController.php index e770508853dc..e42f036ae0fa 100644 --- a/app/Http/Controllers/Auth/LoginController.php +++ b/app/Http/Controllers/Auth/LoginController.php @@ -311,9 +311,9 @@ class LoginController extends BaseController if(array_key_exists('refresh_token', $token)) $refresh_token = $token['refresh_token']; - - $access_token = $token['access_token']; - + + //$access_token = $token['access_token']; + $name = OAuth::splitName($google->harvestName($user)); $new_account = [ @@ -322,7 +322,7 @@ class LoginController extends BaseController 'password' => '', 'email' => $google->harvestEmail($user), 'oauth_user_id' => $google->harvestSubField($user), - 'oauth_user_token' => $access_token, + 'oauth_user_token' => $token, 'oauth_user_refresh_token' => $refresh_token, 'oauth_provider_id' => 'google' ]; diff --git a/app/Http/Controllers/EmailController.php b/app/Http/Controllers/EmailController.php index b731a92b0c51..c137b444caa9 100644 --- a/app/Http/Controllers/EmailController.php +++ b/app/Http/Controllers/EmailController.php @@ -14,6 +14,7 @@ namespace App\Http\Controllers; use App\Helpers\Email\InvoiceEmail; use App\Http\Requests\Email\SendEmailRequest; use App\Jobs\Invoice\EmailInvoice; +use App\Jobs\Mail\EntitySentEmail; use App\Models\Credit; use App\Models\Invoice; use App\Models\Quote; @@ -118,6 +119,9 @@ class EmailController extends BaseController $when = now()->addSeconds(1); $invitation->contact->notify((new SendGenericNotification($invitation, $entity_string, $subject, $body))->delay($when)); + + EntitySentEmail::dispatch($invitation, $entity_string, $entity_obj->user, $invitation->company); + } }); diff --git a/app/Http/Requests/Invoice/UpdateInvoiceRequest.php b/app/Http/Requests/Invoice/UpdateInvoiceRequest.php index 3c60f6139ed6..41959bdc20f9 100644 --- a/app/Http/Requests/Invoice/UpdateInvoiceRequest.php +++ b/app/Http/Requests/Invoice/UpdateInvoiceRequest.php @@ -69,9 +69,10 @@ class UpdateInvoiceRequest extends Request foreach ($input['invitations'] as $key => $value) { if (is_numeric($input['invitations'][$key]['id'])) { unset($input['invitations'][$key]['id']); + } - if (is_string($input['invitations'][$key]['id'])) { + if (array_key_exists('id', $input['invitations'][$key]) && is_string($input['invitations'][$key]['id'])) { $input['invitations'][$key]['id'] = $this->decodePrimaryKey($input['invitations'][$key]['id']); } diff --git a/app/Jobs/Mail/BaseMailerJob.php b/app/Jobs/Mail/BaseMailerJob.php index dea597c57345..fdeb3e2ca92f 100644 --- a/app/Jobs/Mail/BaseMailerJob.php +++ b/app/Jobs/Mail/BaseMailerJob.php @@ -18,7 +18,7 @@ class BaseMailerJob implements ShouldQueue use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; - private function setMailDriver(string $driver) + public function setMailDriver(string $driver) { switch ($driver) { case 'default': @@ -32,16 +32,16 @@ class BaseMailerJob implements ShouldQueue } - private function setGmailMailer() + public function setGmailMailer() { $sending_user = $this->entity->client->getSetting('gmail_sending_user_id'); $user = User::find($sending_user); $google = (new Google())->init(); - $google->getClient->setAccessToken($user->oauth_user_token); + $google->getClient()->setAccessToken($user->oauth_user_token); - if ($google->getClient->isAccessTokenExpired()) { + if ($google->getClient()->isAccessTokenExpired()) { $google->refreshToken($user); } @@ -52,7 +52,8 @@ class BaseMailerJob implements ShouldQueue */ Config::set('mail.driver', 'gmail'); - Config::set('services.gmail.token', $user->oauth_user_token); + Config::set('services.gmail.token', $user->oauth_user_token['access_token']); + (new MailServiceProvider(app()))->register(); } diff --git a/app/Jobs/Mail/EntitySentEmail.php b/app/Jobs/Mail/EntitySentEmail.php index e2d1bcc8abc0..8f61ac1740c0 100644 --- a/app/Jobs/Mail/EntitySentEmail.php +++ b/app/Jobs/Mail/EntitySentEmail.php @@ -45,6 +45,8 @@ class EntitySentEmail extends BaseMailerJob implements ShouldQueue $this->invitation = $invitation; $this->entity = $invitation->{$entity_type}; + + $this->entity_type = $entity_type; } /** diff --git a/app/Libraries/Google/Google.php b/app/Libraries/Google/Google.php index d5c29f5f6369..b68958d36e27 100644 --- a/app/Libraries/Google/Google.php +++ b/app/Libraries/Google/Google.php @@ -45,19 +45,15 @@ class Google public function refreshToken($user) { if($this->client->isAccessTokenExpired()) { + $this->client->fetchAccessTokenWithRefreshToken($user->oauth_user_refresh_token); $access_token = $this->client->getAccessToken(); - if(is_string($access_token)) - $user->oauth_user_token = $access_token; - elseif(isset($access_token['access_token'])) - $user->oauth_user_token = $access_token['access_token']); + $user->oauth_user_token = $access_token; $user->save(); - $this->client->setAccessToken($access_token); - } return $this; diff --git a/app/Mail/Admin/EntitySentObject.php b/app/Mail/Admin/EntitySentObject.php index da221682dada..b1cdfe6b5620 100644 --- a/app/Mail/Admin/EntitySentObject.php +++ b/app/Mail/Admin/EntitySentObject.php @@ -35,7 +35,6 @@ class EntitySentObject $this->entity = $invitation->{$entity_type}; $this->contact = $invitation->contact; $this->company = $invitation->company; - $this->settings = $this->entity->client->getMergedSettings(); } public function build() @@ -69,19 +68,23 @@ class EntitySentObject private function getData() { + + $settings = $this->entity->client->getMergedSettings(); + $data = [ - 'title' => $this->getSubject, + 'title' => $this->getSubject(), 'message' => ctrans( "texts.notification_{$this->entity_type}_sent", [ - 'amount' => $amount, + 'amount' => $this->getAmount(), + 'client' => $this->contact->present()->name(), 'invoice' => $this->entity->number, ] ), 'url' => $this->invitation->getAdminLink(), 'button' => ctrans("texts.view_{$this->entity_type}"), - 'signature' => $this->settings->email_signature, + 'signature' => $settings->email_signature, 'logo' => $this->company->present()->logo(), ]; diff --git a/app/Models/User.php b/app/Models/User.php index 25f1c3255ed3..27df9d4bf42b 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -95,11 +95,11 @@ class User extends Authenticatable implements MustVerifyEmail ]; protected $casts = [ - 'settings' => 'object', - 'updated_at' => 'timestamp', - 'created_at' => 'timestamp', - 'deleted_at' => 'timestamp', - //'last_login' => 'timestamp', + 'oauth_user_token' => 'array', + 'settings' => 'object', + 'updated_at' => 'timestamp', + 'created_at' => 'timestamp', + 'deleted_at' => 'timestamp', ]; public function getEntityType() diff --git a/routes/api.php b/routes/api.php index c84d766e9adc..2fbfc207ba30 100644 --- a/routes/api.php +++ b/routes/api.php @@ -108,7 +108,7 @@ Route::group(['middleware' => ['api_db', 'token_auth', 'locale'], 'prefix' => 'a Route::post('templates', 'TemplateController@show')->name('templates.show'); - Route::post('preview', 'PreviewController@show')->name('previews.show'); + Route::post('preview', 'PreviewController@show'); Route::post('self-update', 'SelfUpdateController@update')->middleware('password_protected'); diff --git a/tests/Feature/PreviewTest.php b/tests/Feature/PreviewTest.php index e2ecafe9dd90..38a4419b7d39 100644 --- a/tests/Feature/PreviewTest.php +++ b/tests/Feature/PreviewTest.php @@ -56,11 +56,10 @@ class PreviewTest extends TestCase ]; - $response = $this->withHeaders([ 'X-API-SECRET' => config('ninja.api_secret'), 'X-API-TOKEN' => $this->token - ])->post('/api/v1/preview/', $data)->assertStatus(200); + ])->post('/api/v1/preview', $data)->assertStatus(200); }