Passed
Push — devel-3.0 ( 84adb8...e69242 )
by Rubén
03:09
created

Ldap::bind()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * sysPass
4
 *
5
 * @author    nuxsmin
6
 * @link      https://syspass.org
7
 * @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org
8
 *
9
 * This file is part of sysPass.
10
 *
11
 * sysPass is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation, either version 3 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * sysPass is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 *  along with sysPass.  If not, see <http://www.gnu.org/licenses/>.
23
 */
24
25
namespace SP\Providers\Auth\Ldap;
26
27
use SP\Core\Events\EventDispatcher;
28
29
/**
30
 * Class Ldap
31
 *
32
 * @package SP\Providers\Auth\Ldap
33
 */
34
abstract class Ldap implements LdapInterface
35
{
36
    /**
37
     * @var LdapParams
38
     */
39
    protected $ldapParams;
40
    /**
41
     * @var EventDispatcher
42
     */
43
    protected $eventDispatcher;
44
    /**
45
     * @var LdapActions
46
     */
47
    protected $ldapActions;
48
    /**
49
     * @var LdapConnectionInterface
50
     */
51
    protected $ldapConnection;
52
53
    /**
54
     * LdapBase constructor.
55
     *
56
     * @param LdapConnectionInterface $ldapConnection
57
     * @param EventDispatcher         $eventDispatcher
58
     */
59
    public function __construct(LdapConnectionInterface $ldapConnection, EventDispatcher $eventDispatcher)
60
    {
61
        $this->ldapConnection = $ldapConnection;
62
63
        $this->ldapParams = $ldapConnection->getLdapParams();
64
        $this->ldapParams->setServer($this->pickServer());
65
66
        $this->eventDispatcher = $eventDispatcher;
67
        $this->ldapActions = new LdapActions($ldapConnection, $eventDispatcher);
68
    }
69
70
    /**
71
     * Obtener el servidor de LDAP a utilizar
72
     *
73
     * @return mixed
74
     */
75
    protected abstract function pickServer();
76
77
    /**
78
     * @return LdapActions
79
     */
80
    public function getLdapActions(): LdapActions
81
    {
82
        return $this->ldapActions;
83
    }
84
85
    /**
86
     * Realizar la conexión al servidor de LDAP.
87
     *
88
     * @return resource
89
     * @throws LdapException
90
     */
91
    public function connect()
92
    {
93
        return $this->ldapConnection->connectAndBind();
94
    }
95
96
    /**
97
     * @param string $bindDn
98
     * @param string $bindPass
99
     *
100
     * @return bool
101
     */
102
    public function bind(string $bindDn = null, string $bindPass = null): bool
103
    {
104
        return $this->ldapConnection->bind($bindDn, $bindPass);
105
    }
106
107
    /**
108
     * @return string
109
     */
110
    protected function getGroupFromParams(): string
111
    {
112
        if (strpos($this->ldapParams->getGroup(), 'cn') === 0) {
113
            return LdapUtil::getGroupName($this->ldapParams->getGroup());
0 ignored issues
show
Bug Best Practice introduced by
The expression return SP\Providers\Auth...ldapParams->getGroup()) returns the type boolean which is incompatible with the type-hinted return string.
Loading history...
114
        }
115
116
        return $this->ldapParams->getGroup();
117
    }
118
119
    /**
120
     * @return string
121
     * @throws LdapException
122
     */
123
    protected function getGroupDn(): string
124
    {
125
        if (strpos($this->ldapParams->getGroup(), 'cn') === 0) {
126
            return $this->ldapParams->getGroup();
127
        }
128
129
        return $this->ldapActions->searchGroupsDn($this->getGroupObjectFilter())[0];
130
    }
131
}