Passed
Pull Request — master (#91)
by
unknown
04:50
created

AccountController::changeExtraInfo()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 13
nc 7
nop 1
dl 0
loc 18
rs 9.5222
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 App\Models\AccountModel;
9
use Illuminate\Http\Request;
10
use Illuminate\Http\Response;
11
use Illuminate\Support\Facades\Validator;
12
use Illuminate\Support\Facades\Storage;
13
use Illuminate\Support\Facades\Hash;
14
use Auth;
15
16
class AccountController extends Controller
17
{
18
    /**
19
     * The Ajax Update Avatar.
20
     *
21
     * @param Request $request web request
22
     *
23
     * @return Response
24
     */
25
    public function updateAvatar(Request $request)
26
    {
27
        $isValid=$request->file('avatar')->isValid();
28
        if ($isValid) {
29
            $extension=$request->file('avatar')->extension();
30
        } else {
31
            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...
32
        }
33
34
        $allow_extension=['jpg', 'png', 'jpeg', 'gif', 'bmp'];
35
        if ($isValid && in_array($extension, $allow_extension)) {
36
            $path=$request->file('avatar')->store('/static/img/avatar', 'NOJPublic');
37
38
            $user=Auth::user();
39
            $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...
40
            if ($old_path!='/static/img/avatar/default.png' && $old_path!='/static/img/avatar/noj.png') {
41
                Storage::disk('NOJPublic')->delete($old_path);
42
            }
43
44
            $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

44
            $user->avatar='/'./** @scrutinizer ignore-type */ $path;
Loading history...
45
            $user->save();
46
47
            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...
48
        } else {
49
            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...
50
        }
51
    }
52
53
    /**
54
     * The Ajax Change users' basic info.
55
     *
56
     * @param Request $request web request
57
     *
58
     * @return Response
59
     */
60
    public function changeBasicInfo(Request $request){
61
        // if(!$request->has('username')){
62
        //     return ResponseModel::err(1003);
63
        // }
64
        // $username = $request->input('username');
65
        $describes = $request->input('describes');
66
        if(strlen($describes) > 255){
67
            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...
68
        }
69
        // $old_username=Auth::user()->name;
70
        // if($old_username != $username && !empty(UserModel::where('name',$username)->first())){
71
        //     return ResponseModel::err(2003);
72
        // }
73
        $user=Auth::user();
74
        // $user->name = $username;
75
        $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...
76
        $user->save();
77
        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...
78
    }
79
80
    /**
81
     * The Ajax Change users' password.
82
     *
83
     * @param Request $request web request
84
     *
85
     * @return Response
86
     */
87
    public function changePassword(Request $request){
88
        if(!$request->has('old_password') || !$request->has('new_password') || !$request->has('confirm_password')){
89
            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...
90
        }
91
        $old_password = $request->input('old_password');
92
        $new_password = $request->input('new_password');
93
        $confirm_password = $request->input('confirm_password');
94
        if($new_password != $confirm_password){
95
            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...
96
        }
97
        if(strlen($new_password) < 8 || strlen($old_password) < 8){
98
            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...
99
        }
100
        $user = Auth::user();
101
        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

101
        if(!Hash::check($old_password, /** @scrutinizer ignore-type */ $user->password)){
Loading history...
102
            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...
103
        }
104
        $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...
105
        $user->save();
106
        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...
107
    }
108
109
    public function checkEmailCooldown(Request $request){
110
        $last_send = $request->session()->get('last_email_send');
111
        if(empty($last_send) || time() - $last_send >= 300){
112
            $request->session()->put('last_email_send',time());
113
            return ResponseModel::success(200,null,0);
114
        }else{
115
            $cooldown =  300 - (time() - $last_send);
116
            return ResponseModel::success(200,null,$cooldown);
117
        }
118
    }
119
120
    public function changeExtraInfo(Request $request){
121
        $input = $request->input();
122
        $allow_change = ['gender','contact','school','country','location'];
123
        foreach($input as $key => $value){
124
            if(!in_array($key,$allow_change)){
125
                return ResponseModel::error(1007);
126
            }
127
        }
128
        $account_model = new AccountModel();
129
        $user_id = Auth::user()->id;
130
        foreach ($input as $key => $value) {
131
            if(strlen($value) != 0){
132
                $account_model->setExtraInfo($user_id,$key,$value,0);
133
            }else{
134
                $account_model->unsetExtraInfoIfExist($user_id,$key);
135
            }
136
        }
137
        return ResponseModel::success();
138
    }
139
}
140