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 (#248)
by Dane
03:14
created

UserController::updateUser()   A

Complexity

Conditions 3
Paths 7

Size

Total Lines 22
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 22
rs 9.2
cc 3
eloc 17
nc 7
nop 2
1
<?php
2
/**
3
 * Pterodactyl - Panel
4
 * Copyright (c) 2015 - 2016 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\Models\Server;
33
use Pterodactyl\Exceptions\DisplayException;
34
use Pterodactyl\Http\Controllers\Controller;
35
use Pterodactyl\Repositories\UserRepository;
36
use Pterodactyl\Exceptions\DisplayValidationException;
37
38
class UserController extends Controller
39
{
40
    /**
41
     * Controller Constructor.
42
     */
43
    public function __construct()
44
    {
45
        //
46
    }
47
48
    public function getIndex(Request $request)
49
    {
50
        $query = User::select('users.*');
51
        if ($request->input('filter') && ! is_null($request->input('filter'))) {
52
            preg_match_all('/[^\s"\']+|"([^"]*)"|\'([^\']*)\'/', urldecode($request->input('filter')), $matches);
53
            foreach ($matches[0] as $match) {
54
                $match = str_replace('"', '', $match);
55
                if (strpos($match, ':')) {
56
                    list($field, $term) = explode(':', $match);
57
                    $query->orWhere($field, 'LIKE', '%' . $term . '%');
58
                } else {
59
                    $query->where('email', 'LIKE', '%' . $match . '%');
60
                    $query->orWhere([
61
                        ['uuid', 'LIKE', '%' . $match . '%'],
62
                        ['root_admin', 'LIKE', '%' . $match . '%'],
63
                    ]);
64
                }
65
            }
66
        }
67
68
        try {
69
            $users = $query->paginate(20);
70
        } catch (\Exception $ex) {
71
            Alert::warning('There was an error with the search parameters provided.');
72
            $users = User::all()->paginate(20);
73
        }
74
75
        return view('admin.users.index', [
76
            'users' => $users,
77
        ]);
78
    }
79
80
    public function getNew(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...
81
    {
82
        return view('admin.users.new');
83
    }
84
85
    public function getView(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...
86
    {
87
        return view('admin.users.view', [
88
            'user' => User::findOrFail($id),
89
            'servers' => Server::select('servers.*', 'nodes.name as nodeName', 'locations.long as location')
90
                ->join('nodes', 'servers.node', '=', 'nodes.id')
91
                ->join('locations', 'nodes.location', '=', 'locations.id')
92
                ->where('owner', $id)
93
                ->get(),
94
        ]);
95
    }
96
97 View Code Duplication
    public function deleteUser(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...
98
    {
99
        try {
100
            $repo = new UserRepository;
101
            $repo->delete($id);
102
            Alert::success('Successfully deleted user from system.')->flash();
103
104
            return redirect()->route('admin.users');
105
        } catch (DisplayException $ex) {
106
            Alert::danger($ex->getMessage())->flash();
107
        } catch (\Exception $ex) {
108
            Log::error($ex);
109
            Alert::danger('An exception was encountered while attempting to delete this user.')->flash();
110
        }
111
112
        return redirect()->route('admin.users.view', $id);
113
    }
114
115
    public function postNew(Request $request)
116
    {
117
        try {
118
            $user = new UserRepository;
119
            $userid = $user->create($request->only([
120
                'email',
121
                'password',
122
                'name_first',
123
                'name_last',
124
                'username'
125
            ]));
126
            Alert::success('Account has been successfully created.')->flash();
127
128
            return redirect()->route('admin.users.view', $userid);
0 ignored issues
show
Documentation introduced by
$userid is of type boolean|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...
129
        } catch (DisplayValidationException $ex) {
130
            return redirect()->route('admin.users.new')->withErrors(json_decode($ex->getMessage()))->withInput();
131
        } catch (\Exception $ex) {
132
            Log::error($ex);
133
            Alert::danger('An error occured while attempting to add a new user.')->flash();
134
135
            return redirect()->route('admin.users.new');
136
        }
137
    }
138
139
    public function updateUser(Request $request, $user)
140
    {
141
        try {
142
            $repo = new UserRepository;
143
            $repo->update($user, $request->only([
144
                'email',
145
                'password',
146
                'name_first',
147
                'name_last',
148
                'username',
149
                'root_admin',
150
            ]));
151
            Alert::success('User account was successfully updated.')->flash();
152
        } catch (DisplayValidationException $ex) {
153
            return redirect()->route('admin.users.view', $user)->withErrors(json_decode($ex->getMessage()));
154
        } catch (\Exception $e) {
155
            Log::error($e);
156
            Alert::danger('An error occured while attempting to update this user.')->flash();
157
        }
158
159
        return redirect()->route('admin.users.view', $user);
160
    }
161
162
    public function getJson(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...
163
    {
164
        foreach (User::select('email')->get() as $user) {
165
            $resp[] = $user->email;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$resp was never initialized. Although not strictly required by PHP, it is generally a good practice to add $resp = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
166
        }
167
168
        return $resp;
0 ignored issues
show
Bug introduced by
The variable $resp does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
169
    }
170
}
171