PasswordResetController::showReset()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 16
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers\Api\V1\Auth;
4
5
use App\User;
6
use App\PasswordResetApi;
7
use App\Http\Controllers\Controller;
8
use App\Notifications\PasswordResetRequest;
9
use App\Notifications\PasswordResetSuccess;
10
use Carbon\Carbon;
11
use Illuminate\Http\Request;
12
13
class PasswordResetController extends Controller
14
{
15
	/**
16
     * Create token password reset
17
     *
18
     * @param  [string] email
0 ignored issues
show
Bug introduced by
The type App\Http\Controllers\Api\V1\Auth\email was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
     * @return [string] message
0 ignored issues
show
Documentation Bug introduced by
The doc comment [string] at position 0 could not be parsed: Unknown type name '[' at position 0 in [string].
Loading history...
20
     */
21
    public function forget(Request $request)
22
    {
23
    	$rules = [
24
        	'email' => 'required|string|email',
25
    	];
26
27
    	$validator = \Validator::make($request->all(), $rules);
28
29
	    if ($validator->fails()) {
30
	       return response()->json(['status' => 'error', 'errors' => $validator->errors()], 422);
31
	    }
32
33
		$user = User::where('email', $request->email)->first();
34
35
        if (!$user) {
36
        	return response()->json(['status' => 'error', 'message' => 'Não encontramos nenhum usuário com esse endereço de e-mail.']);
37
        }
38
39
        $passwordReset = PasswordResetApi::updateOrCreate([
40
        	'email' => $user->email
41
        ],[
42
        	'email' => $user->email,
43
        	'token' => str_random(60)
44
        ]);
45
46
        if ($user && $passwordReset) {
0 ignored issues
show
introduced by
$passwordReset is of type App\PasswordResetApi, thus it always evaluated to true.
Loading history...
47
        	$user->notify(new PasswordResetRequest($passwordReset->token));
48
        }
49
50
        return response()->json(['status' => 'success', 'message' => 'O link para redefinição de senha foi enviado para o seu e-mail!']);
51
    }
52
53
    /**
54
     * Find token password reset
55
     *
56
     * @param  [string] $token
0 ignored issues
show
Documentation Bug introduced by
The doc comment [string] at position 0 could not be parsed: Unknown type name '[' at position 0 in [string].
Loading history...
57
     * @return [string] message
0 ignored issues
show
Documentation Bug introduced by
The doc comment [string] at position 0 could not be parsed: Unknown type name '[' at position 0 in [string].
Loading history...
58
     * @return [json] passwordReset object
59
     */
60
    public function showReset($token)
61
    {
62
    	$passwordReset = PasswordResetApi::where('token', $token)->first();
63
64
        if (!$passwordReset) {
65
        	return response()->json(['status' => 'error', 'message' => 'O token para recuperação de senha é inválido.']);
66
        }
67
68
        if (Carbon::parse($passwordReset->updated_at)->addMinutes(720)->isPast()) {
69
            
70
            $passwordReset->delete();
71
72
            return response()->json(['status' => 'error', 'message' => 'O token para recuperação de senha é inválido.']);
73
        }
74
75
        return response()->json(['status' => 'success', 'data' => $passwordReset]);
76
    }
77
78
    /**
79
     * Reset password
80
     *
81
     * @param  [string] email
82
     * @param  [string] password
83
     * @param  [string] password_confirmation
84
     * @param  [string] token
0 ignored issues
show
Bug introduced by
The type App\Http\Controllers\Api\V1\Auth\token was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
85
     * @return [string] message
0 ignored issues
show
Documentation Bug introduced by
The doc comment [string] at position 0 could not be parsed: Unknown type name '[' at position 0 in [string].
Loading history...
86
     * @return [json] user object
87
     */
88
    public function reset(Request $request)
89
    {
90
    	$rules = [
91
        	'email' => 'required|string|email',
92
            'password' => 'required|string|confirmed',
93
            'token' => 'required|string'
94
    	];
95
96
    	$validator = \Validator::make($request->all(), $rules);
97
98
	    if ($validator->fails()) {
99
	       return response()->json(['status' => 'error', 'errors' => $validator->errors()], 422);
100
	    }
101
102
        $passwordReset = PasswordResetApi::where([
103
            ['token', $request->token],
104
            ['email', $request->email]
105
        ])->first();
106
107
        if (!$passwordReset) {
108
        	return response()->json(['status' => 'error', 'message' => 'O token para recuperação de senha é inválido.']);
109
        }
110
111
        $user = User::where('email', $passwordReset->email)->first();
112
113
        if (!$user) {
114
        	return response()->json(['status' => 'error', 'message' => 'Não podemos encontrar um usuário com esse endereço de e-mail.']);
115
        }
116
117
        $user->password = bcrypt($request->password);
118
        $user->save();
119
120
        $passwordReset->delete();
121
122
        $user->notify(new PasswordResetSuccess($passwordReset));
0 ignored issues
show
Unused Code introduced by
The call to App\Notifications\Passwo...tSuccess::__construct() has too many arguments starting with $passwordReset. ( Ignorable by Annotation )

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

122
        $user->notify(/** @scrutinizer ignore-call */ new PasswordResetSuccess($passwordReset));

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...
123
124
        return response()->json(['status' => 'success', 'message' => 'Sua senha foi redefinida com sucesso. Agora você já pode realizar o login com a nova senha!']);
125
    }
126
}
127