Skip to content

A free open source IT asset/license management system

Adds #13997 - API endpoint to sync users via LDAP

snipebcf301ac17c2
diff --git a/app/Http/Controllers/Api/UsersController.php b/app/Http/Controllers/Api/UsersController.php
index ff54880086..4188e1e066 100644
--- a/app/Http/Controllers/Api/UsersController.php
+++ b/app/Http/Controllers/Api/UsersController.php
@@ -20,6 +20,7 @@ use App\Models\Consumable;
use App\Models\License;
use App\Models\User;
use App\Notifications\CurrentInventory;
+use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Auth;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
@@ -822,4 +823,37 @@ class UsersController extends Controller
return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/users/message.user_not_found')), 200);
}
+
+
+ /**
+ * Run the LDAP sync command to import users from LDAP via API.
+ *
+ * @author A. Gianotto <[email protected]>
+ * @since 8.2.2
+ *
+ * @return \Illuminate\Http\JsonResponse
+ */
+ public function syncLdapUsers(Request $request)
+ {
+ $this->authorize('update', User::class);
+ // Call Artisan LDAP import command.
+
+ Artisan::call('snipeit:ldap-sync', ['--location_id' => $request->input('location_id'), '--json_summary' => true]);
+
+ // Collect and parse JSON summary.
+ $ldap_results_json = Artisan::output();
+ $ldap_results = json_decode($ldap_results_json, true);
+
+ if (!$ldap_results) {
+ return response()->json(Helper::formatStandardApiResponse('error', null,trans('general.no_results')), 200);
+ }
+
+ // Direct user to appropriate status page.
+ if ($ldap_results['error']) {
+ return response()->json(Helper::formatStandardApiResponse('error', null, $ldap_results['error_message']), 200);
+ }
+
+ return response()->json(Helper::formatStandardApiResponse('success', null, $ldap_results['summary']), 200);
+
+ }
}
diff --git a/routes/api.php b/routes/api.php
index be8983aba3..edde2b9b71 100644
--- a/routes/api.php
+++ b/routes/api.php
@@ -1031,6 +1031,13 @@ Route::group(['prefix' => 'v1', 'middleware' => ['api', 'api-throttle:api']], fu
]
)->name('api.users.selectlist');
+ Route::post('ldapsync',
+ [
+ Api\UsersController::class,
+ 'syncLdapUsers'
+ ]
+ )->name('api.users.ldapsync');
+
Route::post('two_factor_reset',
[
Api\UsersController::class,