Skip to content

Commit

Permalink
add comment and make entity store with new xml_document
Browse files Browse the repository at this point in the history
  • Loading branch information
temaotl committed Jun 12, 2024
1 parent 6774791 commit bf5f8d2
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 103 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
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
1 change: 0 additions & 1 deletion app/Http/Controllers/FederationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,6 @@ public function update(UpdateFederation $request, Federation $federation)
$state = $federation->trashed() ? 'deleted' : 'restored';
$color = $federation->trashed() ? 'red' : 'green';

//TODO ask about what we want to do with cfg and tag files

/* if ($federation->trashed()) {
GitDeleteFederation::dispatch($federation, Auth::user());
Expand Down
42 changes: 0 additions & 42 deletions app/Jobs/SaveMetadataToFolders.php

This file was deleted.

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
52 changes: 30 additions & 22 deletions app/Traits/DumpFromGit/EntitiesHelp/UpdateEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,37 @@ private function updateRegistrationInfo(string $xml_document, string $entityId,a
return $dom->saveXML();
}

/**
* @param array $timestampDocumentArray for add registration time from git file
* @return void update entity in db and return
*/
public function updateEntityXml($entity,array $timestampDocumentArray = []): void
{
if(empty($entity->xml_file))
return;

$xml_document = $entity->xml_file;
$isIdp = false;
if($entity->type == "idp")
$isIdp = true;


if($entity->rs)
{
$xml_document = $this->updateResearchAndScholarship($xml_document,$isIdp);
}
if(!empty($entity->category_id))
{
$xml_document = $this->updateXmlCategories($xml_document,$entity->category_id);
}

$xml_document = $this->updateRegistrationInfo($xml_document,$entity->entityid,$timestampDocumentArray);


Entity::whereId($entity->id)->update(['xml_file' => $xml_document]);

}


public function updateEntitiesXml() : void
{
Expand All @@ -196,28 +225,7 @@ public function updateEntitiesXml() : void

foreach (Entity::select()->get() as $entity)
{
if(empty($entity->xml_file))
continue;

$xml_document = $entity->xml_file;
$isIdp = false;
if($entity->type == "idp")
$isIdp = true;


if($entity->rs)
{
$xml_document = $this->updateResearchAndScholarship($xml_document,$isIdp);
}
if(!empty($entity->category_id))
{
$xml_document = $this->updateXmlCategories($xml_document,$entity->category_id);
}

$xml_document = $this->updateRegistrationInfo($xml_document,$entity->entityid,$timestampDocumentArray);


Entity::whereId($entity->id)->update(['xml_file' => $xml_document]);
$this->updateEntityXml($entity,$timestampDocumentArray);
}
}

Expand Down
26 changes: 0 additions & 26 deletions app/Traits/EntityFolderTrait.php

This file was deleted.

0 comments on commit bf5f8d2

Please sign in to comment.