Completed
Push — master ( 9abb37...19490a )
by Patrick
03:52
created

groups.php ➔ isAdmin()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 4
rs 10
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 hasUser($app)
33
{
34
    return ($app->user || $app->isLocal);
35
}
36
37
function isAdmin($app)
38
{
39
    return ($app->isLocal || $app->user->isInGroupNamed('LDAPAdmins'));
40
}
41
42
function expandGroupMembers($group, $odata, $directOnly)
43
{
44
    if($odata->expand !== false && in_array('member', $odata->expand))
45
    {
46
        $ret = array();
47
        $ret['cn'] = $group->getGroupName();
48
        $ret['description'] = $group->getDescription();
49
        $ret['member'] = $group->members(true, ($directOnly !== true));
50
        return json_decode(json_encode($ret), true);
51
    }
52
    else if($directOnly)
53
    {
54
        $ret = array();
55
        $ret['cn'] = $group->getGroupName();
56
        $ret['description'] = $group->getDescription();
57
        $ret['member'] = $group->getMemberUids(false);
58
        return json_decode(json_encode($ret), true);
59
    }
60
    return json_decode(json_encode($group), true);
61
}
62
63
function getFlippedKeys($keys)
64
{
65
    $ret = array();
66
    $count = count($keys);
67
    for($i = 0; $i < $count; $i++)
68
    {
69
        $key = $keys[$i];
70
        if(strstr($key, '.'))
71
        {
72
            $parts = explode('.', $key);
73
            $tmp = array_shift($parts);
74
            if(!isset($flipped[$tmp]))
75
            {
76
                $ret[$tmp] = array();
77
            }
78
            $ret[$tmp][] = $parts[0];
79
            continue;
80
        }
81
        $ret[$key] = 1;
82
    }
83
    return $ret;
84
}
85
86
function selectFieldsFromGroup($group, $select)
87
{
88
    if($select !== false)
89
    {
90
        $flipped = getFlippedKeys($select);
91
        foreach($flipped as $key=>$value)
92
        {
93
            if($value !== 1)
94
            {
95
                $tmp = array_flip($value);
96
                if(isset($group[$key][0]))
97
                {
98
                    $count = count($group[$key]);
99
                    for($i = 0; $i < $count; $i++)
100
                    {
101
                        $group[$key][$i] = array_intersect_key($group[$key][$i], $tmp);
102
                    }
103
                    continue;
104
                }
105
                $group[$key] = array_intersect_key($group[$key], $tmp);
106
            }
107
        }
108
    }
109
    return $group;
110
}
111
112
function getGroup($name)
113
{
114
    global $app;
115
    if(!hasUser($app))
116
    {
117
        $app->response->setStatus(401);
118
        return;
119
    }
120
    if(isAdmin($app))
121
    {
122
        $auth = AuthProvider::getInstance();
123
        $users = $auth->getGroupByName($name);
124
        $params = $app->request->params();
125
        $directOnly = false;
126
        if(isset($params['directOnly']) && $params['directOnly'] === 'true')
127
        {
128
            $directOnly = true;
129
        }
130
        $users = expandGroupMembers($users, $app->odata, $directOnly);
131
        $users = selectFieldsFromGroup($users, $app->odata->select);
132
        echo json_encode($users);
133
    }
134
    else
135
    {
136
        $groups = $app->user->getGroups();
137
        foreach($groups as $group)
138
        {
139
            if($group->getGroupName() === $name)
140
            {
141
                echo json_encode($group);
142
                die();
143
            }
144
        }
145
        $app->notFound();
146
    }
147
}
148
149
function updateGroup($name)
150
{
151
    global $app;
152
    if(!$app->user->isInGroupNamed('LDAPAdmins'))
153
    {
154
        $app->response->setStatus(401);
155
        return;
156
    }
157
    $auth = AuthProvider::getInstance();
158
    $group = $auth->getGroupByName($name);
159
    if($group === false)
160
    {
161
        $app->notFound();
162
        return;
163
    }
164
    $obj = $app->getJsonBody();
165
    echo json_encode($group->editGroup($obj));
166
}
167
168
function getNonGroupMembers($name)
169
{
170
    global $app;
171
    $isLocal = false;
172
    if($_SERVER['SERVER_ADDR'] === $_SERVER['REMOTE_ADDR'])
173
    {
174
        $isLocal = true;
175
    }
176
    if(!$app->user && !$isLocal)
177
    {
178
        $app->response->setStatus(401);
179
        return;
180
    }
181
    if(($isLocal === false) && !$app->user->isInGroupNamed('LDAPAdmins'))
182
    {
183
        $app->response->setStatus(401);
184
        return;
185
    }
186
    $auth = AuthProvider::getInstance();
187
    if($name === 'none')
188
    {
189
        $res = array();
190
        $groups = $auth->getGroupsByFilter(false);
191
        $count  = count($groups);
192
        $keys   = false;
193
        if($app->odata->select !== false)
194
        {
195
            $keys = array_flip($app->odata->select);
196
        }
197 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...
198
        {
199
            $tmp = json_decode(json_encode($groups[$i]), true);
200
            $tmp['type'] = 'Group';
201
            if($keys !== false)
202
            {
203
                $tmp = array_intersect_key($tmp, $keys);
204
            } 
205
            array_push($res, $tmp);
206
        }
207
        $users  = $auth->getUsersByFilter(false);
208
        $count  = count($users);
209 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...
210
        {
211
            $tmp = json_decode(json_encode($users[$i]), true);
212
            $tmp['type'] = 'User';
213
            if($keys !== false)
214
            {
215
                $tmp = array_intersect_key($tmp, $keys);
216
            }
217
            array_push($res, $tmp);
218
        }
219
        echo json_encode($res);
220
        return;
221
    }
222
    $group = $auth->getGroupByName($name);
223
    if($group === false)
224
    {
225
        $app->notFound();
226
    }
227
    $res = $group->getNonMemebers($app->odata->select);
228
    if($app->odata->select !== false)
229
    {
230
        $count = count($res);
231
        $keys = array_flip($app->odata->select);
232
        for($i = 0; $i < $count; $i++)
233
        {
234
            $tmp = json_decode(json_encode($res[$i]), true);
235
            if(is_subclass_of($res[$i], 'Auth\Group'))
236
            {
237
                $tmp['type'] = 'Group';
238
            }
239
            else
240
            {
241
                $tmp['type'] = 'User';
242
            }
243
            $res[$i] = array_intersect_key($tmp, $keys);
244
        }
245
    }
246
    echo json_encode($res);
247
}
248
/* vim: set tabstop=4 shiftwidth=4 expandtab: */
249