Completed
Push — development ( 7b263d...51bdc1 )
by Claudio
02:54
created

AccountController::uniqueName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Facades\Nux;
6
use App\Facades\Session;
7
use App\Models\ChocolateyId;
8
use App\Models\User;
9
use App\Models\UserPreferences;
10
use App\Models\UserSettings;
11
use Illuminate\Http\JsonResponse;
12
use Illuminate\Http\Request;
13
use Illuminate\Http\Response;
14
use Illuminate\Support\Facades\Config;
15
use Laravel\Lumen\Routing\Controller as BaseController;
16
17
/**
18
 * Class AccountController
19
 * @package App\Http\Controllers
20
 */
21
class AccountController extends BaseController
22
{
23
    /**
24
     * Check an User Name
25
     *
26
     * @param Request $request
27
     * @return JsonResponse
28
     */
29
    public function checkName(Request $request): JsonResponse
30
    {
31
        if (User::where('username', $request->json()->get('name'))->count() > 0 && $request->json()->get('name') != $request->user()->name)
32
            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...
33
34
        if (strlen($request->json()->get('name')) > 50 || !$this->filterName($request->json()->get('name')))
35
            return response()->json(['code' => 'INVALID_NAME', 'validationResult' => ['resultType' => 'VALIDATION_ERROR_ILLEGAL_WORDS'], 'suggestions' => []]);
36
37
        return response()->json(['code' => 'OK', 'validationResult' => null, 'suggestions' => []]);
38
    }
39
40
    /**
41
     * Filter an Username from the Invalid Names Base
42
     *
43
     * @param string $userName
44
     * @return bool
45
     */
46
    protected function filterName(string $userName): bool
47
    {
48
        return count(array_filter(Config::get('chocolatey.invalid'), function ($username) use ($userName) {
49
                return strpos($userName, $username) !== false;
50
            })) == 0;
51
    }
52
53
    /**
54
     * Select an User Name
55
     *
56
     * @param Request $request
57
     * @return JsonResponse
58
     */
59
    public function selectName(Request $request): JsonResponse
60
    {
61
        $request->user()->update(['username' => $request->json()->get('name')]);
62
63
        Session::set(Config::get('chocolatey.security.session'), $request->user());
64
65
        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...
66
    }
67
68
    /**
69
     * Save User Look
70
     *
71
     * @param Request $request
72
     * @return JsonResponse
73
     */
74
    public function saveLook(Request $request): JsonResponse
75
    {
76
        $request->user()->update([
77
            'look' => $request->json()->get('figure'),
78
            'gender' => $request->json()->get('gender')]);
79
80
        Session::set(Config::get('chocolatey.security.session'), $request->user());
81
82
        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...
83
    }
84
85
    /**
86
     * Select a Room
87
     *
88
     * @TODO: Generate the Room for the User
89
     * @TODO: Get Room Models.
90
     *
91
     * @param Request $request
92
     * @return Response
93
     */
94
    public function selectRoom(Request $request): Response
95
    {
96
        if (!in_array($request->json()->get('roomIndex'), [1, 2, 3]))
97
            return response('', 400);
98
99
        $nuxRoom = Nux::generateRoom($request);
100
101
        $request->user()->traits = $nuxRoom ? ["USER"] : ["NEW_USER", "USER"];
102
103
        return response('', $nuxRoom ? 200 : 400);
104
    }
105
106
    /**
107
     * Get User Non Read Messenger Discussions
108
     *
109
     * @TODO: Code Integration with HabboMessenger
110
     * @TODO: Create Messenger Model
111
     *
112
     * @return JsonResponse
113
     */
114
    public function getDiscussions(): JsonResponse
115
    {
116
        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...
117
    }
118
119
    /**
120
     * Get User Preferences
121
     *
122
     * @param Request $request
123
     * @return JsonResponse
124
     */
125
    public function getPreferences(Request $request): JsonResponse
126
    {
127
        $userPreferences = UserPreferences::find($request->user()->uniqueId);
128
129
        foreach ($userPreferences->getAttributes() as $attributeName => $attributeValue)
130
            $userPreferences->{$attributeName} = $attributeValue == 1;
131
132
        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...
133
    }
134
135
    /**
136
     * Save New User Preferences
137
     *
138
     * @param Request $request
139
     * @return Response
140
     */
141
    public function savePreferences(Request $request): Response
142
    {
143
        UserSettings::updateOrCreate(['user_id' => $request->user()->uniqueId], [
144
            'block_following' => $request->json()->get('friendCanFollow') == false ? '1' : '0',
145
            'block_friendrequests' => $request->json()->get('friendRequestEnabled') == false ? '1' : '0'
146
        ]);
147
148
        foreach ((array)$request->json()->all() as $setting => $value)
149
            UserPreferences::find($request->user()->uniqueId)->update([$setting => $value == true ? '1' : '0']);
150
151
        return response('');
152
    }
153
154
    /**
155
     * Get All E-Mail Accounts
156
     *
157
     * @param Request $request
158
     * @return JsonResponse
159
     */
160
    public function getAvatars(Request $request): JsonResponse
161
    {
162
        $azureIdAccounts = ChocolateyId::where('mail', $request->user()->email)->first();
163
164
        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...
165
    }
166
167
    /**
168
     * Check if an Username is available
169
     * for a new Avatar Account
170
     *
171
     * @param Request $request
172
     * @return JsonResponse
173
     */
174
    public function checkNewName(Request $request): JsonResponse
175
    {
176 View Code Duplication
        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...
Duplication introduced by
This code seems to be duplicated across 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...
177
            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...
178
179
        return response()->json(['isAvailable' => true]);
180
    }
181
182
    /**
183
     * Create a New User Avatar
184
     *
185
     * @param Request $request
186
     * @return JsonResponse
187
     */
188
    public function createAvatar(Request $request): JsonResponse
189
    {
190 View Code Duplication
        if (User::where('username', $request->json()->get('name'))->count() > 0 || !$this->filterName($request->json()->get('name')))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
191
            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...
192
193
        $request->user()->name = $request->json()->get('name');
194
195
        $this->createUser($request, $request->user()->getAttributes());
196
197
        return response()->json('');
198
    }
199
200
    /**
201
     * Create a New User
202
     *
203
     * @param Request $request
204
     * @param array $userInfo
205
     * @param bool $newUser If is a New User
206
     * @return User
207
     */
208
    public function createUser(Request $request, array $userInfo, bool $newUser = false): User
209
    {
210
        $userName = $newUser ? $this->uniqueName($userInfo['email']) : $userInfo['username'];
211
        $userMail = $newUser ? $userInfo['email'] : $userInfo['mail'];
212
213
        $mailController = new MailController;
214
215
        $mailController->send(['mail' => $userMail, 'name' => $userName, 'subject' => 'Welcome to ' . Config::get('chocolatey.name'),
216
            'url' => "/activate/{$mailController->prepare($userMail, 'public/registration/activate')}", 'sendMail' => $userMail
217
        ]);
218
219
        $userData = new User;
220
        $userData->store($userName, $userInfo['password'], $userMail, $request->ip())->save();
221
        $userData->createData();
222
223
        Session::set(Config::get('chocolatey.security.session'), $userData);
224
225
        return $userData;
226
    }
227
228
    /**
229
     * Create Random Unique Username
230
     *
231
     * @WARNING: Doesn't create Like Habbo Way
232
     *
233
     * @param string $userMail
234
     * @return string
235
     */
236
    protected function uniqueName(string $userMail): string
237
    {
238
        $partialMail = strstr($userMail, '@', true);
239
240
        $firstPart = substr(uniqid(), 0, 4);
241
        $secondPart = substr(uniqid(), 6, 10);
242
243
        return $firstPart . $partialMail . $secondPart;
244
    }
245
246
    /**
247
     * Change Logged In User
248
     *
249
     * @param Request $request
250
     */
251
    public function selectAvatar(Request $request)
252
    {
253
        $userData = User::find($request->json()->get('uniqueId'));
254
255
        Session::set(Config::get('chocolatey.security.session'), $userData);
256
    }
257
258
    /**
259
     * Confirm E-Mail Activation
260
     *
261
     * @param Request $request
262
     * @return JsonResponse
263
     */
264
    public function confirmActivation(Request $request): JsonResponse
265
    {
266 View Code Duplication
        if (($mail = (new MailController)->getMail($request->json()->get('token'))) == null)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
267
            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...
268
269
        if (strpos($mail->link, 'change-email') !== false):
270
            $mail = str_replace('change-email/', '', $mail->link);
271
272
            User::where('mail', $mail->mail)->update(['mail' => $mail]);
273
274
            ChocolateyId::where('mail', $mail->mail)->update(['mail' => $mail]);
275
        endif;
276
277
        User::where('mail', $mail->mail)->update(['mail_verified' => '1']);
278
279
        return response()->json(['email' => $mail->mail, 'emailVerified' => true, 'identityVerified' => true]);
280
    }
281
282
    /**
283
     * Send User Forgot E-Mail
284
     *
285
     * @param Request $request
286
     * @return JsonResponse
287
     */
288
    public function forgotPassword(Request $request): JsonResponse
289
    {
290
        if (($user = User::where('mail', $request->json()->get('email'))->first()) == null)
291
            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...
292
293
        $mailController = new MailController;
294
295
        $mailController->send(['name' => $user->name, 'mail' => $user->email, 'sendMail' => $user->email, 'subject' => 'Password reset confirmation',
296
            'url' => "/reset-password/{$mailController->prepare($user->email, 'public/forgotPassword')}"
297
        ], 'habbo-web-mail.password-reset');
298
299
        return response()->json(['email' => $user->email]);
300
    }
301
302
    /**
303
     * Send an Account Confirmation E-Mail
304
     *
305
     * @param Request $request
306
     * @return Response
307
     */
308
    public function verifyAccount(Request $request): Response
309
    {
310
        $mailController = new MailController;
311
312
        $mailController->send(['name' => $request->user()->name, 'mail' => $request->user()->email, 'subject' => 'Welcome to ' . Config::get('chocolatey.name'),
313
            'url' => "/activate/{$mailController->prepare($request->user()->email, 'public/registration/activate')}", 'sendMail' => $request->user()->email
314
        ]);
315
316
        return response(null);
317
    }
318
}
319