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 — master ( c31bff...117582 )
by Caspar
10:25
created

ProfilesController::getUserByUsername()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Models\Profile;
6
use App\Models\Theme;
7
use App\Models\User;
8
use App\Traits\CaptureIpTrait;
9
use File;
10
use Illuminate\Database\Eloquent\ModelNotFoundException;
11
use Illuminate\Http\Request;
12
use Illuminate\Support\Facades\Input;
13
use Image;
14
use jeremykenedy\Uuid\Uuid;
15
use Validator;
16
use View;
17
18
class ProfilesController extends Controller
19
{
20
    protected $idMultiKey = '618423'; //int
21
    protected $seperationKey = '****';
22
23
    /**
24
     * Create a new controller instance.
25
     *
26
     * @return void
27
     */
28
    public function __construct()
29
    {
30
        $this->middleware('auth');
31
    }
32
33
    /**
34
     * Get a validator for an incoming registration request.
35
     *
36
     * @param array $data
37
     *
38
     * @return \Illuminate\Contracts\Validation\Validator
39
     */
40
    public function profile_validator(array $data)
41
    {
42
        return Validator::make($data, [
43
            'theme_id'      => '',
44
            'avatar'        => '',
45
            'avatar_status' => '',
46
        ]);
47
    }
48
49
    /**
50
     * Fetch user
51
     * (You can extract this to repository method).
52
     *
53
     * @param $name_gen
54
     *
55
     * @return mixed
56
     *
57
     * @internal param $username
58
     */
59
    public function getUserByUsername($name_gen)
60
    {
61
        return User::with('profile')->where('name_gen', $name_gen)->firstOrFail();
62
    }
63
64
    /**
65
     * Display the specified resource.
66
     *
67
     * @param $name_gen
68
     *
69
     * @return Response
0 ignored issues
show
Bug introduced by
The type App\Http\Controllers\Response was not found. Did you mean Response? If so, make sure to prefix the type with \.
Loading history...
70
     *
71
     * @internal param string $username
72
     */
73
    public function show($name_gen)
74
    {
75
        try {
76
            $user = $this->getUserByUsername($name_gen);
77
        } catch (ModelNotFoundException $exception) {
78
            abort(404);
79
        }
80
81
        $currentTheme = Theme::find($user->profile->theme_id);
82
83
        $data = [
84
            'user'         => $user,
85
            'currentTheme' => $currentTheme,
86
        ];
87
88
        return view('profiles.show')->with($data);
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('profiles.show')->with($data) returns the type Illuminate\View\View which is incompatible with the documented return type App\Http\Controllers\Response.
Loading history...
89
    }
90
91
    /**
92
     * /profiles/username/edit.
93
     *
94
     * @param $name_gen
95
     *
96
     * @return mixed
97
     *
98
     * @internal param $username
99
     */
100
    public function edit($name_gen)
101
    {
102
        try {
103
            $user = $this->getUserByUsername($name_gen);
104
        } catch (ModelNotFoundException $exception) {
105
            return view('pages.status')->with('error', trans('profile.notYourProfile'))->with('error_title', trans('profile.notYourProfileTitle'));
106
        }
107
108
        $themes = Theme::where('status', 1)->orderBy('name', 'asc')->get();
109
110
        $currentTheme = Theme::find($user->profile->theme_id);
111
112
        $data = [
113
            'user'         => $user,
114
            'themes'       => $themes,
115
            'currentTheme' => $currentTheme,
116
117
        ];
118
119
        return view('profiles.edit')->with($data);
120
    }
121
122
    /**
123
     * Update a user's profile.
124
     *
125
     * @param $name_gen
126
     * @param Request $request
127
     *
128
     * @return mixed
129
     *
130
     * @internal param $username
131
     */
132
    public function update($name_gen, Request $request)
133
    {
134
        $user = $this->getUserByUsername($name_gen);
135
136
        $input = Input::only('theme_id', 'avatar_status');
137
138
        $ipAddress = new CaptureIpTrait();
139
140
        $profile_validator = $this->profile_validator($request->all());
141
142
        if ($profile_validator->fails()) {
143
            return back()->withErrors($profile_validator)->withInput();
144
        }
145
146
        if ($user->profile == null) {
147
            $profile = new Profile();
148
            $profile->fill($input);
149
            $user->profile()->save($profile);
150
        } else {
151
            $user->profile->fill($input)->save();
152
        }
153
154
        $user->updated_ip_address = $ipAddress->getClientIp();
155
156
        $user->save();
157
158
        return redirect('profile/'.$user->name_gen.'/edit')->with('success', trans('profile.updateSuccess'));
159
    }
160
161
    /**
162
     * Get a validator for an incoming update user request.
163
     *
164
     * @param array $data
165
     *
166
     * @return \Illuminate\Contracts\Validation\Validator
167
     */
168
    public function validator(array $data)
169
    {
170
        return Validator::make($data, [
171
            'pfadiname' => 'max:255',
172
        ]);
173
    }
174
175
    /**
176
     * Update the specified resource in storage.
177
     *
178
     * @param \Illuminate\Http\Request $request
179
     * @param int                      $id
180
     *
181
     * @return \Illuminate\Http\Response
182
     */
183
    public function updateUserAccount(Request $request, $id)
184
    {
185
        $user = User::findOrFail($id);
186
        $ipAddress = new CaptureIpTrait();
187
        $name_gen = (($request->input('scoutname') != null) ? $request->input('first_name').'_'.$request->input('scoutname').'_'.$request->input('last_name') : $request->input('first_name').'_'.$request->input('last_name'));
188
189
        $rules = [];
190
191
        $validator = $this->validator($request->all(), $rules);
0 ignored issues
show
Unused Code introduced by
The call to App\Http\Controllers\Pro...Controller::validator() has too many arguments starting with $rules. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

191
        /** @scrutinizer ignore-call */ 
192
        $validator = $this->validator($request->all(), $rules);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
192
193
        if ($validator->fails()) {
194
            return back()->withErrors($validator)->withInput();
0 ignored issues
show
Bug Best Practice introduced by
The expression return back()->withError...validator)->withInput() returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
195
        }
196
197
        $user->scoutname = $request->input('scoutname');
198
        $user->first_name = $request->input('first_name');
199
        $user->last_name = $request->input('last_name');
200
        $user->name_gen = $name_gen;
201
202
        $user->updated_ip_address = $ipAddress->getClientIp();
203
204
        $user->save();
205
206
        return redirect('profile/'.$user->name_gen.'/edit')->with('success', trans('profile.updateAccountSuccess'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect('profile...updateAccountSuccess')) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
207
    }
208
209
    /**
210
     * Update the specified resource in storage.
211
     *
212
     * @param \Illuminate\Http\Request $request
213
     * @param int                      $id
214
     *
215
     * @return \Illuminate\Http\Response
216
     */
217
    public function updateUserPassword(Request $request, $id)
218
    {
219
        $user = User::findOrFail($id);
220
        $ipAddress = new CaptureIpTrait();
221
222
        $validator = Validator::make($request->all(),
223
            [
224
                'password'              => 'required|min:6|max:20|confirmed',
225
                'password_confirmation' => 'required|same:password',
226
            ],
227
            [
228
                'password.required' => trans('auth.passwordRequired'),
229
                'password.min'      => trans('auth.PasswordMin'),
230
                'password.max'      => trans('auth.PasswordMax'),
231
            ]
232
        );
233
234
        if ($validator->fails()) {
235
            return back()->withErrors($validator)->withInput();
0 ignored issues
show
Bug Best Practice introduced by
The expression return back()->withError...validator)->withInput() returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
236
        }
237
238
        if ($request->input('password') != null) {
239
            $user->password = bcrypt($request->input('password'));
240
        }
241
242
        $user->updated_ip_address = $ipAddress->getClientIp();
243
244
        $user->save();
245
246
        return redirect('profile/'.$user->name_gen.'/edit')->with('success', trans('profile.updatePWSuccess'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect('profile...file.updatePWSuccess')) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
247
    }
248
249
    /**
250
     * Upload and Update user avatar.
251
     *
252
     * @param $file
253
     *
254
     * @return mixed
255
     */
256
    public function upload()
257
    {
258
        if (Input::hasFile('file')) {
259
            $currentUser = \Auth::user();
260
            $avatar = Input::file('file');
261
            $filename = 'avatar.'.$avatar->getClientOriginalExtension();
262
            $save_path = storage_path().'/users/id/'.$currentUser->id.'/uploads/images/avatar/';
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
263
            $path = $save_path.$filename;
264
            $public_path = '/images/profile/'.$currentUser->id.'/avatar/'.$filename;
265
266
            // Make the user a folder and set permissions
267
            File::makeDirectory($save_path, $mode = 0755, true, true);
268
269
            // Save the file to the server
270
            Image::make($avatar)->resize(300, 300)->save($save_path.$filename);
271
272
            // Save the public image path
273
            $currentUser->profile->avatar = $public_path;
0 ignored issues
show
Bug introduced by
Accessing profile on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
274
            $currentUser->profile->save();
275
276
            return response()->json(['path' => $path], 200);
277
        } else {
278
            return response()->json(false, 200);
279
        }
280
    }
281
282
    /**
283
     * Show user avatar.
284
     *
285
     * @param $id
286
     * @param $image
287
     *
288
     * @return string
289
     */
290
    public function userProfileAvatar($id, $image)
291
    {
292
        return Image::make(storage_path().'/users/id/'.$id.'/uploads/images/avatar/'.$image)->response();
293
    }
294
295
    /**
296
     * Update the specified resource in storage.
297
     *
298
     * @param \Illuminate\Http\Request $request
299
     * @param int                      $id
300
     *
301
     * @throws \Exception
302
     *
303
     * @return \Illuminate\Http\Response
304
     */
305
    public function deleteUserAccount(Request $request, $id)
306
    {
307
        $currentUser = \Auth::user();
308
        $user = User::findOrFail($id);
309
        $ipAddress = new CaptureIpTrait();
310
311
        $validator = Validator::make($request->all(),
312
            [
313
                'checkConfirmDelete' => 'required',
314
            ],
315
            [
316
                'checkConfirmDelete.required' => trans('profile.confirmDeleteRequired'),
317
            ]
318
        );
319
320
        if ($user->id != $currentUser->id) {
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
321
            return redirect('profile/'.$user->name_gen.'/edit')->with('error', trans('profile.errorDeleteNotYour'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect('profile...e.errorDeleteNotYour')) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
322
        }
323
324
        if ($validator->fails()) {
325
            return back()->withErrors($validator)->withInput();
0 ignored issues
show
Bug Best Practice introduced by
The expression return back()->withError...validator)->withInput() returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
326
        }
327
328
        // Create and encrypt user account restore token
329
        $sepKey = $this->getSeperationKey();
330
        $userIdKey = $this->getIdMultiKey();
331
        $restoreKey = config('settings.restoreKey');
332
        $encrypter = config('settings.restoreUserEncType');
333
        $level1 = $user->id * $userIdKey;
334
        $level2 = urlencode(Uuid::generate(4).$sepKey.$level1);
335
        $level3 = base64_encode($level2);
336
        $level4 = openssl_encrypt($level3, $encrypter, $restoreKey);
337
        $level5 = base64_encode($level4);
338
339
        // Save Restore Token and Ip Address
340
        $user->token = $level5;
341
        $user->deleted_ip_address = $ipAddress->getClientIp();
342
        $user->save();
343
344
        // Soft Delete User
345
        $user->delete();
346
347
        // Clear out the session
348
        $request->session()->flush();
349
        $request->session()->regenerate();
350
351
        return redirect('/login/')->with('success', trans('profile.successUserAccountDeleted'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect('/login/...ssUserAccountDeleted')) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
352
    }
353
354
    /**
355
     * Get User Restore ID Multiplication Key.
356
     *
357
     * @return string
358
     */
359
    public function getIdMultiKey()
360
    {
361
        return $this->idMultiKey;
362
    }
363
364
    /**
365
     * Get User Restore Seperation Key.
366
     *
367
     * @return string
368
     */
369
    public function getSeperationKey()
370
    {
371
        return $this->seperationKey;
372
    }
373
}
374