diff --git a/app/Http/Controllers/EntityController.php b/app/Http/Controllers/EntityController.php index 087e2e6..d77d791 100644 --- a/app/Http/Controllers/EntityController.php +++ b/app/Http/Controllers/EntityController.php @@ -23,12 +23,9 @@ use App\Notifications\EntityDeletedFromRs; use App\Notifications\EntityDestroyed; use App\Notifications\EntityEdugainStatusChanged; -use App\Notifications\EntityOperatorsChanged; use App\Notifications\EntityRequested; use App\Notifications\EntityUpdated; use App\Notifications\IdpCategoryChanged; -use App\Notifications\YourEntityRightsChanged; -use App\Services\NotificationService; use App\Traits\DumpFromGit\EntitiesHelp\DeleteFromEntity; use App\Traits\DumpFromGit\EntitiesHelp\UpdateEntity; use App\Traits\GitTrait; @@ -302,61 +299,6 @@ function () use ($entity) { break; - case 'state': - - break; - - case 'add_operators': - $this->authorize('update', $entity); - - if (! request('operators')) { - return to_route('entities.operators', $entity) - ->with('status', __('entities.add_empty_operators')) - ->with('color', 'red'); - } - - $old_operators = $entity->operators; - $new_operators = User::whereIn('id', request('operators'))->get(); - $entity->operators()->attach(request('operators')); - - $admins = User::activeAdmins()->select('id', 'email')->get(); - Notification::sendNow($new_operators, new YourEntityRightsChanged($entity, 'added')); - NotificationService::sendOperatorNotification($old_operators, new EntityOperatorsChanged($entity, $new_operators, 'added')); - - /* Notification::send($old_operators, new EntityOperatorsChanged($entity, $new_operators, 'added')); - Notification::send($admins, new EntityOperatorsChanged($entity, $new_operators, 'added'));*/ - - return redirect() - ->route('entities.show', $entity) - ->with('status', __('entities.operators_added')); - - break; - - case 'delete_operators': - $this->authorize('update', $entity); - - if (! request('operators')) { - return to_route('entities.operators', $entity) - ->with('status', __('entities.delete_empty_operators')) - ->with('color', 'red'); - } - - $old_operators = User::whereIn('id', request('operators'))->get(); - $entity->operators()->detach(request('operators')); - $new_operators = $entity->operators; - - $admins = User::activeAdmins()->select('id', 'email')->get(); - Notification::sendNow($old_operators, new YourEntityRightsChanged($entity, 'deleted')); - NotificationService::sendOperatorNotification($old_operators, new EntityOperatorsChanged($entity, $old_operators, 'deleted')); - /* Notification::send($new_operators, new EntityOperatorsChanged($entity, $old_operators, 'deleted')); - Notification::send($admins, new EntityOperatorsChanged($entity, $old_operators, 'deleted'));*/ - - return redirect() - ->route('entities.show', $entity) - ->with('status', __('entities.operators_deleted')); - - break; - case 'edugain': $this->authorize('update', $entity); diff --git a/app/Http/Controllers/EntityOperatorController.php b/app/Http/Controllers/EntityOperatorController.php index af50178..3433cf4 100644 --- a/app/Http/Controllers/EntityOperatorController.php +++ b/app/Http/Controllers/EntityOperatorController.php @@ -4,14 +4,13 @@ use App\Models\Entity; use App\Models\User; +use App\Notifications\EntityOperatorsChanged; +use App\Notifications\YourEntityRightsChanged; +use App\Services\NotificationService; +use Illuminate\Support\Facades\Notification; class EntityOperatorController extends Controller { - public function __construct() - { - - } - public function index(Entity $entity) { $this->authorize('view', $entity); @@ -29,4 +28,54 @@ public function index(Entity $entity) 'users' => $users, ]); } + + /** + * Store a newly created resource in storage. + */ + public function store(Entity $entity) + { + $this->authorize('update', $entity); + + if (! request('operators')) { + return to_route('entities.operators.index', $entity) + ->with('status', __('entities.add_empty_operators')) + ->with('color', 'red'); + } + + $old_operators = $entity->operators; + $new_operators = User::whereIn('id', request('operators'))->get(); + $entity->operators()->attach(request('operators')); + + Notification::sendNow($new_operators, new YourEntityRightsChanged($entity, 'added')); + NotificationService::sendOperatorNotification($old_operators, new EntityOperatorsChanged($entity, $new_operators, 'added')); + + return redirect() + ->route('entities.operators.index', $entity) + ->with('status', __('entities.operators_added')); + } + + /** + * Remove the specified resource from storage. + */ + public function destroy(Entity $entity) + { + $this->authorize('update', $entity); + + if (! request('operators')) { + return to_route('entities.operators.index', $entity) + ->with('status', __('entities.delete_empty_operators')) + ->with('color', 'red'); + } + + $old_operators = User::whereIn('id', request('operators'))->get(); + $entity->operators()->detach(request('operators')); + + Notification::sendNow($old_operators, new YourEntityRightsChanged($entity, 'deleted')); + NotificationService::sendOperatorNotification($old_operators, new EntityOperatorsChanged($entity, $old_operators, 'deleted')); + + return redirect() + ->route('entities.operators.index', $entity) + ->with('status', __('entities.operators_deleted')); + + } } diff --git a/resources/views/entities/navigation.blade.php b/resources/views/entities/navigation.blade.php index 9061b8e..4bcb8c9 100644 --- a/resources/views/entities/navigation.blade.php +++ b/resources/views/entities/navigation.blade.php @@ -21,7 +21,7 @@ ), 'cursor-default' => request()->routeIs('entities.operators'), ]) - href="{{ route('entities.operators', $entity) }}">{{ __('common.operators') }} + href="{{ route('entities.operators.index', $entity) }}">{{ __('common.operators') }} + @csrf +
+ + + + + + + + + + @forelse ($users as $user) + + + + + + @empty + + + + @endforelse + +
+   + + {{ __('common.name') }} + + {{ __('common.email') }} +
+ + + {{ $user->name }} + + {{ $user->email }} +
+ {{ __('common.no_operators') }}
+ {{ $users->links() }} + @if (count($users)) +
+ {{ __('common.add_operators') }} + + + + + {{ __('common.confirm_add_operators') }} + + {{ __('common.confirm_add_operators_body') }} + +
+ @endif +
+ diff --git a/resources/views/entities/operatorForm/delete.blade.php b/resources/views/entities/operatorForm/delete.blade.php new file mode 100644 index 0000000..58ed714 --- /dev/null +++ b/resources/views/entities/operatorForm/delete.blade.php @@ -0,0 +1,61 @@ +
+ @csrf + @method('DELETE') +
+ + + + + + + + + + @forelse ($operators->sortBy('name') as $operator) + + + + + + @empty + + + + @endforelse + +
+   + + {{ __('common.name') }} + + {{ __('common.email') }} +
+ + + {{ $operator->name }} + + {{ $operator->email }} +
+ {{ __('common.no_operators') }}
+ {{ $operators->links() }} + @if (count($operators)) +
+ {{ __('common.delete_operators') }} + + + + + {{ __('common.confirm_delete_operators') }} + + {{ __('common.confirm_delete_operators_body') }} + +
+ @endif +
+
diff --git a/resources/views/entities/operators.blade.php b/resources/views/entities/operators.blade.php index 3154a15..c939a23 100644 --- a/resources/views/entities/operators.blade.php +++ b/resources/views/entities/operators.blade.php @@ -9,68 +9,7 @@

{{ __('common.present_operators') }}

-
- @csrf - @method('patch') - -
- - - - - - - - - - @forelse ($operators->sortBy('name') as $operator) - - - - - - @empty - - - - @endforelse - -
-   - - {{ __('common.name') }} - - {{ __('common.email') }} -
- - - {{ $operator->name }} - - {{ $operator->email }} -
- {{ __('common.no_operators') }}
- {{ $operators->links() }} - @if (count($operators)) -
- {{ __('common.delete_operators') }} - - - - - {{ __('common.confirm_delete_operators') }} - - {{ __('common.confirm_delete_operators_body') }} - -
- @endif -
-
+ @include('entities.operatorForm.delete')
@@ -82,68 +21,7 @@ class="dark:bg-gray-700 px-6 py-3 text-xs tracking-widest text-left uppercase bg id="search" value="{{ request('search') }}" placeholder="{{ __('users.searchbox') }}">
-
- @csrf - @method('patch') - -
- - - - - - - - - - @forelse ($users as $user) - - - - - - @empty - - - - @endforelse - -
-   - - {{ __('common.name') }} - - {{ __('common.email') }} -
- - - {{ $user->name }} - - {{ $user->email }} -
- {{ __('common.no_operators') }}
- {{ $users->links() }} - @if (count($users)) -
- {{ __('common.add_operators') }} - - - - - {{ __('common.confirm_add_operators') }} - - {{ __('common.confirm_add_operators_body') }} - -
- @endif -
-
+ @include('entities.operatorForm.add') @else

{{ __('common.operators_list') }}

diff --git a/routes/web.php b/routes/web.php index 790d628..38eb12b 100644 --- a/routes/web.php +++ b/routes/web.php @@ -95,11 +95,13 @@ Route::post('import', [EntityManagementController::class, 'store'])->name('import'); Route::get('refresh', [EntityManagementController::class, 'update'])->name('refresh'); - Route::get('{entity}/operators', [EntityOperatorController::class, 'index'])->name('operators')->withTrashed(); Route::get('{entity}/federations', [EntityFederationController::class, 'index'])->name('federations')->withTrashed(); Route::post('{entity}/join', [EntityFederationController::class, 'store'])->name('join'); Route::post('{entity}/leave', [EntityFederationController::class, 'destroy'])->name('leave'); + Route::resource('{entity}/operators', EntityOperatorController::class)->only(['index', 'store'])->withTrashed(); + Route::delete('{entity}/operators', [EntityOperatorController::class, 'destroy'])->name('operators.destroy')->withTrashed(); + Route::patch('{entity}/state', [EntityStateController::class, 'state'])->name('state')->withTrashed(); Route::post('{entity}/rs', [EntityRsController::class, 'store'])->name('rs'); diff --git a/tests/Feature/Http/Controllers/EntityControllerTest.php b/tests/Feature/Http/Controllers/EntityControllerTest.php index d9edc71..131a4e3 100644 --- a/tests/Feature/Http/Controllers/EntityControllerTest.php +++ b/tests/Feature/Http/Controllers/EntityControllerTest.php @@ -712,28 +712,26 @@ public function a_user_with_operator_permission_can_change_an_existing_entities_ $this ->followingRedirects() ->actingAs($user) - ->patch(route('entities.update', $entity), [ - 'action' => 'add_operators', + ->post(route('entities.operators.store', $entity), [ 'operators' => [$new_operator->id], ]) ->assertSeeText(__('entities.operators_added')); $entity->refresh(); $this->assertEquals(2, $entity->operators()->count()); - $this->assertEquals(route('entities.show', $entity), url()->current()); + $this->assertEquals(route('entities.operators.index', $entity), url()->current()); $this ->followingRedirects() ->actingAs($user) - ->patch(route('entities.update', $entity), [ - 'action' => 'delete_operators', + ->delete(route('entities.operators.destroy', $entity), [ 'operators' => [$new_operator->id], ]) ->assertSeeText(__('entities.operators_deleted')); $entity->refresh(); $this->assertEquals(1, $entity->operators()->count()); - $this->assertEquals(route('entities.show', $entity), url()->current()); + $this->assertEquals(route('entities.operators.index', $entity), url()->current()); } @@ -818,8 +816,7 @@ public function a_user_without_operator_permission_cannot_change_an_existing_ent $this ->actingAs($user) - ->patch(route('entities.update', $entity), [ - 'action' => 'add_operators', + ->post(route('entities.operators.store', $entity), [ 'operators' => [$new_operator->id], ]) ->assertForbidden(); @@ -829,8 +826,7 @@ public function a_user_without_operator_permission_cannot_change_an_existing_ent $this ->actingAs($user) - ->patch(route('entities.update', $entity), [ - 'action' => 'delete_operators', + ->delete(route('entities.operators.destroy', $entity), [ 'operators' => [$new_operator->id], ]) ->assertForbidden(); @@ -1369,48 +1365,46 @@ public function an_admin_can_change_an_existing_entities_operators() $this ->followingRedirects() ->actingAs($admin) - ->patch(route('entities.update', $entity), ['action' => 'add_operators']) + ->post(route('entities.operators.store', $entity)) ->assertSeeText(__('entities.add_empty_operators')); $entity->refresh(); $this->assertEquals(0, $entity->operators()->count()); - $this->assertEquals(route('entities.operators', $entity), url()->current()); + $this->assertEquals(route('entities.operators.index', $entity), url()->current()); $this ->followingRedirects() ->actingAs($admin) - ->patch(route('entities.update', $entity), [ - 'action' => 'add_operators', + ->post(route('entities.operators.store', $entity), [ 'operators' => [$new_operator->id], ]) ->assertSeeText(__('entities.operators_added')); $entity->refresh(); $this->assertEquals(1, $entity->operators()->count()); - $this->assertEquals(route('entities.show', $entity), url()->current()); + $this->assertEquals(route('entities.operators.index', $entity), url()->current()); $this ->followingRedirects() ->actingAs($admin) - ->patch(route('entities.update', $entity), ['action' => 'delete_operators']) + ->delete(route('entities.operators.destroy', $entity)) ->assertSeeText(__('entities.delete_empty_operators')); $entity->refresh(); $this->assertEquals(1, $entity->operators()->count()); - $this->assertEquals(route('entities.operators', $entity), url()->current()); + $this->assertEquals(route('entities.operators.index', $entity), url()->current()); $this ->followingRedirects() ->actingAs($admin) - ->patch(route('entities.update', $entity), [ - 'action' => 'delete_operators', + ->delete(route('entities.operators.destroy', $entity), [ 'operators' => [$new_operator->id], ]) ->assertSeeText(__('entities.operators_deleted')); $entity->refresh(); $this->assertEquals(0, $entity->operators()->count()); - $this->assertEquals(route('entities.show', $entity), url()->current()); + $this->assertEquals(route('entities.operators.index', $entity), url()->current()); }