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
Push — develop ( 6dc1c1...b539f2 )
by Dane
03:05
created

UserController::index()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
dl 12
loc 12
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
/**
3
 * Pterodactyl - Panel
4
 * Copyright (c) 2015 - 2017 Dane Everitt <[email protected]>
5
 * Some Modifications (c) 2015 Dylan Seidt <[email protected]>.
6
 *
7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8
 * of this software and associated documentation files (the "Software"), to deal
9
 * in the Software without restriction, including without limitation the rights
10
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
 * copies of the Software, and to permit persons to whom the Software is
12
 * furnished to do so, subject to the following conditions:
13
 *
14
 * The above copyright notice and this permission notice shall be included in all
15
 * copies or substantial portions of the Software.
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
 * SOFTWARE.
24
 */
25
26
namespace Pterodactyl\Http\Controllers\Admin;
27
28
use Log;
29
use Alert;
30
use Illuminate\Http\Request;
31
use Pterodactyl\Models\User;
32
use Pterodactyl\Exceptions\DisplayException;
33
use Pterodactyl\Http\Controllers\Controller;
34
use Pterodactyl\Repositories\UserRepository;
35
use Pterodactyl\Exceptions\DisplayValidationException;
36
37
class UserController extends Controller
38
{
39
    /**
40
     * Display user index page.
41
     *
42
     * @param  \Illuminate\Http\Request  $request
43
     * @return \Illuminate\View\View
44
     */
45 View Code Duplication
    public function index(Request $request)
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...
46
    {
47
        $users = User::withCount('servers', 'subuserOf');
48
49
        if (! is_null($request->input('query'))) {
50
            $users->search($request->input('query'));
51
        }
52
53
        return view('admin.users.index', [
0 ignored issues
show
Bug Compatibility introduced by
The expression view('admin.users.index'...$users->paginate(25))); 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...n\UserController::index of type Illuminate\View\View.
Loading history...
54
            'users' => $users->paginate(25),
55
        ]);
56
    }
57
58
    /**
59
     * Display new user page.
60
     *
61
     * @param  \Illuminate\Http\Request  $request
62
     * @return \Illuminate\View\View
63
     */
64
    public function create(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...
65
    {
66
        return view('admin.users.new');
0 ignored issues
show
Bug Compatibility introduced by
The expression view('admin.users.new'); of type Illuminate\View\View|Ill...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 66 which is incompatible with the return type documented by Pterodactyl\Http\Control...\UserController::create of type Illuminate\View\View.
Loading history...
67
    }
68
69
    /**
70
     * Display user view page.
71
     *
72
     * @param  \Illuminate\Http\Request  $request
73
     * @param  int                       $id
74
     * @return \Illuminate\View\View
75
     */
76
    public function view(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...
77
    {
78
        return view('admin.users.view', [
0 ignored issues
show
Bug Compatibility introduced by
The expression view('admin.users.view',...e')->findOrFail($id))); 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...in\UserController::view of type Illuminate\View\View.
Loading history...
79
            'user' => User::with('servers.node')->findOrFail($id),
0 ignored issues
show
Bug introduced by
The method findOrFail 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...
80
        ]);
81
    }
82
83
    /**
84
     * Delete a user.
85
     *
86
     * @param  \Illuminate\Http\Request  $request
87
     * @param  int                       $id
88
     * @return \Illuminate\Http\RedirectResponse
89
     */
90 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...
91
    {
92
        try {
93
            $repo = new UserRepository;
94
            $repo->delete($id);
95
            Alert::success('Successfully deleted user from system.')->flash();
96
97
            return redirect()->route('admin.users');
98
        } catch (DisplayException $ex) {
99
            Alert::danger($ex->getMessage())->flash();
100
        } catch (\Exception $ex) {
101
            Log::error($ex);
102
            Alert::danger('An exception was encountered while attempting to delete this user.')->flash();
103
        }
104
105
        return redirect()->route('admin.users.view', $id);
0 ignored issues
show
Documentation introduced by
$id is of type integer, 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...
106
    }
107
108
    /**
109
     * Create a user.
110
     *
111
     * @param  \Illuminate\Http\Request  $request
112
     * @return \Illuminate\Http\RedirectResponse
113
     */
114
    public function store(Request $request)
115
    {
116
        try {
117
            $user = new UserRepository;
118
            $userid = $user->create($request->only([
119
                'email', 'password', 'name_first',
120
                'name_last', 'username', 'root_admin',
121
            ]));
122
            Alert::success('Account has been successfully created.')->flash();
123
124
            return redirect()->route('admin.users.view', $userid);
0 ignored issues
show
Documentation introduced by
$userid is of type object<Pterodactyl\Models\User>, 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...
125
        } catch (DisplayValidationException $ex) {
126
            return redirect()->route('admin.users.new')->withErrors(json_decode($ex->getMessage()))->withInput();
127
        } catch (\Exception $ex) {
128
            Log::error($ex);
129
            Alert::danger('An error occured while attempting to add a new user.')->flash();
130
131
            return redirect()->route('admin.users.new');
132
        }
133
    }
134
135
    /**
136
     * Update a user.
137
     *
138
     * @param  \Illuminate\Http\Request  $request
139
     * @param  int                       $id
140
     * @return \Illuminate\Http\RedirectResponse
141
     */
142
    public function update(Request $request, $id)
143
    {
144
        // Rename variables because autofill cannot be disabled
145
        // in any logical manner, and editing users is impossible.
146
        $fixedData = array_filter(
147
            collect($request->all())->mapWithKeys(function ($item, $key) {
148
                return [str_replace('input_', '', $key) => $item];
149
            })->only([
150
                'email', 'password', 'name_first',
151
                'name_last', 'username', 'root_admin',
152
            ])->toArray()
153
        );
154
155
        try {
156
            $repo = new UserRepository;
157
            $user = $repo->update($id, $fixedData);
0 ignored issues
show
Unused Code introduced by
$user 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...
158
            Alert::success('User account was successfully updated.')->flash();
159
        } catch (DisplayValidationException $ex) {
160
            return redirect()->route('admin.users.view', $id)->withErrors(json_decode($ex->getMessage()));
0 ignored issues
show
Documentation introduced by
$id is of type integer, 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...
161
        } catch (\Exception $ex) {
162
            Log::error($ex);
163
            Alert::danger('An error occured while attempting to update this user.')->flash();
164
        }
165
166
        return redirect()->route('admin.users.view', $id);
0 ignored issues
show
Documentation introduced by
$id is of type integer, 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...
167
    }
168
169
    /**
170
     * Get a JSON response of users on the system.
171
     *
172
     * @param  \Illuminate\Http\Request  $request
173
     * @return \Pterodactyl\Models\User
174
     */
175
    public function json(Request $request)
176
    {
177
        return User::select('id', 'email', 'username', 'name_first', 'name_last')
178
            ->search($request->input('q'))
179
            ->get()->transform(function ($item) {
180
                $item->md5 = md5(strtolower($item->email));
181
182
                return $item;
183
            });
184
    }
185
}
186