SSOGroupEnricher::process()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 32
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 32
ccs 0
cts 24
cp 0
rs 9.6333
cc 4
nc 6
nop 2
crap 20
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DMK\MKSamlAuth\Enricher;
6
7
use DMK\MKSamlAuth\Model\FrontendUser;
8
use Doctrine\DBAL\Connection;
9
use TYPO3\CMS\Core\Database\ConnectionPool;
10
use TYPO3\CMS\Core\Utility\GeneralUtility;
11
12
class SSOGroupEnricher implements EnricherInterface
13
{
14
    const ATTRIBUTE_KEY = 'memberof';
15
16
    public function process(FrontendUser $user, array $context)
17
    {
18
        $names = $context['attributes'][self::ATTRIBUTE_KEY] ?? [];
19
        $names = \is_array($names) ? $names : [$names];
20
21
        if (0 === \count($names)) {
22
            return;
23
        }
24
25
        $qb = GeneralUtility::makeInstance(ConnectionPool::class)
26
            ->getQueryBuilderForTable('tx_mksamlauth_domain_model_group_mapping');
27
28
        $qb->select('e.group_ids');
29
        $qb->from('tx_mksamlauth_domain_model_group_mapping', 'e');
30
        $qb->where($qb->expr()->andX(
31
            $qb->expr()->eq('e.idp_id', ':id'),
32
            $qb->expr()->in('e.idp_name', ':names')
33
        ));
34
        $qb->setParameter('id', $context['idp']['uid']);
35
        $qb->setParameter('names', $names, Connection::PARAM_STR_ARRAY);
36
37
        $groups = $qb->execute()->fetchAll();
38
39
        // If there are no groups, we don't want to override them.
40
        if (0 === \count($groups)) {
41
            return;
42
        }
43
44
        $groups = array_map('current', $groups);
45
        $groups = implode(',', $groups);
46
47
        $user->setProperty('usergroup', $groups);
48
    }
49
}
50