Skip to content

Commit

Permalink
rewrite entity state case to new EntityStateController.php
Browse files Browse the repository at this point in the history
  • Loading branch information
temaotl committed Aug 2, 2024
1 parent 7970dbc commit 80ed569
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 93 deletions.
68 changes: 0 additions & 68 deletions app/Http/Controllers/EntityController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
use App\Jobs\GitDeleteFromEdugain;
use App\Jobs\GitDeleteFromHfd;
use App\Jobs\GitDeleteFromRs;
use App\Jobs\GitRestoreToCategory;
use App\Jobs\GitRestoreToEdugain;
use App\Ldap\CesnetOrganization;
use App\Ldap\EduidczOrganization;
use App\Mail\NewIdentityProvider;
Expand All @@ -27,9 +25,7 @@
use App\Notifications\EntityEdugainStatusChanged;
use App\Notifications\EntityOperatorsChanged;
use App\Notifications\EntityRequested;
use App\Notifications\EntityStateChanged;
use App\Notifications\EntityUpdated;
use App\Notifications\FederationMemberChanged;
use App\Notifications\IdpCategoryChanged;
use App\Notifications\YourEntityRightsChanged;
use App\Services\NotificationService;
Expand Down Expand Up @@ -307,70 +303,6 @@ function () use ($entity) {
break;

case 'state':
$this->authorize('delete', $entity);

// TODO restore
if ($entity->trashed()) {
$entity->restore();

//TODO restore chain (functional)
/* Bus::chain([
new GitAddEntity($entity, Auth::user()),
new GitAddToHfd($entity, Auth::user()),
new GitRestoreToEdugain($entity, Auth::user()),
new GitRestoreToCategory($entity, Auth::user()),
function () use ($entity) {
$admins = User::activeAdmins()->select('id', 'email')->get();
Notification::send($entity->operators, new EntityStateChanged($entity));
Notification::send($admins, new EntityStateChanged($entity));
if ($entity->hfd) {
Notification::send($entity->operators, new EntityAddedToHfd($entity));
Notification::send(User::activeAdmins()->select('id', 'email')->get(), new EntityAddedToHfd($entity));
}
},
])->dispatch();*/

// TODO here M:N connection wit federation
/* foreach ($entity->federations as $federation) {
Bus::chain([
new GitAddMember($federation, $entity, Auth::user()),
function () use ($federation, $entity) {
$admins = User::activeAdmins()->select('id', 'email')->get();
Notification::send($federation->operators, new FederationMemberChanged($federation, $entity, 'added'));
Notification::send($admins, new FederationMemberChanged($federation, $entity, 'added'));
},
])->dispatch();
}*/
} else {
$entity->delete();

//TODO delete chain (functional)
/* Bus::chain([
new GitDeleteEntity($entity, Auth::user()),
new GitDeleteFromHfd($entity, Auth::user()),
new GitDeleteFromEdugain($entity, Auth::user()),
new GitDeleteFromCategory($entity->category ?? null, $entity, Auth::user()),
function () use ($entity) {
$admins = User::activeAdmins()->select('id', 'email')->get();
Notification::send($entity->operators, new EntityStateChanged($entity));
Notification::send($admins, new EntityStateChanged($entity));
if ($entity->hfd) {
Notification::send($entity->operators, new EntityDeletedFromHfd($entity));
Notification::send(User::activeAdmins()->select('id', 'email')->get(), new EntityDeletedFromHfd($entity));
}
},
])->dispatch();*/
}

$state = $entity->trashed() ? 'deleted' : 'restored';
$color = $entity->trashed() ? 'red' : 'green';

$locale = app()->getLocale();

return redirect()
->route('entities.show', $entity)
->with('status', __("entities.$state", ['name' => $entity->{"name_$locale"} ?? $entity->entityid]))
->with('color', $color);

break;

Expand Down
26 changes: 26 additions & 0 deletions app/Http/Controllers/EntityStateController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App\Http\Controllers;

use App\Models\Entity;

class EntityStateController extends Controller
{
public function state(Entity $entity)
{
$this->authorize('delete', $entity);

$entity->trashed() ? $entity->restore() : $entity->delete();

$state = $entity->trashed() ? 'deleted' : 'restored';
$color = $entity->trashed() ? 'red' : 'green';

$locale = app()->getLocale();

return redirect()
->route('entities.show', $entity)
->with('status', __("entities.$state", ['name' => $entity->{"name_$locale"} ?? $entity->entityid]))
->with('color', $color);

}
}
4 changes: 1 addition & 3 deletions resources/views/entities/partials/state.blade.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<form x-data="{ open: false }" class="inline-block" action="{{ route('entities.update', $entity) }}" method="POST">
<form x-data="{ open: false }" class="inline-block" action="{{ route('entities.state', $entity) }}" method="POST">
@csrf
@method('patch')
<input type="hidden" name="action" value="state">

@if ($entity->trashed())
<x-button @click.prevent="open =! open" color="green">{{ __('common.restore') }}</x-button>
@else
Expand Down
1 change: 0 additions & 1 deletion resources/views/federations/partials/state.blade.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<form x-data="{ open: false }" class="inline-block" action="{{ route('federations.state', $federation) }}" method="POST">
@csrf
@method('patch')

@if ($federation->trashed())
<x-button @click.prevent="open = !open" color="green">{{ __('common.restore') }}</x-button>
@else
Expand Down
3 changes: 3 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use App\Http\Controllers\EntityOrganizationController;
use App\Http\Controllers\EntityPreviewMetadataController;
use App\Http\Controllers\EntityRsController;
use App\Http\Controllers\EntityStateController;
use App\Http\Controllers\FakeController;
use App\Http\Controllers\FederationApprovalController;
use App\Http\Controllers\FederationController;
Expand Down Expand Up @@ -99,6 +100,8 @@
Route::post('{entity}/join', [EntityFederationController::class, 'store'])->name('join');
Route::post('{entity}/leave', [EntityFederationController::class, 'destroy'])->name('leave');

Route::patch('{entity}/state', [EntityStateController::class, 'state'])->name('state')->withTrashed();

Route::post('{entity}/rs', [EntityRsController::class, 'store'])->name('rs');

Route::get('{entity}/metadata', [EntityMetadataController::class, 'store'])->name('metadata');
Expand Down
26 changes: 5 additions & 21 deletions tests/Feature/Http/Controllers/EntityControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -680,30 +680,22 @@ public function a_user_with_operator_permission_can_change_an_existing_entities_
$this
->followingRedirects()
->actingAs($user)
->patch(route('entities.update', $entity), ['action' => 'state'])
->patch(route('entities.state', $entity))
->assertSeeText(__('entities.deleted', ['name' => $entity->name_en]));

/* Bus::assertDispatched(GitDeleteEntity::class, function ($job) use ($entity) {
return $job->entity->is($entity);
});*/

$entity->refresh();
$this->assertTrue($entity->trashed());
$this->assertEquals(route('entities.show', $entity), url()->current());

$this
->followingRedirects()
->actingAs($user)
->patch(route('entities.update', $entity), ['action' => 'state'])
->patch(route('entities.state', $entity))
->assertSeeText(__('entities.restored', ['name' => $entity->name_en]));

$entity->refresh();
$this->assertFalse($entity->trashed());
$this->assertEquals(route('entities.show', $entity), url()->current());

/* Bus::assertDispatched(GitAddEntity::class, function ($job) use ($entity) {
return $job->entity->is($entity);
});*/
}

/** @test */
Expand Down Expand Up @@ -811,7 +803,7 @@ public function a_user_without_operator_permission_cannot_change_an_existing_ent

$this
->actingAs($user)
->patch(route('entities.update', $entity), ['action' => 'state'])
->patch(route('entities.state', $entity))
->assertForbidden();

}
Expand Down Expand Up @@ -1344,30 +1336,22 @@ public function an_admin_can_change_an_existing_entities_state()
$this
->followingRedirects()
->actingAs($admin)
->patch(route('entities.update', $entity), ['action' => 'state'])
->patch(route('entities.state', $entity))
->assertSeeText(__('entities.deleted', ['name' => $entity->name_en]));

/* Bus::assertDispatched(GitDeleteEntity::class, function ($job) use ($entity) {
return $job->entity->is($entity);
});*/

$entity->refresh();
$this->assertTrue($entity->trashed());
$this->assertEquals(route('entities.show', $entity), url()->current());

$this
->followingRedirects()
->actingAs($admin)
->patch(route('entities.update', $entity), ['action' => 'state'])
->patch(route('entities.state', $entity))
->assertSeeText(__('entities.restored', ['name' => $entity->name_en]));

$entity->refresh();
$this->assertFalse($entity->trashed());
$this->assertEquals(route('entities.show', $entity), url()->current());

/* Bus::assertDispatched(GitAddEntity::class, function ($job) use ($entity) {
return $job->entity->is($entity);
});*/
}

/** @test */
Expand Down

0 comments on commit 80ed569

Please sign in to comment.