Skip to content

Commit

Permalink
[AIDAPP-284]: Ensure that the system status of New cannot be deleted …
Browse files Browse the repository at this point in the history
…or edited (#270)

* Add the new is_system_protected column to the service_request_statuses table

Signed-off-by: Kevin Ullyott <kevin.ullyott@canyongbs.com>

* Create a migration to mark the new status as protected

Signed-off-by: Kevin Ullyott <kevin.ullyott@canyongbs.com>

* Only create the new status if it does not already exist in seeder

Signed-off-by: Kevin Ullyott <kevin.ullyott@canyongbs.com>

* Fix the data migration

Signed-off-by: Kevin Ullyott <kevin.ullyott@canyongbs.com>

* Update the Service Request Status Policy to prevent update and delete of system protected

Signed-off-by: Kevin Ullyott <kevin.ullyott@canyongbs.com>

* Setup Feature Flag

Signed-off-by: Kevin Ullyott <kevin.ullyott@canyongbs.com>

* Add lock icon to Service Request Type view page

Signed-off-by: Kevin Ullyott <kevin.ullyott@canyongbs.com>

* Fix issue with data seed

Signed-off-by: Kevin Ullyott <kevin.ullyott@canyongbs.com>

* Adjust edit tests

Signed-off-by: Kevin Ullyott <kevin.ullyott@canyongbs.com>

* Update the ViewServiceRequestStatusTest cases

Signed-off-by: Kevin Ullyott <kevin.ullyott@canyongbs.com>

* Add Postgres protections from modifying system protected rows

Signed-off-by: Kevin Ullyott <kevin.ullyott@canyongbs.com>

* Fix the trigger

Signed-off-by: Kevin Ullyott <kevin.ullyott@canyongbs.com>

* Don't modify the Service Request status

Signed-off-by: Kevin Ullyott <kevin.ullyott@canyongbs.com>

* Update the bulk delete option

Signed-off-by: Kevin Ullyott <kevin.ullyott@canyongbs.com>

* Finish updating the bulk delete option

Signed-off-by: Kevin Ullyott <kevin.ullyott@canyongbs.com>

* chore: fix enforcement of copyright on all files

* chore: fix code style

* Fix CreateServiceRequestStatusTest

Signed-off-by: Kevin Ullyott <kevin.ullyott@canyongbs.com>

* Fix ListServiceRequestStatusesTest

Signed-off-by: Kevin Ullyott <kevin.ullyott@canyongbs.com>

---------

Signed-off-by: Kevin Ullyott <kevin.ullyott@canyongbs.com>
Co-authored-by: joelicatajr <joelicatajr@users.noreply.github.com>
  • Loading branch information
Orrison and joelicatajr authored Oct 7, 2024
1 parent 6bcf62d commit 9b777eb
Show file tree
Hide file tree
Showing 17 changed files with 574 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,15 @@ public function definition(): array
'classification' => $this->faker->randomElement(SystemServiceRequestClassification::cases()),
'name' => $this->faker->word,
'color' => $this->faker->randomElement(ColumnColorOptions::cases()),
'is_system_protected' => false,
];
}

public function systemProtected(): self
{
return $this->state(fn (): array => ['is_system_protected' => true]);
}

public function open(): Factory
{
return $this->state(function (array $attributes) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

/*
<COPYRIGHT>
Copyright © 2016-2024, Canyon GBS LLC. All rights reserved.
Aiding App™ is licensed under the Elastic License 2.0. For more details,
see <https://github.com/canyongbs/aidingapp/blob/main/LICENSE.>
Notice:
- You may not provide the software to third parties as a hosted or managed
service, where the service provides users with access to any substantial set of
the features or functionality of the software.
- You may not move, change, disable, or circumvent the license key functionality
in the software, and you may not remove or obscure any functionality in the
software that is protected by the license key.
- You may not alter, remove, or obscure any licensing, copyright, or other notices
of the licensor in the software. Any use of the licensor’s trademarks is subject
to applicable law.
- Canyon GBS LLC respects the intellectual property rights of others and expects the
same in return. Canyon GBS™ and Aiding App™ are registered trademarks of
Canyon GBS LLC, and we are committed to enforcing and protecting our trademarks
vigorously.
- The software solution, including services, infrastructure, and code, is offered as a
Software as a Service (SaaS) by Canyon GBS LLC.
- Use of this software implies agreement to the license terms and conditions as stated
in the Elastic License 2.0.
For more information or inquiries please visit our website at
<https://www.canyongbs.com> or contact us via email at legal@canyongbs.com.
</COPYRIGHT>
*/

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

return new class () extends Migration {
public function up(): void
{
Schema::table('service_request_statuses', function (Blueprint $table) {
$table->boolean('is_system_protected')->default(false);
});
}

public function down(): void
{
Schema::table('service_request_statuses', function (Blueprint $table) {
$table->dropColumn('is_system_protected');
});
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

/*
<COPYRIGHT>
Copyright © 2016-2024, Canyon GBS LLC. All rights reserved.
Aiding App™ is licensed under the Elastic License 2.0. For more details,
see <https://github.com/canyongbs/aidingapp/blob/main/LICENSE.>
Notice:
- You may not provide the software to third parties as a hosted or managed
service, where the service provides users with access to any substantial set of
the features or functionality of the software.
- You may not move, change, disable, or circumvent the license key functionality
in the software, and you may not remove or obscure any functionality in the
software that is protected by the license key.
- You may not alter, remove, or obscure any licensing, copyright, or other notices
of the licensor in the software. Any use of the licensor’s trademarks is subject
to applicable law.
- Canyon GBS LLC respects the intellectual property rights of others and expects the
same in return. Canyon GBS™ and Aiding App™ are registered trademarks of
Canyon GBS LLC, and we are committed to enforcing and protecting our trademarks
vigorously.
- The software solution, including services, infrastructure, and code, is offered as a
Software as a Service (SaaS) by Canyon GBS LLC.
- Use of this software implies agreement to the license terms and conditions as stated
in the Elastic License 2.0.
For more information or inquiries please visit our website at
<https://www.canyongbs.com> or contact us via email at legal@canyongbs.com.
</COPYRIGHT>
*/

use Illuminate\Support\Str;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Migrations\Migration;

return new class () extends Migration {
public function up(): void
{
$status = DB::table('service_request_statuses')->where([
'name' => 'New',
'classification' => 'open',
])->first();

if ($status && $status->is_system_protected) {
return;
}

if ($status) {
DB::table('service_request_statuses')
->where('name', 'New')
->where('classification', 'open')
->update([
'is_system_protected' => true,
]);

return;
}

DB::table('service_request_statuses')->insert([
'id' => Str::orderedUuid(),
'classification' => 'open',
'name' => 'New',
'color' => 'info',
'is_system_protected' => true,
'created_at' => now(),
]);
}

public function down(): void
{
DB::table('service_request_statuses')
->where('name', 'New')
->where('classification', 'open')
->update([
'is_system_protected' => false,
]);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,16 @@ class ServiceRequestStatusSeeder extends Seeder
{
public function run(): void
{
ServiceRequestStatus::query()->createOrFirst([
'classification' => SystemServiceRequestClassification::Open,
'name' => 'New',
'color' => ColumnColorOptions::Info,
'is_system_protected' => true,
]);

ServiceRequestStatus::factory()
->createMany(
[
[
'classification' => SystemServiceRequestClassification::Open,
'name' => 'New',
'color' => ColumnColorOptions::Info,
],
[
'classification' => SystemServiceRequestClassification::InProgress,
'name' => 'In-Progress',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{{--
<COPYRIGHT>
Copyright © 2016-2024, Canyon GBS LLC. All rights reserved.
Aiding App™ is licensed under the Elastic License 2.0. For more details,
see <https://github.com/canyongbs/aidingapp/blob/main/LICENSE.>
Notice:
- You may not provide the software to third parties as a hosted or managed
service, where the service provides users with access to any substantial set of
the features or functionality of the software.
- You may not move, change, disable, or circumvent the license key functionality
in the software, and you may not remove or obscure any functionality in the
software that is protected by the license key.
- You may not alter, remove, or obscure any licensing, copyright, or other notices
of the licensor in the software. Any use of the licensor’s trademarks is subject
to applicable law.
- Canyon GBS LLC respects the intellectual property rights of others and expects the
same in return. Canyon GBS™ and Aiding App™ are registered trademarks of
Canyon GBS LLC, and we are committed to enforcing and protecting our trademarks
vigorously.
- The software solution, including services, infrastructure, and code, is offered as a
Software as a Service (SaaS) by Canyon GBS LLC.
- Use of this software implies agreement to the license terms and conditions as stated
in the Elastic License 2.0.
For more information or inquiries please visit our website at
<https://www.canyongbs.com> or contact us via email at legal@canyongbs.com.
</COPYRIGHT>
--}}
@php
use App\Features\ServiceRequestStatusSystemProtection;
@endphp

@if (ServiceRequestStatusSystemProtection::active() && $this->getRecord()?->is_system_protected)
<x-filament::icon-button
data-identifier="service_request_type_system_protected"
icon="heroicon-m-lock-closed"
color="gray"
size="lg"
tooltip="This record is protected as it is a system status."
/>
@endif
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,13 @@

use Filament\Tables\Table;
use Filament\Actions\CreateAction;
use Illuminate\Support\Collection;
use Filament\Tables\Actions\EditAction;
use Filament\Tables\Actions\ViewAction;
use Filament\Tables\Columns\TextColumn;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\QueryException;
use Filament\Notifications\Notification;
use App\Filament\Tables\Columns\IdColumn;
use Filament\Resources\Pages\ListRecords;
use Filament\Tables\Filters\TrashedFilter;
Expand Down Expand Up @@ -83,7 +87,49 @@ public function table(Table $table): Table
])
->bulkActions([
BulkActionGroup::make([
DeleteBulkAction::make(),
DeleteBulkAction::make()
->action(function (DeleteBulkAction $component): void {
$total = 0;
$totalDeleted = 0;

$component->process(static function (Collection $records) use (&$total, &$totalDeleted) {
$total = $records->count();

$records->each(function (Model $record) use (&$totalDeleted) {
try {
$record->delete();

$totalDeleted++;
} catch (QueryException $e) {
if (str_contains($e->getMessage(), 'Cannot modify system protected rows')) {
Notification::make()
->title('Cannot Delete System Protected record')
->body('A system protected record cannot be deleted.')
->danger()
->send();
}
}
});
});

$notification = Notification::make()
->title('Service Request Statuses Deleted')
->body("{$totalDeleted} of {$total} selected service request statuses have been deleted.");

if ($totalDeleted > 0) {
$notification->success();
} else {
$notification->danger();
}

$notification->send();

if ($totalDeleted > 0) {
$component->dispatchSuccessRedirect();
} else {
$component->dispatchFailureRedirect();
}
}),
]),
])
->filters([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@

use Filament\Actions\EditAction;
use Filament\Infolists\Infolist;
use Filament\View\PanelsRenderHook;
use Illuminate\Contracts\View\View;
use Filament\Resources\Pages\ViewRecord;
use Filament\Infolists\Components\Section;
use Filament\Support\Facades\FilamentView;
use Filament\Infolists\Components\TextEntry;
use AidingApp\ServiceManagement\Models\ServiceRequestStatus;
use AidingApp\ServiceManagement\Filament\Resources\ServiceRequestStatusResource;
Expand All @@ -48,6 +51,15 @@ class ViewServiceRequestStatus extends ViewRecord
{
protected static string $resource = ServiceRequestStatusResource::class;

public function boot(): void
{
FilamentView::registerRenderHook(
PanelsRenderHook::PAGE_HEADER_ACTIONS_AFTER,
fn (): View => view('service-management::filament.pages.service-request-type-lock-icon'),
scopes: static::class,
);
}

public function infolist(Infolist $infolist): Infolist
{
return $infolist
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,13 @@ class ServiceRequestStatus extends BaseModel implements Auditable
'classification',
'name',
'color',
'is_system_protected',
];

protected $casts = [
'classification' => SystemServiceRequestClassification::class,
'color' => ColumnColorOptions::class,
'is_system_protected' => 'boolean',
];

public function serviceRequests(): HasMany
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
use Illuminate\Support\Facades\Gate;
use AidingApp\Contact\Models\Contact;
use App\Support\FeatureAccessResponse;
use App\Features\ServiceRequestStatusSystemProtection;
use AidingApp\ServiceManagement\Models\ServiceRequestStatus;

class ServiceRequestStatusPolicy
Expand Down Expand Up @@ -87,6 +88,10 @@ public function create(Authenticatable $authenticatable): Response

public function update(Authenticatable $authenticatable, ServiceRequestStatus $serviceRequestStatus): Response
{
if (ServiceRequestStatusSystemProtection::active() && $serviceRequestStatus->is_system_protected) {
return Response::deny('You cannot update this service request status because it is system protected.');
}

return $authenticatable->canOrElse(
abilities: ['service_request_status.*.update', "service_request_status.{$serviceRequestStatus->id}.update"],
denyResponse: 'You do not have permissions to update this service request status.'
Expand All @@ -95,6 +100,10 @@ public function update(Authenticatable $authenticatable, ServiceRequestStatus $s

public function delete(Authenticatable $authenticatable, ServiceRequestStatus $serviceRequestStatus): Response
{
if (ServiceRequestStatusSystemProtection::active() && $serviceRequestStatus->is_system_protected) {
return Response::deny('You cannot delete this service request status because it is system protected.');
}

return $authenticatable->canOrElse(
abilities: ['service_request_status.*.delete', "service_request_status.{$serviceRequestStatus->id}.delete"],
denyResponse: 'You do not have permissions to delete this service request status.'
Expand All @@ -111,6 +120,10 @@ public function restore(Authenticatable $authenticatable, ServiceRequestStatus $

public function forceDelete(Authenticatable $authenticatable, ServiceRequestStatus $serviceRequestStatus): Response
{
if (ServiceRequestStatusSystemProtection::active() && $serviceRequestStatus->is_system_protected) {
return Response::deny('You cannot delete this service request status because it is system protected.');
}

if ($serviceRequestStatus->serviceRequests()->exists()) {
return Response::deny('You cannot force delete this service request status because it has associated service requests.');
}
Expand Down
Loading

0 comments on commit 9b777eb

Please sign in to comment.