From d0eedcb183efbe2ad9ff3bde3523fa9c017a9279 Mon Sep 17 00:00:00 2001 From: Stefano Date: Mon, 23 Sep 2024 10:24:50 +0200 Subject: [PATCH] remove checkUsermanIsUnlocked() (#262) * Remove checkUsermanIsUnlocked() Instead of checkoing for userman syncing ad fail if it's locked, call the update low level function without checks. * Call userman updateUser directly * set mainextension default to none instead of null * log errors --- .../www/html/freepbx/rest/lib/csvimport.php | 25 ++++----- .../html/freepbx/rest/lib/libExtensions.php | 55 +++++++++---------- .../freepbx/rest/modules/mainextensions.php | 18 +++--- 3 files changed, 42 insertions(+), 56 deletions(-) diff --git a/freepbx/var/www/html/freepbx/rest/lib/csvimport.php b/freepbx/var/www/html/freepbx/rest/lib/csvimport.php index ab4ca26c6..60a1d3f9a 100644 --- a/freepbx/var/www/html/freepbx/rest/lib/csvimport.php +++ b/freepbx/var/www/html/freepbx/rest/lib/csvimport.php @@ -129,22 +129,17 @@ # create extension if (isset($row[2]) && preg_match('/^[0-9]+$/',$row[2])) { - if (checkUsermanIsUnlocked()) { - $create = createMainExtensionForUser($username,$row[2]); - if ($create !== true) { - $result += 1; - $err .= "Error adding main extension ".$row[2]." to user ".$username.": ".$create['message']."\n"; - } else { - // assign physical device to user if it is a migration - if (function_exists('isMigration') && isMigration()) { - $secret = getOldSecret($row[2]); - $extension = createExtension($row[2],false); - useExtensionAsCustomPhysical($extension,$secret,'temporaryphysical'); - } - } + $create = createMainExtensionForUser($username,$row[2]); + if ($create !== true) { + $result += 1; + $err .= "Error adding main extension ".$row[2]." to user ".$username.": ".$create['message']."\n"; } else { - $err .= "Error adding main extension ".$row[2]." to user ".$username.": directory is locked"; - continue; + // assign physical device to user if it is a migration + if (function_exists('isMigration') && isMigration()) { + $secret = getOldSecret($row[2]); + $extension = createExtension($row[2],false); + useExtensionAsCustomPhysical($extension,$secret,'temporaryphysical'); + } } } diff --git a/freepbx/var/www/html/freepbx/rest/lib/libExtensions.php b/freepbx/var/www/html/freepbx/rest/lib/libExtensions.php index 842c7f521..d35cf44b0 100644 --- a/freepbx/var/www/html/freepbx/rest/lib/libExtensions.php +++ b/freepbx/var/www/html/freepbx/rest/lib/libExtensions.php @@ -730,10 +730,8 @@ function createMainExtensionForUser($username,$mainextension,$outboundcid='') { //Update user to add this extension as default extension //get uid - if (checkUsermanIsUnlocked()) { - $user = $fpbx->Userman->getUserByUsername($username); - $uid = $user['id']; - } + $user = $fpbx->Userman->getUserByUsername($username); + $uid = $user['id']; if (!isset($uid)) { return [array('message'=>'User not found' ), 404]; } @@ -780,9 +778,7 @@ function createMainExtensionForUser($username,$mainextension,$outboundcid='') { $stmt = $dbh->prepare($sql); $stmt->execute(array($uid)); - if (checkUsermanIsUnlocked()) { - $fpbx->Userman->updateUser($uid, $username, $username); - } + $status = updateUsermanUser($username); } //exit if extension is empty @@ -815,10 +811,7 @@ function createMainExtensionForUser($username,$mainextension,$outboundcid='') { $astman->database_del("CW",$mainextension); //update user with $extension as default extension - $res['status'] = false; - if (checkUsermanIsUnlocked()) { - $res = $fpbx->Userman->updateUser($uid, $username, $username, $mainextension); - } + $res = updateUsermanUser($username,$mainextension); if (!$res['status']) { //Can't assign extension to user, delete extension deleteExtension($mainextension); @@ -831,25 +824,6 @@ function createMainExtensionForUser($username,$mainextension,$outboundcid='') { return true; } -function checkUsermanIsUnlocked(){ - // Check if user directory is locked, wait if it is and exit fail - $locked=1; - $dbh = FreePBX::Database(); - for ($i=0; $i<30; $i++) { - $sql = 'SELECT `locked` FROM userman_directories WHERE `name` LIKE "NethServer %"'; - $sth = $dbh->prepare($sql); - $sth->execute(array()); - $locked = $sth->fetchAll()[0][0]; - if ($locked == 0) { - return true; - } - sleep(1+0.2*$i); - } - if ($locked == 1) { - return false; - } -} - function checkTableExists($table) { try { $dbh = FreePBX::Database(); @@ -1010,3 +984,24 @@ function setExtensionCustomContextProfile($extension) { function getProvisioningEngine() { return 'tancredi'; } + +function updateUsermanUser($username, $mainextension = 'none') { + global $amp_conf; + $fpbx = FreePBX::create(); + $uid = $fpbx->Userman->getUserByUsername($username)['id']; + $u = $fpbx->Userman->getUserByID($uid); + foreach ($fpbx->Userman->getAllDirectories() as $directory) { + if ($directory['id'] == $u['auth']) { + $class = 'FreePBX\modules\Userman\Auth\\'.$directory['driver']; + if(!class_exists($class)) { + include($amp_conf['AMPWEBROOT']."/admin/modules/userman/functions.inc/auth/modules/".$directory['driver'].".php"); + } + $d = new $class($fpbx->Userman, $fpbx, $directory['config']); + $status = $d->updateUser($uid, $username, $username, $mainextension, null, array(), null, false); + if ($status['status'] == false) { + error_log(__FUNCTION__ . 'ERROR: ' . $status['message']); + } + return $status; + } + } +} \ No newline at end of file diff --git a/freepbx/var/www/html/freepbx/rest/modules/mainextensions.php b/freepbx/var/www/html/freepbx/rest/modules/mainextensions.php index 3c019899b..95a2ae7f2 100644 --- a/freepbx/var/www/html/freepbx/rest/modules/mainextensions.php +++ b/freepbx/var/www/html/freepbx/rest/modules/mainextensions.php @@ -48,18 +48,14 @@ $mainextension = $params['extension']; $outboundcid = $params['outboundcid']; - if (checkUsermanIsUnlocked()) { - if ( - configuredUsersCount() >= communityUsersLimit() && // check if there are more configured users than the allowed limit - (empty($_ENV['SUBSCRIPTION_SYSTEMID']) || empty($_ENV['SUBSCRIPTION_SECRET'])) && // it isn't registered as enterprise - !empty($mainextension) // the user is trying to create a new extension - ) { - return $response->withJson(array("status"=>'ERROR: community version is limited to 8 users'), 403); - } - $ret = createMainExtensionForUser($username,$mainextension,$outboundcid); - } else { - return $response->withJson(array("status"=>'ERROR: directory is locked'), 500); + if ( + configuredUsersCount() >= communityUsersLimit() && // check if there are more configured users than the allowed limit + (empty($_ENV['SUBSCRIPTION_SYSTEMID']) || empty($_ENV['SUBSCRIPTION_SECRET'])) && // it isn't registered as enterprise + !empty($mainextension) // the user is trying to create a new extension + ) { + return $response->withJson(array("status"=>'ERROR: community version is limited to 8 users'), 403); } + $ret = createMainExtensionForUser($username,$mainextension,$outboundcid); system('/var/www/html/freepbx/rest/lib/retrieveHelper.sh > /dev/null &');