2021-10-02 14:33:14 +02:00
|
|
|
<?php
|
2021-10-02 16:58:33 +02:00
|
|
|
declare(strict_types=1);
|
2021-10-02 14:33:14 +02:00
|
|
|
|
|
|
|
|
namespace FireflyIII\Ldap\Rules;
|
|
|
|
|
|
|
|
|
|
use LdapRecord\Laravel\Auth\Rule;
|
2021-10-30 06:50:04 +02:00
|
|
|
use LdapRecord\Models\Attributes\DistinguishedName;
|
2021-10-23 08:32:33 +02:00
|
|
|
use LdapRecord\Query\ObjectNotFoundException;
|
2021-10-03 15:31:36 +02:00
|
|
|
use Log;
|
2021-10-02 14:33:14 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class UserDefinedRule
|
|
|
|
|
*/
|
|
|
|
|
class UserDefinedRule extends Rule
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Check if the rule passes validation.
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
2021-10-23 08:32:33 +02:00
|
|
|
* @throws ObjectNotFoundException
|
2021-10-02 14:33:14 +02:00
|
|
|
*/
|
|
|
|
|
public function isValid()
|
|
|
|
|
{
|
2021-10-30 06:53:21 +02:00
|
|
|
$extraFilter = config('ldap.extra_filter');
|
|
|
|
|
Log::debug(sprintf('UserDefinedRule with extra filter "%s"', $extraFilter));
|
2021-10-30 06:50:04 +02:00
|
|
|
|
2021-10-30 06:53:21 +02:00
|
|
|
if (empty($extraFilter)) {
|
|
|
|
|
Log::debug('Extra filter is empty, return true.');
|
2021-10-30 06:50:04 +02:00
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2021-10-30 06:53:21 +02:00
|
|
|
Log::debug('Extra filter is not empty, continue.');
|
2021-10-30 06:50:04 +02:00
|
|
|
|
|
|
|
|
// group class:
|
|
|
|
|
// use ;
|
|
|
|
|
$openLDAP = class_exists(\LdapRecord\Models\OpenLDAP\Group::class) ? \LdapRecord\Models\OpenLDAP\Group::class : '';
|
|
|
|
|
$activeDirectory = class_exists(\LdapRecord\Models\ActiveDirectory\Group::class) ? \LdapRecord\Models\ActiveDirectory\Group::class : '';
|
2021-10-30 06:53:21 +02:00
|
|
|
$groupClass = config('ldap.dialect') === 'OpenLDAP' ? $openLDAP : $activeDirectory;
|
2021-10-30 06:50:04 +02:00
|
|
|
|
2021-10-30 06:53:21 +02:00
|
|
|
Log::debug(sprintf('Will use dialect group class "%s"', $groupClass));
|
2021-10-30 06:50:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
// We've been given an invalid group filter. We will assume the
|
|
|
|
|
// developer is using some group ANR attribute, and attempt
|
|
|
|
|
// to check the user's membership with the resulting group.
|
2021-10-30 06:53:21 +02:00
|
|
|
if (!DistinguishedName::isValid($extraFilter)) {
|
2021-10-30 06:50:04 +02:00
|
|
|
Log::debug('UserDefinedRule: Is not valid DN');
|
|
|
|
|
|
2021-10-30 06:53:21 +02:00
|
|
|
return $this->user->groups()->recursive()->exists($groupClass::findByAnrOrFail($extraFilter));
|
2021-10-30 06:50:04 +02:00
|
|
|
}
|
|
|
|
|
|
2021-10-30 06:53:21 +02:00
|
|
|
$head = strtolower(DistinguishedName::make($extraFilter)->head());
|
2021-10-30 06:50:04 +02:00
|
|
|
Log::debug(sprintf('UserDefinedRule: Head is "%s"', $head));
|
|
|
|
|
// If the head of the DN we've been given is an OU, we will assume
|
|
|
|
|
// the developer is looking to filter users based on hierarchy.
|
|
|
|
|
// Otherwise, we'll attempt locating a group by the given
|
|
|
|
|
// group filter and checking the users group membership.
|
|
|
|
|
if ('ou' === $head) {
|
|
|
|
|
Log::debug('UserDefinedRule: Will return if user is a descendant of.');
|
|
|
|
|
|
2021-10-30 06:53:21 +02:00
|
|
|
return $this->user->isDescendantOf($extraFilter);
|
2021-10-02 14:33:14 +02:00
|
|
|
}
|
2021-10-30 06:50:04 +02:00
|
|
|
Log::debug('UserDefinedRule: Will return if user exists in group.');
|
2021-10-02 14:33:14 +02:00
|
|
|
|
2021-10-30 06:53:21 +02:00
|
|
|
return $this->user->groups()->recursive()->exists($groupClass::findOrFail($extraFilter));
|
2021-10-02 14:33:14 +02:00
|
|
|
}
|
|
|
|
|
}
|