2023-01-30 10:37:33 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Ldap;
|
|
|
|
|
|
|
|
use Illuminate\Contracts\Support\Arrayable;
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
use LdapRecord\Laravel\Events\Auth\DiscoveredWithCredentials;
|
|
|
|
use LdapRecord\Laravel\LdapUserRepository as LdapUserRepositoryBase;
|
|
|
|
use LdapRecord\Models\Model;
|
|
|
|
|
2023-02-19 05:35:07 +00:00
|
|
|
use App\Classes\LDAP\Server;
|
|
|
|
|
2023-01-30 10:37:33 +00:00
|
|
|
class LdapUserRepository extends LdapUserRepositoryBase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Retrieve a user by the given credentials.
|
|
|
|
*
|
|
|
|
* @param array $credentials
|
|
|
|
*
|
|
|
|
* @return Model|null
|
|
|
|
* @throws \LdapRecord\Query\ObjectNotFoundException
|
|
|
|
*/
|
|
|
|
public function findByCredentials(array $credentials = []): ?Model
|
|
|
|
{
|
|
|
|
if (empty($credentials)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2024-01-09 02:29:10 +00:00
|
|
|
// For DN based logins
|
|
|
|
if (! empty($credentials['dn']))
|
|
|
|
return $this->query()->find($credentials['dn']);
|
|
|
|
|
2023-01-30 10:37:33 +00:00
|
|
|
// Look for a user using all our baseDNs
|
2023-02-19 05:35:07 +00:00
|
|
|
foreach (Server::baseDNs() as $base) {
|
2023-01-30 10:37:33 +00:00
|
|
|
$query = $this->query()->setBaseDn($base);
|
|
|
|
|
|
|
|
foreach ($credentials as $key => $value) {
|
|
|
|
if (Str::contains($key, $this->bypassCredentialKeys)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_array($value) || $value instanceof Arrayable) {
|
|
|
|
$query->whereIn($key, $value);
|
|
|
|
} else {
|
|
|
|
$query->where($key, $value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! is_null($user = $query->first())) {
|
|
|
|
event(new DiscoveredWithCredentials($user));
|
|
|
|
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a user by their object GUID.
|
|
|
|
*
|
|
|
|
* @param string $guid
|
|
|
|
*
|
|
|
|
* @return Model|null
|
|
|
|
* @throws \LdapRecord\Query\ObjectNotFoundException
|
|
|
|
*/
|
|
|
|
public function findByGuid($guid): ?Model
|
|
|
|
{
|
|
|
|
// Look for a user using all our baseDNs
|
2023-02-19 05:35:07 +00:00
|
|
|
foreach (Server::baseDNs() as $base) {
|
2023-01-30 10:37:33 +00:00
|
|
|
$user = $this->query()->setBaseDn($base)->findByGuid($guid);
|
|
|
|
|
|
|
|
if ($user)
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|