Completed
Push — master ( 7701ed...c3eede )
by Patrick
03:23
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 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 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);
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...
174
    $count  = count($groups);
175 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...
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 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...
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 getNonGroupMembers($name)
201
{
202
    global $app;
203
    if(!hasUser($app) || !isAdmin($app))
204
    {
205
        $app->response->setStatus(401);
206
        return;
207
    }
208
    $auth = AuthProvider::getInstance();
209
    if($name === 'none')
210
    {
211
        $keys   = false;
212
        if($app->odata->select !== false)
213
        {
214
            $keys = array_flip($app->odata->select);
215
        }
216
        $res = getAllGroupsAndUsers($keys);
217
        echo json_encode($res);
218
        return;
219
    }
220
    $group = $auth->getGroupByName($name);
221
    if($group === false)
222
    {
223
        $app->notFound();
224
    }
225
    $res = $group->getNonMemebers($app->odata->select);
226
    if($app->odata->select !== false)
227
    {
228
        $count = count($res);
229
        $keys = array_flip($app->odata->select);
230 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...
231
        {
232
            $tmp = json_decode(json_encode($res[$i]), true);
233
            $tmp['type'] = getTypeOfEntity($res[$i]);
234
            $res[$i] = array_intersect_key($tmp, $keys);
235
        }
236
    }
237
    echo json_encode($res);
238
}
239
/* vim: set tabstop=4 shiftwidth=4 expandtab: */
240