Skip to content

A free open source IT asset/license management system

Add a new `--filter` option to Artisan ldap-sync command

Brady Wetheringtonbe3388d64728
diff --git a/app/Console/Commands/LdapSync.php b/app/Console/Commands/LdapSync.php
index 052a99932b..fe264280e0 100755
--- a/app/Console/Commands/LdapSync.php
+++ b/app/Console/Commands/LdapSync.php
@@ -17,7 +17,7 @@ class LdapSync extends Command
*
* @var string
*/
- protected $signature = 'snipeit:ldap-sync {--location=} {--location_id=} {--base_dn=} {--summary} {--json_summary}';
+ protected $signature = 'snipeit:ldap-sync {--location=} {--location_id=} {--base_dn=} {--filter=} {--summary} {--json_summary}';
/**
* The console command description.
@@ -80,7 +80,11 @@ class LdapSync extends Command
} else {
$search_base = null;
}
- $results = Ldap::findLdapUsers($search_base);
+ if ($this->option('filter') != '') {
+ $results = Ldap::findLdapUsers($search_base, -1, $this->option('filter'));
+ } else {
+ $results = Ldap::findLdapUsers($search_base);
+ }
} catch (\Exception $e) {
if ($this->option('json_summary')) {
$json_summary = ['error' => true, 'error_message' => $e->getMessage(), 'summary' => []];
@@ -109,7 +113,7 @@ class LdapSync extends Command
}
/* Process locations with explicitly defined OUs, if doing a full import. */
- if ($this->option('base_dn') == '') {
+ if ($this->option('base_dn') == '' && $this->option('filter') == '') {
// Retrieve locations with a mapped OU, and sort them from the shallowest to deepest OU (see #3993)
$ldap_ou_locations = Location::where('ldap_ou', '!=', '')->get()->toArray();
$ldap_ou_lengths = [];
diff --git a/app/Models/Ldap.php b/app/Models/Ldap.php
index 935270a563..a24e062f97 100644
--- a/app/Models/Ldap.php
+++ b/app/Models/Ldap.php
@@ -275,9 +275,10 @@ class Ldap extends Model
* @since [v3.0]
* @param $base_dn
* @param $count
+ * @param $filter
* @return array|bool
*/
- public static function findLdapUsers($base_dn = null, $count = -1)
+ public static function findLdapUsers($base_dn = null, $count = -1, $filter = null)
{
$ldapconn = self::connectToLdap();
self::bindAdminToLdap($ldapconn);
@@ -285,7 +286,9 @@ class Ldap extends Model
if (is_null($base_dn)) {
$base_dn = Setting::getSettings()->ldap_basedn;
}
- $filter = Setting::getSettings()->ldap_filter;
+ if($filter === null) {
+ $filter = Setting::getSettings()->ldap_filter;
+ }
// Set up LDAP pagination for very large databases
$page_size = 500;