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

LdapAuth::isAuthGranted()   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
use SP\DataModel\UserLoginData;
29
use SP\Providers\Auth\AuthInterface;
30
31
/**
32
 * Class LdapBase
33
 *
34
 * @package Auth\Ldap
35
 */
36
class LdapAuth implements AuthInterface
37
{
38
    /**
39
     * @var string
40
     */
41
    protected $userLogin;
42
    /**
43
     * @var LdapAuthData
44
     */
45
    protected $ldapAuthData;
46
    /**
47
     * @var LdapParams
48
     */
49
    protected $ldapParams;
50
    /**
51
     * @var EventDispatcher
52
     */
53
    protected $eventDispatcher;
54
    /**
55
     * @var string
56
     */
57
    protected $server;
58
    /**
59
     * @var Ldap
60
     */
61
    private $ldap;
62
63
    /**
64
     * LdapBase constructor.
65
     *
66
     * @param Ldap            $ldap
67
     * @param EventDispatcher $eventDispatcher
68
     */
69
    public function __construct(Ldap $ldap, EventDispatcher $eventDispatcher)
70
    {
71
        $this->ldap = $ldap;
72
        $this->eventDispatcher = $eventDispatcher;
73
74
        $this->ldapAuthData = new LdapAuthData();
75
    }
76
77
    /**
78
     * @return LdapAuthData
79
     */
80
    public function getLdapAuthData()
81
    {
82
        return $this->ldapAuthData;
83
    }
84
85
    /**
86
     * @return string
87
     */
88
    public function getUserLogin()
89
    {
90
        return $this->userLogin;
91
    }
92
93
    /**
94
     * @param string $userLogin
95
     */
96
    public function setUserLogin($userLogin)
97
    {
98
        $this->userLogin = strtolower($userLogin);
99
    }
100
101
    /**
102
     * Autentificar al usuario
103
     *
104
     * @param UserLoginData $userLoginData Datos del usuario
105
     *
106
     * @return bool
107
     */
108
    public function authenticate(UserLoginData $userLoginData)
109
    {
110
        try {
111
            $this->ldapAuthData->setAuthGranted($this->isAuthGranted());
112
            $this->setUserLogin($userLoginData->getLoginUser());
113
114
            $ldapConnection = $this->ldap->getLdapConnection();
115
            $ldapConnection->connect();
116
            $ldapConnection->bind($this->ldapAuthData->getDn(), $userLoginData->getLoginPass());
117
118
            $this->getAttributes($userLoginData->getLoginUser());
119
        } catch (LdapException $e) {
120
            processException($e);
121
122
            $this->ldapAuthData->setStatusCode($e->getCode());
123
124
            return false;
125
        }
126
127
        return true;
128
    }
129
130
    /**
131
     * Indica si es requerida para acceder a la aplicación
132
     *
133
     * @return boolean
134
     */
135
    public function isAuthGranted()
136
    {
137
        return true;
138
    }
139
140
    /**
141
     * Obtener los atributos del usuario.
142
     *
143
     * @param string $userLogin
144
     *
145
     * @return LdapAuthData con los atributos disponibles y sus valores
146
     * @throws LdapException
147
     */
148
    public function getAttributes(string $userLogin)
149
    {
150
        $attributes = $this->ldap->getLdapActions()
151
            ->getAttributes($this->ldap->getUserDnFilter($userLogin));
152
153
        if (!empty($attributes['fullname'])) {
154
            $this->ldapAuthData->setName($attributes['fullname']);
0 ignored issues
show
Bug introduced by
It seems like $attributes['fullname'] can also be of type array; however, parameter $name of SP\Providers\Auth\AuthDataBase::setName() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

154
            $this->ldapAuthData->setName(/** @scrutinizer ignore-type */ $attributes['fullname']);
Loading history...
155
        } else {
156
            $this->ldapAuthData->setName($attributes['name'] . ' ' . $attributes['sn']);
157
        }
158
159
        $this->ldapAuthData->setDn($attributes['dn']);
160
        $this->ldapAuthData->setEmail($attributes['mail']);
161
        $this->ldapAuthData->setExpire($attributes['expire']);
162
163
        $groups = is_array($attributes['group']) ? $attributes['group'] : [$attributes['group']];
0 ignored issues
show
introduced by
The condition is_array($attributes['group']) is always true.
Loading history...
164
165
        $this->ldapAuthData->setInGroup(
166
            $this->ldap->isUserInGroup(
167
                $attributes['dn'],
168
                $groups));
169
170
        return $this->ldapAuthData;
171
    }
172
}