AccountController::updateAvatar()   A
last analyzed

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 Illuminate\Http\Request;
8
use Illuminate\Http\Response;
9
use Illuminate\Support\Facades\Validator;
10
use Illuminate\Support\Facades\Storage;
11
use Illuminate\Support\Facades\Hash;
12
use Illuminate\Validation\Rule;
13
use Auth;
14
15
class AccountController extends Controller
16
{
17
18
    public function updateAvatar(Request $request) {
19
        $isValid=$request->file('avatar')->isValid();
20
        if ($isValid) {
21
            $extension=$request->file('avatar')->extension();
22
        } else {
23
            return ResponseModel::err(1005);
24
        }
25
26
        $allow_extension=['jpg', 'png', 'jpeg', 'gif', 'bmp'];
27
        if ($isValid && in_array($extension, $allow_extension)) {
28
            $path=$request->file('avatar')->store('/static/img/avatar', 'NOJPublic');
29
30
            $user=Auth::user();
31
            $old_path=$user->avatar;
0 ignored issues
show
Bug introduced by
Accessing avatar on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
32
            if ($old_path!='/static/img/avatar/default.png' && $old_path!='/static/img/avatar/noj.png') {
33
                Storage::disk('NOJPublic')->delete($old_path);
34
            }
35
36
            $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

36
            $user->avatar='/'./** @scrutinizer ignore-type */ $path;
Loading history...
37
            $user->save();
38
39
            return ResponseModel::success(200, null, '/'.$path);
40
        } else {
41
            return ResponseModel::err(1005);
42
        }
43
    }
44
45
    public function changeBasicInfo(Request $request) {
46
        $request->validate([
47
            "username" => [
48
                "required",
49
                "string",
50
                "max:16",
51
                "min:1",
52
                Rule::unique('users', 'name')->ignore(Auth::user()->id)
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
53
            ],
54
            "describes" => "required|string|max:255"
55
        ]);
56
        $username=$request->input('username');
57
        $describes=$request->input('describes');
58
        $user=Auth::user();
59
        if (!Auth::user()->contest_account) {
0 ignored issues
show
Bug introduced by
Accessing contest_account on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
60
            $user->name=$username;
0 ignored issues
show
Bug introduced by
Accessing name on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
61
        }
62
        $user->describes=$describes;
0 ignored issues
show
Bug introduced by
Accessing describes on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
63
        $user->save();
64
        return ResponseModel::success();
65
    }
66
67
    public function changePassword(Request $request) {
68
        if (!$request->has('old_password') || !$request->has('new_password') || !$request->has('confirm_password')) {
69
            return ResponseModel::err(1003);
70
        }
71
        $old_password=$request->input('old_password');
72
        $new_password=$request->input('new_password');
73
        $confirm_password=$request->input('confirm_password');
74
        if ($new_password!=$confirm_password) {
75
            return ResponseModel::err(2004);
76
        }
77
        if (strlen($new_password)<8 || strlen($old_password)<8) {
78
            return ResponseModel::err(1006);
79
        }
80
        $user=Auth::user();
81
        if ($user->hasIndependentPassword() && !Hash::check($old_password, $user->password)) {
0 ignored issues
show
Bug introduced by
Accessing password on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
Bug introduced by
The method hasIndependentPassword() does not exist on Illuminate\Contracts\Auth\Authenticatable. It seems like you code against a sub-type of said class. However, the method does not exist in Illuminate\Auth\GenericUser. Are you sure you never get one of those? ( Ignorable by Annotation )

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

81
        if ($user->/** @scrutinizer ignore-call */ hasIndependentPassword() && !Hash::check($old_password, $user->password)) {
Loading history...
82
            return ResponseModel::err(2005);
83
        }
84
        $user->password=Hash::make($new_password);
85
        $user->save();
86
        return ResponseModel::success();
87
    }
88
89
    public function checkEmailCooldown(Request $request) {
90
        $last_send=$request->session()->get('last_email_send');
91
        if (empty($last_send) || time()-$last_send>=300) {
92
            $request->session()->put('last_email_send', time());
93
            return ResponseModel::success(200, null, 0);
94
        } else {
95
            $cooldown=300-(time()-$last_send);
96
            return ResponseModel::success(200, null, $cooldown);
97
        }
98
    }
99
100
    public function changeExtraInfo(Request $request) {
101
        $input=$request->input();
102
        $allow_change=['gender', 'contact', 'school', 'country', 'location'];
103
        foreach ($input as $key => $value) {
104
            if (!in_array($key, $allow_change)) {
105
                return ResponseModel::error(1007);
106
            }
107
        }
108
        foreach ($input as $key => $value) {
109
            if (strlen($value)!=0) {
110
                Auth::user()->setExtra($key, $value, 0);
0 ignored issues
show
Bug introduced by
The method setExtra() does not exist on Illuminate\Contracts\Auth\Authenticatable. It seems like you code against a sub-type of said class. However, the method does not exist in Illuminate\Auth\GenericUser. Are you sure you never get one of those? ( Ignorable by Annotation )

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

110
                Auth::user()->/** @scrutinizer ignore-call */ setExtra($key, $value, 0);
Loading history...
111
            } else {
112
                Auth::user()->setExtra($key, null);
113
            }
114
        }
115
        return ResponseModel::success();
116
    }
117
118
    public function saveEditorWidth(Request $request) {
119
        $input=$request->input();
120
        $allow_change=['editor_left_width'];
121
        foreach ($input as $key => $value) {
122
            if (!in_array($key, $allow_change)) {
123
                return ResponseModel::error(1007);
124
            }
125
        }
126
        foreach ($input as $key => $value) {
127
            if (strlen($value)!=0) {
128
                Auth::user()->setExtra($key, $value, 0);
129
            } else {
130
                Auth::user()->setExtra($key, null);
131
            }
132
        }
133
        return ResponseModel::success();
134
    }
135
136
    public function saveEditorTheme(Request $request) {
137
        $input=$request->input();
138
        $allow_change=['editor_theme'];
139
        foreach ($input as $key => $value) {
140
            if (!in_array($key, $allow_change)) {
141
                return ResponseModel::error(1007);
142
            }
143
        }
144
        foreach ($input as $key => $value) {
145
            if (strlen($value)!=0) {
146
                Auth::user()->setExtra($key, $value, 0);
147
            } else {
148
                Auth::user()->setExtra($key, null);
149
            }
150
        }
151
        return ResponseModel::success();
152
    }
153
}
154