Explorar el Código

Removed useless if conditions and refactored queries to `firstOrFail` for implicit validation.

Zdravko Vajagic hace 5 años
padre
commit
242c95168b

+ 5 - 15
app/Http/Controllers/AliasesController.php

@@ -3,8 +3,7 @@
 namespace App\Http\Controllers;
 
 use Illuminate\Http\Request;
-use App\Application;
-use App\Alias;
+use App\{Application, Alias};
 use phpseclib\Net\SSH2 as SSH;
 
 class AliasesController extends Controller {
@@ -19,10 +18,7 @@ class AliasesController extends Controller {
             'domain' => 'required',
             'application_id' => 'required'
         ]);
-        $application = Application::where('id', $request->application_id)->with('server')->with('aliases')->first();
-        if(!$application) {
-            abort(403);
-        }
+        $application = Application::find($request->application_id)->with('server')->with('aliases')->firstOrFail();
         if(Application::where('server_id', $application->server_id)->where('domain', $request->domain)->first()) {
             $request->session()->flash('alert-error', 'This domain is already taken on this server');
             return redirect('/aliases');
@@ -60,12 +56,9 @@ class AliasesController extends Controller {
 
     public function destroy(Request $request) {
         $this->validate($request, [
-            'id' => 'required',
+            'id' => 'required|exists:aliases,id',
         ]);
-        $alias = Alias::where('id', $request->id)->with('application')->first();
-        if(!$alias) {
-            return abort(403);
-        }
+        $alias = Alias::find($request->id)->with('application')->firstOrFail();
         $ssh = New SSH($alias->application->server->ip, $alias->application->server->port);
         if(!$ssh->login($alias->application->server->username, $alias->application->server->password)) {
             $request->session()->flash('alert-error', 'There was a problem with server connection.');
@@ -86,10 +79,7 @@ class AliasesController extends Controller {
     }
 
     public function ssl($id) {
-        $alias = Alias::where('id', $id)->with('application')->first();
-        if(!$alias) {
-            return abort(403);
-        }
+        $alias = Alias::find($id)->with('application')->firstOrFail();
         $ssh = New SSH($alias->application->server->ip, $alias->application->server->port);
         if(!$ssh->login($alias->application->server->username, $alias->application->server->password)) {
             return abort(403);

+ 4 - 12
app/Http/Controllers/ApplicationsController.php

@@ -4,9 +4,7 @@ namespace App\Http\Controllers;
 
 use Illuminate\Support\Str;
 use Illuminate\Http\Request;
-use App\Application;
-use App\Server;
-use App\Alias;
+use App\{Application, Server, Alias};
 use phpseclib\Net\SSH2 as SSH;
 use PDF;
 
@@ -38,10 +36,7 @@ class ApplicationsController extends Controller {
                 return redirect('/applications');
             }
         }
-        $server = Server::where('id', $request->server_id)->where('complete', 2)->first();
-        if(!$server) {
-            return abort(403);
-        }
+        $server = Server::where('id', $request->server_id)->where('complete', 2)->firstOrFail();
         $user   = 'u'.hash('crc32', (Str::uuid()->toString())).rand(1,9);
         $pass   = sha1(uniqid().microtime().$request->domain);
         $dbpass = sha1(microtime().uniqid().$request->ip);
@@ -96,10 +91,7 @@ class ApplicationsController extends Controller {
         $this->validate($request, [
             'appcode' => 'required',
         ]);
-        $application = Application::where('appcode', $request->appcode)->first();
-        if(!$application) {
-            return abort(403);
-        }
+        $application = Application::where('appcode', $request->appcode)->firstOrFail();
         $ssh = New SSH($application->server->ip, $application->server->port);
         if(!$ssh->login($application->server->username, $application->server->password)) {
             $request->session()->flash('alert-error', 'There was a problem with server connection.');
@@ -117,7 +109,7 @@ class ApplicationsController extends Controller {
     }
 
     public function pdf($appcode) {
-        $application = Application::where('appcode', $appcode)->first();
+        $application = Application::where('appcode', $appcode)->firstOrFail();
         $data = [
             'username'      => $application->username,
             'password'      => $application->password,

+ 3 - 6
app/Http/Controllers/ServersController.php

@@ -21,10 +21,7 @@ class ServersController extends Controller
 
 
     public function get($servercode) {
-        $server = Server::where('servercode', $servercode)->with('applications')->first();
-        if(!$server) {
-            abort(404);
-        }
+        $server = Server::where('servercode', $servercode)->with('applications')->firstOrFail();
         return view('server', compact('server'));
     }
 
@@ -75,9 +72,9 @@ class ServersController extends Controller
         $this->validate($request, [
             'servercode' => 'required',
         ]);
-        $server = Server::where('servercode', $request->servercode)->first();
-        $request->session()->flash('alert-success', 'Server '.$server->name.' has been deleted!');
+        $server = Server::where('servercode', $request->servercode)->firstOrFail();
         $server->delete();
+        $request->session()->flash('alert-success', 'Server '.$server->name.' has been deleted!');
         return redirect('/servers');
     }
 

+ 8 - 33
app/Http/Controllers/ShellController.php

@@ -18,10 +18,7 @@ class ShellController extends Controller
     }
 
     public function install($servercode) {
-        $server = Server::where('servercode', $servercode)->where('complete', 0)->first();
-        if(!$server) {
-            return abort(403);
-        }
+        $server = Server::where('servercode', $servercode)->where('complete', 0)->firstOrFail();
         $script = Storage::get('scripts/install.sh');
         $script = Str::replaceArray('???', [
             $this->url->to('/'),
@@ -35,10 +32,7 @@ class ShellController extends Controller
     }
 
     public function hostadd($servercode) {
-        $server = Server::where('servercode', $servercode)->where('complete', 1)->first();
-        if(!$server) {
-            return abort(403);
-        }
+        $server = Server::where('servercode', $servercode)->where('complete', 1)->firstOrFail();
         $script = Storage::get('scripts/hostadd.sh');
         $script = Str::replaceArray('???', [
             $this->url->to('/'),
@@ -48,10 +42,7 @@ class ShellController extends Controller
     }
 
     public function hostget($appcode) {
-        $application = Application::where('appcode', $appcode)->first();
-        if(!$application) {
-            return abort(403);
-        }
+        $application = Application::where('appcode', $appcode)->firstOrFail();
         if($application->basepath) {
             $basepath = '/home/'.$application->username.'/web/'.$application->basepath;
         } else {
@@ -66,32 +57,22 @@ class ShellController extends Controller
     }
 
     public function hostdel($servercode) {
-        $server = Server::where('servercode', $servercode)->where('complete', 1)->first();
-        if(!$server) {
-            return abort(403);
-        }
+        $server = Server::where('servercode', $servercode)->where('complete', 1)->firstOrFail();
         $script = Storage::get('scripts/hostdel.sh');
         $script = Str::replaceArray('???', [
             $server->dbroot,
         ], $script);
         return response($script)->withHeaders(['Content-Type' =>'application/x-sh']);
-
     }
 
     public function passwd($servercode) {
-        $server = Server::where('servercode', $servercode)->where('complete', 1)->first();
-        if(!$server) {
-            return abort(403);
-        }
+        $server = Server::where('servercode', $servercode)->where('complete', 1)->firstOrFail();
         $script = Storage::get('scripts/passwd.sh');
         return response($script)->withHeaders(['Content-Type' =>'application/x-sh']);
     }
 
     public function aliasadd($servercode) {
-        $server = Server::where('servercode', $servercode)->where('complete', 1)->first();
-        if(!$server) {
-            return abort(403);
-        }
+        $server = Server::where('servercode', $servercode)->where('complete', 1)->firstOrFail();
         $script = Storage::get('scripts/aliasadd.sh');
         $script = Str::replaceArray('???', [
             $this->url->to('/')
@@ -100,19 +81,13 @@ class ShellController extends Controller
     }
 
     public function aliasdel($servercode) {
-        $server = Server::where('servercode', $servercode)->where('complete', 1)->first();
-        if(!$server) {
-            return abort(403);
-        }
+        $server = Server::where('servercode', $servercode)->where('complete', 1)->firstOrFail();
         $script = Storage::get('scripts/aliasdel.sh');
         return response($script)->withHeaders(['Content-Type' =>'application/x-sh']);
     }
 
     public function aliasget($appcode,$domain) {
-        $application = Application::where('appcode', $appcode)->first();
-        if(!$application) {
-            return abort(403);
-        }
+        $application = Application::where('appcode', $appcode)->firstOrFail();
         if($application->basepath) {
             $basepath = '/home/'.$application->username.'/web/'.$application->basepath;
         } else {