Passed
Push — 3.0 ( 5e1ed3...5a5495 )
by Rubén
04:22
created

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