Completed
Pull Request — develop (#51)
by Patrick
02:57
created

GroupsAPI::updateGroup()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 3
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
2
class GroupsAPI extends ProfilesAdminAPI
3
{
4
    public function setup($app)
5
    {
6
        $app->get('[/]', array($this, 'getGroups'));
7
        $app->get('/{name}[/]', array($this, 'getGroup'));
8
        $app->patch('/{name}[/]', array($this, 'updateGroup'));
9
        $app->get('/{name}/non-members', array($this, 'getNonMembers'));
10
    }
11
12
    public function getGroups($request, $response, $args)
0 ignored issues
show
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
13
    {
14
        if($this->validateIsAdmin($request, true) === false)
15
        {
16
            return $response->withStatus(301)->withHeader('Location', '../users/me/groups');
17
        }
18
        $auth = AuthProvider::getInstance();
19
        $odata = $request->getAttribute('odata', new \ODataParams(array()));
20
        $groups = $auth->getGroupsByFilter($odata->filter, $odata->select, $odata->top, $odata->skip, 
21
                                           $odata->orderby);
22
        return $response->withJson($groups);
23
    }
24
25
    private function expandGroupMembers($group, $odata, $directOnly)
26
    {
27
        if($odata->expand !== false && in_array('member', $odata->expand))
28
        {
29
            $ret = array();
30
            $ret['cn'] = $group->getGroupName();
31
            $ret['description'] = $group->getDescription();
32
            $ret['member'] = $group->members(true, ($directOnly !== true));
33
            return json_decode(json_encode($ret), true);
34
        }
35
        else if($directOnly)
36
        {
37
            $ret = array();
38
            $ret['cn'] = $group->getGroupName();
39
            $ret['description'] = $group->getDescription();
40
            $ret['member'] = $group->getMemberUids(false);
41
            return json_decode(json_encode($ret), true);
42
        }
43
        return json_decode(json_encode($group), true);
44
    }
45
46
    private function getGroupForUserByName($name)
47
    {
48
        $groups = $this->user->getGroups();
49
        $count = count($groups);
50 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...
51
        {
52
            if(strcasecmp($groups[$i]->getGroupName(), $name) === 0)
53
            {
54
                return $groups[$i];
55
            }
56
        }
57
        return false;
58
    }
59
60
    public function getGroup($request, $response, $args)
61
    {
62
        $odata = $request->getAttribute('odata', new \ODataParams(array()));
63
        $group = false;
0 ignored issues
show
Unused Code introduced by
$group is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
64
        $expand = false;
65
        $user = $request->getAttribute('user');
66
        if($user === false)
67
        {
68
            $local = $request->getServerParam('SERVER_ADDR');
69
            $remote = $request->getServerParam('REMOTE_ADDR');
70
            if($local === $remote)
71
            {
72
                $auth = AuthProvider::getInstance();
73
                $group = $auth->getGroupByName($args['name']);
74
                $expand = true;
75
            }
76
            else
77
            {
78
                return $response->withStatus(401);
79
            }
80
        }
81
        else if($this->validateIsAdmin($request, true) === false)
82
        {
83
            $group = $this->getGroupForUserByName($args['name']);
84
        }
85
        else
86
        {
87
            $auth = AuthProvider::getInstance();
88
            $group = $auth->getGroupByName($args['name']);
89
            $expand = true;
90
        }
91
        if(empty($group))
92
        {
93
            return $response->withStatus(404);
94
        }
95
        $params = $request->getQueryParams();
96
        $directOnly = false;
97
        if(isset($params['directOnly']) && $params['directOnly'] === 'true')
98
        {
99
            $directOnly = true;
100
        }
101
        if($expand)
102
        {
103
            $group = $this->expandGroupMembers($group, $odata, $directOnly);
104
        }
105
        return $response->withJson($group);
106
    }
107
108
    public function getAllGroupsAndUsers($keys)
109
    {
110
        $auth = AuthProvider::getInstance();
111
        $groups = $auth->getGroupsByFilter(false);
112
        $count  = count($groups);
113
        $res = array();
114 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...
115
        {
116
            $tmp = json_decode(json_encode($groups[$i]), true);
117
            $tmp['type'] = 'Group';
118
            if($keys !== false)
119
            {
120
                $tmp = array_intersect_key($tmp, $keys);
121
            }
122
            $res[] = $tmp;
123
        }
124
        $users  = $auth->getUsersByFilter(false);
125
        $count  = count($users);
126 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...
127
        {
128
            $tmp = json_decode(json_encode($users[$i]), true);
129
            $tmp['type'] = 'User';
130
            if($keys !== false)
131
            {
132
                $tmp = array_intersect_key($tmp, $keys);
133
            }
134
            $res[] = $tmp;
135
        }
136
        return $res;
137
    }
138
139
    public function getTypeOfEntity($entity)
140
    {
141
        if(is_subclass_of($entity, 'Auth\Group'))
142
        {
143
            return 'Group';
144
        }
145
        else
146
        {
147
            return 'User';
148
        }
149
    }
150
151
    public function getNonMemberEntities($nonMembers, $keys)
152
    {
153
        if($keys !== false)
154
        {
155
            $count = count($nonMembers);
156 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...
157
            {
158
                $tmp = json_decode(json_encode($nonMembers[$i]), true);
159
                $tmp['type'] = $this->getTypeOfEntity($nonMembers[$i]);
160
                $nonMembers[$i] = array_intersect_key($tmp, $keys);
161
            }
162
        }
163
        return $nonMembers;
164
    }
165
166
    public function getNonMembers($request, $response, $args)
167
    {
168
        $this->validateIsAdmin($request);
169
        $odata = $request->getAttribute('odata', new \ODataParams(array()));
170
        $keys = false;
171
        if($odata->select !== false)
172
        {
173
            $keys = array_flip($odata->select);
174
        }
175
        $auth = AuthProvider::getInstance();
176
        if($args['name'] === 'none')
177
        {
178
            $res = $this->getAllGroupsAndUsers($keys);
179
            return $response->withJson($res);
180
        }
181
        $group = $auth->getGroupByName($args['name']);
182
        if($group === false)
183
        {
184
            return $response->withStatus(404);
185
        }
186
        $res = $group->getNonMembers($odata->select);
187
        $res = $this->getNonMemberEntities($res, $keys);
188
        return $response->withJson($res);
189
    }
190
191
    public function updateGroup($request, $response, $args)
192
    {
193
        $this->validateIsAdmin($request);
194
        $auth = AuthProvider::getInstance();
195
        $group = $auth->getGroupByName($args['name']);
196
        if($group === false)
197
        {
198
            return $response->withStatus(404);
199
        }
200
        $obj = $request->getParsedBody();
201
        $ret = $group->editGroup($obj);
202
        return $response->withJson($ret);
203
    }
204
}
205
/* vim: set tabstop=4 shiftwidth=4 expandtab: */
206