Skip to content

Commit

Permalink
rewrite hfd from EntityController.php
Browse files Browse the repository at this point in the history
  • Loading branch information
temaotl committed Aug 2, 2024
1 parent 564776f commit d68a8ec
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 133 deletions.
196 changes: 67 additions & 129 deletions app/Http/Controllers/EntityController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,12 @@
namespace App\Http\Controllers;

use App\Http\Requests\StoreEntity;
use App\Jobs\GitAddToHfd;
use App\Jobs\GitDeleteFromHfd;
use App\Ldap\CesnetOrganization;
use App\Ldap\EduidczOrganization;
use App\Mail\NewIdentityProvider;
use App\Models\Category;
use App\Models\Entity;
use App\Models\Federation;
use App\Models\User;
use App\Notifications\EntityAddedToHfd;
use App\Notifications\EntityDeletedFromHfd;
use App\Notifications\EntityDestroyed;
use App\Notifications\EntityRequested;
use App\Notifications\EntityUpdated;
Expand All @@ -25,7 +20,6 @@
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\Facades\Storage;

Expand All @@ -34,11 +28,6 @@ class EntityController extends Controller
use DeleteFromEntity,UpdateEntity;
use GitTrait, ValidatorTrait;

public function __construct()
{

}

/**
* Display a listing of the resource.
*
Expand Down Expand Up @@ -193,143 +182,92 @@ public function edit(Entity $entity)
*/
public function update(Request $request, Entity $entity)
{
switch (request('action')) {
case 'update':
$this->authorize('update', $entity);
$this->authorize('update', $entity);

$validated = $request->validate([
'metadata' => 'nullable|string',
'file' => 'required_without:metadata|file',
]);
$validated = $request->validate([
'metadata' => 'nullable|string',
'file' => 'required_without:metadata|file',
]);

$metadata = $this->getMetadata($request);
if (! $metadata) {
return redirect()
->back()
->with('status', __('entities.metadata_couldnt_be_read'))
->with('color', 'red');
}
$metadata = $this->getMetadata($request);
if (! $metadata) {
return redirect()
->back()
->with('status', __('entities.metadata_couldnt_be_read'))
->with('color', 'red');
}

$result = json_decode($this->validateMetadata($metadata), true);
$updated_entity = json_decode($this->parseMetadata($metadata), true);
$result = json_decode($this->validateMetadata($metadata), true);
$updated_entity = json_decode($this->parseMetadata($metadata), true);

if (array_key_exists('result', $updated_entity) && ! is_null($updated_entity['result'])) {
return redirect()
->back()
->with('status', __('entities.no_metadata'))
->with('color', 'red');
}
if (array_key_exists('result', $updated_entity) && ! is_null($updated_entity['result'])) {
return redirect()
->back()
->with('status', __('entities.no_metadata'))
->with('color', 'red');
}

if ($entity->entityid !== $updated_entity['entityid']) {
return redirect()
->back()
->with('status', __('entities.different_entityid'))
->with('color', 'red');
}
if ($entity->entityid !== $updated_entity['entityid']) {
return redirect()
->back()
->with('status', __('entities.different_entityid'))
->with('color', 'red');
}

switch ($result['code']) {
case '0':

$xml_file = $this->deleteTags($updated_entity['metadata']);

DB::transaction(function () use ($entity, $updated_entity, $xml_file) {
$entity->update([
'name_en' => $updated_entity['name_en'],
'name_cs' => $updated_entity['name_cs'],
'description_en' => $updated_entity['description_en'],
'description_cs' => $updated_entity['description_cs'],
'cocov1' => $updated_entity['cocov1'],
'sirtfi' => $updated_entity['sirtfi'],
'metadata' => $updated_entity['metadata'],
'xml_file' => $xml_file,
]);

if ($entity->type->value === 'idp') {
$entity->update(['rs' => $updated_entity['rs']]);
}
});

if (! $entity->wasChanged()) {
return redirect()
->back()
->with('status', __('entities.not_changed'));
}

// TODO entityUpdated (functional)
/* Bus::chain([
new GitUpdateEntity($entity, Auth::user()),
function () use ($entity) {
$admins = User::activeAdmins()->select('id', 'email')->get();
Notification::send($entity->operators, new EntityUpdated($entity));
Notification::send($admins, new EntityUpdated($entity));
},
])->dispatch();*/

return redirect()
->route('entities.show', $entity)
->with('status', __('entities.entity_updated')." {$result['message']}");

break;

case '1':
return redirect()
->back()
->with('status', "{$result['error']} {$result['message']}")
->with('color', 'red');

break;

default:
return redirect()
->back()
->with('status', __('entities.unknown_error_while_registration'))
->with('color', 'red');

break;
}
switch ($result['code']) {
case '0':

break;
$xml_file = $this->deleteTags($updated_entity['metadata']);

DB::transaction(function () use ($entity, $updated_entity, $xml_file) {
$entity->update([
'name_en' => $updated_entity['name_en'],
'name_cs' => $updated_entity['name_cs'],
'description_en' => $updated_entity['description_en'],
'description_cs' => $updated_entity['description_cs'],
'cocov1' => $updated_entity['cocov1'],
'sirtfi' => $updated_entity['sirtfi'],
'metadata' => $updated_entity['metadata'],
'xml_file' => $xml_file,
]);

case 'hfd':
$this->authorize('do-everything');
if ($entity->type->value === 'idp') {
$entity->update(['rs' => $updated_entity['rs']]);
}
});

if ($entity->type->value !== 'idp') {
if (! $entity->wasChanged()) {
return redirect()
->back()
->with('status', __('categories.hfd_controlled_for_idps_only'));
->with('status', __('entities.not_changed'));
}

$entity = DB::transaction(function () use ($entity) {
$entity->hfd = $entity->hfd ? false : true;
$entity->update();

return $entity;
});

$status = $entity->hfd ? 'hfd' : 'no_hfd';
$color = $entity->hfd ? 'red' : 'green';

//TODO change HfD status (not working)
/* if ($entity->hfd) {
GitAddToHfd::dispatch($entity, Auth::user());
Notification::send($entity->operators, new EntityAddedToHfd($entity));
Notification::send(User::activeAdmins()->select('id', 'email')->get(), new EntityAddedToHfd($entity));
} else {
GitDeleteFromHfd::dispatch($entity, Auth::user());
Mail::to(config('mail.ra.address'))->send(new NewIdentityProvider($entity));
Notification::send($entity->operators, new EntityDeletedFromHfd($entity));
Notification::send(User::activeAdmins()->select('id', 'email')->get(), new EntityDeletedFromHfd($entity));
}*/
// TODO entityUpdated (functional)
/* Bus::chain([
new GitUpdateEntity($entity, Auth::user()),
function () use ($entity) {
$admins = User::activeAdmins()->select('id', 'email')->get();
Notification::send($entity->operators, new EntityUpdated($entity));
Notification::send($admins, new EntityUpdated($entity));
},
])->dispatch();*/

return redirect()
->route('entities.show', $entity)
->with('status', __("entities.$status"))
->with('color', $color);
->with('status', __('entities.entity_updated')." {$result['message']}");

break;
case '1':
return redirect()
->back()
->with('status', "{$result['error']} {$result['message']}")
->with('color', 'red');

default:
return redirect()->back();
return redirect()
->back()
->with('status', __('entities.unknown_error_while_registration'))
->with('color', 'red');

}
}

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

namespace App\Http\Controllers;

use App\Models\Entity;
use Illuminate\Support\Facades\DB;

class EntityHfdController extends Controller
{
public function update(Entity $entity)
{
$this->authorize('do-everything');

if ($entity->type->value !== 'idp') {
return redirect()
->back()
->with('status', __('categories.hfd_controlled_for_idps_only'));
}

$entity = DB::transaction(function () use ($entity) {
$entity->hfd = $entity->hfd ? false : true;
$entity->update();

return $entity;
});

$status = $entity->hfd ? 'hfd' : 'no_hfd';
$color = $entity->hfd ? 'red' : 'green';

//TODO change HfD status (not working)
/* if ($entity->hfd) {
GitAddToHfd::dispatch($entity, Auth::user());
Notification::send($entity->operators, new EntityAddedToHfd($entity));
Notification::send(User::activeAdmins()->select('id', 'email')->get(), new EntityAddedToHfd($entity));
} else {
GitDeleteFromHfd::dispatch($entity, Auth::user());
Mail::to(config('mail.ra.address'))->send(new NewIdentityProvider($entity));
Notification::send($entity->operators, new EntityDeletedFromHfd($entity));
Notification::send(User::activeAdmins()->select('id', 'email')->get(), new EntityDeletedFromHfd($entity));
}*/

return redirect()
->route('entities.show', $entity)
->with('status', __("entities.$status"))
->with('color', $color);
}
}
5 changes: 1 addition & 4 deletions resources/views/entities/partials/hfd.blade.php
Original file line number Diff line number Diff line change
@@ -1,9 +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.hfd', $entity) }}" method="POST">
@csrf
@method('patch')

<input type="hidden" name="action" value="hfd">

<input type="checkbox" name="hfdbox" @click.prevent="open = !open" @if ($entity->hfd) checked @endif>

<x-modal>
Expand Down
2 changes: 2 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Http\Controllers\EntityController;
use App\Http\Controllers\EntityEduGainController;
use App\Http\Controllers\EntityFederationController;
use App\Http\Controllers\EntityHfdController;
use App\Http\Controllers\EntityManagementController;
use App\Http\Controllers\EntityMetadataController;
use App\Http\Controllers\EntityOperatorController;
Expand Down Expand Up @@ -111,6 +112,7 @@
Route::patch('{entity}/rs', [EntityRsController::class, 'rsState'])->name('rs.state')->withTrashed();

Route::patch('{entity}/category', [EntityCategoryController::class, 'update'])->name('category.update');
Route::patch('{entity}/hfd', [EntityHfdController::class, 'update'])->name('hfd');

Route::get('{entity}/metadata', [EntityMetadataController::class, 'store'])->name('metadata');
Route::get('{entity}/showmetadata', [EntityMetadataController::class, 'show'])->name('showmetadata');
Expand Down

0 comments on commit d68a8ec

Please sign in to comment.