Completed
Push — master ( ced03c...d460d6 )
by Patrick
03:17
created

groups.php ➔ getGroupForUserByName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 13
rs 9.4285
1
<?php
2
3
function groups()
4
{
5
    global $app;
6
    $app->get('', 'listGroups');
7
    $app->get('/:name', 'getGroup');
8
    $app->patch('/:name', 'updateGroup');
9
    $app->get('/:name/non-members', 'getNonGroupMembers');
10
}
11
12
function listGroups()
13
{
14
    global $app;
15
    if(!$app->user)
16
    {
17
        $app->response->setStatus(401);
18
        return;
19
    }
20 View Code Duplication
    if($app->user->isInGroupNamed("LDAPAdmins"))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
21
    {
22
        $auth = AuthProvider::getInstance();
23
        $users = $auth->getGroupsByFilter($app->odata->filter, $app->odata->select, $app->odata->top, $app->odata->skip, $app->odata->orderby);
24
        echo json_encode($users);
25
    }
26
    else
27
    {
28
        list_groups_for_user();
29
    }
30
}
31
32
function expandGroupMembers($group, $odata, $directOnly)
33
{
34
    if($odata->expand !== false && in_array('member', $odata->expand))
35
    {
36
        $ret = array();
37
        $ret['cn'] = $group->getGroupName();
38
        $ret['description'] = $group->getDescription();
39
        $ret['member'] = $group->members(true, ($directOnly !== true));
40
        return json_decode(json_encode($ret), true);
41
    }
42
    else if($directOnly)
43
    {
44
        $ret = array();
45
        $ret['cn'] = $group->getGroupName();
46
        $ret['description'] = $group->getDescription();
47
        $ret['member'] = $group->getMemberUids(false);
48
        return json_decode(json_encode($ret), true);
49
    }
50
    return json_decode(json_encode($group), true);
51
}
52
53
function getFlippedKeys($keys)
54
{
55
    $ret = array();
56
    $count = count($keys);
57
    for($i = 0; $i < $count; $i++)
58
    {
59
        $key = $keys[$i];
60
        if(strstr($key, '.'))
61
        {
62
            $parts = explode('.', $key);
63
            $tmp = array_shift($parts);
64
            if(!isset($flipped[$tmp]))
65
            {
66
                $ret[$tmp] = array();
67
            }
68
            $ret[$tmp][] = $parts[0];
69
            continue;
70
        }
71
        $ret[$key] = 1;
72
    }
73
    return $ret;
74
}
75
76
function selectFieldsFromGroup($group, $select)
77
{
78
    if($select !== false)
79
    {
80
        $flipped = getFlippedKeys($select);
81
        foreach($flipped as $key=>$value)
82
        {
83
            if($value !== 1)
84
            {
85
                $tmp = array_flip($value);
86
                if(isset($group[$key][0]))
87
                {
88
                    $count = count($group[$key]);
89
                    for($i = 0; $i < $count; $i++)
90
                    {
91
                        $group[$key][$i] = array_intersect_key($group[$key][$i], $tmp);
92
                    }
93
                    continue;
94
                }
95
                $group[$key] = array_intersect_key($group[$key], $tmp);
96
            }
97
        }
98
    }
99
    return $group;
100
}
101
102
function getGroupForUserByName($name, $app)
103
{
104
    $groups = $app->user->getGroups();
105
    $count = count($groups);
106
    for($i = 0; $i < $count; $i++)
107
    {
108
        if(strcasecmp($groups[$i]->getGroupName(), $name) === 0)
109
        {
110
            return $groups[$i];
111
        }
112
    }
113
    return false;
114
}
115
116
function getGroup($name)
117
{
118
    global $app;
119
    if(!hasUser($app))
120
    {
121
        $app->response->setStatus(401);
122
        return;
123
    }
124
    if(isAdmin($app))
125
    {
126
        $auth = AuthProvider::getInstance();
127
        $users = $auth->getGroupByName($name);
128
        $params = $app->request->params();
129
        $directOnly = false;
130
        if(isset($params['directOnly']) && $params['directOnly'] === 'true')
131
        {
132
            $directOnly = true;
133
        }
134
        $users = expandGroupMembers($users, $app->odata, $directOnly);
135
        $users = selectFieldsFromGroup($users, $app->odata->select);
136
        echo json_encode($users);
137
    }
138
    else
139
    {
140
        $group = getGroupForUserByName($name, $app);
141
        if($group === false)
142
        {
143
            $app->notFound();
144
            return;
145
        }
146
        echo json_encode($group);
147
    }
148
}
149
150
function updateGroup($name)
151
{
152
    global $app;
153
    if(!$app->user->isInGroupNamed('LDAPAdmins'))
154
    {
155
        $app->response->setStatus(401);
156
        return;
157
    }
158
    $auth = AuthProvider::getInstance();
159
    $group = $auth->getGroupByName($name);
160
    if($group === false)
161
    {
162
        $app->notFound();
163
        return;
164
    }
165
    $obj = $app->getJsonBody();
166
    echo json_encode($group->editGroup($obj));
167
}
168
169
function getTypeOfEntity($entity)
170
{
171
    if(is_subclass_of($entity, 'Auth\Group'))
172
    {
173
        return 'Group';
174
    }
175
    else
176
    {
177
        return 'User';
178
    }
179
}
180
181
function getAllGroupsAndUsers($keys)
182
{
183
    $res = array();
184
    $groups = $auth->getGroupsByFilter(false);
0 ignored issues
show
Bug introduced by
The variable $auth does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
185
    $count  = count($groups);
186 View Code Duplication
    for($i = 0; $i < $count; $i++)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
187
    {
188
        $tmp = json_decode(json_encode($groups[$i]), true);
189
        $tmp['type'] = 'Group';
190
        if($keys !== false)
191
        {
192
            $tmp = array_intersect_key($tmp, $keys);
193
        }
194
        $res[] = $tmp;
195
    }
196
    $users  = $auth->getUsersByFilter(false);
197
    $count  = count($users);
198 View Code Duplication
    for($i = 0; $i < $count; $i++)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
199
    {
200
        $tmp = json_decode(json_encode($users[$i]), true);
201
        $tmp['type'] = 'User';
202
        if($keys !== false)
203
        {
204
            $tmp = array_intersect_key($tmp, $keys);
205
        }
206
        $res[] = $tmp;
207
    }
208
    return $res;
209
}
210
211
function getNonMemberEntities($nonMembers, $keys)
212
{
213
    if($keys !== false)
214
    {
215
        $count = count($nonMembers);
216 View Code Duplication
        for($i = 0; $i < $count; $i++)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
217
        {
218
            $tmp = json_decode(json_encode($nonMembers[$i]), true);
219
            $tmp['type'] = getTypeOfEntity($nonMembers[$i]);
220
            $nonMembers[$i] = array_intersect_key($tmp, $keys);
221
        }
222
    }
223
    return $nonMembers;
224
}
225
226
function getNonGroupMembers($name)
227
{
228
    global $app;
229
    if(!hasUser($app) || !isAdmin($app))
230
    {
231
        $app->response->setStatus(401);
232
        return;
233
    }
234
    $keys = false;
235
    if($app->odata->select !== false)
236
    {
237
        $keys = array_flip($app->odata->select);
238
    }
239
    $auth = AuthProvider::getInstance();
240
    if($name === 'none')
241
    {
242
        $res = getAllGroupsAndUsers($keys);
243
        echo json_encode($res);
244
        return;
245
    }
246
    $group = $auth->getGroupByName($name);
247
    if($group === false)
248
    {
249
        $app->notFound();
250
    }
251
    $res = $group->getNonMemebers($app->odata->select);
252
    $res = getNonMemberEntities($res, $keys);
253
    echo json_encode($res);
254
}
255
/* vim: set tabstop=4 shiftwidth=4 expandtab: */
256