GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — develop (#206)
by
unknown
02:23
created

UserController::lists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * Pterodactyl - Panel
4
 * Copyright (c) 2015 - 2016 Dane Everitt <[email protected]>.
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in all
14
 * copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
 * SOFTWARE.
23
 */
24
25
namespace Pterodactyl\Http\Controllers\API;
26
27
use Pterodactyl\Models;
28
use Illuminate\Http\Request;
29
use Dingo\Api\Exception\ResourceException;
30
use Pterodactyl\Exceptions\DisplayException;
31
use Pterodactyl\Repositories\UserRepository;
32
use Pterodactyl\Exceptions\DisplayValidationException;
33
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
34
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
35
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
36
37
/**
38
 * @Resource("Users")
39
 */
40
class UserController extends BaseController
41
{
42
    public function __construct()
43
    {
44
    }
45
46
    /**
47
     * List All Users.
48
     *
49
     * Lists all users currently on the system.
50
     *
51
     * @Get("/users/{?page}")
52
     * @Versions({"v1"})
53
     * @Parameters({
54
     *      @Parameter("page", type="integer", description="The page of results to view.", default=1)
55
     * })
56
     * @Response(200)
57
     */
58
    public function lists(Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request 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...
59
    {
60
        return Models\User::all()->toArray();
61
    }
62
63
    /**
64
     * List Specific User.
65
     *
66
     * Lists specific fields about a user or all fields pertaining to that user.
67
     *
68
     * @Get("/users/{id}/{fields}")
69
     * @Versions({"v1"})
70
     * @Parameters({
71
     *      @Parameter("id", type="integer", required=true, description="The ID of the user to get information on."),
72
     *      @Parameter("fields", type="string", required=false, description="A comma delimidated list of fields to include.")
73
     * })
74
     * @Response(200)
75
     */
76
    public function view(Request $request, $id)
77
    {
78
        $query = Models\User::where('id', $id);
79
80 View Code Duplication
        if (! is_null($request->input('fields'))) {
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...
81
            foreach (explode(',', $request->input('fields')) as $field) {
82
                if (! empty($field)) {
83
                    $query->addSelect($field);
84
                }
85
            }
86
        }
87
88
        try {
89
            if (! $query->first()) {
90
                throw new NotFoundHttpException('No user by that ID was found.');
91
            }
92
93
            $user = $query->first();
94
            $userArray = $user->toArray();
95
            $userArray['servers'] = Models\Server::select('id', 'uuid', 'node', 'suspended')->where('owner', $user->id)->get();
96
97
            return $userArray;
98
        } catch (NotFoundHttpException $ex) {
99
            throw $ex;
100
        } catch (\Exception $ex) {
101
            throw new BadRequestHttpException('There was an issue with the fields passed in the request.');
102
        }
103
    }
104
105
    /**
106
     * Create a New User.
107
     *
108
     * @Post("/users")
109
     * @Versions({"v1"})
110
     * @Transaction({
111
     *      @Request({
112
     *          "email": "[email protected]",
113
     *          "password": "foopassword",
114
     *          "admin": false,
115
     *          "custom_id": 123
116
     *       }, headers={"Authorization": "Bearer <token>"}),
117
     *       @Response(201),
118
     *       @Response(422)
119
     * })
120
     */
121
    public function create(Request $request)
122
    {
123
        try {
124
            $user = new UserRepository;
125
            $create = $user->create($request->input('email'), $request->input('password'), $request->input('admin'), $request->input('custom_id'));
0 ignored issues
show
Bug introduced by
It seems like $request->input('email') targeting Illuminate\Http\Request::input() can also be of type array; however, Pterodactyl\Repositories\UserRepository::create() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Bug introduced by
It seems like $request->input('password') targeting Illuminate\Http\Request::input() can also be of type array; however, Pterodactyl\Repositories\UserRepository::create() does only seem to accept string|null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Documentation introduced by
$request->input('admin') is of type string|array, 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...
Documentation introduced by
$request->input('custom_id') is of type string|array, but the function expects a integer|null.

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...
126
127
            return ['id' => $create];
128
        } catch (DisplayValidationException $ex) {
129
            throw new ResourceException('A validation error occured.', json_decode($ex->getMessage(), true));
130
        } catch (DisplayException $ex) {
131
            throw new ResourceException($ex->getMessage());
132
        } catch (\Exception $ex) {
133
            throw new ServiceUnavailableHttpException('Unable to create a user on the system due to an error.');
134
        }
135
    }
136
137
    /**
138
     * Update an Existing User.
139
     *
140
     * The data sent in the request will be used to update the existing user on the system.
141
     *
142
     * @Patch("/users/{id}")
143
     * @Versions({"v1"})
144
     * @Transaction({
145
     *      @Request({
146
     *          "email": "[email protected]"
147
     *      }, headers={"Authorization": "Bearer <token>"}),
148
     *      @Response(200, body={"email": "[email protected]"}),
149
     *      @Response(422)
150
     * })
151
     * @Parameters({
152
     *         @Parameter("id", type="integer", required=true, description="The ID of the user to modify.")
153
     * })
154
     */
155 View Code Duplication
    public function update(Request $request, $id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
156
    {
157
        try {
158
            $user = new UserRepository;
159
            $user->update($id, $request->all());
160
161
            return Models\User::findOrFail($id);
162
        } catch (DisplayValidationException $ex) {
163
            throw new ResourceException('A validation error occured.', json_decode($ex->getMessage(), true));
164
        } catch (DisplayException $ex) {
165
            throw new ResourceException($ex->getMessage());
166
        } catch (\Exception $ex) {
167
            throw new ServiceUnavailableHttpException('Unable to update a user on the system due to an error.');
168
        }
169
    }
170
171
    /**
172
     * Delete a User.
173
     *
174
     * @Delete("/users/{id}")
175
     * @Versions({"v1"})
176
     * @Transaction({
177
     *      @Request(headers={"Authorization": "Bearer <token>"}),
178
     *      @Response(204),
179
     *      @Response(422)
180
     * })
181
     * @Parameters({
182
     *      @Parameter("id", type="integer", required=true, description="The ID of the user to delete.")
183
     * })
184
     */
185 View Code Duplication
    public function delete(Request $request, $id)
0 ignored issues
show
Unused Code introduced by
The parameter $request 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...
Duplication introduced by
This method seems to be duplicated in 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...
186
    {
187
        try {
188
            $user = new UserRepository;
189
            $user->delete($id);
190
191
            return $this->response->noContent();
192
        } catch (DisplayException $ex) {
193
            throw new ResourceException($ex->getMessage());
194
        } catch (\Exception $ex) {
195
            throw new ServiceUnavailableHttpException('Unable to delete this user due to an error.');
196
        }
197
    }
198
}
199