Skip to content

Commit

Permalink
rewrite rs tag function to service
Browse files Browse the repository at this point in the history
  • Loading branch information
temaotl committed Aug 6, 2024
1 parent 636f481 commit 8b5f38d
Show file tree
Hide file tree
Showing 8 changed files with 256 additions and 103 deletions.
3 changes: 3 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\RsTag;
use App\Models\Entity;
use App\Models\Federation;
use App\Traits\DumpFromGit\EntitiesHelp\FixEntityTrait;
Expand Down Expand Up @@ -98,6 +99,8 @@ private function runMDA(Federation $federation)

public function handle()
{
RsTag::hello();

/* $federation = Federation::where('id', 1)->first();
$this->runMDA($federation);*/

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

namespace App\Facades;

use App\Models\Entity;
use App\Services\RsTagService;
use DOMXPath;
use Illuminate\Support\Facades\Facade;

/**
* Class RsTagFacade
*
* @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
{
protected static function getFacadeAccessor()
{
return RsTagService::class;
}
}
5 changes: 4 additions & 1 deletion 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\RsTagService;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Mail;
Expand All @@ -19,7 +20,9 @@ class AppServiceProvider extends ServiceProvider
*/
public function register()
{

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

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

namespace App\Services;

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

class RsTagService
{
use TagTrait,ValidatorTrait;

private string $value = 'http://refeds.org/category/research-and-scholarship';

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

$xml_document = $entity->xml_file;
$isIdp = $entity->type == 'idp';

$dom = $this->createDOM($xml_document);
$xPath = $this->createXPath($dom);

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

$attributeValue = $dom->createElementNS($samlURI, 'saml:AttributeValue', 'http://refeds.org/category/research-and-scholarship');
$attribute->appendChild($attributeValue);

$dom->normalize();

return $dom->saveXML();
}

public function delete(Entity $entity): void
{

$dom = $this->createDOM($entity->xml_file);
$xPath = $this->createXPath($dom);
$this->deleteByXpath($xPath);
}

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

public function update(Entity $entity): void
{
if ($entity->rs) {

if (! $this->hasResearchAndScholarshipTag($entity->xml_file)) {
$this->create($entity);
}

} else {
if ($this->hasResearchAndScholarshipTag($entity->xml_file)) {
$this->delete($entity);
}
}

}

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);

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 buildXPathQuery(): string
{
return "//saml:AttributeValue[text()='$this->value']";
}

private function getRootTag(\DOMXPath $xPath, \DOMDocument $dom): \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, bool $isIdp): \DOMNode
{
$attribute = $xPath->query('//mdattr:EntityAttributes/saml:Attribute', $entityAttributes);
if ($attribute->length === 0) {
$attribute = $dom->createElementNS($samlURI, 'saml:Attribute');
$attribute->setAttribute('NameFormat', 'urn:oasis:names:tc:SAML:2.0:attrname-format:uri');
$attribute->setAttribute('Name', $isIdp ? 'http://macedir.org/entity-category-support' : 'http://macedir.org/entity-category');
$entityAttributes->appendChild($attribute);
} else {
$attribute = $attribute->item(0);
}

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;
}
}
58 changes: 5 additions & 53 deletions app/Traits/DumpFromGit/EntitiesHelp/DeleteFromEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,13 @@

namespace App\Traits\DumpFromGit\EntitiesHelp;

use App\Facades\RsTag;
use App\Traits\EntitiesXML\TagTrait;
use App\Traits\ValidatorTrait;

trait DeleteFromEntity
{
use ValidatorTrait;

private function hasChildElements(object $parent): bool
{
foreach ($parent->childNodes as $child) {
if ($child->nodeType === XML_ELEMENT_NODE) {
return true;
}
}

return false;
}

private function deleteTag(object $tag): void
{
if ($tag->parentNode) {
$tag->parentNode->removeChild($tag);
}
}

private function deleteNoChilledTag(object $tag): void
{
if (! $this->hasChildElements($tag)) {
$this->deleteTag($tag);
}
}

private function DeleteAllTags(string $xpathQuery, \DOMXPath $xPath): void
{
$tags = $xPath->query($xpathQuery);

foreach ($tags as $tag) {
$parent = $tag->parentNode;
$grandParent = $parent->parentNode;
$this->deleteTag($tag);

$this->deleteNoChilledTag($parent);
$this->deleteNoChilledTag($grandParent);
}
}
use TagTrait, ValidatorTrait;

private function deleteCategories(\DOMXPath $xPath): void
{
Expand All @@ -55,16 +18,7 @@ private function deleteCategories(\DOMXPath $xPath): void
}, $values);

$xpathQuery = '//saml:AttributeValue['.implode(' or ', $xpathQueryParts).']';
$this->DeleteAllTags($xpathQuery, $xPath);
}

private function deleteResearchAndScholarship(\DOMXPath $xPath): void
{
$value = 'http://refeds.org/category/research-and-scholarship';
$xpathQueryParts = "text()='$value'";

$xpathQuery = '//saml:AttributeValue['.$xpathQueryParts.']';
$this->DeleteAllTags($xpathQuery, $xPath);
$this->deleteAllTags($xpathQuery, $xPath);
}

private function deleteRegistrationInfo(\DOMXPath $xPath): void
Expand All @@ -76,7 +30,6 @@ private function deleteRegistrationInfo(\DOMXPath $xPath): void
$this->deleteTag($tag);
}
}

}

private function deleteFromIdp(\DOMXPath $xPath): void
Expand All @@ -86,8 +39,7 @@ private function deleteFromIdp(\DOMXPath $xPath): void

private function deleteFromSP(\DOMXPath $xpath): void
{
$this->deleteResearchAndScholarship($xpath);

RsTag::deleteByXpath($xpath);
}

private function deleteRepublishRequest(\DOMXPath $xPath): void
Expand Down
50 changes: 1 addition & 49 deletions app/Traits/DumpFromGit/EntitiesHelp/UpdateEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,54 +62,6 @@ private function updateXmlCategories(string $xml_document, int $category_id): st
return $dom->saveXML();
}

private function updateResearchAndScholarship(string $xml_document, bool $isIdp): string
{
$dom = $this->createDOM($xml_document);
$xPath = $this->createXPath($dom);

$rootTag = $xPath->query("//*[local-name()='EntityDescriptor']")->item(0);

$extensions = $xPath->query('//md:Extensions');
if ($extensions->length === 0) {
$extensions = $dom->createElementNS($this->mdURI, 'md:Extensions');
$rootTag->appendChild($extensions);
} else {
$extensions = $extensions->item(0);
}

$entityAttributes = $xPath->query('//mdattr:EntityAttributes');
if ($entityAttributes->length === 0) {
$entityAttributes = $dom->createElementNS($this->mdattrURI, 'mdattr:EntityAttributes');
$extensions->appendChild($entityAttributes);
} else {
$entityAttributes = $entityAttributes->item(0);
}

$attribute = $xPath->query('//mdattr:EntityAttributes/saml:Attribute', $entityAttributes);
if ($attribute->length === 0) {
$attribute = $dom->createElementNS($this->samlURI, 'saml:Attribute');
$attribute->setAttribute('NameFormat', 'urn:oasis:names:tc:SAML:2.0:attrname-format:uri');

if ($isIdp) {
$attribute->setAttribute('Name', 'http://macedir.org/entity-category-support');
} else {
$attribute->setAttribute('Name', 'http://macedir.org/entity-category');
}

$entityAttributes->appendChild($attribute);
} else {
$attribute = $attribute->item(0);
}

$attributeValue = $dom->createElementNS($this->samlURI, 'saml:AttributeValue', 'http://refeds.org/category/research-and-scholarship');
$attribute->appendChild($attributeValue);

$dom->normalize();

return $dom->saveXML();

}

/**
* @throws Exception if exist more or less then 2 part something gone wrong
*/
Expand Down Expand Up @@ -196,7 +148,7 @@ public function updateEntityXml($entity, array $timestampDocumentArray = []): vo
}

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

0 comments on commit 8b5f38d

Please sign in to comment.