Skip to content

Commit

Permalink
write CategoryTag with facade (not tested)
Browse files Browse the repository at this point in the history
  • Loading branch information
temaotl committed Aug 9, 2024
1 parent d9f5f84 commit f9c7c64
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 68 deletions.
7 changes: 3 additions & 4 deletions app/Console/Commands/ValidateMetaConsole.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace App\Console\Commands;

use App\Facades\RsTag;
use App\Models\Entity;
use App\Models\Federation;
use App\Services\CategoryTagService;
use App\Traits\DumpFromGit\EntitiesHelp\FixEntityTrait;
use App\Traits\ValidatorTrait;
use Exception;
Expand Down Expand Up @@ -100,9 +100,8 @@ private function runMDA(Federation $federation)
public function handle()
{
$ent = Entity::find(1);

$ent->xml_file = RsTag::create($ent);
$ent->update();
$service = new CategoryTagService();
dump($service->create($ent));

/* $federation = Federation::where('id', 1)->first();
$this->runMDA($federation);*/
Expand Down
21 changes: 21 additions & 0 deletions app/Facades/CategoryTag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Facades;

use App\Models\Entity;
use App\Services\CategoryTagService;
use Illuminate\Support\Facades\Facade;

/**
* Class CategoryTag facade
*
* @method static false|string create(Entity $entity)
* @method static void delete(Entity $entity)
*/
class CategoryTag extends Facade
{
protected static function getFacadeAccessor()
{
return CategoryTagService::class;
}
}
5 changes: 5 additions & 0 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Providers;

use App\Jobs\RunMdaScript;
use App\Services\CategoryTagService;
use App\Services\HfdTagService;
use App\Services\RsTagService;
use Illuminate\Cache\RateLimiting\Limit;
Expand All @@ -29,6 +30,10 @@ public function register()
return new HfdTagService();
});

$this->app->singleton(CategoryTagService::class, function () {
return new CategoryTagService();
});

}

/**
Expand Down
110 changes: 110 additions & 0 deletions app/Services/CategoryTagService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

namespace App\Services;

use App\Models\Category;
use App\Models\Entity;
use App\Traits\EntitiesXML\TagTrait;
use App\Traits\HandlesJobsFailuresTrait;
use App\Traits\ValidatorTrait;
use DOMXPath;
use Mockery\Exception;

class CategoryTagService extends TagService
{
use HandlesJobsFailuresTrait;
use TagTrait,ValidatorTrait;

public function create(Entity $entity): false|string
{
$mdURI = config('xmlNameSpace.md');
$mdattrURI = config('xmlNameSpace.mdattr');
$samlURI = config('xmlNameSpace.saml');

$category = self::hasCategoryInDatabase($entity);
if (! $category) {
return false;
}
$attributeValue = $this->hasAtributeValueInConfig($category);
if (! $attributeValue) {
return false;
}
$xml_document = $entity->xml_file;

$dom = $this->createDOM($xml_document);
$xPath = $this->createXPath($dom);
$rootTag = $this->getRootTag($xPath);
$extensions = $this->getOrCreateExtensions($xPath, $dom, $rootTag, $mdURI);
$entityAttributes = $this->getOrCreateEntityAttributes($xPath, $dom, $extensions, $mdattrURI);
$attribute = $this->getOrCreateAttribute($xPath, $dom, $entityAttributes, $samlURI);

$attributeValue = $dom->createElementNS($samlURI, 'saml:AttributeValue', $attributeValue);
$attribute->appendChild($attributeValue);

$dom->normalize();

return $dom->saveXML();
}

public function delete(Entity $entity): false|string
{
$category = self::hasCategoryInDatabase($entity);
if (! $category) {
return false;
}
$attributeValue = $this->hasAtributeValueInConfig($category);
if (! $attributeValue) {
return false;
}

$dom = $this->createDOM($entity->xml_file);
$xPath = $this->createXPath($dom);
$xpathQuery = $this->buildXPathQuery($attributeValue);
$this->deleteAllTags($xpathQuery, $xPath);
$dom->normalize();

return $dom->saveXML();

}

private static function hasCategoryInDatabase(Entity $entity): false|Category
{
$category = $entity->category;
if (is_null($category)) {
return false;
} else {
return $category;
}
}

private function hasAtributeValueInConfig(Category $category): false|string
{
try {
$attributeValue = config("categories.$category->name");
if (is_null($attributeValue)) {
throw new Exception('No category attribute in config please update');
} else {
return $attributeValue;
}
} catch (Exception $exception) {
$this->failed($exception);
}

return false;
}

protected function getOrCreateAttribute(DOMXPath $xPath, \DOMDocument $dom, \DOMNode $entityAttributes, string $samlURI): \DOMNode|bool|\DOMElement|null
{
$attribute = $xPath->query('//mdattr:EntityAttributes/saml:Attribute', $entityAttributes);
if ($attribute->length === 0) {
$attribute = $dom->createElementNS($samlURI, 'saml:Attribute');
$attribute->setAttribute('Name', 'http://macedir.org/entity-category');
$attribute->setAttribute('NameFormat', 'urn:oasis:names:tc:SAML:2.0:attrname-format:uri');
$entityAttributes->appendChild($attribute);
} else {
$attribute = $attribute->item(0);
}

return $attribute;
}
}
56 changes: 4 additions & 52 deletions app/Services/HfdTagService.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use App\Traits\ValidatorTrait;
use DOMXPath;

class HfdTagService
class HfdTagService extends TagService
{
use TagTrait,ValidatorTrait;

Expand Down Expand Up @@ -53,44 +53,27 @@ public function delete(Entity $entity): false|string

public function deleteByXpath(DOMXPath $xPath): void
{
$xpathQuery = $this->buildXPathQuery();
$xpathQuery = $this->buildXPathQuery($this->value);
$this->DeleteAllTags($xpathQuery, $xPath);
}

public function update(Entity $entity): false|string
{
if ($entity->hfd) {

if (! $this->hasHideFromDiscovery($entity->xml_file)) {
if (! $this->hasTagInDocument($entity->xml_file, $this->value)) {
return $this->create($entity->xml_file);
}

} else {
if ($this->hasHideFromDiscovery($entity->xml_file)) {
if ($this->hasTagInDocument($entity->xml_file, $this->value)) {
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);
if (! $rootTag) {
throw new \RuntimeException('Root tag EntityDescriptor not found');
}

return $rootTag;
}

private function getOrCreateAttribute(DOMXPath $xPath, \DOMDocument $dom, \DOMNode $entityAttributes, string $samlURI): \DOMNode
{
$attribute = $xPath->query('//mdattr:EntityAttributes/saml:Attribute', $entityAttributes);
Expand All @@ -105,35 +88,4 @@ private function getOrCreateAttribute(DOMXPath $xPath, \DOMDocument $dom, \DOMNo

return $attribute;
}

private function getOrCreateEntityAttributes(DOMXPath $xPath, \DOMDocument $dom, \DOMNode $extensions, string $mdattrURI): \DOMNode
{
$entityAttributes = $xPath->query('//mdattr:EntityAttributes');
if ($entityAttributes->length === 0) {
$entityAttributes = $dom->createElementNS($mdattrURI, 'mdattr:EntityAttributes');
$extensions->appendChild($entityAttributes);
} else {
$entityAttributes = $entityAttributes->item(0);
}

return $entityAttributes;
}

private function getOrCreateExtensions(DOMXPath $xPath, \DOMDocument $dom, \DOMNode $rootTag, string $mdURI): \DOMNode
{
$extensions = $xPath->query('//md:Extensions');
if ($extensions->length === 0) {
$extensions = $dom->createElementNS($mdURI, 'md:Extensions');
$rootTag->appendChild($extensions);
} else {
$extensions = $extensions->item(0);
}

return $extensions;
}

private function buildXPathQuery(): string
{
return "//saml:AttributeValue[text()='$this->value']";
}
}
12 changes: 2 additions & 10 deletions app/Services/RsTagService.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,27 +59,19 @@ public function update(Entity $entity): false|string
{
if ($entity->rs) {

if (! $this->hasResearchAndScholarshipTag($entity->xml_file)) {
if (! $this->hasTagInDocument($entity->xml_file, $this->value)) {
return $this->create($entity);
}

} else {
if ($this->hasResearchAndScholarshipTag($entity->xml_file)) {
if ($this->hasTagInDocument($entity->xml_file, $this->value)) {
return $this->delete($entity);
}
}

return false;
}

private function hasResearchAndScholarshipTag(string $xml_document): bool
{
$xpathQuery = $this->buildXPathQuery($this->value);

return $this->hasXpathQueryInDocument($xml_document, $xpathQuery);

}

protected function getOrCreateAttribute(\DOMXPath $xPath, \DOMDocument $dom, \DOMNode $entityAttributes, string $samlURI, bool $isIdp): \DOMNode
{
$attribute = $xPath->query('//mdattr:EntityAttributes/saml:Attribute', $entityAttributes);
Expand Down
12 changes: 10 additions & 2 deletions app/Services/TagService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@
namespace App\Services;

use App\Models\Entity;
use App\Traits\EntitiesXML\TagTrait;
use DOMNode;
use DOMXPath;

abstract class TagService
{
use TagTrait;

abstract public function create(Entity $entity);

abstract public function delete(Entity $entity);

abstract public function update(Entity $entity);

protected function buildXPathQuery(string $value): string
{
return "//saml:AttributeValue[text()='$value']";
Expand Down Expand Up @@ -54,4 +55,11 @@ protected function getOrCreateEntityAttributes(\DOMXPath $xPath, \DOMDocument $d

return $entityAttributes;
}

protected function hasTagInDocument(string $xml_document, string $value): bool
{
$xpathQuery = $this->buildXPathQuery($value);

return $this->hasXpathQueryInDocument($xml_document, $xpathQuery);
}
}
1 change: 1 addition & 0 deletions config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@
'EntityFacade' => App\Facades\EntityFacade::class,
'RsTag' => App\Facades\RsTag::class,
'HfdTag' => App\Facades\HfdTag::class,
'CategoryTag' => App\Facades\CategoryTag::class,
],

];

0 comments on commit f9c7c64

Please sign in to comment.