Completed
Push — master ( c3eede...c80ee3 )
by Patrick
02:48
created

groups.php ➔ getAllGroupsAndUsers()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 29
Code Lines 19

Duplication

Lines 20
Ratio 68.97 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 19
c 1
b 0
f 0
nc 9
nop 1
dl 20
loc 29
rs 8.439
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
    if($app->user->isInGroupNamed("LDAPAdmins"))
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 getGroup($name)
103
{
104
    global $app;
105
    if(!hasUser($app))
106
    {
107
        $app->response->setStatus(401);
108
        return;
109
    }
110
    if(isAdmin($app))
111
    {
112
        $auth = AuthProvider::getInstance();
113
        $users = $auth->getGroupByName($name);
114
        $params = $app->request->params();
115
        $directOnly = false;
116
        if(isset($params['directOnly']) && $params['directOnly'] === 'true')
117
        {
118
            $directOnly = true;
119
        }
120
        $users = expandGroupMembers($users, $app->odata, $directOnly);
121
        $users = selectFieldsFromGroup($users, $app->odata->select);
122
        echo json_encode($users);
123
    }
124
    else
125
    {
126
        $groups = $app->user->getGroups();
127
        foreach($groups as $group)
128
        {
129
            if($group->getGroupName() === $name)
130
            {
131
                echo json_encode($group);
132
                die();
133
            }
134
        }
135
        $app->notFound();
136
    }
137
}
138
139
function updateGroup($name)
140
{
141
    global $app;
142
    if(!$app->user->isInGroupNamed('LDAPAdmins'))
143
    {
144
        $app->response->setStatus(401);
145
        return;
146
    }
147
    $auth = AuthProvider::getInstance();
148
    $group = $auth->getGroupByName($name);
149
    if($group === false)
150
    {
151
        $app->notFound();
152
        return;
153
    }
154
    $obj = $app->getJsonBody();
155
    echo json_encode($group->editGroup($obj));
156
}
157
158
function getTypeOfEntity($entity)
159
{
160
    if(is_subclass_of($entity, 'Auth\Group'))
161
    {
162
        return 'Group';
163
    }
164
    else
165
    {
166
        return 'User';
167
    }
168
}
169
170
function getAllGroupsAndUsers($keys)
171
{
172
    $res = array();
173
    $groups = $auth->getGroupsByFilter(false);
174
    $count  = count($groups);
175
    for($i = 0; $i < $count; $i++)
176
    {
177
        $tmp = json_decode(json_encode($groups[$i]), true);
178
        $tmp['type'] = 'Group';
179
        if($keys !== false)
180
        {
181
            $tmp = array_intersect_key($tmp, $keys);
182
        }
183
        $res[] = $tmp;
184
    }
185
    $users  = $auth->getUsersByFilter(false);
186
    $count  = count($users);
187
    for($i = 0; $i < $count; $i++)
188
    {
189
        $tmp = json_decode(json_encode($users[$i]), true);
190
        $tmp['type'] = 'User';
191
        if($keys !== false)
192
        {
193
            $tmp = array_intersect_key($tmp, $keys);
194
        }
195
        $res[] = $tmp;
196
    }
197
    return $res;
198
}
199
200
function getNonMemberEntities($nonMembers, $keys)
201
{
202
    if($keys !== false)
203
    {
204
        $count = count($nonMembers;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected ';', expecting ',' or ')'
Loading history...
205
        for($i = 0; $i < $count; $i++)
206
        {
207
            $tmp = json_decode(json_encode($nonMembers[$i]), true);
208
            $tmp['type'] = getTypeOfEntity($nonMembers[$i]);
209
            $nonMembers[$i] = array_intersect_key($tmp, $keys);
210
        }
211
    }
212
    return $nonMembers;
213
}
214
215
function getNonGroupMembers($name)
216
{
217
    global $app;
218
    if(!hasUser($app) || !isAdmin($app))
219
    {
220
        $app->response->setStatus(401);
221
        return;
222
    }
223
    $keys = false;
224
    if($app->odata->select !== false)
225
    {
226
        $keys = array_flip($app->odata->select);
227
    }
228
    $auth = AuthProvider::getInstance();
229
    if($name === 'none')
230
    {
231
        $res = getAllGroupsAndUsers($keys);
232
        echo json_encode($res);
233
        return;
234
    }
235
    $group = $auth->getGroupByName($name);
236
    if($group === false)
237
    {
238
        $app->notFound();
239
    }
240
    $res = $group->getNonMemebers($app->odata->select);
241
    $res = getNonMemberEntities($res, $keys);
242
    echo json_encode($res);
243
}
244
/* vim: set tabstop=4 shiftwidth=4 expandtab: */
245