Completed
Push — development ( 680f26...f4ae4f )
by Claudio
08:15
created

AccountController::filterName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Facades\Session;
6
use App\Models\ChocolateyId;
7
use App\Models\User;
8
use App\Models\UserPreferences;
9
use App\Models\UserSettings;
10
use Illuminate\Http\JsonResponse;
11
use Illuminate\Http\Request;
12
use Illuminate\Support\Facades\Config;
13
use Laravel\Lumen\Routing\Controller as BaseController;
14
15
/**
16
 * Class AccountController
17
 * @package App\Http\Controllers
18
 */
19
class AccountController extends BaseController
20
{
21
    /**
22
     * Filter an Username from the Invalid Names Base
23
     *
24
     * @param string $userName
25
     * @return bool
26
     */
27
    protected function filterName(string $userName): bool
28
    {
29
        return count(array_filter(Config::get('chocolatey.invalid'), function ($username) use ($userName) {
30
                return $username == $userName;
31
            })) == 0;
32
    }
33
34
    /**
35
     * Check an User Name
36
     *
37
     * @param Request $request
38
     * @return JsonResponse
39
     */
40
    public function checkName(Request $request): JsonResponse
41
    {
42
        if (User::where('username', $request->json()->get('name'))->count() > 0 && $request->json()->get('name') != $request->user()->name)
43
            return response()->json(['code' => 'NAME_IN_USE', 'validationResult' => null, 'suggestions' => []]);
0 ignored issues
show
Bug introduced by
The method json does only exist in Laravel\Lumen\Http\ResponseFactory, but not in Illuminate\Http\Response.

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...
44
45
        if (strlen($request->json()->get('name')) > 50 || $this->filterName($request->json()->get('name')))
46
            return response()->json(['code' => 'INVALID_NAME', 'validationResult' => null, 'suggestions' => []]);
47
48
        return response()->json(['code' => 'OK', 'validationResult' => null, 'suggestions' => []]);
49
    }
50
51
    /**
52
     * Select an User Name
53
     *
54
     * @param Request $request
55
     * @return JsonResponse
56
     */
57
    public function selectName(Request $request): JsonResponse
58
    {
59
        $request->user()->update(['username' => $request->json()->get('name')]);
60
61
        Session::set(Config::get('chocolatey.security.session'), $request->user());
62
63
        return response()->json(['code' => 'OK', 'validationResult' => null, 'suggestions' => []]);
0 ignored issues
show
Bug introduced by
The method json does only exist in Laravel\Lumen\Http\ResponseFactory, but not in Illuminate\Http\Response.

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...
64
    }
65
66
    /**
67
     * Save User Look
68
     *
69
     * @param Request $request
70
     * @return JsonResponse
71
     */
72
    public function saveLook(Request $request): JsonResponse
73
    {
74
        $request->user()->update([
75
            'look' => $request->json()->get('figure'),
76
            'gender' => $request->json()->get('gender')]);
77
78
        Session::set(Config::get('chocolatey.security.session'), $request->user());
79
80
        return response()->json($request->user());
0 ignored issues
show
Bug introduced by
The method json does only exist in Laravel\Lumen\Http\ResponseFactory, but not in Illuminate\Http\Response.

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...
81
    }
82
83
    /**
84
     * Select a Room
85
     *
86
     * @TODO: Generate the Room for the User
87
     * @TODO: Get Room Models.
88
     *
89
     * @param Request $request
90
     * @return JsonResponse
91
     */
92
    public function selectRoom(Request $request): JsonResponse
93
    {
94
        if (!in_array($request->json()->get('roomIndex'), [1, 2, 3]))
95
            return response('', 400);
96
97
        $request->user()->traits = ["USER"];
98
99
        return response()->json('');
0 ignored issues
show
Bug introduced by
The method json does only exist in Laravel\Lumen\Http\ResponseFactory, but not in Illuminate\Http\Response.

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...
100
    }
101
102
    /**
103
     * Get User Non Read Messenger Discussions
104
     *
105
     * @TODO: Code Integration with HabboMessenger
106
     * @TODO: Create Messenger Model
107
     *
108
     * @return JsonResponse
109
     */
110
    public function getDiscussions(): JsonResponse
111
    {
112
        return response()->json([]);
0 ignored issues
show
Bug introduced by
The method json does only exist in Laravel\Lumen\Http\ResponseFactory, but not in Illuminate\Http\Response.

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...
113
    }
114
115
    /**
116
     * Get User Preferences
117
     *
118
     * @param Request $request
119
     * @return JsonResponse
120
     */
121
    public function getPreferences(Request $request): JsonResponse
122
    {
123
        $userPreferences = UserPreferences::find($request->user()->uniqueId);
124
125
        foreach ($userPreferences->getAttributes() as $attributeName => $attributeValue)
126
            $userPreferences->{$attributeName} = $attributeValue == 1;
127
128
        return response()->json($userPreferences);
0 ignored issues
show
Bug introduced by
The method json does only exist in Laravel\Lumen\Http\ResponseFactory, but not in Illuminate\Http\Response.

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...
129
    }
130
131
    /**
132
     * Save New User Preferences
133
     *
134
     * @param Request $request
135
     * @return JsonResponse
136
     */
137
    public function savePreferences(Request $request): JsonResponse
138
    {
139
        UserSettings::updateOrCreate([
140
            'user_id' => $request->user()->uniqueId,
141
            'block_following' => $request->json()->get('friendCanFollow') == false,
142
            'block_friendrequests' => $request->json()->get('friendRequestEnabled') == false
143
        ]);
144
145
        UserPreferences::find($request->user()->uniqueId)->update((array)$request->json()->all());
146
147
        return response()->json('');
0 ignored issues
show
Bug introduced by
The method json does only exist in Laravel\Lumen\Http\ResponseFactory, but not in Illuminate\Http\Response.

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...
148
    }
149
150
    /**
151
     * Get All E-Mail Accounts
152
     *
153
     * @param Request $request
154
     * @return JsonResponse
155
     */
156
    public function getAvatars(Request $request): JsonResponse
157
    {
158
        $azureIdAccounts = ChocolateyId::where('mail', $request->user()->email)->first();
159
160
        return response()->json($azureIdAccounts->relatedAccounts);
0 ignored issues
show
Bug introduced by
The method json does only exist in Laravel\Lumen\Http\ResponseFactory, but not in Illuminate\Http\Response.

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...
161
    }
162
163
    /**
164
     * Check if an Username is available
165
     * for a new Avatar Account
166
     *
167
     * @param Request $request
168
     * @return JsonResponse
169
     */
170
    public function checkNewName(Request $request): JsonResponse
171
    {
172
        if (User::where('username', $request->input('name'))->count() > 0 || !$this->filterName($request->input('name')))
0 ignored issues
show
Bug introduced by
It seems like $request->input('name') targeting Illuminate\Http\Concerns...ractsWithInput::input() can also be of type array; however, App\Http\Controllers\Acc...ontroller::filterName() 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...
173
            return response()->json(['isAvailable' => false]);
0 ignored issues
show
Bug introduced by
The method json does only exist in Laravel\Lumen\Http\ResponseFactory, but not in Illuminate\Http\Response.

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...
174
175
        return response()->json(['isAvailable' => true]);
176
    }
177
178
    /**
179
     * Create a New User Avatar
180
     *
181
     * @param Request $request
182
     * @return JsonResponse
183
     */
184
    public function createAvatar(Request $request): JsonResponse
185
    {
186
        if (User::where('username', $request->json()->get('name'))->count() > 0)
187
            return response()->json(['isAvailable' => false]);
0 ignored issues
show
Bug introduced by
The method json does only exist in Laravel\Lumen\Http\ResponseFactory, but not in Illuminate\Http\Response.

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...
188
189
        $request->user()->name = $request->json()->get('name');
190
191
        $this->createUser($request, $request->user()->getAttributes());
192
193
        return response()->json('');
194
    }
195
196
    /**
197
     * Create a New User
198
     *
199
     * @param Request $request
200
     * @param array $userInfo
201
     * @param bool $newUser If is a New User
202
     * @return User
203
     */
204
    public function createUser(Request $request, array $userInfo, bool $newUser = false): User
205
    {
206
        $userName = $newUser ? uniqid(strstr($userInfo['email'], '@', true)) : $userInfo['username'];
207
        $userMail = $newUser ? $userInfo['email'] : $userInfo['mail'];
208
209
        $mailController = new MailController;
210
211
        $mailController->send(['mail' => $userMail, 'name' => $userName,
212
            'url' => "/activate/{$mailController->prepare($userMail, 'public/registration/activate')}"
213
        ]);
214
215
        $userData = new User;
216
        $userData->store($userName, $userInfo['password'], $userMail, $request->ip())->save();
217
        $userData->createData();
218
219
        Session::set(Config::get('chocolatey.security.session'), $userData);
220
221
        return $userData;
222
    }
223
224
    /**
225
     * Change Logged In User
226
     *
227
     * @param Request $request
228
     */
229
    public function selectAvatar(Request $request)
230
    {
231
        $userData = User::find($request->json()->get('uniqueId'));
232
233
        Session::set(Config::get('chocolatey.security.session'), $userData);
234
    }
235
236
    /**
237
     * Confirm E-Mail Activation
238
     *
239
     * @param Request $request
240
     * @return JsonResponse
241
     */
242
    public function confirmActivation(Request $request): JsonResponse
243
    {
244
        if (($mail = (new MailController)->getMail($request->json()->get('token'))) == null)
245
            return response()->json(['error' => 'activation.invalid_token'], 400);
0 ignored issues
show
Bug introduced by
The method json does only exist in Laravel\Lumen\Http\ResponseFactory, but not in Illuminate\Http\Response.

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...
246
247
        if (strpos($mail->link, 'change-email') !== false):
248
            $mail = str_replace('change-email/', '', $mail->link);
249
250
            User::where('mail', $mail->mail)->update(['mail' => $mail]);
251
252
            ChocolateyId::where('mail', $mail->mail)->update(['mail' => $mail]);
253
        endif;
254
255
        User::where('mail', $mail->mail)->update(['mail_verified' => '1']);
256
257
        return response()->json(['email' => $mail->mail, 'emailVerified' => true, 'identityVerified' => true]);
258
    }
259
260
    /**
261
     * Send User Forgot E-Mail
262
     *
263
     * @param Request $request
264
     * @return JsonResponse
265
     */
266
    public function forgotPassword(Request $request): JsonResponse
267
    {
268
        if (($user = User::where('mail', $request->json()->get('email'))->first()) == null)
269
            return response()->json(['email' => $request->json()->get('email')]);
0 ignored issues
show
Bug introduced by
The method json does only exist in Laravel\Lumen\Http\ResponseFactory, but not in Illuminate\Http\Response.

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...
270
271
        $mailController = new MailController;
272
273
        $mailController->send([
274
            'name' => $user->name,
275
            'mail' => $user->email,
276
            'url' => "/reset-password/{$mailController->prepare($user->email, 'public/forgotPassword')}"
277
        ], 'habbo-web-mail.password-reset');
278
279
        return response()->json(['email' => $user->email]);
280
    }
281
}
282