mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2024-11-05 18:52:44 +01:00
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:
parent
d05bd7d2c1
commit
84a64773a6
@ -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'
|
||||
];
|
||||
|
@ -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);
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -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']);
|
||||
}
|
||||
|
||||
|
@ -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();
|
||||
|
||||
}
|
||||
|
@ -45,6 +45,8 @@ class EntitySentEmail extends BaseMailerJob implements ShouldQueue
|
||||
$this->invitation = $invitation;
|
||||
|
||||
$this->entity = $invitation->{$entity_type};
|
||||
|
||||
$this->entity_type = $entity_type;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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;
|
||||
|
@ -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(),
|
||||
];
|
||||
|
||||
|
@ -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()
|
||||
|
@ -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');
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user