Completed
Push — master ( 26ea5b...d16af1 )
by Patrick
01:54
created

GroupsAPI::serializeArray()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 5
nop 4
dl 0
loc 21
rs 9.0534
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)
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
        $expand = false;
64
        $user = $request->getAttribute('user');
65
        if($user === false)
66
        {
67
            $local = $request->getServerParam('SERVER_ADDR');
68
            $remote = $request->getServerParam('REMOTE_ADDR');
69
            if($local === $remote)
70
            {
71
                $auth = AuthProvider::getInstance();
72
                $group = $auth->getGroupByName($args['name']);
73
                $expand = true;
74
            }
75
            else
76
            {
77
                return $response->withStatus(401);
78
            }
79
        }
80
        else if($this->validateIsAdmin($request, true) === false)
81
        {
82
            $group = $this->getGroupForUserByName($args['name']);
83
        }
84
        else
85
        {
86
            $auth = AuthProvider::getInstance();
87
            $group = $auth->getGroupByName($args['name']);
88
            $expand = true;
89
        }
90
        if(empty($group))
91
        {
92
            return $response->withStatus(404);
93
        }
94
        $params = $request->getQueryParams();
95
        $directOnly = false;
96
        if(isset($params['directOnly']) && $params['directOnly'] === 'true')
97
        {
98
            $directOnly = true;
99
        }
100
        if($expand)
101
        {
102
            $group = $this->expandGroupMembers($group, $odata, $directOnly);
103
        }
104
        return $response->withJson($group);
105
    }
106
107
    protected function serializeArray(&$res, $array, $keys, $type=false)
108
    {
109
        $count = count($array);
110
        for($i = 0; $i < $count; $i++)
111
        {
112
            $tmp = json_decode(json_encode($array[$i]), true);
113
            if($type === false)
114
            {
115
                $tmp['type'] = $this->getTypeOfEntity($array[$i]);
116
            }
117
            else
118
            {
119
                $tmp['type'] = $type;
120
            }
121
            if($keys !== false)
122
            {
123
                $tmp = array_intersect_key($tmp, $keys);
124
            }
125
            $res[] = $tmp;
126
        }
127
    }
128
129
    public function getAllGroupsAndUsers($keys)
130
    {
131
        $auth = AuthProvider::getInstance();
132
        $groups = $auth->getGroupsByFilter(false);
133
        $res = array();
134
        $this->serializeArray($res, $groups, $keys, 'Group');
0 ignored issues
show
Documentation introduced by
'Group' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
135
        $users  = $auth->getUsersByFilter(false);
136
        $this->serializeArray($res, $users, $keys, 'User');
0 ignored issues
show
Documentation introduced by
'User' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
137
        return $res;
138
    }
139
140
    public function getTypeOfEntity($entity)
141
    {
142
        if(is_subclass_of($entity, 'Auth\Group'))
143
        {
144
            return 'Group';
145
        }
146
        else
147
        {
148
            return 'User';
149
        }
150
    }
151
152
    public function getNonMemberEntities($nonMembers, $keys)
153
    {
154
        if($keys !== false)
155
        {
156
            $res = array();
157
            $this->serializeArray($res, $nonMembers, $keys);
158
            return $res;
159
        }
160
        return $nonMembers;
161
    }
162
163
    public function getNonMembers($request, $response, $args)
164
    {
165
        $this->validateIsAdmin($request);
166
        $odata = $request->getAttribute('odata', new \ODataParams(array()));
167
        $keys = false;
168
        if($odata->select !== false)
169
        {
170
            $keys = array_flip($odata->select);
171
        }
172
        $auth = AuthProvider::getInstance();
173
        if($args['name'] === 'none')
174
        {
175
            $res = $this->getAllGroupsAndUsers($keys);
176
            return $response->withJson($res);
177
        }
178
        $group = $auth->getGroupByName($args['name']);
179
        if($group === false)
180
        {
181
            return $response->withStatus(404);
182
        }
183
        $res = $group->getNonMembers($odata->select);
184
        $res = $this->getNonMemberEntities($res, $keys);
185
        return $response->withJson($res);
186
    }
187
188
    public function updateGroup($request, $response, $args)
189
    {
190
        $this->validateIsAdmin($request);
191
        $auth = AuthProvider::getInstance();
192
        $group = $auth->getGroupByName($args['name']);
193
        if($group === false)
194
        {
195
            return $response->withStatus(404);
196
        }
197
        $obj = $request->getParsedBody();
198
        $ret = $group->editGroup($obj);
199
        return $response->withJson($ret);
200
    }
201
}
202
/* vim: set tabstop=4 shiftwidth=4 expandtab: */
203