Better user group management and object observers

This commit is contained in:
James Cole
2023-09-21 11:29:09 +02:00
parent c3bc1af7b5
commit 724a16944a
31 changed files with 1253 additions and 73 deletions

View File

@@ -0,0 +1,58 @@
<?php
/*
* AccountObserver.php
* Copyright (c) 2023 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Handlers\Observer;
use FireflyIII\Models\Account;
/**
* Class AccountObserver
*/
class AccountObserver
{
/**
* Also delete related objects.
*
* @param Account $account
*
* @return void
*/
public function deleting(Account $account): void
{
app('log')->debug('Observe "deleting" of an account.');
$account->accountMeta()->delete();
foreach ($account->piggyBanks()->get() as $piggy) {
$piggy->delete();
}
foreach ($account->attachments()->get() as $attachment) {
$attachment->delete();
}
foreach ($account->transactions()->get() as $transaction) {
$transaction->delete();
}
$account->notes()->delete();
$account->locations()->delete();
}
}

View File

@@ -0,0 +1,36 @@
<?php
/*
* AttachmentObserver.php
* Copyright (c) 2023 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace FireflyIII\Handlers\Observer;
use FireflyIII\Models\Attachment;
/**
* Class AttachmentObserver
*/
class AttachmentObserver
{
public function deleting(Attachment $attachment): void
{
app('log')->debug('Observe "deleting" of an attachment.');
$attachment->notes()->delete();
}
}

View File

@@ -0,0 +1,45 @@
<?php
/*
* BillObserver.php
* Copyright (c) 2023 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace FireflyIII\Handlers\Observer;
use FireflyIII\Models\Bill;
/**
* Class BillObserver
*/
class BillObserver
{
/**
* @param Bill $bill
*
* @return void
*/
public function deleting(Bill $bill): void
{
app('log')->debug('Observe "deleting" of a bill.');
foreach ($bill->attachments()->get() as $attachment) {
$attachment->delete();
}
$bill->notes()->delete();
}
}

View File

@@ -0,0 +1,49 @@
<?php
/*
* BudgetObserver.php
* Copyright (c) 2023 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace FireflyIII\Handlers\Observer;
use FireflyIII\Models\Budget;
/**
* Class BudgetObserver
*/
class BudgetObserver
{
/**
* @param Budget $budget
*
* @return void
*/
public function deleting(Budget $budget): void
{
app('log')->debug('Observe "deleting" of a budget.');
foreach ($budget->attachments()->get() as $attachment) {
$attachment->delete();
}
$budget->budgetlimits()->delete();
$budget->notes()->delete();
$budget->autoBudgets()->delete();
}
}

View File

@@ -0,0 +1,45 @@
<?php
/*
* CategoryObserver.php
* Copyright (c) 2023 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace FireflyIII\Handlers\Observer;
use FireflyIII\Models\Category;
/**
* Class CategoryObserver
*/
class CategoryObserver
{
/**
* @param Category $category
*
* @return void
*/
public function deleting(Category $category): void
{
app('log')->debug('Observe "deleting" of a category.');
foreach ($category->attachments()->get() as $attachment) {
$attachment->delete();
}
$category->notes()->delete();
}
}

View File

@@ -0,0 +1,71 @@
<?php
/*
* PiggyBankObserver.php
* Copyright (c) 2023 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Handlers\Observer;
use FireflyIII\Models\PiggyBank;
use FireflyIII\Models\PiggyBankRepetition;
/**
* Class PiggyBankObserver
*/
class PiggyBankObserver
{
/**
* @param PiggyBank $piggyBank
*
* @return void
*/
public function created(PiggyBank $piggyBank): void
{
app('log')->debug('Observe "created" of a piggy bank.');
$repetition = new PiggyBankRepetition();
$repetition->piggyBank()->associate($piggyBank);
$repetition->startdate = $piggyBank->startdate;
$repetition->targetdate = $piggyBank->targetdate;
$repetition->currentamount = 0;
$repetition->save();
}
/**
* Also delete related objects.
*
* @param PiggyBank $piggyBank
*
* @return void
*/
public function deleting(PiggyBank $piggyBank): void
{
app('log')->debug('Observe "deleting" of a piggy bank.');
foreach ($piggyBank->attachments()->get() as $attachment) {
$attachment->delete();
}
$piggyBank->piggyBankEvents()->delete();
$piggyBank->piggyBankRepetitions()->delete();
$piggyBank->notes()->delete();
}
}

View File

@@ -0,0 +1,52 @@
<?php
/*
* RecurrenceObserver.php
* Copyright (c) 2023 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace FireflyIII\Handlers\Observer;
use FireflyIII\Models\Recurrence;
/**
* Class RecurrenceObserver
*/
class RecurrenceObserver
{
/**
* @param Recurrence $recurrence
*
* @return void
*/
public function deleting(Recurrence $recurrence): void
{
app('log')->debug('Observe "deleting" of a recurrence.');
foreach ($recurrence->attachments()->get() as $attachment) {
$attachment->delete();
}
$recurrence->recurrenceRepetitions()->delete();
$recurrence->recurrenceMeta()->delete();
foreach ($recurrence->recurrenceTransactions()->get() as $transaction) {
$transaction->delete();
}
$recurrence->notes()->delete();
}
}

View File

@@ -0,0 +1,42 @@
<?php
/*
* RecurrenceTransactionObserver.php
* Copyright (c) 2023 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace FireflyIII\Handlers\Observer;
use FireflyIII\Models\RecurrenceTransaction;
/**
* Class RecurrenceTransactionObserver
*/
class RecurrenceTransactionObserver
{
/**
* @param RecurrenceTransaction $transaction
*
* @return void
*/
public function deleting(RecurrenceTransaction $transaction): void
{
app('log')->debug('Observe "deleting" of a recurrence transaction.');
$transaction->recurrenceTransactionMeta()->delete();
}
}

View File

@@ -0,0 +1,45 @@
<?php
/*
* RuleGroupObserver.php
* Copyright (c) 2023 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace FireflyIII\Handlers\Observer;
use FireflyIII\Models\RuleGroup;
/**
* Class RuleGroupObserver
*/
class RuleGroupObserver
{
/**
* @param RuleGroup $ruleGroup
*
* @return void
*/
public function deleting(RuleGroup $ruleGroup): void
{
app('log')->debug('Observe "deleting" of a rule group.');
foreach ($ruleGroup->rules()->get() as $rule) {
$rule->delete();
}
}
}

View File

@@ -0,0 +1,43 @@
<?php
/*
* RuleObserver.php
* Copyright (c) 2023 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace FireflyIII\Handlers\Observer;
use FireflyIII\Models\Rule;
/**
* Class RuleObserver
*/
class RuleObserver
{
/**
* @param Rule $rule
*
* @return void
*/
public function deleting(Rule $rule): void
{
app('log')->debug('Observe "deleting" of a rule.');
$rule->ruleActions()->delete();
$rule->ruleTriggers()->delete();
}
}

View File

@@ -0,0 +1,48 @@
<?php
/*
* TagObserver.php
* Copyright (c) 2023 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace FireflyIII\Handlers\Observer;
use FireflyIII\Models\Tag;
/**
* Class TagObserver
*/
class TagObserver
{
/**
* @param Tag $tag
*
* @return void
*/
public function deleting(Tag $tag): void
{
app('log')->debug('Observe "deleting" of a tag.');
foreach ($tag->attachments()->get() as $attachment) {
$attachment->delete();
}
$tag->locations()->delete();
}
}

View File

@@ -0,0 +1,39 @@
<?php
/*
* TransactionGroup.php
* Copyright (c) 2023 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace FireflyIII\Handlers\Observer;
use FireflyIII\Models\TransactionGroup;
/**
* Class TransactionGroup
*/
class TransactionGroupObserver
{
public function deleting(TransactionGroup $transactionGroup): void
{
app('log')->debug('Observe "deleting" of a transaction group.');
foreach ($transactionGroup->transactionJournals()->get() as $journal) {
$journal->delete();
}
}
}

View File

@@ -0,0 +1,55 @@
<?php
/*
* TransactionJournalObserver.php
* Copyright (c) 2023 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace FireflyIII\Handlers\Observer;
use FireflyIII\Models\TransactionJournal;
/**
* Class TransactionJournalObserver
*/
class TransactionJournalObserver
{
/**
* @param TransactionJournal $transactionJournal
*
* @return void
*/
public function deleting(TransactionJournal $transactionJournal): void
{
app('log')->debug('Observe "deleting" of a transaction journal.');
// to make sure the listener doesn't get back to use and loop
TransactionJournal::withoutEvents(function () use ($transactionJournal) {
foreach ($transactionJournal->transactions()->get() as $transaction) {
$transaction->delete();
}
});
foreach ($transactionJournal->attachments()->get() as $attachment) {
$attachment->delete();
}
$transactionJournal->locations()->delete();
$transactionJournal->sourceJournalLinks()->delete();
$transactionJournal->destJournalLinks()->delete();
$transactionJournal->auditLogEntries()->delete();
}
}

View File

@@ -0,0 +1,37 @@
<?php
/*
* TransactionObserver.php
* Copyright (c) 2023 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace FireflyIII\Handlers\Observer;
use FireflyIII\Models\Transaction;
/**
* Class TransactionObserver
*/
class TransactionObserver
{
public function deleting(Transaction $transaction): void
{
app('log')->debug('Observe "deleting" of a transaction.');
$transaction->transactionJournal->delete();
}
}

View File

@@ -0,0 +1,43 @@
<?php
/*
* WebhookMessageObserver.php
* Copyright (c) 2023 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace FireflyIII\Handlers\Observer;
use FireflyIII\Models\Webhook;
use FireflyIII\Models\WebhookMessage;
/**
* Class WebhookMessageObserver
*/
class WebhookMessageObserver
{
/**
* @param WebhookMessage $webhookMessage
*
* @return void
*/
public function deleting(WebhookMessage $webhookMessage): void
{
app('log')->debug('Observe "deleting" of a webhook message.');
$webhookMessage->webhookAttempts()->delete();
}
}

View File

@@ -0,0 +1,44 @@
<?php
/*
* WebhookObserver.php
* Copyright (c) 2023 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace FireflyIII\Handlers\Observer;
use FireflyIII\Models\Webhook;
/**
* Class WebhookObserver
*/
class WebhookObserver
{
/**
* @param Webhook $webhook
*
* @return void
*/
public function deleting(Webhook $webhook): void
{
app('log')->debug('Observe "deleting" of a webhook.');
foreach ($webhook->webhookMessages() as $message) {
$message->delete();
}
}
}