Passed
Push — dev5 ( 90e844...bb8d10 )
by Ron
08:27
created

SettingsController::customizeSystem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers\Installer;
4
5
use App\User;
6
use App\Settings;
7
use Carbon\Carbon;
8
use App\Mail\TestEmail;
9
use Illuminate\Http\Request;
10
use Illuminate\Support\Facades\Log;
11
use Illuminate\Support\Facades\Auth;
12
use Illuminate\Support\Facades\Mail;
13
use App\Http\Controllers\Controller;
14
use Illuminate\Support\Facades\Config;
15
16
class SettingsController extends Controller
17
{
18
    public function __construct()
19
    {
20
        $this->middleware('auth');
21
    }
22
    
23
    //  Bring up the user security settings form
24
    public function userSecurity()
25
    {
26
        $passExpire = config('users.passExpires') != null ? config('users.passExpires') : 0;
27
        
28
        return view('installer.userSecurity', [
29
            'passExpire' => $passExpire
30
        ]);
31
    }
32
    
33
    //  Submit the user security settings form
34
    public function submitUserSecurity(Request $request)
35
    {
36
        $request->validate([
37
            'passExpire' => 'required|numeric'
38
        ]);
39
        
40
        //  Determine if the password expires field is updated
41
        $oldExpire = config('users.passExpires');
42
        if($request->passExpire != $oldExpire)
43
        {
44
            //  Update the setting in the database
45
            Settings::where('key', 'users.passExpires')->update([
46
                'value' => $request->passExpire
47
            ]);
48
            //  If the setting is changing from never to xx days, update all users
49
            if($request->passExpire == 0)
50
            {
51
                User::whereNotNull('password_expires')->update([
52
                    'password_expires' => null
53
                ]);
54
            }
55
            else
56
            {
57
                $newExpire = Carbon::now()->addDays($request->passExpire);
58
                User::whereNull('password_expires')->update([
59
                    'password_expires' => $newExpire
60
                ]);
61
            }
62
        }
63
        
64
        Log::info('User Settings have been changed by User ID-'.Auth::user()->user_id);
65
        return redirect()->back()->with('success', 'User Security Updated');
66
    }
67
    
68
    //  Timezone and Logo forms
69
    public function customizeSystem()
70
    {
71
        return view('installer.customize');
72
    }
73
    
74
    //  Submit the timezone  form
75
    public function submitCustomizeSystem(Request $request)
76
    {
77
        $request->validate([
78
            'timezone' => 'required'
79
        ]);
80
        
81
        Settings::where('key', 'app.timezone')->update([
82
            'value' => $request->timezone
83
        ]);
84
        
85
        Log::info('Tech Bench Settings Updated', ['user_id' => Auth::user()->user_id]);
86
        
87
        return redirect()->back()->with('success', 'Timezone Successfully Updated');
88
    }
89
    
90
    //  Submit the new company logo
91
    public function submitLogo(Request $request)
92
    {
93
        $file     = $request->file;
94
        $fileName = $file->getClientOriginalName();
95
        $file->storeAs('img', $fileName, 'public');
96
        
97
        Settings::where('key', 'app.logo')->update([
98
            'value' => '/storage/img/'.$fileName
99
        ]);
100
        
101
        Log::info('A new company logo has been uploaded by User ID-'.Auth::user()->user_id);
102
        
103
        return response()->json(['url' => '/storage/img/'.$fileName]);
104
    }
105
    
106
    //  Email Settings form
107
    public function emailSettings()
108
    {
109
        return view('installer.emailSettings');
110
    }
111
    
112
    //  Send a test email
113
    public function sendTestEmail(Request $request)
114
    {
115
        
116
//        echo $request->host;
117
//        echo ' ';
118
//        die();
119
        Log::info(config('mail.host'));
120
        
121
        //  Make sure that all of the information properly validates
122
        $request->validate([
123
            'host'       => 'required',
124
            'port'       => 'required|numeric',
125
            'encryption' => 'required',
126
            'username'   => 'required'
127
        ]);
128
        
129
        //  Temporarily set the email settings
130
//        config([
131
//            'mail.host'       => $request->host,
132
//            'mail.port'       => $request->port,
133
//            'mail.encryption' => $request->encryption,
134
//            'mail.username'   => $request->username,
135
//        ]);
136
        
137
        Config::set('mail.host', $request->host);
138
        
139
        
140
//        
141
//        echo config('mail.host');
142
//        die();
143
        
144
        if(!empty($request->password))
145
        {
146
            config(['mail.password' => $request->password]);
147
        }
148
        
149
        //  Try and send the test email
150
        try
151
        {
152
//            Log::info('Test Email Successfully Sent to '.Auth::user()->email);
153
            Log::info(config('mail.host'));
154
            Mail::to(Auth::user()->email)->send(new TestEmail());
155
//            return 'success';
156
            return response()->json([
157
                'success' => true,
158
                'sentTo'  => Auth::user()->email
159
            ]);
160
        }
161
        catch(Exception $e)
0 ignored issues
show
Bug introduced by
The type App\Http\Controllers\Installer\Exception was not found. Did you mean Exception? If so, make sure to prefix the type with \.
Loading history...
162
        {
163
            Log::notice('Test Email Failed.  Message: '.$e);
164
            $msg = '['.$e->getCode().'] "'.$e->getMessage().'" on line '.$e->getTrace()[0]['line'].' of file '.$e->getTrace()[0]['file'];
165
//            return $msg;
166
            return response()->json(['message' => $msg]);
167
        }
168
    }
169
    
170
    //  Submit the test email form
171
    public function submitEmailSettings(Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

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

171
    public function submitEmailSettings(/** @scrutinizer ignore-unused */ Request $request)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
172
    {
173
        return response('submitted');
174
    }
175
}
176