CoreFiles by mySites.guru
MemberOfRoles.php56 lines, 1,411 bytes, from Joomla_6.2.0-Stable-Full_Package.zip
Raw History Compare with 6.1.4
md5
91247e127903089e3cbdbcede7ec98ad
sha1
7375da43296a92b5b8f19bc82a000d052a430ec9
sha256
2e9a040c794d818eaf96175d1115d17d4d8049464b43c68e357549f608be2c48
<?php

/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <[email protected]>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Symfony\Component\Ldap\Security;

use Symfony\Component\Ldap\Entry;

final class MemberOfRoles implements RoleFetcherInterface
{
    /**
     * @param array<string, string> $mapping
     */
    public function __construct(
        private readonly array $mapping,
        private readonly string $attributeName = 'ismemberof',
        private readonly string $groupNameRegex = '/^CN=(?P<group>[^,]+),ou.*$/i',
    ) {
    }

    /**
     * @return string[]
     */
    public function fetchRoles(Entry $entry): array
    {
        if (!$entry->hasAttribute($this->attributeName)) {
            return [];
        }

        $roles = [];
        foreach ($entry->getAttribute($this->attributeName) as $group) {
            $groupName = $this->getGroupName($group);
            if (\array_key_exists($groupName, $this->mapping)) {
                $roles[] = $this->mapping[$groupName];
            }
        }

        return array_unique($roles);
    }

    private function getGroupName(string $group): string
    {
        if (preg_match($this->groupNameRegex, $group, $matches)) {
            return $matches['group'];
        }

        return $group;
    }
}

The same content is in 32 releases, Joomla 6.1.0-alpha2 to 6.3.0-alpha1. See them all, or every version of this file.