Skip to content

Commit

Permalink
make update for hfd
Browse files Browse the repository at this point in the history
  • Loading branch information
temaotl committed Aug 8, 2024
1 parent 8fbacfd commit f3882f0
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 25 deletions.
4 changes: 4 additions & 0 deletions app/Console/Commands/ValidateMetaConsole.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);*/

Expand Down
5 changes: 2 additions & 3 deletions app/Facades/HfdTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
1 change: 0 additions & 1 deletion app/Facades/RsTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion app/Http/Controllers/EntityController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down
9 changes: 7 additions & 2 deletions app/Http/Controllers/EntityHfdController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers;

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

Expand All @@ -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;
});
Expand Down
34 changes: 30 additions & 4 deletions app/Services/HfdTagService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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
Expand All @@ -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);
Expand Down
20 changes: 7 additions & 13 deletions app/Services/RsTagService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Traits\EntitiesXML\TagTrait;
use App\Traits\ValidatorTrait;
use DOMXPath;
use Illuminate\Support\Facades\DB;

class RsTagService
{
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
1 change: 0 additions & 1 deletion app/Traits/DumpFromGit/EntitiesHelp/DeleteFromEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ private function deleteRepublishRequest(\DOMXPath $xPath): void
foreach ($tags as $tag) {
$this->deleteTag($tag);
}

}

private function deleteTags(string $metadata): string
Expand Down
22 changes: 22 additions & 0 deletions app/Traits/EntitiesXML/TagTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down

0 comments on commit f3882f0

Please sign in to comment.