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
|
2017-01-24 22:57:08 +00:00
|
|
|
* Copyright (c) 2015 - 2017 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;
|
|
|
|
use Validator;
|
2017-04-18 01:47:11 +00:00
|
|
|
use Pterodactyl\Models\User;
|
|
|
|
use Pterodactyl\Models\Server;
|
|
|
|
use Pterodactyl\Models\Subuser;
|
|
|
|
use Pterodactyl\Models\Permission;
|
2016-01-18 06:24:33 +00:00
|
|
|
use Pterodactyl\Services\UuidService;
|
2017-04-18 01:47:11 +00:00
|
|
|
use GuzzleHttp\Exception\TransferException;
|
2016-01-18 06:24:33 +00:00
|
|
|
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.
|
2017-03-19 23:36:50 +00:00
|
|
|
*
|
2016-01-19 00:57:10 +00:00
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $coreDaemonPermissions = [
|
|
|
|
's:get',
|
2016-12-07 22:46:38 +00:00
|
|
|
's:console',
|
2016-01-19 00:57:10 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new subuser on the server.
|
2017-03-19 23:36:50 +00:00
|
|
|
*
|
|
|
|
* @param int $sid
|
2016-01-19 00:57:10 +00:00
|
|
|
* @param array $data
|
2017-02-09 22:43:54 +00:00
|
|
|
* @return \Pterodactyl\Models\Subuser
|
2017-03-19 23:36:50 +00:00
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\DisplayException
|
|
|
|
* @throws \Pterodactyl\Exceptions\DisplayValidationException
|
2016-01-19 00:57:10 +00:00
|
|
|
*/
|
|
|
|
public function create($sid, array $data)
|
|
|
|
{
|
2017-04-18 01:47:11 +00:00
|
|
|
$server = Server::with('node')->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.
|
2017-04-18 01:47:11 +00:00
|
|
|
$user = 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-02-09 22:43:54 +00:00
|
|
|
$user = $repo->create([
|
2017-01-21 05:04:09 +00:00
|
|
|
'email' => $data['email'],
|
2017-02-09 22:43:54 +00:00
|
|
|
'username' => str_random(8),
|
|
|
|
'name_first' => 'Unassigned',
|
|
|
|
'name_last' => 'Name',
|
2017-01-21 05:04:09 +00:00
|
|
|
'root_admin' => false,
|
|
|
|
]);
|
2016-02-15 02:49:00 +00:00
|
|
|
} catch (\Exception $ex) {
|
|
|
|
throw $ex;
|
|
|
|
}
|
2017-02-02 23:21:36 +00:00
|
|
|
} elseif ($server->owner_id === $user->id) {
|
2016-12-30 21:28:43 +00:00
|
|
|
throw new DisplayException('You cannot add the owner of a server as a subuser.');
|
2017-04-18 01:47:11 +00:00
|
|
|
} elseif (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;
|
2017-04-18 01:47:11 +00:00
|
|
|
$subuser = Subuser::create([
|
2016-02-15 02:49:00 +00:00
|
|
|
'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
|
|
|
]);
|
|
|
|
|
2017-04-18 01:47:11 +00:00
|
|
|
$perms = Permission::list(true);
|
2016-02-15 02:49:00 +00:00
|
|
|
$daemonPermissions = $this->coreDaemonPermissions;
|
2017-03-30 19:30:59 +00:00
|
|
|
|
2016-12-07 22:46:38 +00:00
|
|
|
foreach ($data['permissions'] as $permission) {
|
2017-03-30 19:30:59 +00:00
|
|
|
if (array_key_exists($permission, $perms)) {
|
2016-02-15 02:49:00 +00:00
|
|
|
// Build the daemon permissions array for sending.
|
2017-03-30 19:30:59 +00:00
|
|
|
if (! is_null($perms[$permission])) {
|
|
|
|
array_push($daemonPermissions, $perms[$permission]);
|
2016-02-15 02:49:00 +00:00
|
|
|
}
|
2016-12-30 21:28:43 +00:00
|
|
|
|
2017-04-18 01:47:11 +00:00
|
|
|
Permission::create([
|
2017-02-10 00:38:54 +00:00
|
|
|
'subuser_id' => $subuser->id,
|
2016-12-07 22:46:38 +00:00
|
|
|
'permission' => $permission,
|
2016-02-15 02:49:00 +00:00
|
|
|
]);
|
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
|
|
|
|
2017-02-09 22:43:54 +00:00
|
|
|
$server->node->guzzleClient([
|
|
|
|
'X-Access-Server' => $server->uuid,
|
2017-02-17 23:26:47 +00:00
|
|
|
'X-Access-Token' => $server->node->daemonSecret,
|
2017-02-09 22:43:54 +00:00
|
|
|
])->request('PATCH', '/server', [
|
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
|
|
|
|
2017-02-09 22:43:54 +00:00
|
|
|
return $subuser;
|
2017-04-18 01:47:11 +00:00
|
|
|
} catch (TransferException $ex) {
|
2016-01-19 00:57:10 +00:00
|
|
|
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.
|
2017-03-19 23:36:50 +00:00
|
|
|
*
|
|
|
|
* @param int $id
|
2016-01-19 00:57:10 +00:00
|
|
|
* @return void
|
2017-03-19 23:36:50 +00:00
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\DisplayException
|
2016-01-19 00:57:10 +00:00
|
|
|
*/
|
|
|
|
public function delete($id)
|
|
|
|
{
|
2017-04-18 01:47:11 +00:00
|
|
|
$subuser = Subuser::with('server.node')->findOrFail($id);
|
2017-02-09 22:43:54 +00:00
|
|
|
$server = $subuser->server;
|
2016-01-19 00:57:10 +00:00
|
|
|
|
|
|
|
DB::beginTransaction();
|
|
|
|
|
|
|
|
try {
|
2017-02-09 22:43:54 +00:00
|
|
|
$server->node->guzzleClient([
|
|
|
|
'X-Access-Server' => $server->uuid,
|
|
|
|
'X-Access-Token' => $server->node->daemonSecret,
|
|
|
|
])->request('PATCH', '/server', [
|
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
|
|
|
]);
|
|
|
|
|
2017-02-12 20:10:39 +00:00
|
|
|
foreach ($subuser->permissions as &$permission) {
|
2017-02-10 00:38:54 +00:00
|
|
|
$permission->delete();
|
|
|
|
}
|
2016-01-19 00:57:10 +00:00
|
|
|
$subuser->delete();
|
|
|
|
DB::commit();
|
2017-04-18 01:47:11 +00:00
|
|
|
} catch (TransferException $ex) {
|
2016-01-19 00:57:10 +00:00
|
|
|
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-01-18 06:24:33 +00:00
|
|
|
/**
|
|
|
|
* Updates permissions for a given subuser.
|
2017-03-19 23:36:50 +00:00
|
|
|
*
|
|
|
|
* @param int $id
|
2016-01-18 06:24:33 +00:00
|
|
|
* @param array $data
|
|
|
|
* @return void
|
2017-03-19 23:36:50 +00:00
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\DisplayException
|
|
|
|
* @throws \Pterodactyl\Exceptions\DisplayValidationException
|
2016-01-18 06:24:33 +00:00
|
|
|
*/
|
|
|
|
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()));
|
|
|
|
}
|
|
|
|
|
2017-04-18 01:47:11 +00:00
|
|
|
$subuser = Subuser::with('server.node')->findOrFail($id);
|
2017-02-09 22:43:54 +00:00
|
|
|
$server = $subuser->server;
|
2016-01-19 00:57:10 +00:00
|
|
|
|
|
|
|
DB::beginTransaction();
|
2016-02-15 02:49:00 +00:00
|
|
|
|
|
|
|
try {
|
2017-02-12 20:10:39 +00:00
|
|
|
foreach ($subuser->permissions as &$permission) {
|
2017-02-10 00:38:54 +00:00
|
|
|
$permission->delete();
|
|
|
|
}
|
2016-02-15 02:49:00 +00:00
|
|
|
|
2017-03-30 19:30:59 +00:00
|
|
|
$perms = Permission::list(true);
|
2016-02-15 02:49:00 +00:00
|
|
|
$daemonPermissions = $this->coreDaemonPermissions;
|
2017-03-30 19:30:59 +00:00
|
|
|
|
2016-12-07 22:46:38 +00:00
|
|
|
foreach ($data['permissions'] as $permission) {
|
2017-03-30 19:30:59 +00:00
|
|
|
if (array_key_exists($permission, $perms)) {
|
2016-02-15 02:49:00 +00:00
|
|
|
// Build the daemon permissions array for sending.
|
2017-03-30 19:30:59 +00:00
|
|
|
if (! is_null($perms[$permission])) {
|
|
|
|
array_push($daemonPermissions, $perms[$permission]);
|
2016-02-15 02:49:00 +00:00
|
|
|
}
|
2017-04-18 01:47:11 +00:00
|
|
|
Permission::create([
|
2017-02-10 00:38:54 +00:00
|
|
|
'subuser_id' => $subuser->id,
|
2016-12-07 22:46:38 +00:00
|
|
|
'permission' => $permission,
|
2016-02-15 02:49:00 +00:00
|
|
|
]);
|
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.
|
2017-02-09 22:43:54 +00:00
|
|
|
$server->node->guzzleClient([
|
|
|
|
'X-Access-Server' => $server->uuid,
|
|
|
|
'X-Access-Token' => $server->node->daemonSecret,
|
|
|
|
])->request('PATCH', '/server', [
|
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();
|
2017-04-18 01:47:11 +00:00
|
|
|
} catch (TransferException $ex) {
|
2016-01-19 00:57:10 +00:00
|
|
|
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-01-18 06:24:33 +00:00
|
|
|
}
|
2016-01-19 00:57:10 +00:00
|
|
|
}
|