LdapConfig::first(), ]); } public function update(Request $request) { $validated = $request->validate([ 'host' => 'required|string', 'port' => 'required|integer', 'use_ssl' => 'boolean', 'use_tls' => 'boolean', 'base_dn' => 'required|string', 'bind_dn' => 'nullable|string', 'bind_password' => 'nullable|string', 'user_search_base' => 'nullable|string', 'user_filter' => 'required|string', 'admin_group_dn' => 'nullable|string', 'is_active' => 'boolean', ]); $config = LdapConfig::firstOrNew([]); $config->fill($validated); if (empty($validated['bind_password'])) { unset($config->bind_password); } $config->save(); activity()->causedBy(auth()->user())->log('LDAP configuration updated'); return back()->with('success', 'LDAP configuration saved.'); } public function test(Request $request) { try { $config = $request->validate([ 'host' => 'required', 'port' => 'required', 'bind_dn' => 'nullable', 'bind_password' => 'nullable', 'base_dn' => 'required', ]); $conn = ldap_connect("ldap://{$config['host']}:{$config['port']}"); ldap_set_option($conn, LDAP_OPT_PROTOCOL_VERSION, 3); ldap_set_option($conn, LDAP_OPT_REFERRALS, 0); ldap_set_option($conn, LDAP_OPT_TIMELIMIT, 5); $bound = $config['bind_dn'] ? @ldap_bind($conn, $config['bind_dn'], $config['bind_password']) : @ldap_bind($conn); ldap_close($conn); if (!$bound) { return response()->json(['success' => false, 'message' => 'Bind failed. Check credentials.']); } return response()->json(['success' => true, 'message' => 'LDAP connection and bind successful!']); } catch (\Throwable $e) { return response()->json(['success' => false, 'message' => $e->getMessage()]); } } public function sync() { // Sync LDAP users to local database return back()->with('success', 'LDAP sync started.'); } }