Commit 48c9ee7f authored by Côme Chilliet's avatar Côme Chilliet
Browse files

feat(ldap) Refactor LDAP size limit handling

Moved it to a class which get instanciated in userinfo and keeps the
 size limit status for the current session.

issue #5135
parent ad5a9da6
......@@ -194,7 +194,7 @@ if ($old_plugin_dir != $plugin_dir && $old_plugin_dir != "") {
}
/* Check for sizelimits */
eval_sizelimit();
$ui->getSizeLimitHandler()->update();
/* Check for memory */
if (memory_get_usage() > (to_byte(ini_get('memory_limit')) - 2048000 )) {
......
......@@ -308,8 +308,9 @@ class config
*/
function get_ldap_link($sizelimit = FALSE)
{
if ($this->ldapLink === NULL || !is_resource($this->ldapLink->cid)) {
global $ui;
if ($this->ldapLink === NULL || !is_resource($this->ldapLink->cid)) {
/* Build new connection */
$this->ldapLink = ldap_init ($this->current['SERVER'], $this->current['BASE'],
$this->current['ADMINDN'], $this->get_credentials($this->current['ADMINPASSWORD']));
......@@ -326,16 +327,11 @@ class config
} else {
$this->ldapLink->referrals = $this->current['REFERRAL'];
}
if (!session::global_is_set('size_limit')) {
session::global_set('size_limit', $this->current['LDAPSIZELIMIT']);
session::global_set('size_ignore', preg_match('/true/i', $this->current['LDAPSIZEIGNORE']));
}
}
$obj = new ldapMultiplexer($this->ldapLink);
if ($sizelimit) {
$obj->set_size_limit(session::global_get('size_limit'));
$obj->set_size_limit($ui->getSizeLimitHandler()->getSizeLimit());
} else {
$obj->set_size_limit(0);
}
......@@ -381,20 +377,9 @@ class config
$this->current['ADMINPASSWORD'] = $this->current['REFERRAL'][$this->current['SERVER']]['ADMINPASSWORD'];
}
/* We need LDAPSIZELIMIT and LDAPSIZEIGNORE set before we connect to the ldap */
if (!isset($this->current['LDAPSIZELIMIT'])) {
$this->current['LDAPSIZELIMIT'] = 200;
}
if (!isset($this->current['LDAPSIZEIGNORE'])) {
$this->current['LDAPSIZEIGNORE'] = "TRUE";
}
/* Load in-ldap configuration */
$this->load_inldap_config();
/* We update LDAPSIZELIMIT as it may have been changed by ldap config */
session::global_set('size_limit', $this->current['LDAPSIZELIMIT']);
if (class_available('systemManagement')) {
/* Load server informations */
$this->load_servers();
......
......@@ -141,7 +141,7 @@ class filterLDAP
// Check for size limit exceeded messages for GUI feedback
if (preg_match("/size limit/i", $ldap->get_error())) {
session::set('limit_exceeded', TRUE);
$ui->getSizeLimitHandler()->setLimitExceeded();
$limit_exceeded = TRUE;
}
......
<?php
/*
This code is part of FusionDirectory (http://www.fusiondirectory.org/)
Copyright (C) 2017-2018 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.
*/
/*!
* \file class_ldapSizeLimit.inc
* Source code for the class ldapSizeLimit
*/
/*!
* \brief Class ldapSizeLimit
* This class contains all informations and functions to handle the LDAP size limit dialogs, configuration and bypass
*/
class ldapSizeLimit
{
/*! \brief Current size limit */
protected $sizeLimit;
/*! \brief Ignore dialogs */
protected $ignore;
/*! \brief Limit was exceeded */
protected $limitExceeded;
function __construct()
{
global $config;
$this->sizeLimit = $config->get_cfg_value('LDAPSIZELIMIT', 200);
$this->ignore = preg_match('/true/i', $config->get_cfg_value('LDAPSIZEIGNORE'));
}
function getSizeLimit()
{
return $this->sizeLimit;
}
function setSizeLimit($limit)
{
$this->sizeLimit = $limit;
}
function setLimitExceeded($exceeded = TRUE)
{
$this->limitExceeded = $exceeded;
}
/*!
* \brief Handle sizelimit dialog related posts
*/
function update()
{
if (isset($_POST['set_size_action']) && isset($_POST['action'])) {
switch ($_POST['action']) {
case 'newlimit':
if (isset($_POST['new_limit']) && tests::is_id($_POST['new_limit'])) {
$this->sizeLimit = intval($_POST['new_limit']);
$this->ignore = FALSE;
}
break;
case 'ignore':
$this->sizeLimit = 0;
$this->ignore = TRUE;
break;
case 'limited':
$this->ignore = TRUE;
break;
default:
break;
}
}
/* Allow fallback to dialog */
if (isset($_POST['edit_sizelimit'])) {
$this->ignore = FALSE;
}
}
/*!
* \brief Show sizelimit configuration dialog
*
* Show sizelimit configuration dialog when number
* of entries exceeded the sizelimit
*/
function check()
{
global $config;
/* Ignore dialog? */
if ($this->ignore) {
return '';
}
/* Eventually show dialog */
if ($this->limitExceeded) {
$smarty = get_smarty();
$smarty->assign('warning', sprintf(_('The size limit of %d entries is exceed!'), $this->sizeLimit));
$smarty->assign('limit_message', sprintf(_('Set the new size limit to %s and show me this message if the limit still exceeds'), '<input type="text" name="new_limit" maxlength="10" size="5" value="'.($this->sizeLimit + 100).'"/>'));
return $smarty->fetch(get_template_path('sizelimit.tpl'));
}
return '';
}
/*!
* \brief Print a sizelimit warning
*
* Print a sizelimit warning when number
* of entries exceeded the sizelimit
*/
function renderWarning()
{
if (($this->sizeLimit >= 10000000) || $this->limitExceeded) {
$config = '<input type="submit" name="edit_sizelimit" value="'._('Configure').'"/>';
} else {
$config = '';
}
if ($this->limitExceeded) {
return '('._('incomplete').") $config";
}
return '';
}
}
......@@ -333,8 +333,10 @@ class listing
*/
function render()
{
global $ui;
// Check for exeeded sizelimit
if (($message = check_sizelimit()) != '') {
if (($message = $ui->sizeLimitHandler()->check()) != '') {
return $message;
}
......@@ -472,7 +474,7 @@ class listing
$smarty = get_smarty();
$smarty->assign("usePrototype", "true");
$smarty->assign("FILTER", $this->filter->render());
$smarty->assign("SIZELIMIT", print_sizelimit_warning());
$smarty->assign("SIZELIMIT", $ui->sizeLimitHandler()->renderWarning());
$smarty->assign("LIST", $result);
$smarty->assign("MULTISELECT", $this->multiSelect);
......@@ -1179,7 +1181,7 @@ class listing
*/
function getAction()
{
global $config;
global $config, $ui;
// Do not do anything if this is not our PID, or there's even no PID available...
if (!isset($_REQUEST['dn']) && (!isset($_REQUEST['PID']) || $_REQUEST['PID'] != $this->pid)) {
......@@ -1224,11 +1226,11 @@ class listing
$this->filter->setCurrentScope('one');
}
/* Bypass size limit just to be sure */
$oldsizelimit = session::global_get('size_limit');
session::global_set('size_limit', 0);
$oldsizelimit = $ui->getSizeLimitHandler()->getSizeLimit();
$ui->getSizeLimitHandler()->setSizeLimit(0);
$this->update();
$this->render();
session::global_set('size_limit', $oldsizelimit);
$ui->getSizeLimitHandler()->setSizeLimit($oldsizelimit);
$this->filter->elementValues['NAME'] = '';
$result['action'] = $action;
......
......@@ -49,6 +49,9 @@ class userinfo
var $ACLperPath = array();
var $ACLperPath_usesFilter = array();
/*! \brief LDAP size limit handler */
protected $sizeLimitHandler;
/* get acl's an put them into the userinfo object
attr subtreeACL (userdn:components, userdn:component1#sub1#sub2,component2,...) */
function __construct($userdn)
......@@ -61,6 +64,8 @@ class userinfo
/* Initialize ACL_CACHE */
$this->reset_acl_cache();
$this->sizeLimitHandler = new ldapSizeLimit();
}
/*! \brief Loads user information from LDAP */
......@@ -1016,5 +1021,10 @@ class userinfo
}
return FALSE;
}
function getSizeLimitHandler()
{
return $this->sizeLimitHandler;
}
}
?>
......@@ -808,83 +808,6 @@ function get_locks($objects, $allow_readonly = FALSE)
return $locks;
}
/*!
* \brief Show sizelimit configuration dialog
*
* Show sizelimit configuration dialog when number
* of entries exceeded the sizelimit
*/
function check_sizelimit()
{
/* Ignore dialog? */
if (session::global_is_set('size_ignore') && session::global_get('size_ignore')) {
return '';
}
/* Eventually show dialog */
if (session::is_set('limit_exceeded') && session::get('limit_exceeded')) {
$smarty = get_smarty();
$smarty->assign('warning', sprintf(_('The size limit of %d entries is exceed!'),
session::global_get('size_limit')));
$smarty->assign('limit_message', sprintf(_('Set the new size limit to %s and show me this message if the limit still exceeds'), '<input type="text" name="new_limit" maxlength="10" size="5" value="'.(session::global_get('size_limit') + 100).'"/>'));
return $smarty->fetch(get_template_path('sizelimit.tpl'));
}
return '';
}
/*!
* \brief Print a sizelimit warning
*
* Print a sizelimit warning when number
* of entries exceeded the sizelimit
*/
function print_sizelimit_warning()
{
if (session::global_is_set('size_limit') && session::global_get('size_limit') >= 10000000 ||
(session::is_set('limit_exceeded') && session::get('limit_exceeded'))) {
$config = '<input type="submit" name="edit_sizelimit" value="'._('Configure').'"/>';
} else {
$config = '';
}
if (session::is_set('limit_exceeded') && session::get('limit_exceeded')) {
return '('._('incomplete').") $config";
}
return '';
}
/*!
* \brief Handle sizelimit dialog related posts
*/
function eval_sizelimit()
{
if (isset($_POST['set_size_action']) && isset($_POST['action'])) {
switch ($_POST['action']) {
case 'newlimit':
if (isset($_POST['new_limit']) && tests::is_id($_POST['new_limit'])) {
session::global_set('size_limit', validate($_POST['new_limit']));
session::set('size_ignore', FALSE);
}
break;
case 'ignore':
session::global_set('size_limit', 0);
session::global_set('size_ignore', TRUE);
break;
case 'limited':
session::global_set('size_ignore', TRUE);
break;
default:
break;
}
}
/* Allow fallback to dialog */
if (isset($_POST['edit_sizelimit'])) {
session::global_set('size_ignore', FALSE);
}
}
/*!
* \brief Return the current userinfo object
*
......
......@@ -420,7 +420,7 @@ class management
function renderList()
{
global $config;
global $config, $ui;
// Rendering things using smarty themselves first
$listRender = $this->listing->render();
......@@ -432,7 +432,7 @@ class management
$smarty->assign('LIST', $listRender);
$smarty->assign('FILTER', $filterRender);
$smarty->assign('ACTIONS', $actionMenu);
$smarty->assign('SIZELIMIT', print_sizelimit_warning());
$smarty->assign('SIZELIMIT', $ui->getSizeLimitHandler()->renderWarning());
$smarty->assign('MULTISELECT', $this->listing->multiSelect);
$smarty->assign('NAVIGATION', $this->listing->renderNavigation());
$smarty->assign('BASE', $this->listing->renderBase());
......
......@@ -138,8 +138,10 @@ class managementListing
*/
function render()
{
global $ui;
// Check for exeeded sizelimit
if (($message = check_sizelimit()) != '') {
if (($message = $ui->getSizeLimitHandler()->check()) != '') {
return $message;
}
......
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