diff --git a/app/Http/Controllers/EntityGroupController.php b/app/Http/Controllers/EntityGroupController.php index 4796c1c..92f68e9 100644 --- a/app/Http/Controllers/EntityGroupController.php +++ b/app/Http/Controllers/EntityGroupController.php @@ -4,10 +4,12 @@ use App\Models\Entity; use App\Models\Group; -use Illuminate\Http\Request; +use App\Traits\DumpFromGit\EntitiesHelp\UpdateEntity; class EntityGroupController extends Controller { + use UpdateEntity; + /** * Display a listing of the resource. */ @@ -31,7 +33,7 @@ public function index(Entity $entity) /** * Store a newly created resource in storage. */ - public function store(Request $request, Entity $entity) + public function store(Entity $entity) { $this->authorize('do-everything'); @@ -40,8 +42,11 @@ public function store(Request $request, Entity $entity) ->with('status', __('entities.join_empty_group')) ->with('color', 'red'); } - $entity->groups()->attach(request('group')); + $groupLink = Group::find(request('group'))->pluck('xml_value')->toArray(); + + $xml_val = $this->updateXmlGroups($entity->xml_file, $groupLink); + $entity->update(['xml_file' => $xml_val]); return redirect() ->back() @@ -64,6 +69,10 @@ public function destroy(Entity $entity) ->groups() ->detach(request('groups')); + $groupLink = Group::find(request('groups'))->pluck('xml_value')->toArray(); + $xml_val = $this->deleteXmlGroups($entity->xml_file, $groupLink); + $entity->update(['xml_file' => $xml_val]); + return redirect() ->back() ->with('status', __('entities.leave_group')); diff --git a/app/Traits/DumpFromGit/EntitiesHelp/UpdateEntity.php b/app/Traits/DumpFromGit/EntitiesHelp/UpdateEntity.php index bf970e6..00ba571 100644 --- a/app/Traits/DumpFromGit/EntitiesHelp/UpdateEntity.php +++ b/app/Traits/DumpFromGit/EntitiesHelp/UpdateEntity.php @@ -5,6 +5,7 @@ use App\Facades\RsTag; use App\Models\Category; use App\Models\Entity; +use App\Traits\EntitiesXML\TagTrait; use App\Traits\ValidatorTrait; use DOMDocument; use DOMElement; @@ -21,7 +22,7 @@ trait UpdateEntity private string $mdrpiURI = 'urn:oasis:names:tc:SAML:metadata:rpi'; - use ValidatorTrait; + use TagTrait,ValidatorTrait; private function prepareXmlStructure(DOMDocument $dom): \DOMNode|bool|DOMElement|\DOMNameSpaceNode|null { @@ -65,7 +66,6 @@ private function prepareXmlStructure(DOMDocument $dom): \DOMNode|bool|DOMElement 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; @@ -86,11 +86,36 @@ private function updateXmlCategories(string $xml_document, int $category_id): st public function updateXmlGroups(string $xml_document, array $groupLink): string { $dom = $this->createDOM($xml_document); + $xPath = $this->createXPath($dom); $attribute = $this->prepareXmlStructure($dom); foreach ($groupLink as $link) { - $attributeValue = $dom->createElementNS($this->samlURI, 'saml:AttributeValue', $link); - $attribute->appendChild($attributeValue); + $query = "//saml:AttributeValue[text()='$link']"; + $existing = $xPath->query($query); + if ($existing->length === 0) { + $attributeValue = $dom->createElementNS($this->samlURI, 'saml:AttributeValue', $link); + $attribute->appendChild($attributeValue); + } + } + + return $dom->saveXML(); + } + + public function deleteXmlGroups(string $xml_document, array $groupLink): string + { + $dom = $this->createDOM($xml_document); + $xPath = $this->createXPath($dom); + + $xPath->registerNamespace('saml', $this->samlURI); + + foreach ($groupLink as $link) { + $query = "//saml:AttributeValue[text()='$link']"; + + $nodes = $xPath->query($query); + + foreach ($nodes as $node) { + $this->deleteTag($node); + } } return $dom->saveXML();