Skip to content

Commit

Permalink
make delete and add groups xml tags in metaman
Browse files Browse the repository at this point in the history
  • Loading branch information
temaotl committed Dec 11, 2024
1 parent a219685 commit 96695e9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
15 changes: 12 additions & 3 deletions app/Http/Controllers/EntityGroupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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');

Expand All @@ -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()
Expand All @@ -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'));
Expand Down
33 changes: 29 additions & 4 deletions app/Traits/DumpFromGit/EntitiesHelp/UpdateEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
Expand Down Expand Up @@ -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;
Expand All @@ -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();
Expand Down

0 comments on commit 96695e9

Please sign in to comment.