|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Console\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
|
6
|
|
|
use Illuminate\Support\Facades\Log; |
|
7
|
|
|
use Illuminate\Support\Facades\File; |
|
8
|
|
|
use Illuminate\Support\Facades\Storage; |
|
9
|
|
|
|
|
10
|
|
|
use App\User; |
|
11
|
|
|
use App\UserSettings; |
|
12
|
|
|
use App\Domains\Maintenance\DatabaseCheck as MaintenanceDatabaseCheck; |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
class databaseCheck extends Command |
|
16
|
|
|
{ |
|
17
|
|
|
protected $signature = 'tb-dbcheck {--fix : Automatically fix any errors that arrise}'; |
|
18
|
|
|
protected $fix = false; |
|
19
|
|
|
protected $description = 'Verify database and file structure has no errors'; |
|
20
|
|
|
|
|
21
|
2 |
|
public function __construct() |
|
22
|
|
|
{ |
|
23
|
2 |
|
parent::__construct(); |
|
24
|
|
|
|
|
25
|
2 |
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function handle() |
|
28
|
|
|
{ |
|
29
|
|
|
// Determine if fix option is enabled |
|
30
|
|
|
if($this->option('fix')) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->fix = true; |
|
33
|
|
|
} |
|
34
|
|
|
// Extend the amount of time the script is allowed to run |
|
35
|
|
|
ini_set('max_execution_time', 600); // 600 seconds = 10 minutes |
|
36
|
|
|
|
|
37
|
|
|
// Begin check |
|
38
|
|
|
$fixStr = $this->fix ? 'on' : 'off'; |
|
39
|
|
|
$this->line(''); |
|
40
|
|
|
$this->line('Running Database Check...'); |
|
41
|
|
|
$this->line(''); |
|
42
|
|
|
Log::alert('The database check has been initiated. Fix option is set to '.$fixStr); |
|
43
|
|
|
|
|
44
|
|
|
$this->checkUsers(); |
|
45
|
|
|
$this->checkForeignKeys(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
// User check will count users and administrators |
|
51
|
|
|
protected function checkUsers() |
|
52
|
|
|
{ |
|
53
|
|
|
$activeUsers = User::all()->count(); |
|
54
|
|
|
$inactiveUsers = User::onlyTrashed()->count(); |
|
55
|
|
|
|
|
56
|
|
|
// All users allowed to login |
|
57
|
|
|
$str = 'Current Active Users........................... '.$activeUsers; |
|
58
|
|
|
$this->line($str); |
|
59
|
|
|
Log::notice($str); |
|
60
|
|
|
|
|
61
|
|
|
// All users that have been disabled |
|
62
|
|
|
$str = 'Current Inactive Users......................... '.$inactiveUsers; |
|
63
|
|
|
$this->line($str); |
|
64
|
|
|
Log::notice($str); |
|
65
|
|
|
|
|
66
|
|
|
// All Active Installers |
|
67
|
|
|
$instArr = []; |
|
68
|
|
|
$installers = User::where('role_id', 1)->get(); |
|
69
|
|
|
foreach($installers as $ins) |
|
70
|
|
|
{ |
|
71
|
|
|
$instArr[] = [ |
|
72
|
|
|
'user_id' => $ins->user_id, |
|
73
|
|
|
'username' => $ins->username, |
|
74
|
|
|
'full_name' => $ins->full_name, |
|
75
|
|
|
]; |
|
76
|
|
|
} |
|
77
|
|
|
$this->line(''); |
|
78
|
|
|
$this->line('Active Installers:'); |
|
79
|
|
|
$this->table(['User ID', 'Username', 'Full Name'], $instArr); |
|
80
|
|
|
Log::notice('Current Active Installers - ', $instArr); |
|
81
|
|
|
|
|
82
|
|
|
// All Active Administrators |
|
83
|
|
|
$adminArr = []; |
|
84
|
|
|
$administrators = User::where('role_id', 2)->get(); |
|
85
|
|
|
foreach($administrators as $adm) |
|
86
|
|
|
{ |
|
87
|
|
|
$adminArr[] = [ |
|
88
|
|
|
'user_id' => $adm->user_id, |
|
89
|
|
|
'username' => $adm->username, |
|
90
|
|
|
'full_name' => $adm->full_name, |
|
91
|
|
|
]; |
|
92
|
|
|
} |
|
93
|
|
|
$this->line(''); |
|
94
|
|
|
$this->line('Active Administrators:'); |
|
95
|
|
|
$this->table(['User ID', 'Username', 'Full Name'], $adminArr); |
|
96
|
|
|
Log::notice('Current Active Administrators - ', $adminArr); |
|
97
|
|
|
|
|
98
|
|
|
// Validate that each user has a user_settings entry |
|
99
|
|
|
$this->line(''); |
|
100
|
|
|
$this->line('Validating User Settings'); |
|
101
|
|
|
$userList = User::all(); |
|
102
|
|
|
foreach($userList as $user) |
|
103
|
|
|
{ |
|
104
|
|
|
$row = UserSettings::where('user_id', $user->user_id)->count(); |
|
105
|
|
|
if($row != 1) |
|
106
|
|
|
{ |
|
107
|
|
|
$str = 'User '.$user->full_name.' missing Settings Table'; |
|
108
|
|
|
$this->error($str); |
|
109
|
|
|
Log::error($str); |
|
110
|
|
|
|
|
111
|
|
|
if($this->fix) |
|
112
|
|
|
{ |
|
113
|
|
|
UserSettings::create([ |
|
114
|
|
|
'user_id' => $user->user_id, |
|
115
|
|
|
]); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
protected function checkForeignKeys() |
|
122
|
|
|
{ |
|
123
|
|
|
$tableList = [ |
|
124
|
|
|
'users', |
|
125
|
|
|
'user_role_permissions', |
|
126
|
|
|
'user_logins', |
|
127
|
|
|
'tech_tips', |
|
128
|
|
|
'tech_tip_systems', |
|
129
|
|
|
'tech_tip_files', |
|
130
|
|
|
'tech_tip_favs', |
|
131
|
|
|
'tech_tip_comments', |
|
132
|
|
|
'system_types', |
|
133
|
|
|
'system_data_fields', |
|
134
|
|
|
'file_links', |
|
135
|
|
|
'file_link_files', |
|
136
|
|
|
'customer_systems', |
|
137
|
|
|
'customer_system_data', |
|
138
|
|
|
'customer_notes', |
|
139
|
|
|
'customer_files', |
|
140
|
|
|
'customer_favs', |
|
141
|
|
|
'customer_contacts', |
|
142
|
|
|
'customer_contact_phones', |
|
143
|
|
|
]; |
|
144
|
|
|
|
|
145
|
|
|
$dbObj = new MaintenanceDatabaseCheck($this->fix); |
|
146
|
|
|
$this->line('Running Foreign Key Check'); |
|
147
|
|
|
|
|
148
|
|
|
foreach($tableList as $table) |
|
149
|
|
|
{ |
|
150
|
|
|
$valid = $dbObj->execute($table); |
|
151
|
|
|
if(!$valid) |
|
152
|
|
|
{ |
|
153
|
|
|
$str = 'Foreign Key Check for '.$table.' failed'; |
|
154
|
|
|
$this->error($str); |
|
155
|
|
|
Log::error($str); |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|