Completed
Push — dev5 ( db7ded...a14de9 )
by Ron
07:55
created

AccountController::submit()   B

Complexity

Conditions 9
Paths 1

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 18
nc 1
nop 1
dl 0
loc 31
rs 8.0555
c 0
b 0
f 0
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\Support\Facades\Log;
10
use Illuminate\Support\Facades\Auth;
11
use Illuminate\Support\Facades\Hash;
12
13
class AccountController extends Controller
14
{
15
    public function __construct()
16
    {
17
        $this->middleware('auth');
18
    }
19
    
20
    //  Index page is the change user settings form
21
    public function index()
22
    {
23
        $userData = User::find(Auth::user()->user_id);
24
        $userSett = UserSettings::where('user_id', Auth::user()->user_id)->first();
25
26
        return view('account.index', [
27
            'userData'     => $userData,
28
            'userSettings' => $userSett,
29
            'userID'       => Auth::user()->user_id
30
        ]);
31
    }
32
    
33
    //  Submit the new user settings
34
    public function submit(Request $request)
35
    {
36
        $request->validate([
37
            'username'   => 'required',
38
            'first_name' => 'required',
39
            'last_name'  => 'required',
40
            'email'      => 'required',
41
        ]);
42
        
43
        $userID = Auth::user()->user_id;
44
45
        User::find($userID)->update(
46
        [
47
            'first_name' => $request->first_name,
48
            'last_name'  => $request->last_name,
49
            'email'      => $request->email
50
        ]);
51
        
52
        UserSettings::where('user_id', $userID)->update(
53
        [
54
            'em_tech_tip'     => isset($request->em_tech_tip) && $request->em_tech_tip === 'on' ? true : false,
55
            'em_file_link'    => isset($request->em_file_link) && $request->em_file_link === 'on' ? true : false,
56
            'em_notification' => isset($request->em_notification) && $request->em_notification === 'on' ? true : false,
57
            'auto_del_link'   => isset($request->auto_del_link) && $request->auto_del_link === 'on' ? true : false,
58
        ]);
59
        
60
        Log::info('User Info Updated', ['user_id' => Auth::user()->user_id]);
61
        
62
        session()->flash('success', 'User Settings Updated');
63
        
64
        return redirect(route('account'));
65
    }
66
    
67
    //  Bring up the change password form
68
    public function changePassword()
69
    {
70
        return view('account.changePassword');
71
    }
72
    
73
    //  Submit the change password form
74
    public function submitPassword(Request $request)
75
    {
76
        //  Make sure that the old password is valid
77
        if(!(Hash::check($request->oldPass, Auth::user()->password)))
78
        {
79
            return redirect()->back()->with('error', 'Your Current Password is not valid.  Please try again.');
80
        }
81
        
82
        //  Make sure that the new password is not the same as the old password
83
        if(strcmp($request->newPass, $request->oldPass) == 0)
84
        {
85
            return redirect()->back()->with('error', 'New Password cannot be the same as the old password');
86
        }
87
        
88
        //  Validate remaining data
89
        $request->validate([
90
            'oldPass' => 'required',
91
            'newPass' => 'required|string|min:6|confirmed'
92
        ]);
93
        
94
        $newExpire = config('users.passExpires') != null ? Carbon::now()->addDays(config('users.passExpires')) : null;
95
        
96
        //  Change the password
97
        $user = Auth::user(); 
98
        $user->password = bcrypt($request->newPass);
99
        $user->password_expires = $newExpire;
100
        $user->save();
101
        
102
        Log::info('User Changed Password', ['user_id' => Auth::user()->user_id]);
103
        
104
        return redirect()->back()->with('success', 'Password Changed Successfully');
105
    }
106
}
107