Skip to content

Commit

Permalink
make group tag in xml document
Browse files Browse the repository at this point in the history
  • Loading branch information
temaotl committed Dec 10, 2024
1 parent cef6b58 commit 2bcfc1b
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 17 deletions.
35 changes: 24 additions & 11 deletions app/Console/Commands/ValidateMetaConsole.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Models\Entity;
use App\Models\Federation;
use App\Traits\DumpFromGit\EntitiesHelp\FixEntityTrait;
use App\Traits\DumpFromGit\EntitiesHelp\UpdateEntity;
use App\Traits\ValidatorTrait;
use Exception;
use Illuminate\Console\Command;
Expand All @@ -27,7 +28,7 @@ class ValidateMetaConsole extends Command
*/
protected $description = 'Command description';

use FixEntityTrait,ValidatorTrait;
use FixEntityTrait,UpdateEntity,ValidatorTrait;

/**
* Execute the console command.
Expand Down Expand Up @@ -100,18 +101,30 @@ private function runMDA(Federation $federation)

public function handle()
{
dump(config('constants.lock_constant'));

/* $lockKey = 'directory-'.md5('aboba').'-lock';
$lock = Cache::lock($lockKey, 61);
RunMdaScript::dispatch(2, $lock->owner());
$lock->release();*/
/* $entity = Entity::find(1);
$xml_document = $entity->xml_file;
/* $federation = Federation::where('id', 1)->first();
$this->runMDA($federation);*/

// $this->fixEntities();
// $this->doc();
$xml_document = $this->updateXmlGroups($xml_document, $entity->groups()->pluck('name')->toArray());
dump($xml_document);*/

/* if(!empty($entity->groups)) {
$groups = $entity->groups()->pluck('name')->toArray();
foreach ($groups as $name) {
$configValue = config("groups.$name");
dump($configValue);
}*/
}

/* $lockKey = 'directory-'.md5('aboba').'-lock';
$lock = Cache::lock($lockKey, 61);
RunMdaScript::dispatch(2, $lock->owner());
$lock->release();*/

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

// $this->fixEntities();
// $this->doc();

}
52 changes: 46 additions & 6 deletions app/Traits/DumpFromGit/EntitiesHelp/UpdateEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
use App\Models\Category;
use App\Models\Entity;
use App\Traits\ValidatorTrait;
use DOMDocument;
use DOMElement;
use Exception;
use Illuminate\Support\Facades\Storage;

trait UpdateEntity
Expand All @@ -20,12 +23,11 @@ trait UpdateEntity

use ValidatorTrait;

private function updateXmlCategories(string $xml_document, int $category_id): string
private function prepareXmlStructure(DOMDocument $dom): \DOMNode|bool|DOMElement|\DOMNameSpaceNode|null
{
$dom = $this->createDOM($xml_document);
$xPath = $this->createXPath($dom);

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

$entityExtensions = $xPath->query('//md:Extensions');
if ($entityExtensions->length === 0) {
$dom->documentElement->lookupNamespaceURI('md');
Expand All @@ -34,6 +36,7 @@ private function updateXmlCategories(string $xml_document, int $category_id): st
} else {
$entityExtensions = $entityExtensions->item(0);
}

$entityAttributes = $xPath->query('//mdattr:EntityAttributes');
if ($entityAttributes->length === 0) {
$entityAttributes = $dom->createElementNS($this->mdattrURI, 'mdattr:EntityAttributes');
Expand All @@ -44,25 +47,55 @@ private function updateXmlCategories(string $xml_document, int $category_id): st

$attribute = $xPath->query('//mdattr:EntityAttributes/saml:Attribute', $entityAttributes);
if ($attribute->length === 0) {

$attribute = $dom->createElementNS($this->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;
}

/**
* @throws \DOMException
* @throws \Exception
*/
private function updateXmlCategories(string $xml_document, int $category_id): string
{
$dom = $this->createDOM($xml_document);

$attribute = $this->prepareXmlStructure($dom);

$categoryXml = Category::whereId($category_id)->first()->xml_value;

if (empty($categoryXml)) {
throw new \Exception("Category with ID $category_id has no XML value.");
}

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

return $dom->saveXML();
}

/**
* @throws \DOMException
*/
public function updateXmlGroups(string $xml_document, array $groupsName): string
{
$dom = $this->createDOM($xml_document);
$attribute = $this->prepareXmlStructure($dom);

foreach ($groupsName as $name) {
$attributeValue = $dom->createElementNS($this->samlURI, 'saml:AttributeValue', config("groups.$name"));
$attribute->appendChild($attributeValue);
}

return $dom->saveXML();
}

/**
* @throws Exception if exist more or less then 2 part something gone wrong
*/
Expand Down Expand Up @@ -135,6 +168,8 @@ private function updateRegistrationInfo(string $xml_document, string $entityId,
/**
* @param array $timestampDocumentArray for add registration time from git file
* @return void update entity in db and return
*
* @throws \DOMException
*/
public function updateEntityXml($entity, array $timestampDocumentArray = []): void
{
Expand All @@ -147,6 +182,11 @@ public function updateEntityXml($entity, array $timestampDocumentArray = []): vo
if (! empty($entity->category_id)) {
$xml_document = $this->updateXmlCategories($xml_document, $entity->category_id);
}
$groupName = $entity->groups()->pluck('name')->toArray();

if (! empty($groupName)) {
$xml_document = $this->updateXmlGroups($xml_document, $groupName);
}

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

Expand Down
6 changes: 6 additions & 0 deletions config/groups.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

return [
'mefanet_sp' => 'http://eduid.cz/uri/group/mefanet',
'mojeid-edu' => 'http://eduid.cz/uri/sp-group/mojeid-edu',
];

0 comments on commit 2bcfc1b

Please sign in to comment.