2016-01-18 06:24:33 +00:00
|
|
|
<?php
|
2016-01-20 00:10:39 +00:00
|
|
|
/**
|
2016-01-20 21:05:16 +00:00
|
|
|
* Pterodactyl - Panel
|
2016-12-07 22:46:38 +00:00
|
|
|
* Copyright (c) 2015 - 2016 Dane Everitt <dane@daneeveritt.com>.
|
2016-01-20 00:10:39 +00:00
|
|
|
*
|
2016-01-20 20:56:40 +00:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
2016-01-20 00:10:39 +00:00
|
|
|
*
|
2016-01-20 20:56:40 +00:00
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
* copies or substantial portions of the Software.
|
2016-01-20 00:10:39 +00:00
|
|
|
*
|
2016-01-20 20:56:40 +00:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
2016-01-20 00:10:39 +00:00
|
|
|
*/
|
2016-12-07 22:46:38 +00:00
|
|
|
|
2016-01-18 06:24:33 +00:00
|
|
|
namespace Pterodactyl\Repositories;
|
|
|
|
|
|
|
|
use DB;
|
2016-12-07 22:46:38 +00:00
|
|
|
use Mail;
|
2016-01-21 03:08:09 +00:00
|
|
|
use Settings;
|
2016-01-18 06:24:33 +00:00
|
|
|
use Validator;
|
|
|
|
use Pterodactyl\Models;
|
|
|
|
use Pterodactyl\Services\UuidService;
|
|
|
|
use Pterodactyl\Exceptions\DisplayException;
|
2016-12-07 22:46:38 +00:00
|
|
|
use Pterodactyl\Exceptions\DisplayValidationException;
|
2016-01-18 06:24:33 +00:00
|
|
|
|
2016-01-19 00:57:10 +00:00
|
|
|
class SubuserRepository
|
2016-01-18 06:24:33 +00:00
|
|
|
{
|
2016-01-19 00:57:10 +00:00
|
|
|
/**
|
|
|
|
* Core permissions required for every subuser on the daemon.
|
|
|
|
* Without this we cannot connect the websocket or get basic
|
|
|
|
* information about the server.
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $coreDaemonPermissions = [
|
|
|
|
's:get',
|
2016-12-07 22:46:38 +00:00
|
|
|
's:console',
|
2016-01-19 00:57:10 +00:00
|
|
|
];
|
|
|
|
|
2016-01-18 06:24:33 +00:00
|
|
|
/**
|
|
|
|
* Allowed permissions and their related daemon permission.
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $permissions = [
|
|
|
|
// Power Permissions
|
|
|
|
'power-start' => 's:power:start',
|
|
|
|
'power-stop' => 's:power:stop',
|
|
|
|
'power-restart' => 's:power:restart',
|
|
|
|
'power-kill' => 's:power:kill',
|
|
|
|
|
|
|
|
// Commands
|
|
|
|
'send-command' => 's:command',
|
|
|
|
|
|
|
|
// File Manager
|
|
|
|
'list-files' => 's:files:get',
|
2016-01-19 00:57:10 +00:00
|
|
|
'edit-files' => 's:files:read',
|
|
|
|
'save-files' => 's:files:post',
|
|
|
|
'create-files' => 's:files:post',
|
|
|
|
'download-files' => null,
|
|
|
|
'upload-files' => 's:files:upload',
|
|
|
|
'delete-files' => 's:files:delete',
|
2016-10-07 00:29:21 +00:00
|
|
|
'move-files' => 's:files:move',
|
|
|
|
'copy-files' => 's:files:copy',
|
|
|
|
'compress-files' => 's:files:compress',
|
|
|
|
'decompress-files' => 's:files:decompress',
|
2016-01-18 06:24:33 +00:00
|
|
|
|
|
|
|
// Subusers
|
|
|
|
'list-subusers' => null,
|
|
|
|
'view-subuser' => null,
|
|
|
|
'edit-subuser' => null,
|
|
|
|
'create-subuser' => null,
|
|
|
|
'delete-subuser' => null,
|
|
|
|
|
2016-03-18 20:23:10 +00:00
|
|
|
// Tasks
|
|
|
|
'list-tasks' => null,
|
|
|
|
'view-task' => null,
|
|
|
|
'toggle-task' => null,
|
|
|
|
'delete-task' => null,
|
|
|
|
'create-task' => null,
|
|
|
|
'queue-task' => null,
|
|
|
|
|
2016-01-18 06:24:33 +00:00
|
|
|
// Management
|
|
|
|
'set-connection' => null,
|
2016-01-19 00:57:10 +00:00
|
|
|
'view-startup' => null,
|
|
|
|
'edit-startup' => null,
|
2016-01-18 06:24:33 +00:00
|
|
|
'view-sftp' => null,
|
2016-08-17 21:36:06 +00:00
|
|
|
'reset-sftp' => 's:set-password',
|
2016-09-02 23:26:48 +00:00
|
|
|
'view-sftp-password' => null,
|
2016-08-17 21:36:06 +00:00
|
|
|
|
|
|
|
// Databases
|
|
|
|
'view-databases' => null,
|
2016-12-07 22:46:38 +00:00
|
|
|
'reset-db-password' => null,
|
2016-01-18 06:24:33 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
2016-01-19 00:57:10 +00:00
|
|
|
/**
|
|
|
|
* Creates a new subuser on the server.
|
2016-12-07 22:46:38 +00:00
|
|
|
* @param int $id The ID of the server to add this subuser to.
|
2016-01-19 00:57:10 +00:00
|
|
|
* @param array $data
|
|
|
|
* @throws DisplayValidationException
|
|
|
|
* @throws DisplayException
|
2016-12-07 22:46:38 +00:00
|
|
|
* @return int Returns the ID of the newly created subuser.
|
2016-01-19 00:57:10 +00:00
|
|
|
*/
|
|
|
|
public function create($sid, array $data)
|
|
|
|
{
|
|
|
|
$server = Models\Server::findOrFail($sid);
|
2016-12-30 21:28:43 +00:00
|
|
|
|
2016-01-19 00:57:10 +00:00
|
|
|
$validator = Validator::make($data, [
|
|
|
|
'permissions' => 'required|array',
|
2016-12-07 22:46:38 +00:00
|
|
|
'email' => 'required|email',
|
2016-01-19 00:57:10 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
if ($validator->fails()) {
|
2016-01-25 23:56:40 +00:00
|
|
|
throw new DisplayValidationException(json_encode($validator->errors()));
|
2016-01-19 00:57:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DB::beginTransaction();
|
|
|
|
|
2016-02-15 02:49:00 +00:00
|
|
|
try {
|
|
|
|
// Determine if this user exists or if we need to make them an account.
|
|
|
|
$user = Models\User::where('email', $data['email'])->first();
|
2016-12-07 22:46:38 +00:00
|
|
|
if (! $user) {
|
2016-02-15 02:49:00 +00:00
|
|
|
try {
|
|
|
|
$repo = new UserRepository;
|
2017-01-21 05:04:09 +00:00
|
|
|
$uid = $repo->create([
|
|
|
|
'email' => $data['email'],
|
|
|
|
'username' => substr(str_replace('@', '', $data['email']), 0, 8),
|
|
|
|
'name_first' => 'John',
|
|
|
|
'name_last' => 'Doe',
|
|
|
|
'root_admin' => false,
|
|
|
|
]);
|
2016-02-15 02:49:00 +00:00
|
|
|
$user = Models\User::findOrFail($uid);
|
|
|
|
} catch (\Exception $ex) {
|
|
|
|
throw $ex;
|
|
|
|
}
|
2016-12-30 22:00:06 +00:00
|
|
|
} elseif ($server->owner === $user->id) {
|
2016-12-30 21:28:43 +00:00
|
|
|
throw new DisplayException('You cannot add the owner of a server as a subuser.');
|
2016-12-30 22:00:06 +00:00
|
|
|
} elseif (Models\Subuser::select('id')->where('user_id', $user->id)->where('server_id', $server->id)->first()) {
|
2016-12-30 21:28:43 +00:00
|
|
|
throw new DisplayException('A subuser with that email already exists for this server.');
|
2016-01-19 00:57:10 +00:00
|
|
|
}
|
|
|
|
|
2016-02-15 02:49:00 +00:00
|
|
|
$uuid = new UuidService;
|
2016-01-19 00:57:10 +00:00
|
|
|
|
2016-02-15 02:49:00 +00:00
|
|
|
$subuser = new Models\Subuser;
|
|
|
|
$subuser->fill([
|
|
|
|
'user_id' => $user->id,
|
|
|
|
'server_id' => $server->id,
|
2016-12-07 22:46:38 +00:00
|
|
|
'daemonSecret' => (string) $uuid->generate('servers', 'uuid'),
|
2016-02-15 02:49:00 +00:00
|
|
|
]);
|
|
|
|
$subuser->save();
|
|
|
|
|
|
|
|
$daemonPermissions = $this->coreDaemonPermissions;
|
2016-12-07 22:46:38 +00:00
|
|
|
foreach ($data['permissions'] as $permission) {
|
2016-02-15 02:49:00 +00:00
|
|
|
if (array_key_exists($permission, $this->permissions)) {
|
|
|
|
// Build the daemon permissions array for sending.
|
2016-12-07 22:46:38 +00:00
|
|
|
if (! is_null($this->permissions[$permission])) {
|
2016-02-15 02:49:00 +00:00
|
|
|
array_push($daemonPermissions, $this->permissions[$permission]);
|
|
|
|
}
|
2016-12-30 21:28:43 +00:00
|
|
|
|
2016-02-15 02:49:00 +00:00
|
|
|
$model = new Models\Permission;
|
|
|
|
$model->fill([
|
|
|
|
'user_id' => $user->id,
|
|
|
|
'server_id' => $server->id,
|
2016-12-07 22:46:38 +00:00
|
|
|
'permission' => $permission,
|
2016-02-15 02:49:00 +00:00
|
|
|
]);
|
|
|
|
$model->save();
|
2016-01-19 00:57:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-15 02:49:00 +00:00
|
|
|
// Contact Daemon
|
|
|
|
// We contact even if they don't have any daemon permissions to overwrite
|
|
|
|
// if they did have them previously.
|
2016-01-19 00:57:10 +00:00
|
|
|
|
|
|
|
$node = Models\Node::getByID($server->node);
|
|
|
|
$client = Models\Node::guzzleRequest($server->node);
|
|
|
|
|
|
|
|
$res = $client->request('PATCH', '/server', [
|
|
|
|
'headers' => [
|
|
|
|
'X-Access-Server' => $server->uuid,
|
2016-12-07 22:46:38 +00:00
|
|
|
'X-Access-Token' => $node->daemonSecret,
|
2016-01-19 00:57:10 +00:00
|
|
|
],
|
|
|
|
'json' => [
|
|
|
|
'keys' => [
|
2016-12-07 22:46:38 +00:00
|
|
|
$subuser->daemonSecret => $daemonPermissions,
|
|
|
|
],
|
|
|
|
],
|
2016-01-19 00:57:10 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
$email = $data['email'];
|
|
|
|
Mail::queue('emails.added-subuser', [
|
|
|
|
'serverName' => $server->name,
|
|
|
|
'url' => route('server.index', $server->uuidShort),
|
|
|
|
], function ($message) use ($email) {
|
|
|
|
$message->to($email);
|
2016-01-21 03:08:09 +00:00
|
|
|
$message->from(Settings::get('email_from', env('MAIL_FROM')), Settings::get('email_sender_name', env('MAIL_FROM_NAME', 'Pterodactyl Panel')));
|
|
|
|
$message->subject(Settings::get('company') . ' - Added to Server');
|
2016-01-19 00:57:10 +00:00
|
|
|
});
|
|
|
|
DB::commit();
|
2016-12-07 22:46:38 +00:00
|
|
|
|
2016-01-19 00:57:10 +00:00
|
|
|
return $subuser->id;
|
|
|
|
} catch (\GuzzleHttp\Exception\TransferException $ex) {
|
|
|
|
DB::rollBack();
|
2016-08-16 23:20:58 +00:00
|
|
|
throw new DisplayException('There was an error attempting to connect to the daemon to add this user.', $ex);
|
2016-01-19 00:57:10 +00:00
|
|
|
} catch (\Exception $ex) {
|
|
|
|
DB::rollBack();
|
|
|
|
throw $ex;
|
|
|
|
}
|
2016-12-07 22:46:38 +00:00
|
|
|
|
2016-01-19 00:57:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Revokes a users permissions on a server.
|
2016-12-07 22:46:38 +00:00
|
|
|
* @param int $id The ID of the subuser row in MySQL.
|
2016-01-19 00:57:10 +00:00
|
|
|
* @param array $data
|
|
|
|
* @throws DisplayValidationException
|
|
|
|
* @throws DisplayException
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function delete($id)
|
|
|
|
{
|
|
|
|
$subuser = Models\Subuser::findOrFail($id);
|
|
|
|
$server = Models\Server::findOrFail($subuser->server_id);
|
|
|
|
|
|
|
|
DB::beginTransaction();
|
|
|
|
|
|
|
|
try {
|
2016-02-15 02:49:00 +00:00
|
|
|
Models\Permission::where('user_id', $subuser->user_id)->where('server_id', $subuser->server_id)->delete();
|
|
|
|
|
2016-01-19 00:57:10 +00:00
|
|
|
$node = Models\Node::getByID($server->node);
|
|
|
|
$client = Models\Node::guzzleRequest($server->node);
|
|
|
|
|
|
|
|
$res = $client->request('PATCH', '/server', [
|
|
|
|
'headers' => [
|
|
|
|
'X-Access-Server' => $server->uuid,
|
2016-12-07 22:46:38 +00:00
|
|
|
'X-Access-Token' => $node->daemonSecret,
|
2016-01-19 00:57:10 +00:00
|
|
|
],
|
|
|
|
'json' => [
|
|
|
|
'keys' => [
|
2016-12-07 22:46:38 +00:00
|
|
|
$subuser->daemonSecret => [],
|
|
|
|
],
|
|
|
|
],
|
2016-01-19 00:57:10 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
$subuser->delete();
|
|
|
|
DB::commit();
|
2016-12-07 22:46:38 +00:00
|
|
|
|
2016-01-19 00:57:10 +00:00
|
|
|
return true;
|
|
|
|
} catch (\GuzzleHttp\Exception\TransferException $ex) {
|
|
|
|
DB::rollBack();
|
2016-08-16 23:20:58 +00:00
|
|
|
throw new DisplayException('There was an error attempting to connect to the daemon to delete this subuser.', $ex);
|
2016-01-19 00:57:10 +00:00
|
|
|
} catch (\Exception $ex) {
|
|
|
|
DB::rollBack();
|
|
|
|
throw $ex;
|
|
|
|
}
|
2016-12-07 22:46:38 +00:00
|
|
|
|
2016-01-19 00:57:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-01-18 06:24:33 +00:00
|
|
|
/**
|
|
|
|
* Updates permissions for a given subuser.
|
2016-12-07 22:46:38 +00:00
|
|
|
* @param int $id The ID of the subuser row in MySQL. (Not the user ID)
|
2016-01-18 06:24:33 +00:00
|
|
|
* @param array $data
|
|
|
|
* @throws DisplayValidationException
|
|
|
|
* @throws DisplayException
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function update($id, array $data)
|
|
|
|
{
|
|
|
|
$validator = Validator::make($data, [
|
2016-01-19 00:57:10 +00:00
|
|
|
'permissions' => 'required|array',
|
|
|
|
'user' => 'required|exists:users,id',
|
|
|
|
'server' => 'required|exists:servers,id',
|
2016-01-18 06:24:33 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
if ($validator->fails()) {
|
|
|
|
throw new DisplayValidationException(json_encode($validator->all()));
|
|
|
|
}
|
|
|
|
|
2016-01-19 00:57:10 +00:00
|
|
|
$subuser = Models\Subuser::findOrFail($id);
|
|
|
|
$server = Models\Server::findOrFail($data['server']);
|
|
|
|
|
|
|
|
DB::beginTransaction();
|
2016-02-15 02:49:00 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
Models\Permission::where('user_id', $subuser->user_id)->where('server_id', $subuser->server_id)->delete();
|
|
|
|
|
|
|
|
$daemonPermissions = $this->coreDaemonPermissions;
|
2016-12-07 22:46:38 +00:00
|
|
|
foreach ($data['permissions'] as $permission) {
|
2016-02-15 02:49:00 +00:00
|
|
|
if (array_key_exists($permission, $this->permissions)) {
|
|
|
|
// Build the daemon permissions array for sending.
|
2016-12-07 22:46:38 +00:00
|
|
|
if (! is_null($this->permissions[$permission])) {
|
2016-02-15 02:49:00 +00:00
|
|
|
array_push($daemonPermissions, $this->permissions[$permission]);
|
|
|
|
}
|
|
|
|
$model = new Models\Permission;
|
|
|
|
$model->fill([
|
|
|
|
'user_id' => $data['user'],
|
|
|
|
'server_id' => $data['server'],
|
2016-12-07 22:46:38 +00:00
|
|
|
'permission' => $permission,
|
2016-02-15 02:49:00 +00:00
|
|
|
]);
|
|
|
|
$model->save();
|
2016-01-19 00:57:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-15 02:49:00 +00:00
|
|
|
// Contact Daemon
|
|
|
|
// We contact even if they don't have any daemon permissions to overwrite
|
|
|
|
// if they did have them previously.
|
2016-01-19 00:57:10 +00:00
|
|
|
$node = Models\Node::getByID($server->node);
|
|
|
|
$client = Models\Node::guzzleRequest($server->node);
|
|
|
|
|
|
|
|
$res = $client->request('PATCH', '/server', [
|
|
|
|
'headers' => [
|
|
|
|
'X-Access-Server' => $server->uuid,
|
2016-12-07 22:46:38 +00:00
|
|
|
'X-Access-Token' => $node->daemonSecret,
|
2016-01-19 00:57:10 +00:00
|
|
|
],
|
|
|
|
'json' => [
|
|
|
|
'keys' => [
|
2016-12-07 22:46:38 +00:00
|
|
|
$subuser->daemonSecret => $daemonPermissions,
|
|
|
|
],
|
|
|
|
],
|
2016-01-19 00:57:10 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
DB::commit();
|
2016-12-07 22:46:38 +00:00
|
|
|
|
2016-01-19 00:57:10 +00:00
|
|
|
return true;
|
|
|
|
} catch (\GuzzleHttp\Exception\TransferException $ex) {
|
|
|
|
DB::rollBack();
|
2016-08-16 23:20:58 +00:00
|
|
|
throw new DisplayException('There was an error attempting to connect to the daemon to update permissions.', $ex);
|
2016-01-19 00:57:10 +00:00
|
|
|
} catch (\Exception $ex) {
|
|
|
|
DB::rollBack();
|
|
|
|
throw $ex;
|
|
|
|
}
|
2016-12-07 22:46:38 +00:00
|
|
|
|
2016-01-19 00:57:10 +00:00
|
|
|
return false;
|
2016-01-18 06:24:33 +00:00
|
|
|
}
|
2016-01-19 00:57:10 +00:00
|
|
|
}
|