This commit is contained in:
James Cole 2018-05-26 13:55:11 +02:00
parent dcfea20973
commit 73aef1b9a4
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
11 changed files with 308 additions and 11 deletions

View File

@ -0,0 +1,80 @@
<?php
/**
* APIEventHandler.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Handlers\Events;
use Exception;
use FireflyIII\Mail\AccessTokenCreatedMail;
use FireflyIII\Repositories\User\UserRepositoryInterface;
use Laravel\Passport\Events\AccessTokenCreated;
use Laravel\Passport\Token;
use Log;
use Mail;
use Request;
use Session;
/**
* Class APIEventHandler
*/
class APIEventHandler
{
/**
* @param AccessTokenCreated $event
*
* @return bool
*/
public function accessTokenCreated(AccessTokenCreated $event): bool
{
/** @var UserRepositoryInterface $repository */
$repository = app(UserRepositoryInterface::class);
$user = $repository->findNull((int)$event->userId);
if (null === $user) {
Log::error('Access Token generated but no user associated.');
return true;
}
$email = $user->email;
$ipAddress = Request::ip();
Log::debug(sprintf('Now in APIEventHandler::accessTokenCreated. Email is %s, IP is %s', $email, $ipAddress));
try {
Log::debug('Trying to send message...');
Mail::to($email)->send(new AccessTokenCreatedMail($email, $ipAddress));
// @codeCoverageIgnoreStart
} catch (Exception $e) {
Log::debug('Send message failed! :(');
Log::error($e->getMessage());
Log::error($e->getTraceAsString());
Session::flash('error', 'Possible email error: ' . $e->getMessage());
}
Log::debug('If no error above this line, message was sent.');
// @codeCoverageIgnoreEnd
return true;
}
}

View File

@ -0,0 +1,68 @@
<?php
/**
* AccessTokenCreatedMail.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Laravel\Passport\Token;
/**
* Class AccessTokenCreatedMail
*/
class AccessTokenCreatedMail extends Mailable
{
use Queueable, SerializesModels;
/** @var string Email address of admin */
public $email;
/** @var string IP address of admin */
public $ipAddress;
/**
* AccessTokenCreatedMail constructor.
*
* @param string $email
* @param string $ipAddress
* @param Token $token
*/
public function __construct(string $email, string $ipAddress)
{
$this->email = $email;
$this->ipAddress = $ipAddress;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->view('emails.access-token-created-html')->text('emails.access-token-created-text')
->subject('A new access token was created');
}
}

View File

@ -68,6 +68,6 @@ class ConfirmEmailChangeMail extends Mailable
public function build()
{
return $this->view('emails.confirm-email-change-html')->text('emails.confirm-email-change-text')
->subject('Your Firefly III email address has changed.');
->subject('Your Firefly III email address has changed');
}
}

View File

@ -0,0 +1,70 @@
<?php
/**
* OAuthTokenCreatedMail.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Laravel\Passport\Client;
/**
* Class OAuthTokenCreatedMail
*/
class OAuthTokenCreatedMail extends Mailable
{
use Queueable, SerializesModels;
/** @var Client The client */
public $client;
/** @var string Email address of admin */
public $email;
/** @var string IP address of admin */
public $ipAddress;
/**
* OAuthTokenCreatedMail constructor.
*
* @param string $email
* @param string $ipAddress
* @param Client $client
*/
public function __construct(string $email, string $ipAddress, Client $client)
{
$this->email = $email;
$this->ipAddress = $ipAddress;
$this->client = $client;
}
/**
* Build the message.
*
* @return $this
*/
public function build(): self
{
return $this->view('emails.oauth-client-created-html')->text('emails.oauth-client-created-text')
->subject('A new OAuth client has been created');
}
}

View File

@ -66,6 +66,6 @@ class UndoEmailChangeMail extends Mailable
public function build()
{
return $this->view('emails.undo-email-change-html')->text('emails.undo-email-change-text')
->subject('Your Firefly III email address has changed.');
->subject('Your Firefly III email address has changed');
}
}

View File

@ -22,6 +22,7 @@ declare(strict_types=1);
namespace FireflyIII\Providers;
use Exception;
use FireflyIII\Events\AdminRequestedTestMessage;
use FireflyIII\Events\RegisteredUser;
use FireflyIII\Events\RequestedNewPassword;
@ -29,10 +30,18 @@ use FireflyIII\Events\RequestedVersionCheckStatus;
use FireflyIII\Events\StoredTransactionJournal;
use FireflyIII\Events\UpdatedTransactionJournal;
use FireflyIII\Events\UserChangedEmail;
use FireflyIII\Mail\OAuthTokenCreatedMail;
use FireflyIII\Models\PiggyBank;
use FireflyIII\Models\PiggyBankRepetition;
use FireflyIII\Repositories\User\UserRepositoryInterface;
use Illuminate\Auth\Events\Login;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Laravel\Passport\Client;
use Laravel\Passport\Events\AccessTokenCreated;
use Log;
use Mail;
use Request;
use Session;
/**
* Class EventServiceProvider.
@ -82,6 +91,10 @@ class EventServiceProvider extends ServiceProvider
UpdatedTransactionJournal::class => [
'FireflyIII\Handlers\Events\UpdatedJournalEventHandler@processRules',
],
// API related events:
AccessTokenCreated::class => [
'FireflyIII\Handlers\Events\APIEventHandler@accessTokenCreated',
],
];
/**
@ -91,14 +104,13 @@ class EventServiceProvider extends ServiceProvider
public function boot()
{
parent::boot();
$this->registerDeleteEvents();
$this->registerCreateEvents();
}
/**
*
*/
protected function registerCreateEvents()
protected function registerCreateEvents(): void
{
// move this routine to a filter
// in case of repeated piggy banks and/or other problems.
@ -112,13 +124,36 @@ class EventServiceProvider extends ServiceProvider
$repetition->save();
}
);
Client::created(
function (Client $oauthClient) {
/** @var UserRepositoryInterface $repository */
$repository = app(UserRepositoryInterface::class);
$user = $repository->findNull((int)$oauthClient->user_id);
if (null === $user) {
Log::error('OAuth client generated but no user associated.');
return;
}
$email = $user->email;
$ipAddress = Request::ip();
Log::debug(sprintf('Now in EventServiceProvider::registerCreateEvents. Email is %s, IP is %s', $email, $ipAddress));
try {
Log::debug('Trying to send message...');
Mail::to($email)->send(new OAuthTokenCreatedMail($email, $ipAddress, $oauthClient));
// @codeCoverageIgnoreStart
} catch (Exception $e) {
Log::debug('Send message failed! :(');
Log::error($e->getMessage());
Log::error($e->getTraceAsString());
Session::flash('error', 'Possible email error: ' . $e->getMessage());
}
Log::debug('If no error above this line, message was sent.');
}
);
}
/**
*
*/
protected function registerDeleteEvents()
{
}
}

View File

@ -56,6 +56,7 @@ use FireflyIII\Models\Attachment;
/**
* Class User.
* @property int $id
* @property string $email
*/
class User extends Authenticatable
{

View File

@ -0,0 +1,13 @@
{% include 'emails.header-html' %}
<p style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;font-size:13px;">
Somebody (hopefully you) just created a new Firefly III API Access Token for your user account.
</p>
<p style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;font-size:13px;">
With this token, they can access <strong>all</strong> of your financial records through the Firefly III API.
</p>
<p style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;font-size:13px;">
If this wasn't you, please revoke this token as soon as possible at {{ route('profile.index') }}.
</p>
{% include 'emails.footer-html' %}

View File

@ -0,0 +1,7 @@
{% include 'emails.header-text' %}
Somebody (hopefully you) just created a new Firefly III API Access Token for your user account.
With this token, they can access all of your financial records through the Firefly III API.
If this wasn't you, please revoke this token as soon as possible at {{ route('profile.index') }}.
{% include 'emails.footer-text' %}

View File

@ -0,0 +1,14 @@
{% include 'emails.header-html' %}
<p style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;font-size:13px;">
Somebody (hopefully you) just created a new Firefly III API OAuth Client for your user account. It's labeled "{{ client.name }}"
and has callback URL <span style="font-family: monospace;">{{ client.redirect }}</span>.
</p>
<p style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;font-size:13px;">
With this client, they can access <strong>all</strong> of your financial records through the Firefly III API.
</p>
<p style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;font-size:13px;">
If this wasn't you, please revoke this client as soon as possible at {{ route('profile.index') }}.
</p>
{% include 'emails.footer-html' %}

View File

@ -0,0 +1,9 @@
{% include 'emails.header-text' %}
Somebody (hopefully you) just created a new Firefly III API OAuth Client for your user account. It's labeled "{{ client.name }}" and has callback URL:
{{ client.redirect }}
With this client, they can access <strong>all</strong> of your financial records through the Firefly III API.
If this wasn't you, please revoke this client as soon as possible at {{ route('profile.index') }}.
{% include 'emails.footer-text' %}