Update column for refresh tokens

This commit is contained in:
David Bomba 2022-06-17 18:28:31 +10:00
parent d9ca212f81
commit 213a51ad62
4 changed files with 59 additions and 22 deletions

View File

@ -163,7 +163,7 @@ class CompanySettings extends BaseSettings
public $require_quote_signature = false; //@TODO ben to confirm
//email settings
public $email_sending_method = 'default'; //enum 'default','gmail' //@implemented
public $email_sending_method = 'default'; //enum 'default','gmail','office365' //@implemented
public $gmail_sending_user_id = '0'; //@implemented
public $reply_to_email = ''; //@implemented

View File

@ -12,6 +12,7 @@
namespace App\Helpers\Mail;
use Illuminate\Mail\Transport\Transport;
use Illuminate\Support\Str;
use Swift_Mime_SimpleMessage;
use Microsoft\Graph\Graph;
use Microsoft\Graph\Model\UploadSession;
@ -29,8 +30,9 @@ class Office365MailTransport extends Transport
$this->beforeSendPerformed($message);
$graph = new Graph();
$token = $message->getHeaders()->get('GmailToken')->getValue();
$graph->setAccessToken($this->getAccessToken());
$graph->setAccessToken($token);
// Special treatment if the message has too large attachments
$messageBody = $this->getBody($message, true);
@ -163,7 +165,7 @@ class Office365MailTransport extends Transport
//add attachments if any
$attachments = [];
foreach ($message->getChildren() as $attachment) {
if ($attachment instanceof \Swift_Mime_SimpleMimeEntity) {
if ($attachment instanceof \Swift_Mime_SimpleMimeEntity && $attachment->getContentType() != 'text/plain') {
$attachments[] = [
"@odata.type" => "#microsoft.graph.fileAttachment",
"name" => $attachment->getHeaders()->get('Content-Type')->getParameter('name'),
@ -285,23 +287,4 @@ class Office365MailTransport extends Transport
);
}
protected function getAccessToken()
{
$guzzle = new \GuzzleHttp\Client();
$url = 'https://login.microsoftonline.com/' . config('ninja.o365.tenant_id') . '/oauth2/v2.0/token';
$token = json_decode($guzzle->post($url, [
'form_params' => [
'client_id' => config('ninja.o365.client_id'),
'client_secret' => config('ninja.o365.client_secret'),
'scope' => 'https://graph.microsoft.com/.default',
'grant_type' => 'client_credentials',
],
])->getBody()->getContents());
nlog($token);
nlog($token->access_token);
return $token->access_token;
}
}

View File

@ -184,12 +184,33 @@ class NinjaMailerJob implements ShouldQueue
$this->mailer = 'gmail';
$this->setGmailMailer();
break;
case 'office365':
$this->mailer = 'office365';
$this->setOfficeMailer();
break;
default:
break;
}
}
private function setOfficeMailer()
{
$sending_user = $this->nmo->settings->gmail_sending_user_id;
$user = User::find($this->decodePrimaryKey($sending_user));
$token = $user->oauth_user_token->access_token;
nlog("Sending via {$user->name()}");
$this->nmo
->mailable
->from($user->email, $user->name())
->withSwiftMessage(function ($message) use($token) {
$message->getHeaders()->addTextHeader('GmailToken', $token);
});
}
private function setGmailMailer()
{
if(LaravelGmail::check())

View File

@ -0,0 +1,33 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class ChangeRefreshTokenColumnSize extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->text('oauth_user_refresh_token')->change();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}