From f3882f0bae20c64331a63181475d87e8823fbf24 Mon Sep 17 00:00:00 2001 From: Artem Otliaguzov Date: Thu, 8 Aug 2024 13:54:28 +0200 Subject: [PATCH] make update for hfd --- app/Console/Commands/ValidateMetaConsole.php | 4 +++ app/Facades/HfdTag.php | 5 ++- app/Facades/RsTag.php | 1 - app/Http/Controllers/EntityController.php | 5 ++- app/Http/Controllers/EntityHfdController.php | 9 +++-- app/Services/HfdTagService.php | 34 ++++++++++++++++--- app/Services/RsTagService.php | 20 ++++------- .../EntitiesHelp/DeleteFromEntity.php | 1 - app/Traits/EntitiesXML/TagTrait.php | 22 ++++++++++++ 9 files changed, 76 insertions(+), 25 deletions(-) diff --git a/app/Console/Commands/ValidateMetaConsole.php b/app/Console/Commands/ValidateMetaConsole.php index 9887141..080de53 100644 --- a/app/Console/Commands/ValidateMetaConsole.php +++ b/app/Console/Commands/ValidateMetaConsole.php @@ -2,6 +2,7 @@ namespace App\Console\Commands; +use App\Facades\HfdTag; use App\Models\Entity; use App\Models\Federation; use App\Traits\DumpFromGit\EntitiesHelp\FixEntityTrait; @@ -98,6 +99,9 @@ private function runMDA(Federation $federation) public function handle() { + $ent = Entity::find(2); + HfdTag::delete($ent); + /* $federation = Federation::where('id', 1)->first(); $this->runMDA($federation);*/ diff --git a/app/Facades/HfdTag.php b/app/Facades/HfdTag.php index 1f97d8c..4d12891 100644 --- a/app/Facades/HfdTag.php +++ b/app/Facades/HfdTag.php @@ -10,11 +10,10 @@ /** * Class HfdTag facade * - * @method static string create(Entity $entity) + * @method static string create(string $xml_document) * @method static void delete(Entity $entity) - * @method static void update(Entity $entity) - * @method static bool hasResearchAndScholarshipTag(string $xml_document) * @method static void deleteByXpath( DOMXPath $xPath) + * @method static false|string update(Entity $entity) */ class HfdTag extends Facade { diff --git a/app/Facades/RsTag.php b/app/Facades/RsTag.php index 4d1fe1f..a4ad6e3 100644 --- a/app/Facades/RsTag.php +++ b/app/Facades/RsTag.php @@ -13,7 +13,6 @@ * @method static string create(Entity $entity) * @method static void delete(Entity $entity) * @method static void update(Entity $entity) - * @method static bool hasResearchAndScholarshipTag(string $xml_document) * @method static void deleteByXpath( DOMXPath $xPath) */ class RsTag extends Facade diff --git a/app/Http/Controllers/EntityController.php b/app/Http/Controllers/EntityController.php index 988f3b1..b4721d3 100644 --- a/app/Http/Controllers/EntityController.php +++ b/app/Http/Controllers/EntityController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers; +use App\Facades\HfdTag; use App\Http\Requests\StoreEntity; use App\Ldap\CesnetOrganization; use App\Ldap\EduidczOrganization; @@ -96,10 +97,12 @@ public function store(StoreEntity $request) case '0': $federation = Federation::findOrFail($validated['federation']); $entity = DB::transaction(function () use ($new_entity, $federation) { + $new_entity = array_merge($new_entity, ['xml_file' => $this->deleteTags($new_entity['metadata'])]); + if ($new_entity['type'] === 'idp') { $new_entity = array_merge($new_entity, ['hfd' => true]); + $new_entity['xml_file'] = HfdTag::create($new_entity['xml_file']); } - $new_entity = array_merge($new_entity, ['xml_file' => $this->deleteTags($new_entity['metadata'])]); $entity = Entity::create($new_entity); $entity->operators()->attach(Auth::id()); diff --git a/app/Http/Controllers/EntityHfdController.php b/app/Http/Controllers/EntityHfdController.php index c258b3d..bacc717 100644 --- a/app/Http/Controllers/EntityHfdController.php +++ b/app/Http/Controllers/EntityHfdController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers; +use App\Facades\HfdTag; use App\Models\Entity; use Illuminate\Support\Facades\DB; @@ -18,8 +19,12 @@ public function update(Entity $entity) } $entity = DB::transaction(function () use ($entity) { - $entity->hfd = $entity->hfd ? false : true; - $entity->update(); + $entity->hfd = ! $entity->hfd; + $xml_document = HfdTag::update($entity); + if ($xml_document) { + $entity->xml_file = $xml_document; + $entity->update(); + } return $entity; }); diff --git a/app/Services/HfdTagService.php b/app/Services/HfdTagService.php index b710444..7f77615 100644 --- a/app/Services/HfdTagService.php +++ b/app/Services/HfdTagService.php @@ -13,14 +13,12 @@ class HfdTagService private string $value = 'http://refeds.org/category/hide-from-discovery'; - public function create(Entity $entity): false|string + public function create(string $xml_document): false|string { $mdURI = config('xmlNameSpace.md'); $mdattrURI = config('xmlNameSpace.mdattr'); $samlURI = config('xmlNameSpace.saml'); - $xml_document = $entity->xml_file; - $dom = $this->createDOM($xml_document); $xPath = $this->createXPath($dom); @@ -37,11 +35,15 @@ public function create(Entity $entity): false|string return $dom->saveXML(); } - public function delete(Entity $entity): void + public function delete(Entity $entity): false|string { $dom = $this->createDOM($entity->xml_file); $xPath = $this->createXPath($dom); $this->deleteByXpath($xPath); + $dom->normalize(); + + return $dom->saveXML(); + } public function deleteByXpath(DOMXPath $xPath): void @@ -50,6 +52,30 @@ public function deleteByXpath(DOMXPath $xPath): void $this->DeleteAllTags($xpathQuery, $xPath); } + public function update(Entity $entity): false|string + { + if ($entity->hfd) { + + if (! $this->hasHideFromDiscovery($entity->xml_file)) { + return $this->create($entity->xml_file); + } + + } else { + if ($this->hasHideFromDiscovery($entity->xml_file)) { + return $this->delete($entity); + } + } + + return false; + } + + private function hasHideFromDiscovery(string $xml_document): bool + { + $xpathQuery = $this->buildXPathQuery(); + + return $this->hasXpathQueryInDocument($xml_document, $xpathQuery); + } + private function getRootTag(DOMXPath $xPath): \DOMNode { $rootTag = $xPath->query("//*[local-name()='EntityDescriptor']")->item(0); diff --git a/app/Services/RsTagService.php b/app/Services/RsTagService.php index 2d580a8..fa0eda0 100644 --- a/app/Services/RsTagService.php +++ b/app/Services/RsTagService.php @@ -6,6 +6,7 @@ use App\Traits\EntitiesXML\TagTrait; use App\Traits\ValidatorTrait; use DOMXPath; +use Illuminate\Support\Facades\DB; class RsTagService { @@ -44,6 +45,10 @@ public function delete(Entity $entity): void $dom = $this->createDOM($entity->xml_file); $xPath = $this->createXPath($dom); $this->deleteByXpath($xPath); + $entity->xml_file = $dom->saveXML(); + DB::transaction(function () use ($entity) { + $entity->update(); + }); } public function deleteByXpath(DOMXPath $xPath): void @@ -70,21 +75,10 @@ public function update(Entity $entity): void private function hasResearchAndScholarshipTag(string $xml_document): bool { - try { - $dom = $this->createDOM($xml_document); - $xPath = $this->createXPath($dom); - $xpathQuery = "//saml:AttributeValue[text()='$this->value']"; - - $nodes = $xPath->query($xpathQuery); + $xpathQuery = $this->buildXPathQuery(); - if ($nodes === false) { - throw new \RuntimeException('Error executing XPath query'); - } + return $this->hasXpathQueryInDocument($xml_document, $xpathQuery); - return $nodes->length > 0; - } catch (\Exception $e) { - throw new \RuntimeException('An error occurred while checking for the tag: '.$e->getMessage()); - } } private function buildXPathQuery(): string diff --git a/app/Traits/DumpFromGit/EntitiesHelp/DeleteFromEntity.php b/app/Traits/DumpFromGit/EntitiesHelp/DeleteFromEntity.php index f38979e..00d6cf2 100644 --- a/app/Traits/DumpFromGit/EntitiesHelp/DeleteFromEntity.php +++ b/app/Traits/DumpFromGit/EntitiesHelp/DeleteFromEntity.php @@ -52,7 +52,6 @@ private function deleteRepublishRequest(\DOMXPath $xPath): void foreach ($tags as $tag) { $this->deleteTag($tag); } - } private function deleteTags(string $metadata): string diff --git a/app/Traits/EntitiesXML/TagTrait.php b/app/Traits/EntitiesXML/TagTrait.php index 40a175c..0a67c42 100644 --- a/app/Traits/EntitiesXML/TagTrait.php +++ b/app/Traits/EntitiesXML/TagTrait.php @@ -2,8 +2,12 @@ namespace App\Traits\EntitiesXML; +use App\Traits\ValidatorTrait; + trait TagTrait { + use ValidatorTrait; + public function hasChildElements(object $parent): bool { if (! $parent instanceof \DOMNode) { @@ -19,6 +23,24 @@ public function hasChildElements(object $parent): bool return false; } + // find attribute in XML document + public function hasXpathQueryInDocument(string $xml_document, string $xpathQuery): bool + { + try { + $dom = $this->createDOM($xml_document); + $xPath = $this->createXPath($dom); + $nodes = $xPath->query($xpathQuery); + + if ($nodes === false) { + throw new \RuntimeException('Error executing XPath query'); + } + + return $nodes->length > 0; + } catch (\Exception $e) { + throw new \RuntimeException('An error occurred while checking for the tag: '.$e->getMessage()); + } + } + private function deleteTag(object $tag): void { if (! $tag instanceof \DOMNode) {