diff --git a/app/Console/Commands/ValidateMetaConsole.php b/app/Console/Commands/ValidateMetaConsole.php index a3f21ae..0fe1fe7 100644 --- a/app/Console/Commands/ValidateMetaConsole.php +++ b/app/Console/Commands/ValidateMetaConsole.php @@ -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; @@ -27,7 +28,7 @@ class ValidateMetaConsole extends Command */ protected $description = 'Command description'; - use FixEntityTrait,ValidatorTrait; + use FixEntityTrait,UpdateEntity,ValidatorTrait; /** * Execute the console command. @@ -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(); + } diff --git a/app/Traits/DumpFromGit/EntitiesHelp/UpdateEntity.php b/app/Traits/DumpFromGit/EntitiesHelp/UpdateEntity.php index 1233520..2bd354a 100644 --- a/app/Traits/DumpFromGit/EntitiesHelp/UpdateEntity.php +++ b/app/Traits/DumpFromGit/EntitiesHelp/UpdateEntity.php @@ -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 @@ -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'); @@ -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'); @@ -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 */ @@ -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 { @@ -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); diff --git a/config/groups.php b/config/groups.php new file mode 100644 index 0000000..cd31543 --- /dev/null +++ b/config/groups.php @@ -0,0 +1,6 @@ + 'http://eduid.cz/uri/group/mefanet', + 'mojeid-edu' => 'http://eduid.cz/uri/sp-group/mojeid-edu', +];