Completed
Push — development ( cb1ff2...fb9a62 )
by Claudio
02:25
created

AccountSecurityController::changeMail()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 28
Code Lines 20

Duplication

Lines 4
Ratio 14.29 %

Importance

Changes 0
Metric Value
dl 4
loc 28
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 20
nc 4
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\Mail;
8
use App\Models\TrustedDevice;
9
use App\Models\User;
10
use App\Models\UserSecurity;
11
use Illuminate\Http\JsonResponse;
12
use Illuminate\Http\Request;
13
use Illuminate\Http\Response;
14
use Illuminate\Support\Facades\DB;
15
use Laravel\Lumen\Routing\Controller as BaseController;
16
use stdClass;
17
18
/**
19
 * Class AccountSecurityController
20
 * @package App\Http\Controllers
21
 */
22
class AccountSecurityController extends BaseController
23
{
24
    /**
25
     * Check if Feature Status is Enabled
26
     *
27
     * @param Request $request
28
     * @return Response
29
     */
30
    public function featureStatus(Request $request): Response
31
    {
32
        if ($request->user()->emailVerified == false)
33
            return response('identity_verification_required', 200);
34
35
        $featureEnabled = UserSecurity::find($request->user()->uniqueId);
36
37
        return response($featureEnabled !== null ? 'enabled' : 'disabled', 200);
38
    }
39
40
    /**
41
     * Save Security Questions
42
     *
43
     * @param Request $request
44
     * @return JsonResponse
45
     */
46
    public function saveQuestions(Request $request): JsonResponse
47
    {
48 View Code Duplication
        if (User::where('password', hash('sha256', $request->json()->get('password')))->count() == 0)
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...
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
        UserSecurity::updateOrCreate([
52
            'user_id' => $request->user()->uniqueId,
53
            'firstQuestion' => $request->json()->get('questionId1'),
54
            'secondQuestion' => $request->json()->get('questionId2'),
55
            'firstAnswer' => $request->json()->get('answer1'),
56
            'secondAnswer' => $request->json()->get('answer2')]);
57
58
        return response()->json('', 204);
59
    }
60
61
    /**
62
     * Disable Safety Lock
63
     *
64
     * @param Request $request
65
     * @return JsonResponse
66
     */
67
    public function disable(Request $request): JsonResponse
68
    {
69
        UserSecurity::find($request->user()->uniqueId)->delete();
70
71
        return response()->json('', 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
     * @param Request $request
78
     * @return JsonResponse
79
     */
80
    public function reset(Request $request): JsonResponse
81
    {
82
        TrustedDevice::find($request->user()->uniqueId)->delete();
83
84
        return response()->json('', 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...
85
    }
86
87
    /**
88
     * Change User Password
89
     *
90
     * @TODO: Implement Notification E-mail of Password change
91
     *
92
     * @param Request $request
93
     * @return JsonResponse
94
     */
95
    public function changePassword(Request $request): JsonResponse
96
    {
97
        if (strlen($request->json()->get('password')) < 6)
98
            return response()->json(['error' => 'password.current_password.invalid'], 409);
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...
99
100
        //@TODO: This search the whole base. If anyone has the same password.. This will give error. Is this good?
101 View Code Duplication
        if (User::where('password', hash('sha256', $request->json()->get('currentPassword')))->count() >= 1)
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...
102
            return response()->json(['error' => 'password.used_earlier'], 409);
103
104
        User::find($request->user()->uniqueId)->update(['password' => hash('sha256', $request->json()->get('password'))]);
105
106
        return response()->json('', 204);
107
    }
108
109
    /**
110
     * Change User E-mail
111
     *
112
     * @TODO: Implement Notification of E-mail Change
113
     * @TODO: Implement Confirmation of E-mail Change
114
     *
115
     * @param Request $request
116
     * @return JsonResponse
117
     */
118
    public function changeMail(Request $request): JsonResponse
119
    {
120 View Code Duplication
        if (User::where('password', hash('sha256', $request->json()->get('currentPassword')))->count() == 0)
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...
121
            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...
122
123 View Code Duplication
        if (strpos($request->json()->get('newEmail'), '@') == false)
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing strpos($request->json()->get('newEmail'), '@') of type integer to the boolean false. If you are specifically checking for 0, consider using something more explicit like === 0 instead.
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...
124
            return response()->json(['error' => 'registration_email'], 400);
125
126
        if (ChocolateyId::where('mail', $request->json()->get('newEmail'))->count() > 0)
127
            return response()->json(['error' => 'changeEmail.email_already_in_use'], 400);
128
129
        $mailController = new MailController;
130
131
        $mailController->send([
132
            'mail' => $request->user()->email,
133
            'newMail' => $request->json()->get('newEmail'),
134
            'name' => $request->user()->name],
135
            'habbo-web-mail.mail-change-alert');
136
137
        $mailController->send([
138
            'mail' => $request->json()->get('newEmail'),
139
            'name' => $request->user()->name,
140
            'url' => "/activate/{$mailController
141
            ->prepare($request->user()->email, "change-email/{$request->json()->get('newEmail')}")}"
142
        ], 'habbo-web-mail.confirm-mail-change');
143
144
        return response()->json(['email' => $request->json()->get('newEmail')], 200);
145
    }
146
147
    /**
148
     * Get User Security Questions
149
     *
150
     * @param Request $request
151
     * @return JsonResponse
152
     */
153
    public function getQuestions(Request $request): JsonResponse
154
    {
155
        if (UserSecurity::find($request->user()->uniqueId) == null)
156
            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...
157
158
        $userSecurity = UserSecurity::find($request->user()->uniqueId);
159
160
        $firstQuestion = new stdClass();
161
        $firstQuestion->questionId = $userSecurity->firstQuestion;
162
        $firstQuestion->questionKey = "IDENTITY_SAFETYQUESTION_{$userSecurity->firstQuestion}";
163
164
        $secondQuestion = new stdClass();
165
        $secondQuestion->questionId = $userSecurity->secondQuestion;
166
        $secondQuestion->questionKey = "IDENTITY_SAFETYQUESTION_{$userSecurity->secondQuestion}";
167
168
        return response()->json([$firstQuestion, $secondQuestion]);
169
    }
170
171
    /**
172
     * Verify User Security Questions
173
     *
174
     * @param Request $request
175
     * @return JsonResponse
176
     */
177
    public function verifyQuestions(Request $request): JsonResponse
178
    {
179
        if (UserSecurity::where('user_id', $request->user()->uniqueId)
180
                ->where('firstAnswer', $request->json()->get('answer1'))
181
                ->where('secondAnswer', $request->json()->get('answer2'))->count() > 0
182
        ):
183
            if ($request->json()->get('trust') == true)
184
                (new TrustedDevice)->store($request->user()->uniqueId, $request->ip())->save();
185
186
            return response()->json('', 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...
187
        endif;
188
189
        return response()->json('', 409);
190
    }
191
192
    /**
193
     * Confirm User Change Password
194
     *
195
     * @param Request $request
196
     * @return mixed
197
     */
198
    public function confirmChangePassword(Request $request): JsonResponse
199
    {
200
        $mailRequest = Mail::where('token', $request->json()->get('token'))->where('used', '0')->first();
201
202
        if ($mailRequest == null)
203
            return response()->json('', 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...
204
205 View Code Duplication
        if (User::where('password', hash('sha256', $request->json()->get('password')))->count() >= 1)
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...
206
            return response()->json(['error' => 'password.used_earlier'], 400);
207
208
        $mailRequest->update(['used' => '1']);
209
210
        DB::table('users')->where('mail', $mailRequest->mail)
211
            ->update(['password' => hash('sha256', $request->json()->get('password'))]);
212
213
        return response()->json('');
214
    }
215
}
216