Skip to content

Commit

Permalink
Added support for allow[Edit|Delete] functions to prevent delete an…
Browse files Browse the repository at this point in the history
…d edit operations of used entities

Fixes #18
  • Loading branch information
MasterZydra committed Feb 24, 2024
1 parent a2aae78 commit 76ce870
Show file tree
Hide file tree
Showing 29 changed files with 304 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ Types of changes: `Added`, `Changed`, `Deprecate`, `Removed`, `Fixed`, `Secruity

## [Unreleased]

### Added
- Added support for `allow[Edit|Delete]` functions to prevent delete and edit operations of used entities

## v2.0.1 - 22.02.2024 - Minor styling improvements

### Added
Expand Down
22 changes: 22 additions & 0 deletions app/Models/DeliveryNote.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,22 @@ protected static function new(array $data = []): self
return new self($data);
}

public function allowEdit(): bool
{
return match (true) {
$this->getInvoiceId() !== null && $this->getInvoice()->getIsPaid() => false,
default => true,
};
}

public function allowDelete(): bool
{
return match (true) {
$this->getInvoiceId() !== null => false,
default => true,
};
}

public function save(): self
{
if ($this->getId() === null) {
Expand All @@ -44,6 +60,7 @@ public function save(): self
$this->getInvoiceId()
);
} else {
$this->checkAllowEdit();
Database::prepared(
'UPDATE ' . $this->getTableName() . ' SET `year`=?, nr=?, deliveryDate=?, amount=?, productId=?, supplierId=?, recipientId=?, isInvoiceReady=?, invoiceId=? WHERE id=?',
'iisdiiiiii',
Expand Down Expand Up @@ -105,6 +122,11 @@ public function getRecipient(): Recipient
return Recipient::findById($this->getRecipientId());
}

public function getInvoice(): Invoice
{
return Invoice::findById($this->getInvoiceId());
}

public static function nextDeliveryNoteNr(int $year = null): int
{
if ($year === null) {
Expand Down
19 changes: 19 additions & 0 deletions app/Models/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,24 @@ protected static function new(array $data = []): self
return new self($data);
}

public function allowEdit(): bool
{
$before = Invoice::findById($this->getId());

return match (true) {
$before->getIsPaid() => false,
default => true,
};
}

public function allowDelete(): bool
{
return match (true) {
$this->getIsPaid() => false,
default => true,
};
}

public function save(): self
{
if ($this->getId() === null) {
Expand All @@ -35,6 +53,7 @@ public function save(): self
Convert::boolToInt($this->getIsPaid())
);
} else {
$this->checkAllowEdit();
Database::prepared(
'UPDATE ' . $this->getTableName() . ' SET `year`=?, nr=?, invoiceDate=?, recipientId=?, isPaid=? WHERE id=?',
'iisiii',
Expand Down
15 changes: 15 additions & 0 deletions app/Models/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Framework\Database\BaseModel;
use Framework\Database\Database;
use Framework\Database\Query\ColType;
use Framework\Database\Query\Condition;

class Language extends BaseModel
{
Expand All @@ -15,6 +17,18 @@ protected static function new(array $data = []): self
return new self($data);
}

public function allowDelete(): bool
{
$users = User::all(
User::getQueryBuilder()->where(ColType::Int, 'languageId', Condition::Equal, $this->getId())
);

return match (true) {
count($users) > 0 => false,
default => true,
};
}

public function save(): self
{
if ($this->getId() === null) {
Expand All @@ -25,6 +39,7 @@ public function save(): self
$this->getName()
);
} else {
$this->checkAllowEdit();
Database::prepared(
'UPDATE ' . $this->getTableName() . ' SET code=?, `name`=? WHERE id=?',
'ssi',
Expand Down
15 changes: 15 additions & 0 deletions app/Models/Plot.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Framework\Database\BaseModel;
use Framework\Database\Database;
use Framework\Database\Query\ColType;
use Framework\Database\Query\Condition;
use Framework\Facades\Convert;

class Plot extends BaseModel
Expand All @@ -19,6 +21,18 @@ protected static function new(array $data = []): self
return new self($data);
}

public function allowDelete(): bool
{
$volumeDistributions = VolumeDistribution::all(
VolumeDistribution::getQueryBuilder()->where(ColType::Int, 'plotId', Condition::Equal, $this->getId())
);

return match (true) {
count($volumeDistributions) > 0 => false,
default => true,
};
}

public function save(): self
{
if ($this->getId() === null) {
Expand All @@ -32,6 +46,7 @@ public function save(): self
Convert::boolToInt($this->getIsLocked())
);
} else {
$this->checkAllowEdit();
Database::prepared(
'UPDATE ' . $this->getTableName() . ' SET nr=?, name=?, subdistrict=?, supplierId=?, isLocked=? WHERE id=?',
'sssiii',
Expand Down
18 changes: 18 additions & 0 deletions app/Models/Price.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Framework\Database\BaseModel;
use Framework\Database\Database;
use Framework\Database\Query\ColType;
use Framework\Database\Query\Condition;

class Price extends BaseModel
{
Expand All @@ -18,6 +20,21 @@ protected static function new(array $data = []): self
return new self($data);
}

public function allowDelete(): bool
{
$deliveryNotes = DeliveryNote::all(
DeliveryNote::getQueryBuilder()
->where(ColType::Int, 'year', Condition::Equal, $this->getYear())
->where(ColType::Int, 'productId', Condition::Equal, $this->getProductId())
->where(ColType::Int, 'recipientId', Condition::Equal, $this->getRecipientId())
);

return match (true) {
count($deliveryNotes) > 0 => false,
default => true,
};
}

public function save(): self
{
if ($this->getId() === null) {
Expand All @@ -31,6 +48,7 @@ public function save(): self
$this->getRecipientId()
);
} else {
$this->checkAllowEdit();
Database::prepared(
'UPDATE ' . $this->getTableName() . ' SET `year` = ?, price = ?, pricePayout = ?, productId = ?, recipientId = ? WHERE id = ?',
'iddiii',
Expand Down
19 changes: 19 additions & 0 deletions app/Models/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Framework\Database\BaseModel;
use Framework\Database\Database;
use Framework\Database\Query\ColType;
use Framework\Database\Query\Condition;
use Framework\Facades\Convert;

class Product extends BaseModel
Expand All @@ -16,6 +18,22 @@ protected static function new(array $data = []): self
return new self($data);
}

public function allowDelete(): bool
{
$deliveryNotes = DeliveryNote::all(
DeliveryNote::getQueryBuilder()->where(ColType::Int, 'productId', Condition::Equal, $this->getId())
);
$prices = Price::all(
Price::getQueryBuilder()->where(ColType::Int, 'productId', Condition::Equal, $this->getId())
);

return match (true) {
count($deliveryNotes) > 0 => false,
count($prices) > 0 => false,
default => true,
};
}

public function save(): self
{
if ($this->getId() === null) {
Expand All @@ -26,6 +44,7 @@ public function save(): self
Convert::boolToInt($this->getIsDiscontinued())
);
} else {
$this->checkAllowEdit();
Database::prepared(
'UPDATE ' . $this->getTableName() . ' SET `name`=?, isDiscontinued=? WHERE id=?',
'sii',
Expand Down
23 changes: 23 additions & 0 deletions app/Models/Recipient.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Framework\Database\BaseModel;
use Framework\Database\Database;
use Framework\Database\Query\ColType;
use Framework\Database\Query\Condition;
use Framework\Facades\Convert;

class Recipient extends BaseModel
Expand All @@ -19,6 +21,26 @@ protected static function new(array $data = []): self
return new self($data);
}

public function allowDelete(): bool
{
$deliveryNotes = DeliveryNote::all(
DeliveryNote::getQueryBuilder()->where(ColType::Int, 'recipientId', Condition::Equal, $this->getId())
);
$invoices = Invoice::all(
Invoice::getQueryBuilder()->where(ColType::Int, 'recipientId', Condition::Equal, $this->getId())
);
$prices = Price::all(
Price::getQueryBuilder()->where(ColType::Int, 'recipientId', Condition::Equal, $this->getId())
);

return match (true) {
count($deliveryNotes) > 0 => false,
count($invoices) > 0 => false,
count($prices) > 0 => false,
default => true,
};
}

public function save(): self
{
if ($this->getId() === null) {
Expand All @@ -32,6 +54,7 @@ public function save(): self
Convert::boolToInt($this->getIsLocked())
);
} else {
$this->checkAllowEdit();
Database::prepared(
'UPDATE ' . $this->getTableName() . ' SET `name`=?, street=?, postalCode=?, city=?, isLocked=? WHERE id=?',
'ssssii',
Expand Down
13 changes: 13 additions & 0 deletions app/Models/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ protected static function new(array $data = []): self
return new self($data);
}

public function allowDelete(): bool
{
$userRoles = UserRole::all(
UserRole::getQueryBuilder()->where(ColType::Int, 'roleId', Condition::Equal, $this->getId())
);

return match (true) {
count($userRoles) > 0 => false,
default => true,
};
}

public function save(): self
{
if ($this->getId() === null) {
Expand All @@ -25,6 +37,7 @@ public function save(): self
$this->getName()
);
} else {
$this->checkAllowEdit();
Database::prepared(
'UPDATE ' . $this->getTableName() . ' SET `name`=? WHERE id=?',
'si',
Expand Down
1 change: 1 addition & 0 deletions app/Models/Setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public function save(): self
$this->getValue()
);
} else {
$this->checkAllowEdit();
Database::prepared(
'UPDATE ' . $this->getTableName() . ' SET `name`=?, description=?, value=? WHERE id=?',
'sssi',
Expand Down
19 changes: 19 additions & 0 deletions app/Models/Supplier.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Framework\Database\BaseModel;
use Framework\Database\Database;
use Framework\Database\Query\ColType;
use Framework\Database\Query\Condition;
use Framework\Facades\Convert;

class Supplier extends BaseModel
Expand All @@ -18,6 +20,22 @@ protected static function new(array $data = []): self
return new self($data);
}

public function allowDelete(): bool
{
$deliveryNotes = DeliveryNote::all(
DeliveryNote::getQueryBuilder()->where(ColType::Int, 'supplierId', Condition::Equal, $this->getId())
);
$plots = Plot::all(
Plot::getQueryBuilder()->where(ColType::Int, 'supplierId', Condition::Equal, $this->getId())
);

return match (true) {
count($deliveryNotes) > 0 => false,
count($plots) > 0 => false,
default => true,
};
}

public function save(): self
{
if ($this->getId() === null) {
Expand All @@ -30,6 +48,7 @@ public function save(): self
Convert::boolToInt($this->getHasNoPayout())
);
} else {
$this->checkAllowEdit();
Database::prepared(
'UPDATE ' . $this->getTableName() . ' SET `name`=?, isLocked=?, hasFullPayout=?, hasNoPayout=? WHERE id=?',
'siiii',
Expand Down
13 changes: 13 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ protected static function new(array $data = []): self
return new self($data);
}

public function allowDelete(): bool
{
$userRoles = UserRole::all(
UserRole::getQueryBuilder()->where(ColType::Int, 'userId', Condition::Equal, $this->getId())
);

return match (true) {
count($userRoles) > 0 => false,
default => true,
};
}

public function save(): self
{
if ($this->getId() === null) {
Expand All @@ -39,6 +51,7 @@ public function save(): self
$this->getLanguageId()
);
} else {
$this->checkAllowEdit();
Database::prepared(
'UPDATE ' . $this->getTableName() . ' SET firstname=?, lastname=?, username=?, password=?, isLocked=?, isPwdChangeForced=?, languageId=? WHERE id=?',
'ssssiiii',
Expand Down
1 change: 1 addition & 0 deletions app/Models/UserRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function save(): self
$this->getRoleId()
);
} else {
$this->checkAllowEdit();
Database::prepared(
'UPDATE ' . $this->getTableName() . ' SET userId=?, roleId=? WHERE id=?',
'iii',
Expand Down
1 change: 1 addition & 0 deletions app/Models/VolumeDistribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public function save(): self
$this->getAmount()
);
} else {
$this->checkAllowEdit();
Database::prepared(
'UPDATE ' . $this->getTableName() . ' SET deliveryNoteId=?, plotId=?, amount=? WHERE id=?',
'iidi',
Expand Down
Loading

0 comments on commit 76ce870

Please sign in to comment.