Skip to content

Commit

Permalink
remove checkUsermanIsUnlocked() (#262)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Stell0 authored Sep 23, 2024
1 parent 337c8c4 commit d0eedcb
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 56 deletions.
25 changes: 10 additions & 15 deletions freepbx/var/www/html/freepbx/rest/lib/csvimport.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
}

Expand Down
55 changes: 25 additions & 30 deletions freepbx/var/www/html/freepbx/rest/lib/libExtensions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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();
Expand Down Expand Up @@ -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;
}
}
}
18 changes: 7 additions & 11 deletions freepbx/var/www/html/freepbx/rest/modules/mainextensions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 &');

Expand Down

0 comments on commit d0eedcb

Please sign in to comment.