Passed
Push — 3.0 ( d2a6b7...b247bd )
by Rubén
04:14
created

Ldap::getLdapActions()   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
     * @param LdapParams      $ldapParams
87
     * @param EventDispatcher $eventDispatcher
88
     * @param bool            $debug
89
     *
90
     * @return LdapInterface
91
     * @throws LdapException
92
     */
93
    public static function factory(LdapParams $ldapParams, EventDispatcher $eventDispatcher, bool $debug)
94
    {
95
        $ldapConnection = new LdapConnection($ldapParams, $eventDispatcher, $debug);
96
        $ldapConnection->checkConnection();
97
98
        switch ($ldapParams->getType()) {
99
            case LdapTypeInterface::LDAP_STD:
100
                return new LdapStd($ldapConnection, $eventDispatcher);
101
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
102
            case LdapTypeInterface::LDAP_ADS:
103
                return new LdapMsAds($ldapConnection, $eventDispatcher);
104
                break;
105
            case LdapTypeInterface::LDAP_AZURE;
106
                return new LdapMsAzureAd($ldapConnection, $eventDispatcher);
107
                break;
108
        }
109
110
        throw new LdapException(__u('LDAP type not set'));
111
    }
112
113
    /**
114
     * @return LdapActions
115
     */
116
    public function getLdapActions(): LdapActions
117
    {
118
        return $this->ldapActions;
119
    }
120
121
    /**
122
     * Realizar la conexión al servidor de LDAP.
123
     *
124
     * @return resource
125
     * @throws LdapException
126
     */
127
    public function connect()
128
    {
129
        return $this->ldapConnection->connectAndBind();
130
    }
131
132
    /**
133
     * @param string $bindDn
134
     * @param string $bindPass
135
     *
136
     * @return bool
137
     */
138
    public function bind(string $bindDn = null, string $bindPass = null): bool
139
    {
140
        return $this->ldapConnection->bind($bindDn, $bindPass);
141
    }
142
143
    /**
144
     * @return string
145
     */
146
    public function getServer(): string
147
    {
148
        return $this->server;
149
    }
150
151
    /**
152
     * @return string
153
     */
154
    protected function getGroupFromParams(): string
155
    {
156
        if (stripos($this->ldapParams->getGroup(), 'cn') === 0) {
157
            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...
158
        }
159
160
        return $this->ldapParams->getGroup();
161
    }
162
163
    /**
164
     * @return string
165
     * @throws LdapException
166
     */
167
    protected function getGroupDn(): string
168
    {
169
        if (stripos($this->ldapParams->getGroup(), 'cn') === 0) {
170
            return $this->ldapParams->getGroup();
171
        }
172
173
        return $this->ldapActions->searchGroupsDn($this->getGroupObjectFilter())[0];
174
    }
175
}
176