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
This commit is contained in:
David Bomba 2020-05-16 20:26:16 +10:00 committed by GitHub
parent d05bd7d2c1
commit 84a64773a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 34 additions and 28 deletions

View File

@ -311,9 +311,9 @@ class LoginController extends BaseController
if(array_key_exists('refresh_token', $token)) if(array_key_exists('refresh_token', $token))
$refresh_token = $token['refresh_token']; $refresh_token = $token['refresh_token'];
$access_token = $token['access_token']; //$access_token = $token['access_token'];
$name = OAuth::splitName($google->harvestName($user)); $name = OAuth::splitName($google->harvestName($user));
$new_account = [ $new_account = [
@ -322,7 +322,7 @@ class LoginController extends BaseController
'password' => '', 'password' => '',
'email' => $google->harvestEmail($user), 'email' => $google->harvestEmail($user),
'oauth_user_id' => $google->harvestSubField($user), 'oauth_user_id' => $google->harvestSubField($user),
'oauth_user_token' => $access_token, 'oauth_user_token' => $token,
'oauth_user_refresh_token' => $refresh_token, 'oauth_user_refresh_token' => $refresh_token,
'oauth_provider_id' => 'google' 'oauth_provider_id' => 'google'
]; ];

View File

@ -14,6 +14,7 @@ namespace App\Http\Controllers;
use App\Helpers\Email\InvoiceEmail; use App\Helpers\Email\InvoiceEmail;
use App\Http\Requests\Email\SendEmailRequest; use App\Http\Requests\Email\SendEmailRequest;
use App\Jobs\Invoice\EmailInvoice; use App\Jobs\Invoice\EmailInvoice;
use App\Jobs\Mail\EntitySentEmail;
use App\Models\Credit; use App\Models\Credit;
use App\Models\Invoice; use App\Models\Invoice;
use App\Models\Quote; use App\Models\Quote;
@ -118,6 +119,9 @@ class EmailController extends BaseController
$when = now()->addSeconds(1); $when = now()->addSeconds(1);
$invitation->contact->notify((new SendGenericNotification($invitation, $entity_string, $subject, $body))->delay($when)); $invitation->contact->notify((new SendGenericNotification($invitation, $entity_string, $subject, $body))->delay($when));
EntitySentEmail::dispatch($invitation, $entity_string, $entity_obj->user, $invitation->company);
} }
}); });

View File

@ -69,9 +69,10 @@ class UpdateInvoiceRequest extends Request
foreach ($input['invitations'] as $key => $value) { foreach ($input['invitations'] as $key => $value) {
if (is_numeric($input['invitations'][$key]['id'])) { if (is_numeric($input['invitations'][$key]['id'])) {
unset($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']); $input['invitations'][$key]['id'] = $this->decodePrimaryKey($input['invitations'][$key]['id']);
} }

View File

@ -18,7 +18,7 @@ class BaseMailerJob implements ShouldQueue
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
private function setMailDriver(string $driver) public function setMailDriver(string $driver)
{ {
switch ($driver) { switch ($driver) {
case 'default': 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'); $sending_user = $this->entity->client->getSetting('gmail_sending_user_id');
$user = User::find($sending_user); $user = User::find($sending_user);
$google = (new Google())->init(); $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); $google->refreshToken($user);
} }
@ -52,7 +52,8 @@ class BaseMailerJob implements ShouldQueue
*/ */
Config::set('mail.driver', 'gmail'); 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(); (new MailServiceProvider(app()))->register();
} }

View File

@ -45,6 +45,8 @@ class EntitySentEmail extends BaseMailerJob implements ShouldQueue
$this->invitation = $invitation; $this->invitation = $invitation;
$this->entity = $invitation->{$entity_type}; $this->entity = $invitation->{$entity_type};
$this->entity_type = $entity_type;
} }
/** /**

View File

@ -45,19 +45,15 @@ class Google
public function refreshToken($user) public function refreshToken($user)
{ {
if($this->client->isAccessTokenExpired()) { if($this->client->isAccessTokenExpired()) {
$this->client->fetchAccessTokenWithRefreshToken($user->oauth_user_refresh_token); $this->client->fetchAccessTokenWithRefreshToken($user->oauth_user_refresh_token);
$access_token = $this->client->getAccessToken(); $access_token = $this->client->getAccessToken();
if(is_string($access_token)) $user->oauth_user_token = $access_token;
$user->oauth_user_token = $access_token;
elseif(isset($access_token['access_token']))
$user->oauth_user_token = $access_token['access_token']);
$user->save(); $user->save();
$this->client->setAccessToken($access_token);
} }
return $this; return $this;

View File

@ -35,7 +35,6 @@ class EntitySentObject
$this->entity = $invitation->{$entity_type}; $this->entity = $invitation->{$entity_type};
$this->contact = $invitation->contact; $this->contact = $invitation->contact;
$this->company = $invitation->company; $this->company = $invitation->company;
$this->settings = $this->entity->client->getMergedSettings();
} }
public function build() public function build()
@ -69,19 +68,23 @@ class EntitySentObject
private function getData() private function getData()
{ {
$settings = $this->entity->client->getMergedSettings();
$data = [ $data = [
'title' => $this->getSubject, 'title' => $this->getSubject(),
'message' => ctrans( 'message' => ctrans(
"texts.notification_{$this->entity_type}_sent", "texts.notification_{$this->entity_type}_sent",
[ [
'amount' => $amount, 'amount' => $this->getAmount(),
'client' => $this->contact->present()->name(), 'client' => $this->contact->present()->name(),
'invoice' => $this->entity->number, 'invoice' => $this->entity->number,
] ]
), ),
'url' => $this->invitation->getAdminLink(), 'url' => $this->invitation->getAdminLink(),
'button' => ctrans("texts.view_{$this->entity_type}"), 'button' => ctrans("texts.view_{$this->entity_type}"),
'signature' => $this->settings->email_signature, 'signature' => $settings->email_signature,
'logo' => $this->company->present()->logo(), 'logo' => $this->company->present()->logo(),
]; ];

View File

@ -95,11 +95,11 @@ class User extends Authenticatable implements MustVerifyEmail
]; ];
protected $casts = [ protected $casts = [
'settings' => 'object', 'oauth_user_token' => 'array',
'updated_at' => 'timestamp', 'settings' => 'object',
'created_at' => 'timestamp', 'updated_at' => 'timestamp',
'deleted_at' => 'timestamp', 'created_at' => 'timestamp',
//'last_login' => 'timestamp', 'deleted_at' => 'timestamp',
]; ];
public function getEntityType() public function getEntityType()

View File

@ -108,7 +108,7 @@ Route::group(['middleware' => ['api_db', 'token_auth', 'locale'], 'prefix' => 'a
Route::post('templates', 'TemplateController@show')->name('templates.show'); 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'); Route::post('self-update', 'SelfUpdateController@update')->middleware('password_protected');

View File

@ -56,11 +56,10 @@ class PreviewTest extends TestCase
]; ];
$response = $this->withHeaders([ $response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'), 'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token 'X-API-TOKEN' => $this->token
])->post('/api/v1/preview/', $data)->assertStatus(200); ])->post('/api/v1/preview', $data)->assertStatus(200);
} }