diff --git a/app/Classes/LDAP/Server.php b/app/Classes/LDAP/Server.php index d673837..3b53039 100644 --- a/app/Classes/LDAP/Server.php +++ b/app/Classes/LDAP/Server.php @@ -2,10 +2,11 @@ namespace App\Classes\LDAP; +use Carbon\Carbon; use Exception; use Illuminate\Support\Arr; use Illuminate\Support\Facades\Cache; - +use Illuminate\Support\Facades\Config; use LdapRecord\Models\Model; use LdapRecord\Query\Collection; use LdapRecord\Query\Model\Builder; @@ -24,6 +25,7 @@ class Server { return ($x=(new Entry) ->query() + ->cache(Carbon::now()->addSeconds(Config::get('ldap.cache.time'))) ->select(['*','hassubordinates']) ->setDn($dn) ->listing() @@ -41,6 +43,7 @@ class Server { return ($x=(new Entry) ->query() + ->cache(Carbon::now()->addSeconds(Config::get('ldap.cache.time'))) ->select($attrs) ->find($dn)) ? $x : NULL; } diff --git a/app/Http/Controllers/HomeController.php b/app/Http/Controllers/HomeController.php index 5af3d11..a19ecf7 100644 --- a/app/Http/Controllers/HomeController.php +++ b/app/Http/Controllers/HomeController.php @@ -30,7 +30,7 @@ class HomeController extends Controller */ public function home() { - $base = (new Entry)->baseDN() ?: collect(); + $base = (new Entry)->baseDNs() ?: collect(); return view('home') ->with('server',config('ldap.connections.default.name')) diff --git a/app/Ldap/Entry.php b/app/Ldap/Entry.php index 9b60322..920cb3e 100644 --- a/app/Ldap/Entry.php +++ b/app/Ldap/Entry.php @@ -2,8 +2,11 @@ namespace App\Ldap; +use Carbon\Carbon; use Illuminate\Support\Arr; use Illuminate\Support\Collection; +use Illuminate\Support\Facades\Config; +use LdapRecord\LdapRecordException; use LdapRecord\Models\Model; use LdapRecord\Query\ObjectNotFoundException; @@ -41,18 +44,33 @@ class Entry extends Model * @throws ObjectNotFoundException * @testedin GetBaseDNTest::testBaseDNExists(); */ - public static function baseDN($connection = NULL): ?Collection + public static function baseDNs($connection = NULL): ?Collection { - $base = static::on($connection ?? (new static)->getConnectionName()) - ->in(NULL) - ->read() - ->select(['namingcontexts']) - ->whereHas('objectclass') - ->firstOrFail(); + $cachetime = Carbon::now()->addSeconds(Config::get('ldap.cache.time')); + try { + $base = static::on($connection ?? (new static)->getConnectionName()) + ->cache($cachetime) + ->in(NULL) + ->read() + ->select(['namingcontexts']) + ->whereHas('objectclass') + ->firstOrFail(); + + // If we cannot get to our LDAP server we'll head straight to the error page + } catch (LdapRecordException $e) { + abort(597,$e->getMessage()); + } + + /** + * @note While we are caching our baseDNs, it seems if we have more than 1, + * our caching doesnt generate a hit on a subsequent call to this function (before the cache expires). + * IE: If we have 5 baseDNs, it takes 5 calls to this function to case them all. + * @todo Possibly a bug wtih ldaprecord, so need to investigate + */ $result = collect(); foreach ($base->namingcontexts as $dn) { - $result->push((new self)->findOrFail($dn)); + $result->push((new self)->cache($cachetime)->findOrFail($dn)); } return $result; diff --git a/config/ldap.php b/config/ldap.php index 47ca281..2a499db 100644 --- a/config/ldap.php +++ b/config/ldap.php @@ -69,6 +69,7 @@ return [ 'cache' => [ 'enabled' => env('LDAP_CACHE', false), 'driver' => env('CACHE_DRIVER', 'file'), + 'time' => env('LDAP_CACHE_TIME',5*60), // Seconds ], ]; diff --git a/resources/themes/architect/views/layouts/error.blade.php b/resources/themes/architect/views/layouts/error.blade.php new file mode 100644 index 0000000..8fdf58a --- /dev/null +++ b/resources/themes/architect/views/layouts/error.blade.php @@ -0,0 +1,56 @@ + + +@section('htmlheader') + @include('architect::layouts.partials.htmlheader') +@show + + +
+ +
+
+
+
+ +
+
+
+
+
+ + + \ No newline at end of file diff --git a/resources/views/errors/597.blade.php b/resources/views/errors/597.blade.php new file mode 100644 index 0000000..3ad1113 --- /dev/null +++ b/resources/views/errors/597.blade.php @@ -0,0 +1,9 @@ +@extends('architect::layouts.error') + +@section('error') + @lang('LDAP Server Unavailable') +@endsection + +@section('content') + {{ $exception->getMessage() }} +@endsection \ No newline at end of file