Passed
Pull Request — master (#101)
by
unknown
05:12
created

AccountController::updateAvatar()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 17
nc 4
nop 1
dl 0
loc 24
rs 9.0777
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
    public function updateAvatar(Request $request){
20
        $isValid=$request->file('avatar')->isValid();
21
        if ($isValid) {
22
            $extension=$request->file('avatar')->extension();
23
        } else {
24
            return ResponseModel::err(1005);
25
        }
26
27
        $allow_extension=['jpg', 'png', 'jpeg', 'gif', 'bmp'];
28
        if ($isValid && in_array($extension, $allow_extension)) {
29
            $path=$request->file('avatar')->store('/static/img/avatar', 'NOJPublic');
30
31
            $user=Auth::user();
32
            $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...
33
            if ($old_path!='/static/img/avatar/default.png' && $old_path!='/static/img/avatar/noj.png') {
34
                Storage::disk('NOJPublic')->delete($old_path);
35
            }
36
37
            $user->avatar='/'.$path;
0 ignored issues
show
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

37
            $user->avatar='/'./** @scrutinizer ignore-type */ $path;
Loading history...
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...
38
            $user->save();
39
40
            return ResponseModel::success(200, null, '/'.$path);
41
        } else {
42
            return ResponseModel::err(1005);
43
        }
44
    }
45
46
    public function changeBasicInfo(Request $request){
47
        // if(!$request->has('username')){
48
        //     return ResponseModel::err(1003);
49
        // }
50
        // $username = $request->input('username');
51
        $describes = $request->input('describes');
52
        if(strlen($describes) > 255){
53
            return ResponseModel::err(1006);
54
        }
55
        // $old_username=Auth::user()->name;
56
        // if($old_username != $username && !empty(UserModel::where('name',$username)->first())){
57
        //     return ResponseModel::err(2003);
58
        // }
59
        $user=Auth::user();
60
        // $user->name = $username;
61
        $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...
62
        $user->save();
63
        return ResponseModel::success();
64
    }
65
66
    public function changePassword(Request $request){
67
        if(!$request->has('old_password') || !$request->has('new_password') || !$request->has('confirm_password')){
68
            return ResponseModel::err(1003);
69
        }
70
        $old_password = $request->input('old_password');
71
        $new_password = $request->input('new_password');
72
        $confirm_password = $request->input('confirm_password');
73
        if($new_password != $confirm_password){
74
            return ResponseModel::err(2004);
75
        }
76
        if(strlen($new_password) < 8 || strlen($old_password) < 8){
77
            return ResponseModel::err(1006);
78
        }
79
        $user = Auth::user();
80
        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

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