Completed
Push — master ( 5d02ec...10cc8d )
by Patrick
02:34
created

groups.php ➔ selectFieldsFromGroup()   C

Complexity

Conditions 7
Paths 2

Size

Total Lines 28
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 14
nc 2
nop 2
dl 0
loc 28
rs 6.7272
c 0
b 0
f 0
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($ret[$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
                        if(is_array($group[$key][$i]))
92
                        {
93
                            $group[$key][$i] = array_intersect_key($group[$key][$i], $tmp);
94
                        }
95
                    }
96
                    continue;
97
                }
98
                $group[$key] = array_intersect_key($group[$key], $tmp);
99
            }
100
        }
101
    }
102
    return $group;
103
}
104
105
function getGroupForUserByName($name, $app)
106
{
107
    $groups = $app->user->getGroups();
108
    $count = count($groups);
109
    for($i = 0; $i < $count; $i++)
110
    {
111
        if(strcasecmp($groups[$i]->getGroupName(), $name) === 0)
112
        {
113
            return $groups[$i];
114
        }
115
    }
116
    return false;
117
}
118
119
function getGroup($name)
120
{
121
    global $app;
122
    if(!hasUser($app))
123
    {
124
        $app->response->setStatus(401);
125
        return;
126
    }
127
    if(isAdmin($app))
128
    {
129
        $auth = AuthProvider::getInstance();
130
        $users = $auth->getGroupByName($name);
131
        $params = $app->request->params();
132
        $directOnly = false;
133
        if(isset($params['directOnly']) && $params['directOnly'] === 'true')
134
        {
135
            $directOnly = true;
136
        }
137
        $users = expandGroupMembers($users, $app->odata, $directOnly);
138
        $users = selectFieldsFromGroup($users, $app->odata->select);
139
        echo json_encode($users);
140
    }
141
    else
142
    {
143
        $group = getGroupForUserByName($name, $app);
144
        if($group === false)
145
        {
146
            $app->notFound();
147
            return;
148
        }
149
        echo json_encode($group);
150
    }
151
}
152
153
function updateGroup($name)
154
{
155
    global $app;
156
    if(!$app->user->isInGroupNamed('LDAPAdmins'))
157
    {
158
        $app->response->setStatus(401);
159
        return;
160
    }
161
    $auth = AuthProvider::getInstance();
162
    $group = $auth->getGroupByName($name);
163
    if($group === false)
164
    {
165
        $app->notFound();
166
        return;
167
    }
168
    $obj = $app->getJsonBody();
169
    echo json_encode($group->editGroup($obj));
170
}
171
172
function getTypeOfEntity($entity)
173
{
174
    if(is_subclass_of($entity, 'Auth\Group'))
175
    {
176
        return 'Group';
177
    }
178
    else
179
    {
180
        return 'User';
181
    }
182
}
183
184
function getAllGroupsAndUsers($keys)
185
{
186
    $res = array();
187
    $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...
188
    $count  = count($groups);
189 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...
190
    {
191
        $tmp = json_decode(json_encode($groups[$i]), true);
192
        $tmp['type'] = 'Group';
193
        if($keys !== false)
194
        {
195
            $tmp = array_intersect_key($tmp, $keys);
196
        }
197
        $res[] = $tmp;
198
    }
199
    $users  = $auth->getUsersByFilter(false);
200
    $count  = count($users);
201 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...
202
    {
203
        $tmp = json_decode(json_encode($users[$i]), true);
204
        $tmp['type'] = 'User';
205
        if($keys !== false)
206
        {
207
            $tmp = array_intersect_key($tmp, $keys);
208
        }
209
        $res[] = $tmp;
210
    }
211
    return $res;
212
}
213
214
function getNonMemberEntities($nonMembers, $keys)
215
{
216
    if($keys !== false)
217
    {
218
        $count = count($nonMembers);
219 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...
220
        {
221
            $tmp = json_decode(json_encode($nonMembers[$i]), true);
222
            $tmp['type'] = getTypeOfEntity($nonMembers[$i]);
223
            $nonMembers[$i] = array_intersect_key($tmp, $keys);
224
        }
225
    }
226
    return $nonMembers;
227
}
228
229
function getNonGroupMembers($name)
230
{
231
    global $app;
232
    if(!hasUser($app) || !isAdmin($app))
233
    {
234
        $app->response->setStatus(401);
235
        return;
236
    }
237
    $keys = false;
238
    if($app->odata->select !== false)
239
    {
240
        $keys = array_flip($app->odata->select);
241
    }
242
    $auth = AuthProvider::getInstance();
243
    if($name === 'none')
244
    {
245
        $res = getAllGroupsAndUsers($keys);
246
        echo json_encode($res);
247
        return;
248
    }
249
    $group = $auth->getGroupByName($name);
250
    if($group === false)
251
    {
252
        $app->notFound();
253
    }
254
    $res = $group->getNonMemebers($app->odata->select);
255
    $res = getNonMemberEntities($res, $keys);
256
    echo json_encode($res);
257
}
258
/* vim: set tabstop=4 shiftwidth=4 expandtab: */
259