Test Failed
Push — dev5 ( 054b32...4c0df5 )
by Ron
12:54
created

AccountController::submit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 17
c 2
b 0
f 0
nc 1
nop 1
dl 0
loc 26
ccs 0
cts 17
cp 0
crap 2
rs 9.7
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\User;
6
use Carbon\Carbon;
7
use App\UserSettings;
8
use Illuminate\Http\Request;
9
use Illuminate\Validation\Rule;
10
use App\Rules\ValidatePassword;
11
use Illuminate\Support\Facades\Log;
12
use Illuminate\Support\Facades\Auth;
13
use Illuminate\Support\Facades\Hash;
14
use Illuminate\Support\Facades\Route;
15
16
class AccountController extends Controller
17
{
18 26
    public function __construct()
19
    {
20 26
        $this->middleware('auth');
21 26
    }
22
23
    //  Index page is the change user settings form
24
    public function index()
25
    {
26
        $userData = User::find(Auth::user()->user_id);
27
        $userSett = UserSettings::where('user_id', Auth::user()->user_id)->first();
28
29
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
30
        return view('account.index', [
31
            'userData'     => $userData,
32
            'userSettings' => $userSett,
33
            'userID'       => Auth::user()->user_id
34
        ]);
35
    }
36
37
    //  Submit the new user settings
38
    public function submit(Request $request)
39
    {
40
        $request->validate([
41
            'username'   => 'required',
42
            'first_name' => 'required',
43
            'last_name'  => 'required',
44
            'email'      => [
45
                'required',
46
                Rule::unique('users')->ignore(Auth::user())
47
            ],
48
        ]);
49
50
        $userID = Auth::user()->user_id;
51
        User::find($userID)->update(
52
        [
53
            'first_name' => $request->first_name,
54
            'last_name'  => $request->last_name,
55
            'email'      => $request->email
56
        ]);
57
58
        session()->flash('success', 'User Settings Updated');
59
60
        Log::notice('User Settings Updated', ['user_id' => Auth::user()->user_id]);
61
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
62
        Log::debug('Submitted Data - ', $request->toArray());
63
        return redirect(route('account'));
64
    }
65
66
    //  Submit the user notification settings
67
    public function notifications(Request $request)
68
    {
69
        UserSettings::where('user_id', Auth::user()->user_id)->update(
70
        [
71
            'em_tech_tip'     => $request->em_tech_tip === 'on' ? true : false,
72
            'em_file_link'    => $request->em_file_link === 'on' ? true : false,
73
            'em_notification' => $request->em_notification === 'on' ? true : false,
74
            'auto_del_link'   => $request->auto_del_link === 'on' ? true : false,
75
        ]);
76
77
        session()->flash('success', 'User Notifications Updated');
78
79
        Log::notice('User Notifications Updated', ['user_id' => Auth::user()->user_id]);
80
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
81
        Log::debug('Submitted Data - ', $request->toArray());
82
        return redirect(route('account'));
83
    }
84
85
    //  Bring up the change password form
86
    public function changePassword()
87
    {
88
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
89
        return view('account.changePassword');
90
    }
91
92
    //  Submit the change password form
93
    public function submitPassword(Request $request)
94
    {
95
        //  Validate form data
96
        $request->validate([
97
            'oldPass' => ['required', new ValidatePassword],
98
            'newPass' => 'required|string|min:6|confirmed|different:oldPass'
99
        ]);
100
101
        //  Determine if there is a new password expire's date
102
        $newExpire = config('auth.passwords.settings.expire') != null ? Carbon::now()->addDays(config('auth.passwords.settings.expire')) : null;
103
104
        //  Change the password
105
        $user = Auth::user();
106
        $user->password = bcrypt($request->newPass);
107
        $user->password_expires = $newExpire;
108
        $user->save();
109
110
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
111
        Log::info('User Changed Password', ['user_id' => Auth::user()->user_id]);
112
113
        return redirect(route('account'))->with('success', 'Password Changed Successfully');
114
    }
115
}
116