LdapAuth::getName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of Jitamin.
5
 *
6
 * Copyright (C) Jitamin Team
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Jitamin\Auth;
13
14
use Jitamin\Foundation\Base;
15
use Jitamin\Foundation\Ldap\Client as LdapClient;
16
use Jitamin\Foundation\Ldap\ClientException as LdapException;
17
use Jitamin\Foundation\Ldap\User as LdapUser;
18
use Jitamin\Foundation\Security\PasswordAuthenticationProviderInterface;
19
use LogicException;
20
21
/**
22
 * LDAP Authentication Provider.
23
 */
24
class LdapAuth extends Base implements PasswordAuthenticationProviderInterface
25
{
26
    /**
27
     * User properties.
28
     *
29
     * @var \Jitamin\Services\User\LdapUserProvider
30
     */
31
    protected $userInfo = null;
32
33
    /**
34
     * Username.
35
     *
36
     * @var string
37
     */
38
    protected $username = '';
39
40
    /**
41
     * Password.
42
     *
43
     * @var string
44
     */
45
    protected $password = '';
46
47
    /**
48
     * Get authentication provider name.
49
     *
50
     * @return string
51
     */
52
    public function getName()
53
    {
54
        return 'LDAP';
55
    }
56
57
    /**
58
     * Authenticate the user.
59
     *
60
     * @return bool
61
     */
62
    public function authenticate()
63
    {
64
        try {
65
            $client = LdapClient::connect($this->getLdapUsername(), $this->getLdapPassword());
66
            $client->setLogger($this->logger);
0 ignored issues
show
Documentation introduced by
The property logger does not exist on object<Jitamin\Auth\LdapAuth>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
67
68
            $user = LdapUser::getUser($client, $this->username);
69
70
            if ($user === null) {
71
                $this->logger->info('User ('.$this->username.') not found in LDAP server');
0 ignored issues
show
Documentation introduced by
The property logger does not exist on object<Jitamin\Auth\LdapAuth>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
72
73
                return false;
74
            }
75
76
            if ($user->getUsername() === '') {
77
                throw new LogicException('Username not found in LDAP profile, check the parameter LDAP_USER_ATTRIBUTE_USERNAME');
78
            }
79
80
            $this->logger->info('Authenticate this user: '.$user->getDn());
0 ignored issues
show
Documentation introduced by
The property logger does not exist on object<Jitamin\Auth\LdapAuth>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
81
82
            if ($client->authenticate($user->getDn(), $this->password)) {
83
                $this->userInfo = $user;
0 ignored issues
show
Documentation Bug introduced by
It seems like $user of type object<Jitamin\Services\...ntity\LdapUserProvider> is incompatible with the declared type object<Jitamin\Services\User\LdapUserProvider> of property $userInfo.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
84
85
                return true;
86
            }
87
        } catch (LdapException $e) {
88
            $this->logger->error($e->getMessage());
0 ignored issues
show
Documentation introduced by
The property logger does not exist on object<Jitamin\Auth\LdapAuth>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
89
        }
90
91
        return false;
92
    }
93
94
    /**
95
     * Get user object.
96
     *
97
     * @return \Jitamin\Services\User\LdapUserProvider
98
     */
99
    public function getUser()
100
    {
101
        return $this->userInfo;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->userInfo; (Jitamin\Services\User\LdapUserProvider) is incompatible with the return type declared by the interface Jitamin\Foundation\Secur...viderInterface::getUser of type Jitamin\Foundation\User\UserProviderInterface.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
102
    }
103
104
    /**
105
     * Set username.
106
     *
107
     * @param string $username
108
     */
109
    public function setUsername($username)
110
    {
111
        $this->username = $username;
112
    }
113
114
    /**
115
     * Set password.
116
     *
117
     * @param string $password
118
     */
119
    public function setPassword($password)
120
    {
121
        $this->password = $password;
122
    }
123
124
    /**
125
     * Get LDAP username (proxy auth).
126
     *
127
     * @return string
128
     */
129
    public function getLdapUsername()
130
    {
131
        switch ($this->getLdapBindType()) {
132
            case 'proxy':
133
                return LDAP_USERNAME;
134
            case 'user':
135
                return sprintf(LDAP_USERNAME, $this->username);
136
            default:
137
                return;
138
        }
139
    }
140
141
    /**
142
     * Get LDAP password (proxy auth).
143
     *
144
     * @return string
145
     */
146
    public function getLdapPassword()
147
    {
148
        switch ($this->getLdapBindType()) {
149
            case 'proxy':
150
                return LDAP_PASSWORD;
151
            case 'user':
152
                return $this->password;
153
            default:
154
                return;
155
        }
156
    }
157
158
    /**
159
     * Get LDAP bind type.
160
     *
161
     * @return int
162
     */
163
    public function getLdapBindType()
164
    {
165
        if (LDAP_BIND_TYPE !== 'user' && LDAP_BIND_TYPE !== 'proxy' && LDAP_BIND_TYPE !== 'anonymous') {
166
            throw new LogicException('Wrong value for the parameter LDAP_BIND_TYPE');
167
        }
168
169
        return LDAP_BIND_TYPE;
170
    }
171
}
172