Skip to content

Commit 713ebf6

Browse files
authored
Merge pull request #18 from mikopbx/develop
Develop
2 parents 9c21865 + a74dd53 commit 713ebf6

File tree

18 files changed

+278
-54
lines changed

18 files changed

+278
-54
lines changed

.DS_Store

0 Bytes
Binary file not shown.

.github/workflows/build.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name: Build and Publish
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
- develop
8+
workflow_dispatch:
9+
10+
jobs:
11+
build:
12+
uses: mikopbx/.github-workflows/.github/workflows/extension-publish.yml@master
13+
with:
14+
initial_version: "1.35"
15+
secrets: inherit

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.idea
22
/.idea/
3-
vendor/
3+
vendor/
4+
.DS_Store

App/Controllers/ModuleLdapSyncController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function indexAction(): void
4949
->addJs('js/cache/'.$this->moduleUniqueID.'/module-ldap-sync-index.js', true);
5050

5151
$servers = LdapServers::find();
52-
$serversList = null;
52+
$serversList = [];
5353
foreach ($servers as $server){
5454
$serversList[]=[
5555
'id'=>$server->id,

App/Forms/LdapConfigForm.php

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/*
34
* MikoPBX - free phone system for small business
45
* Copyright © 2017-2023 Alexey Portnov and Nikolay Beketov
@@ -27,7 +28,6 @@
2728
use Phalcon\Forms\Element\Select;
2829
use Phalcon\Forms\Element\Text;
2930

30-
3131
class LdapConfigForm extends BaseForm
3232
{
3333
public function initialize($entity = null, $options = null): void
@@ -36,11 +36,7 @@ public function initialize($entity = null, $options = null): void
3636
$this->add(new Hidden('id'));
3737

3838
// Autosync checkbox
39-
$checkArr = [];
40-
if ($entity->disabled === '0') {
41-
$checkArr['checked'] = 'checked';
42-
}
43-
$this->add(new Check('autosync', $checkArr));
39+
$this->addCheckBox('autosync', intval($entity->disabled) === 0);
4440

4541
// ServerHost
4642
$this->add(new Text('serverName', [
@@ -50,7 +46,7 @@ public function initialize($entity = null, $options = null): void
5046
// ServerPort
5147
$this->add(new Text('serverPort', [
5248
'placeholder' => '389',
53-
'value' =>$entity->serverPort ?? '389'
49+
'value' => $entity->serverPort ?? '389'
5450
]));
5551

5652
// Use TLS dropdown
@@ -85,7 +81,9 @@ public function initialize($entity = null, $options = null): void
8581
// 'FreeIPA' => 'FreeIPA',
8682
];
8783
$ldapType = new Select(
88-
'ldapType', $types, [
84+
'ldapType',
85+
$types,
86+
[
8987
'using' => [
9088
'id',
9189
'name',
@@ -98,7 +96,7 @@ public function initialize($entity = null, $options = null): void
9896
$this->add($ldapType);
9997

10098

101-
$attributes = json_decode($entity->attributes??'', true);
99+
$attributes = json_decode($entity->attributes ?? '', true);
102100

103101

104102
// UserNameAttribute
@@ -149,11 +147,24 @@ public function initialize($entity = null, $options = null): void
149147
]));
150148

151149
// UpdateAttributes checkbox
152-
$checkArr = [];
153-
if ($entity->updateAttributes === '1') {
154-
$checkArr['checked'] = 'checked';
155-
}
156-
$this->add(new Check('updateAttributes', $checkArr));
150+
$this->addCheckBox('updateAttributes', intval($entity->updateAttributes) === 1);
151+
}
157152

153+
/**
154+
* Adds a checkbox to the form field with the given name.
155+
* Can be deleted if the module depends on MikoPBX later than 2024.3.0
156+
*
157+
* @param string $fieldName The name of the form field.
158+
* @param bool $checked Indicates whether the checkbox is checked by default.
159+
* @param string $checkedValue The value assigned to the checkbox when it is checked.
160+
* @return void
161+
*/
162+
public function addCheckBox(string $fieldName, bool $checked, string $checkedValue = 'on'): void
163+
{
164+
$checkAr = ['value' => null];
165+
if ($checked) {
166+
$checkAr = ['checked' => $checkedValue,'value' => $checkedValue];
167+
}
168+
$this->add(new Check($fieldName, $checkAr));
158169
}
159-
}
170+
}

App/Views/ModuleLdapSync/modify.volt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{{ form('module-ldap-sync/module-ldap-sync/save', 'role': 'form', 'class': 'ui large info form','id':'module-ldap-sync-form') }}
1+
<form method="post" autocomplete="off" action="module-ldap-sync/module-ldap-sync/save" role="form" class="ui large info form" id="module-ldap-sync-form">
22

33
{{ ldapForm.render('id') }}
44

@@ -20,7 +20,7 @@
2020
</div>
2121

2222
{{ partial("partials/submitbutton",['indexurl':'module-ldap-sync/module-ldap-sync/index']) }}
23-
{{ endform() }}
23+
</form>
2424

2525
<script type="text/javascript">
2626
var module_ldap_hiddenAttributes = '{{ hiddenAttributes }}';

Lib/LdapSyncConf.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class LdapSyncConf extends ConfigClass
3232
/**
3333
* Receive information about mikopbx main database changes
3434
*
35-
* @param $data
35+
* @param mixed $data
3636
*/
3737
public function modelsEventChangeData($data): void
3838
{

Lib/LdapSyncConflicts.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
namespace Modules\ModuleLdapSync\Lib;
2222

23-
use Modules\ModuleLdapSync\Models\ADUsers;
2423
use Modules\ModuleLdapSync\Models\Conflicts;
2524
use Phalcon\Di\Injectable;
2625

Lib/LdapSyncConnector.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use LdapRecord\Container;
2323
use MikoPBX\Common\Handlers\CriticalErrorsHandler;
2424
use MikoPBX\Common\Providers\ManagedCacheProvider;
25+
use Phalcon\Di\Injectable;
2526

2627

2728
include_once __DIR__.'/../vendor/autoload.php';
@@ -30,7 +31,7 @@
3031
* Class LdapSyncConnector
3132
* Handles synchronization and interaction with LDAP server.
3233
*/
33-
class LdapSyncConnector extends \Phalcon\Di\Injectable
34+
class LdapSyncConnector extends Injectable
3435
{
3536
/**
3637
* The name or ip of the LDAP server.
@@ -251,15 +252,15 @@ public function getUsersList(): AnswerStructure
251252
}
252253

253254
uksort($record, function($a, $b){
254-
return $a>$this->userAttributes[Constants::USER_NAME_ATTR];
255+
return strcmp($a, $this->userAttributes[Constants::USER_NAME_ATTR]);
255256
});
256257
if (!empty($record)){
257258
$listOfAvailableUsers[] = $record;
258259
}
259260
}
260261
// Sort the array based on the name value
261262
usort($listOfAvailableUsers, function($a, $b){
262-
return $a[$this->userAttributes[Constants::USER_NAME_ATTR]] > $b[$this->userAttributes[Constants::USER_NAME_ATTR]];
263+
return strcmp($a[$this->userAttributes[Constants::USER_NAME_ATTR]] , $b[$this->userAttributes[Constants::USER_NAME_ATTR]]);
263264
});
264265

265266
$res->data = $listOfAvailableUsers;

Lib/LdapSyncMain.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
use Modules\ModuleLdapSync\Lib\Workers\WorkerLdapSync;
2929
use Modules\ModuleLdapSync\Models\ADUsers;
3030
use Modules\ModuleLdapSync\Models\LdapServers;
31-
use Phalcon\Di;
3231
use Phalcon\Di\Injectable;
3332

3433
/**
@@ -170,7 +169,7 @@ public static function updateUserData(array $ldapCredentials, array $userFromLda
170169
$previousSyncUser->disabled = ($userDataFromLdap[Constants::USER_DISABLED] ?? false) ? '1' : '0';
171170

172171
// Do not create disabled users
173-
if ($userDataFromLdap[Constants::USER_DISABLED] === true && $userDataFromMikoPBX === []) {
172+
if ($previousSyncUser->disabled === '1' && $userDataFromMikoPBX === []) {
174173
$response = new AnswerStructure();
175174
$response->data[Constants::USER_SYNC_RESULT] = Constants::SYNC_RESULT_SKIPPED;
176175
$response->success = true;
@@ -344,7 +343,8 @@ public static function getUserOnMikoPBX(string $userId): array
344343
],
345344
];
346345
// Build and execute the query to fetch user information.
347-
$result = Di::getDefault()->get('modelsManager')->createBuilder($parameters)
346+
$di=MikoPBXVersion::getDefaultDi();
347+
$result = $di->get('modelsManager')->createBuilder($parameters)
348348
->getQuery()
349349
->getSingleResult();
350350

@@ -375,7 +375,7 @@ public static function createUpdateUser(array $userDataFromLdap, string $current
375375
}
376376

377377
// Get user data from the API
378-
$di = Di::getDefault();
378+
$di=MikoPBXVersion::getDefaultDi();
379379
$restAnswer = $di->get(PBXCoreRESTClientProvider::SERVICE_NAME, [
380380
'/pbxcore/api/extensions/getRecord',
381381
PBXCoreRESTClientProvider::HTTP_METHOD_GET,
@@ -428,7 +428,7 @@ public static function createUpdateUser(array $userDataFromLdap, string $current
428428
}
429429

430430
// Check if provided email is available
431-
$email = $userDataFromLdap[Constants::USER_EMAIL_ATTR];
431+
$email = $userDataFromLdap[Constants::USER_EMAIL_ATTR]??null;
432432
if (!empty($email) && $email !== $dataStructure->user_email) {
433433
$restAnswer = $di->get(PBXCoreRESTClientProvider::SERVICE_NAME, [
434434
'/pbxcore/api/users/available',
@@ -525,7 +525,8 @@ public static function findUserInMikoPBX(array $userDataFromLdap, string $curren
525525
$parameters['conditions'] = '(' . substr($parameters['conditions'], 3) . ') AND Extensions.type="' . Extensions::TYPE_SIP . '"';
526526
$userDataFromMikoPBX = null;
527527
if (!empty($parameters['bind'])) {
528-
$userDataFromMikoPBX = Di::getDefault()->get('modelsManager')->createBuilder($parameters)
528+
$di=MikoPBXVersion::getDefaultDi();
529+
$userDataFromMikoPBX = $di->get('modelsManager')->createBuilder($parameters)
529530
->getQuery()
530531
->getSingleResult();
531532
}
@@ -629,4 +630,5 @@ public static function postDataToLdapCredentials(array $postData): array
629630
];
630631
}
631632

633+
632634
}

0 commit comments

Comments
 (0)