소스 검색

Merge pull request #102 from thugic/master

Verify in develop Thugic #101 PR - Refactors towards cleaner code
Andrea Pollastri 5 년 전
부모
커밋
0df1e1f0d9

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

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

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

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

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

@@ -21,10 +21,7 @@ class ServersController extends Controller
 
 
 
 
     public function get($servercode) {
     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'));
         return view('server', compact('server'));
     }
     }
 
 
@@ -75,9 +72,9 @@ class ServersController extends Controller
         $this->validate($request, [
         $this->validate($request, [
             'servercode' => 'required',
             '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();
         $server->delete();
+        $request->session()->flash('alert-success', 'Server '.$server->name.' has been deleted!');
         return redirect('/servers');
         return redirect('/servers');
     }
     }
 
 

+ 13 - 17
app/Http/Controllers/SettingsController.php

@@ -4,46 +4,42 @@ namespace App\Http\Controllers;
 
 
 use Illuminate\Http\Request;
 use Illuminate\Http\Request;
 use App\Http\Controllers\Controller;
 use App\Http\Controllers\Controller;
-use Illuminate\Support\Facades\Auth;
 use Illuminate\Support\Facades\Hash;
 use Illuminate\Support\Facades\Hash;
 
 
 class SettingsController extends Controller
 class SettingsController extends Controller
 {
 {
 
 
     public function index() {
     public function index() {
-        $user = Auth::user();
+        $user = auth()->user();
         return view('settings', compact('user'));
         return view('settings', compact('user'));
     }
     }
 
 
-    public function profile(Request $request) {
+    public function updateProfile(Request $request) {
         $this->validate($request, [
         $this->validate($request, [
             'email' => 'required|email',
             'email' => 'required|email',
             'name' => 'required'
             'name' => 'required'
         ]);
         ]);
-        $user = Auth::user();
-        $user->name  = $request->name;
-        $user->email = $request->email;
-        $user->save();
+        auth()->user()->update([
+          'name' => $request->name,
+          'email' => $request->email
+        ]);
         $request->session()->flash('alert-success', 'Profile has been updated!');
         $request->session()->flash('alert-success', 'Profile has been updated!');
         return redirect('/settings');
         return redirect('/settings');
     }
     }
 
 
-    public function password(Request $request) {
+    public function updatePassword(Request $request) {
         $this->validate($request, [
         $this->validate($request, [
             'current' => 'required',
             'current' => 'required',
             'password' => 'required|confirmed|min:8',
             'password' => 'required|confirmed|min:8',
-            'password_confirmation' => 'required|min:8'
+            'password_confirmation' => 'required|min:8|same:password'
         ]);
         ]);
-        $user = Auth::user();
-        if ($request->password != $request->password_confirmation) {
-            return redirect('/settings');
-        }
-        if (!Hash::check($request->current, $user->password)) {
-            $request->session()->flash('alert-error', 'Wrong password!');
+        if (!Hash::check($request->current, auth()->user()->password)) {
+            $request->session()->flash('alert-error', 'Invalid current password!');
             return redirect('/settings');
             return redirect('/settings');
         }
         }
-        $user->password = Hash::make($request->password);
-        $user->save();
+        auth()->user()->update([
+          'password' => Hash::make($request->password)
+        ]);
         $request->session()->flash('alert-success', 'Password has been updated!');
         $request->session()->flash('alert-success', 'Password has been updated!');
         return redirect('/settings');
         return redirect('/settings');
     }
     }

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

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

+ 1 - 5
app/Http/Controllers/UsersController.php

@@ -20,11 +20,7 @@ class UsersController extends Controller {
         $this->validate($request, [
         $this->validate($request, [
             'username' => 'required'
             'username' => 'required'
         ]);
         ]);
-        $application = Application::where('username', $request->username)->with('server')->first();
-        if(!$application) {
-            $request->session()->flash('alert-error', 'User not found!');
-            return redirect('/users');
-        }
+        $application = Application::where('username', $request->username)->with('server')->firstOrFail();
         $ssh = New SSH($application->server->ip, $application->server->port);
         $ssh = New SSH($application->server->ip, $application->server->port);
         if(!$ssh->login($application->server->username, $application->server->password)) {
         if(!$ssh->login($application->server->username, $application->server->password)) {
             $request->session()->flash('alert-error', 'There was a problem with server connection.');
             $request->session()->flash('alert-error', 'There was a problem with server connection.');

+ 10 - 10
resources/views/servers.blade.php

@@ -45,7 +45,7 @@ Servers
                         <tr>
                         <tr>
                             <td class="text-center">
                             <td class="text-center">
                                 @if ($server->complete == 0)
                                 @if ($server->complete == 0)
-                                    <button type="button" class="btn btn-danger btn-sm" data-toggle="modal" data-target="#setupModal" data-servercode="{{ $server->servercode }}" data-serverip="{{ $server->ip }}">"{{ $server->name }}" has to be install</button>
+                                    <button type="button" class="btn btn-danger btn-sm" data-toggle="modal" data-target="#setupModal" data-servercode="{{ $server->servercode }}" data-serverip="{{ $server->ip }}">"{{ $server->name }}" has to be installed</button>
                                 @elseif ($server->complete == 1)
                                 @elseif ($server->complete == 1)
                                     <button type="button" class="btn btn-warning btn-sm">"{{ $server->name }}" is coming...</button>
                                     <button type="button" class="btn btn-warning btn-sm">"{{ $server->name }}" is coming...</button>
                                 @else
                                 @else
@@ -252,12 +252,12 @@ Servers
                     <li>Run this command:<br>
                     <li>Run this command:<br>
                         <code><i>wget -O - {{ url('/sh/go') }}/<span class="server-id"></span> | bash</i></code>
                         <code><i>wget -O - {{ url('/sh/go') }}/<span class="server-id"></span> | bash</i></code>
                     </li>
                     </li>
-                    <li>Installation may take up to about ten minutes which may also depend on your server internet connection speed</li>
-                    <li>Before install Cipi, please make sure your server is a clean Ubuntu 18.04 or 20.04 x86_64 LTS VPS (Fresh installation)</li>
-                    <li>Hardware Requirement: minium 1GB free HDD / at least 1 core processor / 512MB or more RAM / 1 public IPv4 address</li>
+                    <li>Installation may take up to ten minutes depending on your server internet connection speed</li>
+                    <li>Before you install Cipi, please make sure your server is a clean Ubuntu 18.04 or 20.04 x86_64 LTS VPS (Fresh installation)</li>
+                    <li>Hardware Requirements: minimum 1GB free HDD / at least 1 core processor / 512MB or more RAM / 1 public IPv4 address</li>
                     <li>Please open port 22, 80 and 443 of your firewall to install Cipi</li>
                     <li>Please open port 22, 80 and 443 of your firewall to install Cipi</li>
-                    <li>Cipi would not work with NAT VPN and OpenVZ or in localhost</li>
-                    <li>AWS disables root login by default. To login as root inside AWS, login as default user and then use command 'sudo -s'</li>
+                    <li>Cipi doesn't work with NAT VPN and OpenVZ or in localhost</li>
+                    <li>AWS disables root login by default. To gain root privileges, login as default user and then use command 'sudo -s'</li>
                 </ul>
                 </ul>
             </div>
             </div>
             <div class="modal-footer">
             <div class="modal-footer">
@@ -287,7 +287,7 @@ Servers
                             <div class="form-group">
                             <div class="form-group">
                                 <input type="text" id="server-ip" required class="form-control" name="ip">
                                 <input type="text" id="server-ip" required class="form-control" name="ip">
                             </div>
                             </div>
-                            <i class="fas fa-exclamation-circle" style="margin-left: 5px;"></i> Before submit changes, be sure about your new IP!
+                            <i class="fas fa-exclamation-circle" style="margin-left: 5px;"></i> Before submitting changes, double check the IP!
                         </div>
                         </div>
                     </div>
                     </div>
                 </div>
                 </div>
@@ -321,12 +321,12 @@ Servers
                             <div class="form-group">
                             <div class="form-group">
                                 <select class="form-control" name="server_id" required id="server-list">
                                 <select class="form-control" name="server_id" required id="server-list">
                                     <option value="">Select...</option>
                                     <option value="">Select...</option>
-                                    <option value="">YES! I'm sure!!!</option>
+                                    <option value="">YES! Delete this server.</option>
                                 </select>
                                 </select>
                             </div>
                             </div>
                             <div class="space"></div>
                             <div class="space"></div>
-                            <h6 class="text-danger">This action is irreversible.</h6>
-                            <h6 class="text-danger">You will lose control on this server.</h6>
+                            <h6 class="text-danger">This can be undone.</h6>
+                            <h6 class="text-danger">You will lose access to your server.</h6>
                         </div>
                         </div>
                     </div>
                     </div>
                 </div>
                 </div>

+ 12 - 12
resources/views/settings.blade.php

@@ -10,14 +10,14 @@ Settings
 
 
 @section('content')
 @section('content')
 @if(Session::has('alert-success'))
 @if(Session::has('alert-success'))
-    <div class="alert alert-success" role="alert">
-        <b><i class="fa fa-check" aria-hidden="true"></i></b> {{ Session::get('alert-success') }}
-    </div>
+<div class="alert alert-success" role="alert">
+    <b><i class="fa fa-check" aria-hidden="true"></i></b> {{ Session::get('alert-success') }}
+</div>
 @endif
 @endif
-    @if(Session::has('alert-error'))
-    <div class="alert alert-danger" role="alert">
-        <b><i class="fa fa-times" aria-hidden="true"></i></b> {{ Session::get('alert-error') }}
-    </div>
+@if(Session::has('alert-error'))
+<div class="alert alert-danger" role="alert">
+    <b><i class="fa fa-times" aria-hidden="true"></i></b> {{ Session::get('alert-error') }}
+</div>
 @endif
 @endif
 <div class="row">
 <div class="row">
     <div class="col-lg-6 mb-4">
     <div class="col-lg-6 mb-4">
@@ -32,7 +32,7 @@ Settings
                         <label for="name" class="col-md-4 col-form-label text-md-right">Name</label>
                         <label for="name" class="col-md-4 col-form-label text-md-right">Name</label>
                         <div class="col-md-6">
                         <div class="col-md-6">
                             <div class="form-group">
                             <div class="form-group">
-                                <input id="name" type="text" class="form-control" name="name" value="{{ $user->name }}" required autocomplete="name" autofocus>
+                                <input id="name" type="text" class="form-control" name="name" value="{{ auth()->user()->name }}" required autocomplete="name" autofocus>
                             </div>
                             </div>
                         </div>
                         </div>
                     </div>
                     </div>
@@ -41,7 +41,7 @@ Settings
                         <label for="email" class="col-md-4 col-form-label text-md-right">E-mail</label>
                         <label for="email" class="col-md-4 col-form-label text-md-right">E-mail</label>
                         <div class="col-md-6">
                         <div class="col-md-6">
                             <div class="form-group">
                             <div class="form-group">
-                                <input id="email" type="email" class="form-control" name="email" value="{{ $user->email }}" required autocomplete="email">
+                                <input id="email" type="email" class="form-control" name="email" value="{{ auth()->user()->email }}" required autocomplete="email">
                             </div>
                             </div>
                         </div>
                         </div>
                     </div>
                     </div>
@@ -67,7 +67,7 @@ Settings
                     <form method="POST" action="/settings/password" class="ws-validate">
                     <form method="POST" action="/settings/password" class="ws-validate">
                         @csrf
                         @csrf
                         <div class="form-group row">
                         <div class="form-group row">
-                        <label for="password" class="col-md-6 col-form-label text-md-right">Password</label>
+                            <label for="password" class="col-md-6 col-form-label text-md-right">Password</label>
                             <div class="col-md-6">
                             <div class="col-md-6">
                                 <div class="form-group">
                                 <div class="form-group">
                                     <input id="current" type="password" class="form-control" name="current" required autocomplete="new-password">
                                     <input id="current" type="password" class="form-control" name="current" required autocomplete="new-password">
@@ -131,14 +131,14 @@ Settings
 @section('js')
 @section('js')
 <script>
 <script>
     $('#password').keyup(function() {
     $('#password').keyup(function() {
-        if($('#password').val() != $('#password-confirm').val()) {
+        if ($('#password').val() != $('#password-confirm').val()) {
             $('#password-confirm').setCustomValidity("Passwords don't match");
             $('#password-confirm').setCustomValidity("Passwords don't match");
         } else {
         } else {
             $('#password-confirm').setCustomValidity('');
             $('#password-confirm').setCustomValidity('');
         }
         }
     });
     });
     $('#password-confirm').keyup(function() {
     $('#password-confirm').keyup(function() {
-        if($('#password').val() != $('#password-confirm').val()) {
+        if ($('#password').val() != $('#password-confirm').val()) {
             $('#password-confirm').setCustomValidity("Passwords don't match");
             $('#password-confirm').setCustomValidity("Passwords don't match");
         } else {
         } else {
             $('#password-confirm').setCustomValidity('');
             $('#password-confirm').setCustomValidity('');

+ 2 - 2
routes/web.php

@@ -64,7 +64,7 @@ Route::group(['middleware' => 'auth'], function () {
     });
     });
     Route::group(['prefix' => 'settings'], function () {
     Route::group(['prefix' => 'settings'], function () {
         Route::get('/', 'SettingsController@index');
         Route::get('/', 'SettingsController@index');
-        Route::post('/profile', 'SettingsController@profile');
-        Route::post('/password', 'SettingsController@password');
+        Route::post('/profile', 'SettingsController@updateProfile');
+        Route::post('/password', 'SettingsController@updatePassword');
     });
     });
 });
 });