Passed
Push — devel-3.0 ( 5a06ca...84adb8 )
by Rubén
03:54
created

Ldap::connect()   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 0
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 LdapConnection
50
     */
51
    protected $ldapConnection;
52
53
    /**
54
     * LdapBase constructor.
55
     *
56
     * @param LdapConnection  $ldapConnection
57
     * @param EventDispatcher $eventDispatcher
58
     *
59
     * @throws LdapException
60
     */
61
    public function __construct(LdapConnection $ldapConnection, EventDispatcher $eventDispatcher)
62
    {
63
        $this->ldapConnection = $ldapConnection;
64
65
        $this->ldapParams = $ldapConnection->getLdapParams();
66
        $this->ldapParams->setServer($this->pickServer());
67
68
        $this->eventDispatcher = $eventDispatcher;
69
        $this->ldapActions = new LdapActions($ldapConnection, $eventDispatcher);
70
    }
71
72
    /**
73
     * Obtener el servidor de LDAP a utilizar
74
     *
75
     * @return mixed
76
     */
77
    protected abstract function pickServer();
78
79
    /**
80
     * @return LdapConnection
81
     */
82
    public function getLdapConnection(): LdapConnection
83
    {
84
        return $this->ldapConnection;
85
    }
86
87
    /**
88
     * @return LdapActions
89
     */
90
    public function getLdapActions(): LdapActions
91
    {
92
        return $this->ldapActions;
93
    }
94
95
    /**
96
     * Realizar la conexión al servidor de LDAP.
97
     *
98
     * @return resource
99
     * @throws LdapException
100
     */
101
    protected function connect()
102
    {
103
        return $this->ldapConnection->connectAndBind();
104
    }
105
106
    /**
107
     * @return string
108
     */
109
    protected function getGroupFromParams(): string
110
    {
111
        if (strpos($this->ldapParams->getGroup(), 'cn') === 0) {
112
            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...
113
        }
114
115
        return $this->ldapParams->getGroup();
116
    }
117
118
    /**
119
     * @return string
120
     * @throws LdapException
121
     */
122
    protected function getGroupDn(): string
123
    {
124
        if (strpos($this->ldapParams->getGroup(), 'cn') === 0) {
125
            return $this->ldapParams->getGroup();
126
        }
127
128
        return $this->ldapActions->searchGroupsDn($this->getGroupObjectFilter())[0];
129
    }
130
}