CoreFiles by mySites.guru
MemberOfRoles.php56 lines, 1,393 bytes, from Joomla_6.0.2-Stable-Full_Package.zip
Raw History Compare with 6.0.1
md5
148d2232e7461aa644982f45d1fd011a
sha1
fff4f4e9755854e1af0f44a25137e737554777e1
sha256
eaaecf796d64ddf949bb9f45482cda51ab24075b1d28a66cadb3afd4dbd1dca8
<?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 readonly class MemberOfRoles implements RoleFetcherInterface
{
    /**
     * @param array<string, string> $mapping
     */
    public function __construct(
        private array $mapping,
        private string $attributeName = 'ismemberof',
        private 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 17 releases, Joomla 6.0.0-beta1 to 6.1.0-alpha1. See them all, or every version of this file.