Passed
Push — master ( d309df...7ec489 )
by Petr
02:37
created

Ldap::getDataOnly()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 6
rs 10
1
<?php
2
3
namespace kalanis\kw_auth\Sources\Mapper;
4
5
6
use kalanis\kw_auth\Interfaces\IAuth;
7
use kalanis\kw_auth\Interfaces\IUser;
8
9
10
/**
11
 * Class Ldap
12
 * @package kalanis\kw_auth\Sources\Mapper
13
 * Authenticate via ldap
14
 * need kw_mapper!
15
 * @codeCoverageIgnore because access external content
16
 */
17
class Ldap implements IAuth
18
{
19
    /** @var Ldap\LdapRecord */
20
    protected $record = null;
21
22
    public function __construct()
23
    {
24
        $this->record = new Ldap\LdapRecord();
25
    }
26
27
    public function authenticate(string $userName, array $params = []): ?IUser
28
    {
29
        $mapper = $this->record->getMapper();
30
        if (!method_exists($mapper, 'authorize')) {
31
            return null;
32
        }
33
        /** @var Ldap\LdapMapper $mapper */
34
        return ($mapper->authorize([
35
            'user' => $userName,
36
            'password' => $params['password'] ?: ''
37
        ]))
38
            ? $this->getDataOnly($userName)
39
            : null ;
40
    }
41
42
    public function getDataOnly(string $userName): ?IUser
43
    {
44
        $record = new Ldap\LdapRecord();
45
        $record->name = $userName;
46
        $record->load();
47
        return (empty($record->getAuthId())) ? null : $record ;
48
    }
49
}
50