Completed
Branch dev4 (2f299a)
by Ron
08:25
created

InstallerController::submitCat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use Exception;
6
use Carbon\Carbon;
7
use Illuminate\Http\Request;
8
use Illuminate\Support\Facades\Auth;
9
use Illuminate\Support\Facades\Log;
10
use Illuminate\Support\Facades\Mail;
11
use Illuminate\Support\Facades\Storage;
12
use App\Settings;
13
use App\User;
14
use App\Mail\TestEmail;
15
16
class InstallerController extends Controller
17
{
18
    public function __construct()
19
    {
20
        $this->middleware('auth');
21
    }
22
    
23
    //  Home page for Installer Functions
24
    public function index()
25
    {
26
        //  Get the list of system categories
27
        $cats = SystemCategories::all();
28
        $sysArr = [];
29
        //  Populate that list with the matching systems
30
        foreach($cats as $cat)
31
        {
32
            $systems = SystemTypes::where('cat_id', $cat->cat_id)->get();
33
            if(!$systems->isEmpty())
34
            {
35
                foreach($systems as $sys)
36
                {
37
                    $sysArr[$cat->name][] = $sys->name;
38
                }
39
            }
40
            else
41
            {
42
                $sysArr[$cat->name] = null;
43
            }
44
        }
45
        
46
        return view('installer.index', [
47
            'sysArr' => $sysArr
48
        ]); 
49
    }
50
    
51
    //  Server customization form
52
    public function customizeSystem()
53
    {
54
        return view('installer.form.customize');
55
    }
56
    
57
    //  Submit the server customization form
58
    public function submitCustom(Request $request)
59
    {
60
        $request->validate([
61
            'timezone' => 'required'
62
        ]);
63
        
64
        Settings::where('key', 'app.timezone')->update([
65
            'value' => $request->timezone
66
        ]);
67
        
68
        Log::info('Tech Bench Settings Updated', ['user_id' => Auth::user()->user_id]);
69
        
70
        return redirect()->back()->with('success', 'Tech Bench Successfully Updated');//
71
    }
72
    
73
    //  Upload and submit a new site logo
74
    public function submitLogo(Request $request)
75
    {
76
        $file     = $request->file;
77
        $fileName = $file->getClientOriginalName();
78
        $file->storeAs('img', $fileName, 'public');
79
        
80
        Settings::where('key', 'app.logo')->update([
81
            'value' => '/storage/img/'.$fileName
82
        ]);
83
        
84
        Log::info('A new company logo has been uploaded by User ID-'.Auth::user()->user_id);
85
        
86
        return 'success';
87
    }
88
    
89
    //  Email settings form
90
    public function emailSettings()
91
    {
92
        return view('installer.form.email');
93
    }
94
    
95
    //  Send a test email
96
    public function sendTestEmail(Request $request)
97
    {
98
        //  Make sure that all of the information properly validates
99
        $request->validate([
100
            'host'       => 'required',
101
            'port'       => 'required|numeric',
102
            'encryption' => 'required',
103
            'username'   => 'required'
104
        ]);
105
        
106
        //  Temporarily set the email settings
107
        config([
108
            'mail.host'       => $request->host,
109
            'mail.port'       => $request->port,
110
            'mail.encryption' => $request->encryption,
111
            'mail.username'   => $request->username,
112
        ]);
113
        
114
        if(!empty($request->password))
115
        {
116
            config(['mail.password' => $request->password]);
117
        }
118
        
119
        //  Try and send the test email
120
        try
121
        {
122
            Log::info('Test Email Successfully Sent to '.Auth::user()->email);
123
            Mail::to(Auth::user()->email)->send(new TestEmail());
124
            return 'success';
125
        }
126
        catch(Exception $e)
127
        {
128
            Log::notice('Test Email Failed.  Message: '.$e);
129
            $msg = '['.$e->getCode().'] "'.$e->getMessage().'" on line '.$e->getTrace()[0]['line'].' of file '.$e->getTrace()[0]['file'];
130
            return $msg;
131
        }
132
    }
133
    
134
    //  Submit the email settings form
135
    public function submitEmailSettings(Request $request)
136
    {
137
        $request->validate([
138
            'host'       => 'required',
139
            'port'       => 'required|numeric',
140
            'encryption' => 'required',
141
            'username'   => 'required'
142
        ]);
143
        
144
        //  Update each setting
145
        Settings::where('key', 'mail.host')->update(['value' => $request->host]);
146
        Settings::where('key', 'mail.port')->update(['value' => $request->port]);
147
        Settings::where('key', 'mail.encryption')->update(['value' => $request->encryption]);
148
        Settings::where('key', 'mail.username')->update(['value' => $request->username]);
149
        if(!empty($request->password))
150
        {
151
            Settings::where('key', 'mail.password')->update(['value' => $request->password]);
152
        }
153
         
154
        Log::info('Email Settings have been changed by User ID-'.Auth::user()->user_id);
155
        return redirect()->back()->with('success', 'Tech Bench Successfully Updated');//
156
    }
157
    
158
    //  User settings form
159
    public function userSettings()
160
    {
161
        $passExpire = config('users.passExpires') != null ? config('users.passExpires') : 0;
162
        
163
        return view('installer.form.users', [
164
            'passExpire' => $passExpire
165
        ]);
166
    }
167
    
168
    //  Submit the user settings form
169
    public function submitUserSettings(Request $request)
170
    {
171
        $request->validate([
172
            'passExpire' => 'required|numeric'
173
        ]);
174
        
175
        //  Determine if the password expires field is updated
176
        $oldExpire = config('users.passExpires');
177
        if($request->passExpire != $oldExpire)
178
        {
179
            //  Update the setting in the database
180
            Settings::where('key', 'users.passExpires')->update([
181
                'value' => $request->passExpire
182
            ]);
183
            //  If the setting is changing from never to xx days, update all users
184
            if($request->passExpire == 0)
185
            {
186
                User::whereNotNull('password_expires')->update([
187
                    'password_expires' => null
188
                ]);
189
            }
190
            else
191
            {
192
                $newExpire = Carbon::now()->addDays($request->passExpire);
193
                User::whereNull('password_expires')->update([
194
                    'password_expires' => $newExpire
195
                ]);
196
            }
197
        }
198
        
199
        Log::info('User Settings have been changed by User ID-'.Auth::user()->user_id);
200
        return redirect()->back()->with('success', 'User Settings Updated');
201
    }
202
}
203