AccountSecurityController::verifyQuestions()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 3
nop 1
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Facades\Mail;
6
use App\Facades\User as UserFacade;
7
use App\Models\ChocolateyId;
8
use App\Models\Question;
9
use App\Models\TrustedDevice;
10
use App\Models\User;
11
use App\Models\UserSecurity;
12
use Illuminate\Http\JsonResponse;
13
use Illuminate\Http\Request;
14
use Illuminate\Http\Response;
15
use Illuminate\Support\Facades\Config;
16
use Laravel\Lumen\Routing\Controller as BaseController;
17
18
/**
19
 * Class AccountSecurityController.
20
 */
21
class AccountSecurityController extends BaseController
22
{
23
    /**
24
     * Check if Feature Status is Enabled.
25
     *
26
     * @return Response
27
     */
28
    public function featureStatus(): Response
29
    {
30
        if (UserFacade::getUser()->emailVerified == false) {
31
            return response('identity_verification_required', 200);
32
        }
33
34
        $featureEnabled = UserSecurity::find(UserFacade::getUser()->uniqueId);
35
36
        return response($featureEnabled !== null ? 'enabled' : 'disabled', 200);
37
    }
38
39
    /**
40
     * Save Security Questions.
41
     *
42
     * @param Request $request
43
     *
44
     * @return JsonResponse
45
     */
46
    public function saveQuestions(Request $request): JsonResponse
47
    {
48
        if (UserFacade::getUser()->getChocolateyId()->password != hash(Config::get('chocolatey.security.hash'), $request->json()->get('password'))) {
49
            return response()->json(['error' => 'invalid_password'], 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...
50
        }
51
52
        UserSecurity::updateOrCreate([
53
            'user_id'        => UserFacade::getUser()->uniqueId,
54
            'firstQuestion'  => $request->json()->get('questionId1'),
55
            'secondQuestion' => $request->json()->get('questionId2'),
56
            'firstAnswer'    => $request->json()->get('answer1'),
57
            'secondAnswer'   => $request->json()->get('answer2'), ]);
58
59
        return response()->json(null, 204);
60
    }
61
62
    /**
63
     * Disable Safety Lock.
64
     *
65
     * @return JsonResponse
66
     */
67
    public function disable(): JsonResponse
68
    {
69
        UserSecurity::find(UserFacade::getUser()->uniqueId)->delete();
70
71
        return response()->json(null, 204);
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...
72
    }
73
74
    /**
75
     * Reset Trusted Devices.
76
     *
77
     * @return JsonResponse
78
     */
79
    public function reset(): JsonResponse
80
    {
81
        TrustedDevice::find(UserFacade::getUser()->uniqueId)->delete();
82
83
        return response()->json(null, 204);
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...
84
    }
85
86
    /**
87
     * Change User Password.
88
     *
89
     * @TODO: Implement Notification E-mail of Password change
90
     *
91
     * @param Request $request
92
     *
93
     * @return JsonResponse
94
     */
95
    public function changePassword(Request $request): JsonResponse
96
    {
97
        UserFacade::getUser()->getChocolateyId()->update(['password' => hash(Config::get('chocolatey.security.hash'),
98
            $request->json()->get('password'))]);
99
100
        return response()->json(null, 204);
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...
101
    }
102
103
    /**
104
     * Confirm E-Mail Activation.
105
     *
106
     * @param Request $request
107
     *
108
     * @return JsonResponse
109
     */
110
    public function confirmActivation(Request $request): JsonResponse
111
    {
112 View Code Duplication
        if (Mail::get($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...
113
            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...
114
        }
115
116
        ChocolateyId::find(Mail::get()->mail)->update(['mail_verified' => '1']);
117
118
        if (strpos(Mail::get()->link, 'change-email') !== false) {
119
            $email = str_replace('change-email/', '', Mail::get()->link);
120
121
            User::where('mail', Mail::get()->mail)->update(['mail' => $email]);
122
123
            ChocolateyId::find(Mail::get()->mail)->update(['mail' => $email]);
124
        }
125
126
        return response()->json(['email' => Mail::get()->mail, 'emailVerified' => true, 'identityVerified' => true]);
127
    }
128
129
    /**
130
     * Change User E-mail.
131
     *
132
     * @param Request $request
133
     *
134
     * @return JsonResponse
135
     */
136
    public function changeMail(Request $request): JsonResponse
137
    {
138
        if (User::where('password', hash(Config::get('chocolatey.security.hash'), $request->json()->get('currentPassword')))->count() == 0) {
139
            return response()->json(['error' => 'changeEmail.invalid_password'], 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...
140
        }
141
142
        if (ChocolateyId::where('mail', $request->json()->get('newEmail'))->count() > 0) {
143
            return response()->json(['error' => 'changeEmail.email_already_in_use'], 400);
144
        }
145
146
        $this->sendChangeMailConfirmation($request);
147
148
        return response()->json(['email' => $request->json()->get('newEmail')], 200);
149
    }
150
151
    /**
152
     * Send the E-Mail confirmation.
153
     *
154
     * @param Request $request
155
     */
156
    protected function sendChangeMailConfirmation(Request $request)
157
    {
158
        Mail::send(['email' => UserFacade::getUser()->email,
159
            'name'          => UserFacade::getUser()->name, 'subject' => 'Email change alert',
160
        ], 'habbo-web-mail.mail-change-alert');
161
162
        $generatedToken = Mail::store(UserFacade::getUser()->email,
163
            "change-email/{$request->json()->get('newEmail')}");
164
165
        Mail::send(['email' => $request->json()->get('newEmail'), 'name' => UserFacade::getUser()->name,
166
            'subject'       => 'Email change confirmation', 'url' => "/activate/{$generatedToken}",
167
        ], 'habbo-web-mail.confirm-mail-change');
168
    }
169
170
    /**
171
     * Get User Security Questions.
172
     *
173
     * @return JsonResponse
174
     */
175
    public function getQuestions(): JsonResponse
176
    {
177
        if (UserSecurity::find(UserFacade::getUser()->uniqueId) == null) {
178
            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...
179
        }
180
181
        $userSecurity = UserSecurity::find(UserFacade::getUser()->uniqueId);
182
183
        return response()->json([
184
            new Question($userSecurity->firstQuestion,
185
                "IDENTITY_SAFETYQUESTION_{$userSecurity->firstQuestion}"),
186
            new Question($userSecurity->secondQuestion,
187
                "IDENTITY_SAFETYQUESTION_{$userSecurity->secondQuestion}"),
188
        ]);
189
    }
190
191
    /**
192
     * Verify User Security Questions.
193
     *
194
     * @param Request $request
195
     *
196
     * @return JsonResponse
197
     */
198
    public function verifyQuestions(Request $request): JsonResponse
199
    {
200
        $questions = UserSecurity::find(UserFacade::getUser()->uniqueId);
201
202
        if ($questions->firstAnswer == $request->json()->get('answer1') && $questions->secondAnswer == $request->json()->get('answer2')) {
203
            if ($request->json()->get('trust') == true) {
204
                (new TrustedDevice())->store(UserFacade::getUser()->uniqueId, $request->ip());
205
            }
206
207
            return response()->json(null, 204);
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...
208
        }
209
210
        return response()->json(null, 409);
211
    }
212
213
    /**
214
     * Confirm User Change Password.
215
     *
216
     * @param Request $request
217
     *
218
     * @return mixed
219
     */
220
    public function confirmChangePassword(Request $request): JsonResponse
221
    {
222 View Code Duplication
        if (Mail::get($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...
223
            return response()->json(null, 404);
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...
224
        }
225
226
        ChocolateyId::find(Mail::get()->mail)->update(['password' => hash(Config::get('chocolatey.security.hash'), $request->json()->get('password'))]);
227
228
        return response()->json(null);
229
    }
230
}
231