Completed
Push — master ( 287393...7ff50d )
by Raffael
18:27 queued 14:12
created

src/app/Balloon.App.Api/v1/Resource.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * balloon
7
 *
8
 * @copyright   Copryright (c) 2012-2019 gyselroth GmbH (https://gyselroth.com)
9
 * @license     GPL-3.0 https://opensource.org/licenses/GPL-3.0
10
 */
11
12
namespace Balloon\App\Api\v1;
13
14
use Balloon\Server;
15
use Balloon\Server\AttributeDecorator;
16
use Balloon\Server\User;
0 ignored issues
show
This use statement conflicts with another class in this namespace, Balloon\App\Api\v1\User.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
17
use Micro\Http\Response;
18
use MongoDB\BSON\Regex;
19
20
class Resource
21
{
22
    /**
23
     * Server.
24
     *
25
     * @var Server
26
     */
27
    protected $server;
28
29
    /**
30
     * User.
31
     *
32
     * @var User
33
     */
34
    protected $user;
35
36
    /**
37
     * Attribute decorator.
38
     *
39
     * @var AttributeDecorator
40
     */
41
    protected $decorator;
42
43
    /**
44
     * Initialize.
45
     */
46
    public function __construct(Server $server, AttributeDecorator $decorator)
47
    {
48
        $this->server = $server;
49
        $this->user = $server->getIdentity();
50
        $this->decorator = $decorator;
51
    }
52
53
    /**
54
     * @api {get} /api/v1/resource/acl-roles?q=:query&namespace=:namespace Query available acl roles
55
     * @apiVersion 1.0.0
56
     * @apiName getAclRoles
57
     * @apiGroup Resource
58
     * @apiPermission none
59
     * @apiDescription Query available acl roles (user and groups)
60
     *
61
     * @apiExample Example usage:
62
     * curl -XGET "https://SERVER/api/v1/user/acl-roles?q=peter&namespace=organization&pretty"
63
     *
64
     * @apiParam (GET Parameter) {string} [1] Search query (user/group)
65
     * @apiParam (GET Parameter) {boolean} [single] Search request for a single user (Don't have to be in namespace)
66
     * @apiSuccess {number} status Status Code
67
     * @apiSuccess {object[]} roles All roles found with query search string
68
     * @apiSuccess {string} roles.type ACL role type (user|group)
69
     * @apiSuccess {string} roles.id Role identifier (Could be the same as roles.name)
70
     * @apiSuccess {string} roles.name Role name (human readable name)
71
     * @apiSuccessExample {json} Success-Response:
72
     * HTTP/1.1 200 OK
73
     * {
74
     *     "status": 200,
75
     *     "data": [
76
     *          {
77
     *              "type": "user",
78
     *              "id": "peter.meier",
79
     *              "name": "peter.meier"
80
     *          },
81
     *          {
82
     *              "type": "group",
83
     *              "id": "peters",
84
     *              "name": "peters"
85
     *          }
86
     *      ]
87
     * }
88
     */
89
    public function getAclRoles(string $q, bool $single = false, array $attributes = []): Response
0 ignored issues
show
The parameter $attributes 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...
90
    {
91
        if (true === $single) {
92
            $regex = new Regex('^'.preg_quote($q).'$', 'i');
93
            $users_filter = [
94
                'username' => $regex,
95
            ];
96
            $groups_filter = [
97
                'name' => $regex,
98
            ];
99
        } else {
100
            $regex = new Regex('^'.preg_quote($q), 'i');
101
            $users_filter = [
102
                'username' => $regex,
103
                'namespace' => $this->user->getNamespace(),
104
            ];
105
            $groups_filter = [
106
                'name' => $regex,
107
                'namespace' => $this->user->getNamespace(),
108
            ];
109
        }
110
111
        $body = [];
112
113
        foreach ($this->server->getGroups($groups_filter) as $role) {
114
            $body[] = [
115
                'type' => 'group',
116
                'id' => (string) $role->getId(),
117
                'name' => (string) $role->getName(),
118
            ];
119
        }
120
121
        foreach ($this->server->getUsers($users_filter) as $role) {
122
            $body[] = [
123
                'type' => 'user',
124
                'id' => (string) $role->getId(),
125
                'name' => (string) $role->getUsername(),
126
            ];
127
        }
128
129
        if ($single === true) {
130
            $body = array_shift($body);
131
        }
132
133
        return (new Response())->setCode(200)->setBody([
134
            'status' => 200,
135
            'data' => $body,
136
        ]);
137
    }
138
}
139