Commit 617d66c2 authored by Côme Chilliet's avatar Côme Chilliet
Browse files

Merge branch '6039-update-the-system-plugin' into '1.4-dev'

Resolve "update the system plugin"

See merge request fusiondirectory/fd-plugins!751
parents 0cd74bff 2d6632bb
<?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 renderFormInput (): string
{
$value = $this->getValue();
if (is_array($value)) {
$value = join(', ', $value);
}
return '{literal}'.htmlescape($value).'{/literal}';
}
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'])) {
......@@ -99,7 +108,7 @@ class interfacesManagement extends management implements SimpleTab
foreach ($entryIterator as $entry) {
if ($this->is_template) {
$this->interfaces[$entry->dn] = objects::create($entry->getTemplatedType());
$this->interfaces[$entry->dn] = objects::createTemplate($entry->getTemplatedType());
$this->filter->resetFromCache($entry->dn, $this->interfaces[$entry->dn]);
} else {
$this->interfaces[$entry->dn] = objects::open($entry->dn, $entry->getTemplatedType());
......@@ -254,6 +263,9 @@ class interfacesManagement extends management implements SimpleTab
} else {
$this->tabObject->getBaseObject()->base = 'new,'.$this->parent->getBaseObject()->base;
}
if ($this->is_template) {
$this->tabObject->setTemplateMode('');
}
}
public function check (): array
......@@ -296,12 +308,14 @@ class interfacesManagement extends management implements SimpleTab
$macAddress = [];
$ipHostNumber = [];
foreach ($entryIterator as $entry) {
if (!empty($entry['macAddress'])) {
array_push($macAddress, ...$entry['macAddress']);
}
if (!empty($entry['ipHostNumber'])) {
array_push($ipHostNumber, ...$entry['ipHostNumber']);
if (!$this->is_template) {
foreach ($entryIterator as $entry) {
if (!empty($entry['macAddress'])) {
array_push($macAddress, ...$entry['macAddress']);
}
if (!empty($entry['ipHostNumber'])) {
array_push($ipHostNumber, ...$entry['ipHostNumber']);
}
}
}
$this->parent->getBaseObject()->macAddress = array_values(array_unique($macAddress));
......@@ -396,7 +410,23 @@ class interfacesManagement extends management implements SimpleTab
public function adapt_from_template (array $attrs, array $skip = [])
{
trigger_error('Not implemented');
$this->attrs = array_merge($this->attrs, $attrs);
if (isset($this->attrs['networkInterfaces']) && !in_array('networkInterfaces', $skip)) {
unset($this->attrs['networkInterfaces']['count']);
$this->filter->loadTemplateValues($this->attrs['networkInterfaces']);
$this->listing->update();
$entryIterator = $this->listing->getIterator();
$this->interfaces = [];
foreach ($entryIterator as $entry) {
$this->interfaces[$entry->dn] = objects::create($entry->getTemplatedType());
$this->filter->resetFromCache($entry->dn, $this->interfaces[$entry->dn]);
$this->interfaces[$entry->dn]->getBaseObject()->base = $this->dn;
}
}
}
/*!
......@@ -433,7 +463,27 @@ class interfacesManagement extends management implements SimpleTab
*/
public function showInTemplate (string $attr, array $templateAttrs): bool
{
return FALSE;
return TRUE;
}
/*! \brief Check if logged in user have enough right to read this attribute value
*
* \param mixed $attr Attribute object or name (in this case it will be fetched from attributesAccess)
*/
function attrIsReadable ($attr): bool
{
/* FIXME */
return TRUE;
}
/*! \brief Check if logged in user have enough right to write this attribute value
*
* \param mixed $attr Attribute object or name (in this case it will be fetched from attributesAccess)
*/
function attrIsWriteable ($attr): bool
{
/* FIXME */
return TRUE;
}
/*!
......
......@@ -103,8 +103,13 @@ 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++);
$entries[$values['dn']]->aclBase = 'new,'.$this->parent->dn;
}
$this->entriesCache = [$entries, [$this->parent->objectTypes[0] => $row]];
}
......@@ -113,17 +118,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 +134,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;
}
}
......@@ -112,7 +112,25 @@ class networkInterface extends simplePlugin
}
}
function vlanChanged ()
/* Override setTemplate method to hide the _template_cn special field */
public function setTemplate (bool $bool)
{
$this->is_template = $bool;
if ($this->is_template && $this->mainTab) {
$this->attributesInfo['main']['attrs']['_template_cn'] = new HiddenAttribute(
_('Template name'), _('This is the name of the template'),
'_template_cn', TRUE,
'', 'template_cn'
);
$this->attributesAccess['_template_cn'] =& $this->attributesInfo['main']['attrs']['_template_cn'];
$this->attributesAccess['_template_cn']->setInLdap(FALSE);
$this->attributesAccess['_template_cn']->setValue($this->_template_cn);
$this->attributesAccess['_template_cn']->setParent($this);
unset($this->_template_cn);
}
}
public function vlanChanged ()
{
if ($this->fdNetworkInterfaceVlanDN != '') {
/* List subnets associated to our VLAN */
......
......@@ -52,6 +52,9 @@ class servicesManagement extends management implements SimpleTab
var $acl;
var $cn;
/* Used by template and webservice */
public $attributesAccess = [];
/* Default columns */
public static $columns = [
['Column', ['attributes' => 'ServiceStatusColumn', 'label' => '!']],
......@@ -620,6 +623,24 @@ class servicesManagement extends management implements SimpleTab
return FALSE;
}
/*! \brief Check if logged in user have enough right to read this attribute value
*
* \param mixed $attr Attribute object or name (in this case it will be fetched from attributesAccess)
*/
function attrIsReadable ($attr): bool
{
return FALSE;
}
/*! \brief Check if logged in user have enough right to write this attribute value
*
* \param mixed $attr Attribute object or name (in this case it will be fetched from attributesAccess)
*/
function attrIsWriteable ($attr): bool
{
return FALSE;
}
/*!
* \brief Deserialize values
*/
......
......@@ -314,7 +314,7 @@ class fdRestService extends fdRPCService
$object = $tabobject->by_object[$tab];
if (!is_subclass_of($object, 'simplePlugin')) {
if (!is_subclass_of($object, 'SimpleTab')) {
throw new WebServiceError('Invalid tab', 501);
}
......@@ -322,7 +322,7 @@ class fdRestService extends fdRPCService
throw new WebServiceError('Unknown attribute', 404);
}
if (!$object->acl_is_readable($object->attributesAccess[$attribute]->getAcl())) {
if (!$object->attrIsReadable($attribute)) {
throw new WebServiceError('Not enough rights to read "'.$attribute.'"', 403);
}
......
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