Passed
Push — master ( 615c41...810217 )
by John
04:27
created

AccountController::changeBasicInfo()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 1
dl 0
loc 18
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers\Ajax;
4
5
use App\Http\Controllers\Controller;
6
use App\Models\ResponseModel;
7
use App\Models\UserModel;
8
use Illuminate\Http\Request;
9
use Illuminate\Http\Response;
10
use Illuminate\Support\Facades\Validator;
11
use Illuminate\Support\Facades\Storage;
12
use Illuminate\Support\Facades\Hash;
13
use Auth;
14
15
class AccountController extends Controller
16
{
17
    /**
18
     * The Ajax Update Avatar.
19
     *
20
     * @param Request $request web request
21
     *
22
     * @return Response
23
     */
24
    public function updateAvatar(Request $request)
25
    {
26
        $isValid=$request->file('avatar')->isValid();
27
        if ($isValid) {
28
            $extension=$request->file('avatar')->extension();
29
        } else {
30
            return ResponseModel::err(1005);
0 ignored issues
show
Bug Best Practice introduced by
The expression return App\Models\ResponseModel::err(1005) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
31
        }
32
33
        $allow_extension=['jpg', 'png', 'jpeg', 'gif', 'bmp'];
34
        if ($isValid && in_array($extension, $allow_extension)) {
35
            $path=$request->file('avatar')->store('/static/img/avatar', 'NOJPublic');
36
37
            $user=Auth::user();
38
            $old_path=$user->avatar;
0 ignored issues
show
Bug Best Practice introduced by
The property avatar does not exist on App\User. Since you implemented __get, consider adding a @property annotation.
Loading history...
39
            if ($old_path!='/static/img/avatar/default.png' && $old_path!='/static/img/avatar/noj.png') {
40
                Storage::disk('NOJPublic')->delete($old_path);
41
            }
42
43
            $user->avatar='/'.$path;
0 ignored issues
show
Bug Best Practice introduced by
The property avatar does not exist on App\User. Since you implemented __set, consider adding a @property annotation.
Loading history...
Bug introduced by
Are you sure $path of type false|string can be used in concatenation? ( Ignorable by Annotation )

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

43
            $user->avatar='/'./** @scrutinizer ignore-type */ $path;
Loading history...
44
            $user->save();
45
46
            return ResponseModel::success(200, null, '/'.$path);
0 ignored issues
show
Bug Best Practice introduced by
The expression return App\Models\Respon...200, null, '/' . $path) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
47
        } else {
48
            return ResponseModel::err(1005);
0 ignored issues
show
Bug Best Practice introduced by
The expression return App\Models\ResponseModel::err(1005) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
49
        }
50
    }
51
52
    /**
53
     * The Ajax Change users' basic info.
54
     *
55
     * @param Request $request web request
56
     *
57
     * @return Response
58
     */
59
    public function changeBasicInfo(Request $request){
60
        // if(!$request->has('username')){
61
        //     return ResponseModel::err(1003);
62
        // }
63
        // $username = $request->input('username');
64
        $describes = $request->input('describes');
65
        if(strlen($describes) > 255){
66
            return ResponseModel::err(1006);
0 ignored issues
show
Bug Best Practice introduced by
The expression return App\Models\ResponseModel::err(1006) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
67
        }
68
        // $old_username=Auth::user()->name;
69
        // if($old_username != $username && !empty(UserModel::where('name',$username)->first())){
70
        //     return ResponseModel::err(2003);
71
        // }
72
        $user=Auth::user();
73
        // $user->name = $username;
74
        $user->describes = $describes;
0 ignored issues
show
Bug Best Practice introduced by
The property describes does not exist on App\User. Since you implemented __set, consider adding a @property annotation.
Loading history...
75
        $user->save();
76
        return ResponseModel::success();
0 ignored issues
show
Bug Best Practice introduced by
The expression return App\Models\ResponseModel::success() returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
77
    }
78
79
    /**
80
     * The Ajax Change users' password.
81
     *
82
     * @param Request $request web request
83
     *
84
     * @return Response
85
     */
86
    public function changePassword(Request $request){
87
        if(!$request->has('old_password') || !$request->has('new_password') || !$request->has('confirm_password')){
88
            return ResponseModel::err(1003);
0 ignored issues
show
Bug Best Practice introduced by
The expression return App\Models\ResponseModel::err(1003) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
89
        }
90
        $old_password = $request->input('old_password');
91
        $new_password = $request->input('new_password');
92
        $confirm_password = $request->input('confirm_password');
93
        if($new_password != $confirm_password){
94
            return ResponseModel::err(2004);
0 ignored issues
show
Bug Best Practice introduced by
The expression return App\Models\ResponseModel::err(2004) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
95
        }
96
        if(strlen($new_password) < 8 || strlen($old_password) < 8){
97
            return ResponseModel::err(1006);
0 ignored issues
show
Bug Best Practice introduced by
The expression return App\Models\ResponseModel::err(1006) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
98
        }
99
        $user = Auth::user();
100
        if(!Hash::check($old_password, $user->password)){
0 ignored issues
show
Bug Best Practice introduced by
The property password does not exist on App\User. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug introduced by
It seems like $user->password can also be of type Illuminate\Database\Eloq...uent\Relations\Relation and Illuminate\Database\Eloquent\Relations\Relation; however, parameter $hashedValue of Illuminate\Support\Facades\Hash::check() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

100
        if(!Hash::check($old_password, /** @scrutinizer ignore-type */ $user->password)){
Loading history...
101
            return ResponseModel::err(2005);
0 ignored issues
show
Bug Best Practice introduced by
The expression return App\Models\ResponseModel::err(2005) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
102
        }
103
        $user->password = Hash::make($new_password);
0 ignored issues
show
Bug Best Practice introduced by
The property password does not exist on App\User. Since you implemented __set, consider adding a @property annotation.
Loading history...
104
        $user->save();
105
        return ResponseModel::success();
0 ignored issues
show
Bug Best Practice introduced by
The expression return App\Models\ResponseModel::success() returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
106
    }
107
108
    public function checkEmailCooldown(Request $request){
109
        $last_send = $request->session()->get('last_email_send');
110
        if(empty($last_send) || time() - $last_send >= 300){
111
            $request->session()->put('last_email_send',time());
112
            return ResponseModel::success(200,null,0);
113
        }else{
114
            $cooldown =  300 - (time() - $last_send);
115
            return ResponseModel::success(200,null,$cooldown);
116
        }
117
    }
118
}
119