Completed
Push — master ( 46577c...c1feec )
by Patrick
03:39
created

LDAPGroup::addMember()   C

Complexity

Conditions 8
Paths 24

Size

Total Lines 46
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
cc 8
eloc 26
nc 24
nop 3
dl 0
loc 46
rs 5.5555
c 2
b 1
f 1
1
<?php
2
namespace Auth;
3
4
class LDAPGroup extends Group
5
{
6
    use LDAPCachableObject;
7
8
    private $ldapObj;
9
    private $server;
10
11
    public function __construct($data)
12
    {
13
        $this->ldapObj = $data;
14
        $this->server = \LDAP\LDAPServer::getInstance();
15
        if(!is_object($data))
16
        {
17
            throw new \Exception('Unable to setup LDAPGroup!');
18
        }
19
    }
20
21
    public function getGroupName()
22
    {
23
        return $this->getFieldSingleValue('cn');
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->getFieldSingleValue('cn'); (string) is incompatible with the return type of the parent method Auth\Group::getGroupName of type boolean.

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...
24
    }
25
26
    public function getDescription()
27
    {
28
        return $this->getFieldSingleValue('description');
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->getFieldSingleValue('description'); (string) is incompatible with the return type of the parent method Auth\Group::getDescription of type boolean.

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...
29
    }
30
31
    public function setDescription($name)
32
    {
33
        return $this->setField('description', $name);
34
    }
35
36
    private function getMembersField(&$fieldName = false)
37
    {
38
        $rawMembers = $this->getField('member');
39
        $fieldName  = 'member';
40
        if($rawMembers === false)
41
        {
42
            $rawMembers = $this->getField('uniqueMember');
43
            $fieldName  = 'uniqueMember';
44
        }
45
        if($rawMembers === false)
46
        {
47
            $rawMembers = $this->getField('memberUid');
48
            $fieldName  = 'memberUid';
49
        }
50
        if(!isset($rawMembers['count']))
51
        {
52
            $rawMembers['count'] = count($rawMembers);
53
        }
54
        return $rawMembers;
55
    }
56
57
    private function getIDFromDN($dn)
58
    {
59
        $split = explode(',', $dn);
60
        if(strncmp('cn=', $split[0], 3) === 0)
61
        {
62
            return substr($split[0], 3);
63
        }
64
        return substr($split[0], 4);
65
    }
66
67
    public function getMemberUids($recursive = true)
68
    {
69
        $members = array();
70
        $rawMembers = $this->getMembersField();
71
        for($i = 0; $i < $rawMembers['count']; $i++)
72
        {
73
            if($recursive && strncmp($rawMembers[$i], 'cn=', 3) === 0)
74
            {
75
                $child = self::from_dn($rawMembers[$i], $this->server);
76
                if($child !== false)
77
                {
78
                    $members = array_merge($members, $child->members());
79
                }
80
            }
81
            else
82
            {
83
                array_push($members, $rawMembers[$i]);
84
            }
85
        }
86
        $count = count($members);
87
        for($i = 0; $i < $count; $i++)
88
        {
89
            $members[$i] = $this->getIDFromDN($members[$i]);
90
        }
91
        return $members;
92
    }
93
94
    private function getObjectFromDN($dn)
95
    {
96
        $split = explode(',', $dn);
97
        if(strncmp('cn=', $dn, 3) === 0)
98
        {
99
            if(count($split) === 1)
100
            {
101
                return LDAPGroup::from_name($dn, $this->server);
102
            }
103
            return LDAPGroup::from_name(substr($split[0], 3), $this->server);
104
        }
105
        if(count($split) === 1)
106
        {
107
            return LDAPUser::from_name($dn, $this->server);
108
        }
109
        return LDAPUser::from_name(substr($split[0], 4), $this->server);
110
    }
111
112
    private function getMemberDetail($members)
113
    {
114
        $details = array();
115
        $count = count($members);
116
        for($i = 0; $i < $count; $i++)
117
        {
118
            $details[$i] = $this->getObjectFromDN($members[$i]);
119
        }
120
        return $details;
121
    }
122
123
    public function members($details = false, $recursive = true, $includeGroups = true)
124
    {
125
        $members = array();
126
        $rawMembers = $this->getMembersField();
127
        for($i = 0; $i < $rawMembers['count']; $i++)
128
        {
129
            if($recursive && strncmp($rawMembers[$i], 'cn=', 3) === 0)
130
            {
131
                $child = self::from_dn($rawMembers[$i], $this->server);
132
                if($child !== false)
133
                {
134
                    $members = array_merge($members, $child->members());
135
                }
136
            }
137
            else if($includeGroups !== false || strncmp($rawMembers[$i], 'cn=', 3) !== 0)
138
            {
139
                array_push($members, $rawMembers[$i]);
140
            }
141
        }
142
        if($details === true)
143
        {
144
            $members = $this->getMemberDetail($members);
145
        }
146
        return $members;
147
    }
148
149
    public function getNonMemebers($select = false)
150
    {
151
        $data = array();
152
        $groupFilter = '(&(cn=*)(!(cn='.$this->getGroupName().'))';
153
        $userFilter = '(&(cn=*)';
154
        $members = $this->members();
155
        $count = count($members);
156
        for($i = 0; $i < $count; $i++)
157
        {
158
            $dnComps = explode(',', $members[$i]);
159
            if(strncmp($members[$i], "uid=", 4) == 0)
160
            {
161
                $userFilter .= '(!('.$dnComps[0].'))';
162
            }
163
            else
164
            {
165
                $groupFilter .= '(!('.$dnComps[0].'))';
166
            }
167
        }
168
        $userFilter .= ')';
169
        $groupFilter .= ')';
170
        $groups = $this->server->read($this->server->group_base, $groupFilter);
171
        $count = count($groups);
172
        for($i = 0; $i < $count; $i++)
173
        {
174
            if($groups[$i] === false || $groups[$i] === null)
175
            {
176
                continue;
177
            }
178
            array_push($data, new LDAPGroup($groups[$i]));
179
        }
180
        $users = $this->server->read($this->server->user_base, $userFilter, false, $select);
181
        $count = count($users);
182
        for($i = 0; $i < $count; $i++)
183
        {
184
            array_push($data, new LDAPUser($users[$i]));
185
        } 
186
        return $data;
187
    }
188
189
    public function clearMembers()
190
    {
191
        if(isset($this->ldapObj['member']))
192
        {
193
            $this->ldapObj['member'] = array();
194
        }
195
        else if(isset($this->ldapObj['uniquemember']))
196
        {
197
            $this->ldapObj['uniquemember'] = array();
198
        }
199
        else if(isset($this->ldapObj['memberuid']))
200
        {
201
            $this->ldapObj['memberuid'] = array();
202
        }
203
    }
204
205
    public function addMember($name, $isGroup = false, $flush = true)
206
    {
207
        $dn = false;
0 ignored issues
show
Unused Code introduced by
$dn is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
208
        if($isGroup)
209
        {
210
            $dn = 'cn='.$name.','.$this->server->group_base;
211
        }
212
        else
213
        {
214
            $dn = 'uid='.$name.','.$this->server->user_base;
215
        }
216
        $propName   = false;
217
        $rawMembers = $this->getMembersField($propName);
218
        if(isset($rawMembers['count']))
219
        {
220
            unset($rawMembers['count']);
221
        }
222
        if(in_array($dn, $rawMembers) || in_array($name, $rawMembers))
223
        {
224
            return true;
225
        }
226
        if($propName === 'memberUid')
227
        {
228
            if($isGroup)
229
            {
230
                throw new \Exception('Unable to add a group as a child of this group type');
231
            }
232
            array_push($rawMembers, $name);
233
        }
234
        else
235
        {
236
            array_push($rawMembers, $dn);
237
        }
238
        $tmp = strtolower($propName);
239
        $this->ldapObj->$tmp = $rawMembers;
240
        if($flush === true)
241
        {
242
            $obj = array('dn'=>$this->ldapObj->dn);
243
            $obj[$propName] = $rawMembers;
244
            return $this->server->update($obj);
245
        }
246
        else
247
        {
248
            return true;
249
        }
250
    }
251
252
    public static function from_dn($dn, $data = false)
253
    {
254
        if($data === false)
255
        {
256
            throw new \Exception('data must be set for LDAPGroup');
257
        }
258
        $group = $data->read($dn, false, true);
0 ignored issues
show
Bug introduced by
The method read cannot be called on $data (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
259
        if($group === false || !isset($group[0]))
260
        {
261
            return false;
262
        }
263
        return new static($group[0]);
264
    }
265
266 View Code Duplication
    public static function from_name($name, $data = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
267
    {
268
        if($data === false)
269
        {
270
            throw new \Exception('data must be set for LDAPGroup');
271
        }
272
        $filter = new \Data\Filter("cn eq $name");
273
        $group = $data->read($data->group_base, $filter);
0 ignored issues
show
Bug introduced by
The method read cannot be called on $data (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
274
        if($group === false || !isset($group[0]))
275
        {
276
            return false;
277
        }
278
        return new static($group[0]);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new static($group[0]); (Auth\LDAPGroup) is incompatible with the return type of the parent method Auth\Group::from_name of type boolean.

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...
279
    }
280
}
281
/* vim: set tabstop=4 shiftwidth=4 expandtab: */
282