Skip to content

Commit

Permalink
Merge pull request #24 from CESNET/FederationJob-feature
Browse files Browse the repository at this point in the history
Federation job feature
  • Loading branch information
temaotl authored Jul 2, 2024
2 parents 5ddd507 + 31567cd commit 6473425
Show file tree
Hide file tree
Showing 15 changed files with 171 additions and 127 deletions.
23 changes: 16 additions & 7 deletions app/Console/Commands/DumpFromGit.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@


use App\Facades\EntityFacade;
use App\Models\Membership;
use App\Models\User;
use App\Traits\DumpFromGit\CreateCategoriesAndGroupsTrait;
use App\Traits\DumpFromGit\CreateEntitiesTrait;
use App\Traits\DumpFromGit\CreateFederationTrait;
use App\Traits\DumpFromGit\EntitiesHelp\FixEntityTrait;
use App\Traits\DumpFromGit\EntitiesHelp\UpdateEntity;
use App\Traits\EdugainTrait;
use App\Traits\EntityFolderTrait;
use App\Traits\FederationTrait;
use App\Traits\GitTrait;
use Exception;
use Illuminate\Console\Command;
use App\Traits\ValidatorTrait;
use Illuminate\Support\Facades\Artisan;


class DumpFromGit extends Command
{
use GitTrait, ValidatorTrait,EntityFolderTrait;
use GitTrait, ValidatorTrait;
use CreateFederationTrait,CreateEntitiesTrait,CreateCategoriesAndGroupsTrait;
use UpdateEntity,FederationTrait,FixEntityTrait;
use EdugainTrait;
Expand All @@ -38,18 +38,27 @@ class DumpFromGit extends Command
*
* @var string
*/
protected $description = 'Command description';
protected $description = 'Dump all old information from git';

private function createMetadataFiles(): void
{
$this->updateFederationFolders();
$membership = Membership::select('entity_id','federation_id')->whereApproved(1)->get();
foreach ($membership as $member) {
EntityFacade::saveMetadataToFederationFolder($member->entity_id, $member->federation_id);
}
}


/**
* Execute the console command.
* @throws \Exception no amin
* @throws Exception no amin
*/
public function handle()
{
$firstAdminId = User::where('admin', 1)->first()->id;
if(empty($firstAdminId))
throw new \Exception('firstAdminId is null');
throw new Exception('firstAdminId is null');


$this->initializeGit();
Expand All @@ -60,7 +69,7 @@ public function handle()
$this->updateEntitiesXml();
$this->updateFederationFolders();
$this->fixEntities();
$this->createAllMetadataFiles();
$this->createMetadataFiles();
$this->makeEdu2Edugain();


Expand Down
28 changes: 28 additions & 0 deletions app/Events/FederationApprove.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\Events;

use App\Models\Federation;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class FederationApprove
{
use Dispatchable, InteractsWithSockets, SerializesModels;

public Federation $federation;

/**
* Create a new event instance.
*/
public function __construct(Federation $federation)
{
$this->federation = $federation;
}

}
6 changes: 6 additions & 0 deletions app/Http/Controllers/EntityController.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
use App\Notifications\FederationMemberChanged;
use App\Notifications\IdpCategoryChanged;
use App\Notifications\YourEntityRightsChanged;
use App\Traits\DumpFromGit\EntitiesHelp\DeleteFromEntity;
use App\Traits\DumpFromGit\EntitiesHelp\UpdateEntity;
use App\Traits\GitTrait;
use App\Traits\ValidatorTrait;
use Illuminate\Http\Request;
Expand All @@ -50,6 +52,7 @@
class EntityController extends Controller
{
use ValidatorTrait, GitTrait;
use DeleteFromEntity,UpdateEntity;

public function __construct()
{
Expand Down Expand Up @@ -127,13 +130,16 @@ public function store(StoreEntity $request)
if ($new_entity['type'] === 'idp') {
$new_entity = array_merge($new_entity, ['hfd' => true]);
}
$new_entity= array_merge($new_entity, ['xml_file' => $this->deleteTags($new_entity['metadata']) ]);

$entity = Entity::create($new_entity);
$entity->operators()->attach(Auth::id());
$entity->federations()->attach($federation, [
'explanation' => request('explanation'),
'requested_by' => Auth::id(),
]);

$this->updateEntityXml(Entity::where('id', $entity['id'])->first());
return $entity;
});

Expand Down
13 changes: 6 additions & 7 deletions app/Http/Controllers/FederationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@

use App\Http\Requests\StoreFederation;
use App\Http\Requests\UpdateFederation;
use App\Jobs\GitAddFederation;
use App\Jobs\GitAddMembers;
use App\Jobs\GitDeleteFederation;
use App\Jobs\GitDeleteMembers;
use App\Jobs\GitUpdateFederation;
use App\Models\Entity;
use App\Models\Federation;
use App\Models\User;
Expand Down Expand Up @@ -157,7 +154,7 @@ public function update(UpdateFederation $request, Federation $federation)
$federation->approved = true;
$federation->update();

GitAddFederation::dispatch($federation, 'approve', Auth::user());
//GitAddFederation::dispatch($federation, 'approve', Auth::user());
Notification::send($federation->operators, new FederationApproved($federation));
Notification::send(User::activeAdmins()->select('id', 'email')->get(), new FederationApproved($federation));

Expand Down Expand Up @@ -194,7 +191,7 @@ public function update(UpdateFederation $request, Federation $federation)
->route('federations.show', $federation);
}

GitUpdateFederation::dispatch($federation, Auth::user());
// GitUpdateFederation::dispatch($federation, Auth::user());
Notification::send($federation->operators, new FederationUpdated($federation));
Notification::send(User::activeAdmins()->select('id', 'email')->get(), new FederationUpdated($federation));

Expand All @@ -212,11 +209,13 @@ public function update(UpdateFederation $request, Federation $federation)
$state = $federation->trashed() ? 'deleted' : 'restored';
$color = $federation->trashed() ? 'red' : 'green';

if ($federation->trashed()) {

/* if ($federation->trashed()) {
GitDeleteFederation::dispatch($federation, Auth::user());
} else {
GitAddFederation::dispatch($federation, 'state', Auth::user());
}
}*/

Notification::send($federation->operators, new FederationStateChanged($federation));
Notification::send(User::activeAdmins()->select('id', 'email')->get(), new FederationStateChanged($federation));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Illuminate\Support\Facades\Storage;
use Throwable;

class GitAddFederation implements ShouldQueue
class Old_GitAddFederation implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, GitTrait;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use Illuminate\Support\Facades\Mail;
use Throwable;

class GitDeleteFederation implements ShouldQueue
class Old_GitDeleteFederation implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, GitTrait;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Illuminate\Support\Facades\Storage;
use Throwable;

class GitUpdateFederation implements ShouldQueue
class Old_GitUpdateFederation implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, GitTrait;

Expand Down
42 changes: 0 additions & 42 deletions app/Jobs/SaveMetadataToFolders.php

This file was deleted.

38 changes: 38 additions & 0 deletions app/Listeners/CreateFederationFolder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace App\Listeners;

use App\Events\FederationApprove;
use App\Traits\FederationTrait;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Storage;

class CreateFederationFolder
{
use FederationTrait;

/**
* Create the event listener.
*/
public function __construct()
{
//
}

/**
* Handle the event.
*/
public function handle(FederationApprove $event): void
{

$federation = $event->federation;
if($federation->approved)
{
if(!Storage::disk('metadata')->exists($federation->name)){
$this->createFederationFolder($federation->name);
}
}

}
}
6 changes: 6 additions & 0 deletions app/Models/Federation.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Models;

use App\Events\FederationApprove;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
Expand Down Expand Up @@ -60,4 +61,9 @@ public function scopeSearch($query, string $search = null)
->orWhere('xml_id', 'like', "%$search%")
->orWhere('xml_name', 'like', "%$search%");
}

protected $dispatchesEvents = [
'updated' => FederationApprove::class,
];

}
6 changes: 6 additions & 0 deletions app/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace App\Providers;

use App\Events\FederationApprove;
use App\Listeners\CreateFederationFolder;
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
Expand All @@ -18,6 +20,10 @@ class EventServiceProvider extends ServiceProvider
Registered::class => [
SendEmailVerificationNotification::class,
],
FederationApprove::class =>
[
CreateFederationFolder::class,
]
];

/**
Expand Down
20 changes: 15 additions & 5 deletions app/Services/EntityService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,36 @@
namespace App\Services;
use App\Models\Entity;
use App\Models\Federation;
use Exception;
use Illuminate\Support\Facades\Storage;

class EntityService
{
public function saveMetadataToFederationFolder($entity_id,$federation_id)

/**
* Save to federation Folder using federation-id
* @throws Exception if federation doesnt exist in database
*/

public function saveMetadataToFederationFolder($entity_id,$federation_id): void
{
$federation = Federation::find($federation_id);

if(!$federation){
throw new \Exception("Federation $federation_id not found");
throw new Exception("Federation $federation_id not found");
}
$this->saveEntityMetadataToFolder($entity_id,$federation->name);
}


public function saveEntityMetadataToFolder($entity_id,$folderName)
/**
* save entity if we know folder name
* @throws Exception if entity doesnt exist in database
*/
public function saveEntityMetadataToFolder($entity_id,$folderName): void
{
$entity = Entity::find($entity_id);
if(!$entity){
throw new \Exception("Entity not found with id $entity_id");
throw new Exception("Entity not found with id $entity_id");
}
$fileName = $entity->file;
if(!Storage::disk('metadata')->exists($folderName))
Expand Down
Loading

0 comments on commit 6473425

Please sign in to comment.