Verified Commit ab79110f authored by Côme Chilliet's avatar Côme Chilliet
Browse files

🚑 fix(interfaces) Attempt at fixing webservice support

issue #6039
parent 0cd74bff
<?php
/*
This code is part of FusionDirectory (http://www.fusiondirectory.org)
Copyright (C) 2019-2020 FusionDirectory
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/*!
* \brief Fake attribute class which is used to allow editing interfaces through templates or webservice
*/
class InterfacesAttribute extends Attribute
{
function __construct (string $label, string $description, string $ldapName, bool $required = FALSE, $defaultValue = '', string $acl = '')
{
parent::__construct($label, $description, $ldapName, $required, $defaultValue, $acl);
$this->setInLdap(FALSE);
}
function setParent (&$plugin)
{
$this->plugin = $plugin;
}
function renderAttribute (array &$attributes, bool $readOnly, bool $readable, bool $writable)
{
}
function getValue ()
{
return $this->plugin->filter->getInterfacesAsArrays();
}
function setValue ($value)
{
return $this->plugin->filter->loadTemplateValues($value);
}
}
......@@ -44,6 +44,9 @@ class interfacesManagement extends management implements SimpleTab
var $acl;
var $cn;
/* Used by template and webservice */
public $attributesAccess;
/* We list all network interfaces attributes in neededAttrs to get all data */
public $neededAttrs = ['cn','macAddress','fdNetworkInterfaceVlanDN','fdNetworkInterfaceVlanTagged','fdNetworkInterfaceSubnetDN','ipHostNumber'];
......@@ -86,6 +89,12 @@ class interfacesManagement extends management implements SimpleTab
parent::__construct(['networkInterface'], []);
$this->attributesAccess['networkInterfaces'] = new InterfacesAttribute(
_('Interfaces'), '',
'networkInterfaces', FALSE
);
$this->attributesAccess['networkInterfaces']->setParent($this);
$this->parent->getBaseObject()->setNetworkAttributesReadOnly(TRUE);
if ($this->is_template && isset($this->attrs['networkInterfaces'])) {
......
......@@ -103,7 +103,11 @@ class interfacesManagementFilter extends managementFilter
$entries = [];
$row = 0;
foreach ($networkInterfaces as $networkInterface) {
$values = json_decode($networkInterface, TRUE);
if (is_array($networkInterface)) {
$values = $networkInterface;
} else {
$values = json_decode($networkInterface, TRUE);
}
$entries[$values['dn']] = new ListingEntry($this->parent->listing, $this->parent->objectTypes[0], $values['dn'], $values, $row++);
}
$this->entriesCache = [$entries, [$this->parent->objectTypes[0] => $row]];
......@@ -113,17 +117,7 @@ class interfacesManagementFilter extends managementFilter
{
global $config;
$attrs = ['networkInterfaces' => []];
foreach ($this->entriesCache[0] as $dn => $entry) {
$arrayEntry = ['dn' => $dn];
foreach ($this->parent->neededAttrs as $attr) {
if (isset($entry[$attr])) {
$arrayEntry[$attr] = $entry[$attr];
}
}
$attrs['networkInterfaces'][] = json_encode($arrayEntry);
}
$attrs = ['networkInterfaces' => array_map('json_encode', $this->getInterfacesAsArrays())];
$ldap = $config->get_ldap_link();
$ldap->cat($this->parent->dn);
......@@ -139,4 +133,21 @@ class interfacesManagementFilter extends managementFilter
return $template_attrs;
}
function getInterfacesAsArrays (): array
{
$result = [];
foreach ($this->entriesCache[0] as $dn => $entry) {
$arrayEntry = ['dn' => $dn];
foreach ($this->parent->neededAttrs as $attr) {
if (isset($entry[$attr])) {
$arrayEntry[$attr] = $entry[$attr];
}
}
$result[] = $arrayEntry;
}
return $result;
}
}
......@@ -314,17 +314,17 @@ class fdRestService extends fdRPCService
$object = $tabobject->by_object[$tab];
if (!is_subclass_of($object, 'simplePlugin')) {
throw new WebServiceError('Invalid tab', 501);
}
//~ if (!is_subclass_of($object, 'simplePlugin')) {
//~ throw new WebServiceError('Invalid tab', 501);
//~ }
if (!isset($object->attributesAccess[$attribute])) {
throw new WebServiceError('Unknown attribute', 404);
}
if (!$object->acl_is_readable($object->attributesAccess[$attribute]->getAcl())) {
throw new WebServiceError('Not enough rights to read "'.$attribute.'"', 403);
}
//~ if (!$object->attrIsReadable($attribute)) {
//~ throw new WebServiceError('Not enough rights to read "'.$attribute.'"', 403);
//~ }
if (!$object->isActive()) {
throw new WebServiceError(sprintf('Tab "%s" is inactive', $tab));
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment