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 (#371)
by Dane
05:00 queued 02:14
created

SubuserController::index()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 13
Ratio 100 %

Importance

Changes 0
Metric Value
dl 13
loc 13
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 2
1
<?php
2
/**
3
 * Pterodactyl - Panel
4
 * Copyright (c) 2015 - 2017 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\Server;
26
27
use Log;
28
use Auth;
29
use Alert;
30
use Pterodactyl\Models;
31
use Illuminate\Http\Request;
32
use Pterodactyl\Exceptions\DisplayException;
33
use Pterodactyl\Http\Controllers\Controller;
34
use Pterodactyl\Repositories\SubuserRepository;
35
use Pterodactyl\Exceptions\DisplayValidationException;
36
37
class SubuserController extends Controller
38
{
39
    /**
40
     * Displays the subuser overview index.
41
     *
42
     * @param  \Illuminate\Http\Request  $request
43
     * @param  string                    $uuid
44
     * @return \Illuminate\View\View
45
     */
46 View Code Duplication
    public function index(Request $request, $uuid)
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...
47
    {
48
        $server = Models\Server::byUuid($uuid)->load('subusers.user');
49
        $this->authorize('list-subusers', $server);
50
51
        $server->js();
52
53
        return view('server.users.index', [
0 ignored issues
show
Bug Compatibility introduced by
The expression view('server.users.index...=> $server->subusers)); of type Illuminate\View\View|Ill...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 53 which is incompatible with the return type documented by Pterodactyl\Http\Control...ubuserController::index of type Illuminate\View\View.
Loading history...
54
            'server' => $server,
55
            'node' => $server->node,
56
            'subusers' => $server->subusers,
57
        ]);
58
    }
59
60
    /**
61
     * Displays the a single subuser overview.
62
     *
63
     * @param  \Illuminate\Http\Request  $request
64
     * @param  string                    $uuid
65
     * @param  int                       $id
66
     * @return \Illuminate\View\View
67
     */
68
    public function view(Request $request, $uuid, $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...
69
    {
70
        $server = Models\Server::byUuid($uuid)->load('node');
71
        $this->authorize('view-subuser', $server);
72
73
        $subuser = Models\Subuser::with('permissions', 'user')
0 ignored issues
show
Bug introduced by
The method where does only exist in Illuminate\Database\Eloquent\Builder, but not in Illuminate\Database\Eloquent\Model.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
74
            ->where('server_id', $server->id)->findOrFail($id);
75
76
        $server->js();
77
78
        return view('server.users.view', [
0 ignored issues
show
Bug Compatibility introduced by
The expression view('server.users.view'...mission => true); }))); of type Illuminate\View\View|Ill...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 78 which is incompatible with the return type documented by Pterodactyl\Http\Control...SubuserController::view of type Illuminate\View\View.
Loading history...
79
            'server' => $server,
80
            'node' => $server->node,
81
            'subuser' => $subuser,
82
            'permlist' => Models\Permission::list(),
83
            'permissions' => $subuser->permissions->mapWithKeys(function ($item, $key) {
0 ignored issues
show
Unused Code introduced by
The parameter $key 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...
84
                return [$item->permission => true];
85
            }),
86
        ]);
87
    }
88
89
    /**
90
     * Handles editing a subuser.
91
     *
92
     * @param  \Illuminate\Http\Request  $request
93
     * @param  string                    $uuid
94
     * @param  int                       $id
95
     * @return \Illuminate\Http\RedirectResponse
96
     */
97
    public function update(Request $request, $uuid, $id)
98
    {
99
        $server = Models\Server::byUuid($uuid);
100
        $this->authorize('edit-subuser', $server);
101
102
        $subuser = Models\Subuser::where('server_id', $server->id)->findOrFail($id);
103
104
        try {
105
            if ($subuser->user_id === Auth::user()->id) {
106
                throw new DisplayException('You are not authorized to edit you own account.');
107
            }
108
109
            $repo = new SubuserRepository;
110
            $repo->update($subuser->id, [
111
                'permissions' => $request->input('permissions'),
112
                'server' => $server->id,
113
                'user' => $subuser->user_id,
114
            ]);
115
116
            Alert::success('Subuser permissions have successfully been updated.')->flash();
117
        } catch (DisplayValidationException $ex) {
118
            return redirect()->route('server.subusers.view', [
119
                'uuid' => $uuid,
120
                'id' => $id,
121
            ])->withErrors(json_decode($ex->getMessage()));
122
        } catch (DisplayException $ex) {
123
            Alert::danger($ex->getMessage())->flash();
124
        } catch (\Exception $ex) {
125
            Log::error($ex);
126
            Alert::danger('An unknown error occured while attempting to update this subuser.')->flash();
127
        }
128
129
        return redirect()->route('server.subusers.view', [
130
            'uuid' => $uuid,
131
            'id' => $id,
132
        ]);
133
    }
134
135
    /**
136
     * Display new subuser creation page.
137
     *
138
     * @param  \Illuminate\Http\Request  $request
139
     * @param  string                    $uuid
140
     * @return \Illuminate\View\View
141
     */
142 View Code Duplication
    public function create(Request $request, $uuid)
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...
143
    {
144
        $server = Models\Server::byUuid($uuid);
145
        $this->authorize('create-subuser', $server);
146
        $server->js();
147
148
        return view('server.users.new', [
0 ignored issues
show
Bug Compatibility introduced by
The expression view('server.users.new',...de' => $server->node)); of type Illuminate\View\View|Ill...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 148 which is incompatible with the return type documented by Pterodactyl\Http\Control...buserController::create of type Illuminate\View\View.
Loading history...
149
            'server' => $server,
150
            'permissions' => Models\Permission::list(),
151
            'node' => $server->node,
152
        ]);
153
    }
154
155
    /**
156
     * Handles creating a new subuser.
157
     *
158
     * @param  \Illuminate\Http\Request  $request
159
     * @param  string                    $uuid
160
     * @return \Illuminate\Http\RedirectResponse
161
     */
162
    public function store(Request $request, $uuid)
163
    {
164
        $server = Models\Server::byUuid($uuid);
165
        $this->authorize('create-subuser', $server);
166
167
        try {
168
            $repo = new SubuserRepository;
169
            $subuser = $repo->create($server->id, $request->only([
170
                'permissions', 'email',
171
            ]));
172
            Alert::success('Successfully created new subuser.')->flash();
173
174
            return redirect()->route('server.subusers.view', [
175
                'uuid' => $uuid,
176
                'id' => $subuser->id,
177
            ]);
178
        } catch (DisplayValidationException $ex) {
179
            return redirect()->route('server.subusers.new', $uuid)->withErrors(json_decode($ex->getMessage()))->withInput();
0 ignored issues
show
Documentation introduced by
$uuid is of type string, but the function expects a array.

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...
180
        } catch (DisplayException $ex) {
181
            Alert::danger($ex->getMessage())->flash();
182
        } catch (\Exception $ex) {
183
            Log::error($ex);
184
            Alert::danger('An unknown error occured while attempting to add a new subuser.')->flash();
185
        }
186
187
        return redirect()->route('server.subusers.new', $uuid)->withInput();
0 ignored issues
show
Documentation introduced by
$uuid is of type string, but the function expects a array.

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...
188
    }
189
190
    /**
191
     * Handles deleting a subuser.
192
     *
193
     * @param  \Illuminate\Http\Request  $request
194
     * @param  string                    $uuid
195
     * @param  int                       $id
196
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response
197
     */
198
    public function delete(Request $request, $uuid, $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...
199
    {
200
        $server = Models\Server::byUuid($uuid);
201
        $this->authorize('delete-subuser', $server);
202
203
        try {
204
            $subuser = Models\Subuser::where('server_id', $server->id)->findOrFail($id);
205
206
            $repo = new SubuserRepository;
207
            $repo->delete($subuser->id);
208
209
            return response('', 204);
210
        } catch (DisplayException $ex) {
211
            response()->json([
212
                'error' => $ex->getMessage(),
213
            ], 422);
214
        } catch (\Exception $ex) {
215
            Log::error($ex);
216
            response()->json([
217
                'error' => 'An unknown error occured while attempting to delete this subuser.',
218
            ], 503);
219
        }
220
    }
221
}
222