Skip to content

Commit 1ca07e6

Browse files
committed
Add support for viewing SFTP password in panel
requested in pterodactyl#74 closes pterodactyl#74
1 parent 8c40f64 commit 1ca07e6

File tree

7 files changed

+94
-19
lines changed

7 files changed

+94
-19
lines changed

app/Policies/ServerPolicy.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,22 @@ public function resetSftp(User $user, Server $server)
434434
return $user->permissions()->server($server)->permission('reset-sftp')->exists();
435435
}
436436

437+
/**
438+
* Check if user has permission to view the SFTP password for a server.
439+
*
440+
* @param Pterodactyl\Models\User $user
441+
* @param Pterodactyl\Models\Server $server
442+
* @return boolean
443+
*/
444+
public function viewSftpPassword(User $user, Server $server)
445+
{
446+
if ($this->isOwner($user, $server)) {
447+
return true;
448+
}
449+
450+
return $user->permissions()->server($server)->permission('view-sftp-password')->exists();
451+
}
452+
437453
/**
438454
* Check if user has permission to view databases for a server.
439455
*

app/Repositories/ServerRepository.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
*/
2424
namespace Pterodactyl\Repositories;
2525

26+
use Crypt;
2627
use DB;
2728
use Debugbar;
2829
use Validator;
@@ -804,7 +805,12 @@ public function updateSFTPPassword($id, $password)
804805
throw new DisplayValidationException(json_encode($validator->errors()));
805806
}
806807

808+
DB::beginTransaction();
809+
$server->sftp_password = Crypt::encrypt($password);
810+
807811
try {
812+
$server->save();
813+
808814
$client = Models\Node::guzzleRequest($server->node);
809815
$client->request('POST', '/server/password', [
810816
'headers' => [
@@ -815,10 +821,14 @@ public function updateSFTPPassword($id, $password)
815821
'password' => $password,
816822
],
817823
]);
824+
825+
DB::commit();
818826
return true;
819827
} catch (\GuzzleHttp\Exception\TransferException $ex) {
828+
DB::rollBack();
820829
throw new DisplayException('There was an error while attmping to contact the remote service to change the password.', $ex);
821830
} catch (\Exception $ex) {
831+
DB::rollBack();
822832
throw $ex;
823833
}
824834

app/Repositories/SubuserRepository.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ class SubuserRepository
9393
'edit-startup' => null,
9494
'view-sftp' => null,
9595
'reset-sftp' => 's:set-password',
96+
'view-sftp-password' => null,
9697

9798
// Databases
9899
'view-databases' => null,
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
use Illuminate\Database\Schema\Blueprint;
4+
use Illuminate\Database\Migrations\Migration;
5+
6+
class AddSftpPasswordStorage extends Migration
7+
{
8+
/**
9+
* Run the migrations.
10+
*
11+
* @return void
12+
*/
13+
public function up()
14+
{
15+
Schema::table('servers', function (Blueprint $table) {
16+
$table->text('sftp_password')->after('username')->nullable();
17+
});
18+
}
19+
20+
/**
21+
* Reverse the migrations.
22+
*
23+
* @return void
24+
*/
25+
public function down()
26+
{
27+
Schema::table('servers', function (Blueprint $table) {
28+
$table->dropColumn('sftp_password');
29+
});
30+
}
31+
}

resources/views/server/settings.blade.php

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -51,29 +51,34 @@
5151
</div>
5252
</div>
5353
</div>
54-
@can('reset-sftp', $server)
55-
<form action="{{ route('server.settings.sftp', $server->uuidShort) }}" method="POST">
56-
<div class="row">
57-
<div class="col-md-12">
58-
<div id="gen_pass" class=" alert alert-success" style="display:none;margin-bottom: 10px;"></div>
59-
</div>
60-
<div class="form-group col-md-6">
54+
<div class="row">
55+
<div class="col-md-12">
56+
<div id="gen_pass" class=" alert alert-success" style="display:none;margin-bottom: 10px;"></div>
57+
</div>
58+
<div class="form-group col-md-6">
59+
@can('reset-sftp', $server)
60+
<form action="{{ route('server.settings.sftp', $server->uuidShort) }}" method="POST">
6161
<label class="control-label">New SFTP Password:</label>
62-
<div>
63-
<input type="password" name="sftp_pass" class="form-control" />
64-
<p class="text-muted"><small>Passwords must meet the following requirements: at least one uppercase character, one lowercase character, one digit, and be at least 8 characters in length. <a href="#" data-action="generate-password">Click here</a> to generate one to use.</small></p>
65-
</div>
66-
</div>
67-
<div class="form-group col-md-6">
68-
<label class="control-label">&nbsp;</label>
69-
<div>
70-
{!! csrf_field() !!}
71-
<input type="submit" class="btn btn-sm btn-primary" value="Update Password" />
62+
<div class="input-group">
63+
<input type="password" class="form-control" name="sftp_pass" />
64+
<span class="input-group-btn">
65+
{!! csrf_field() !!}
66+
<input type="submit" class="btn btn-primary btn-sm" value="Reset" />
67+
</span>
7268
</div>
69+
<p class="text-muted"><small>Passwords must meet the following requirements: at least one uppercase character, one lowercase character, one digit, and be at least 8 characters in length. <a href="#" data-action="generate-password">Click here</a> to generate one to use.</small></p>
70+
</form>
71+
@endcan
72+
</div>
73+
<div class="form-group col-md-6">
74+
@can('view-sftp-password', $server)
75+
<label class="control-label">Current Password:</label>
76+
<div>
77+
<input type="text" readonly="readonly" class="form-control" value="@if(!is_null($server->sftp_password)){{ Crypt::decrypt($server->sftp_password) }} @endif" />
7378
</div>
79+
@endcan
7480
</div>
75-
</form>
76-
@endcan
81+
</div>
7782
</div>
7883
</div>
7984
</div>

resources/views/server/users/new.blade.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,12 @@
200200
<p class="text-muted"><small>Allows user to view the server's SFTP information (not the password).</small><p>
201201
</label>
202202
</div>
203+
<div class="checkbox highlight">
204+
<label class="checkbox-custom highlight" data-initialize="checkbox">
205+
<input class="sr-only" name="permissions[]" type="checkbox" @if(isset($oldInput['view-sftp-password']))checked="checked"@endif value="view-sftp-password"> <strong>View SFTP Password</strong>
206+
<p class="text-muted"><small><span class="label label-danger">Danger</span> Allows user to view the SFTP password for the server.</small><p>
207+
</label>
208+
</div>
203209
<div class="checkbox highlight">
204210
<label class="checkbox-custom highlight" data-initialize="checkbox">
205211
<input class="sr-only" name="permissions[]" type="checkbox" @if(isset($oldInput['reset-sftp']))checked="checked"@endif value="reset-sftp"> <strong>Reset SFTP Password</strong>

resources/views/server/users/view.blade.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,12 @@
189189
<p class="text-muted"><small>Allows user to view the server's SFTP information (not the password).</small><p>
190190
</label>
191191
</div>
192+
<div class="checkbox highlight">
193+
<label class="checkbox-custom highlight" data-initialize="checkbox">
194+
<input class="sr-only" name="permissions[]" type="checkbox" @if(isset($permissions['view-sftp-password']))checked="checked"@endif @cannot('edit-subuser', $server)disabled="disabled"@endcannot value="view-sftp-password"> <strong>View SFTP Password</strong>
195+
<p class="text-muted"><small><span class="label label-danger">Danger</span> Allows user to view the SFTP password for the server.</small><p>
196+
</label>
197+
</div>
192198
<div class="checkbox highlight">
193199
<label class="checkbox-custom highlight" data-initialize="checkbox">
194200
<input class="sr-only" name="permissions[]" type="checkbox" @if(isset($permissions['reset-sftp']))checked="checked"@endif @cannot('edit-subuser', $server)disabled="disabled"@endcannot value="reset-sftp"> <strong>Reset SFTP Password</strong>

0 commit comments

Comments
 (0)