Remove all references to daemon keys from the codebase

This commit is contained in:
Dane Everitt 2020-09-13 11:38:42 -07:00
parent 4dddcaebb0
commit 703f55271d
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
13 changed files with 43 additions and 529 deletions

View file

@ -1,52 +0,0 @@
<?php
namespace Pterodactyl\Contracts\Repository;
use Pterodactyl\Models\User;
use Pterodactyl\Models\DaemonKey;
use Illuminate\Support\Collection;
interface DaemonKeyRepositoryInterface extends RepositoryInterface
{
/**
* String prepended to keys to identify that they are managed internally and not part of the user API.
*/
const INTERNAL_KEY_IDENTIFIER = 'i_';
/**
* Load the server and user relations onto a key model.
*
* @param \Pterodactyl\Models\DaemonKey $key
* @param bool $refresh
* @return \Pterodactyl\Models\DaemonKey
*/
public function loadServerAndUserRelations(DaemonKey $key, bool $refresh = false): DaemonKey;
/**
* Return a daemon key with the associated server relation attached.
*
* @param string $key
* @return \Pterodactyl\Models\DaemonKey
*
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function getKeyWithServer(string $key): DaemonKey;
/**
* Get all of the keys for a specific user including the information needed
* from their server relation for revocation on the daemon.
*
* @param \Pterodactyl\Models\User $user
* @return \Illuminate\Support\Collection
*/
public function getKeysForRevocation(User $user): Collection;
/**
* Delete an array of daemon keys from the database. Used primarily in
* conjunction with getKeysForRevocation.
*
* @param array $ids
* @return bool|int
*/
public function deleteKeys(array $ids);
}

View file

@ -1,78 +0,0 @@
<?php
namespace Pterodactyl\Models;
use Znck\Eloquent\Traits\BelongsToThrough;
class DaemonKey extends Model
{
use BelongsToThrough;
/**
* @var string
*/
protected $table = 'daemon_keys';
/**
* @var array
*/
protected $casts = [
'user_id' => 'integer',
'server_id' => 'integer',
];
/**
* @var array
*/
protected $dates = [
self::CREATED_AT,
self::UPDATED_AT,
'expires_at',
];
/**
* @var array
*/
protected $fillable = ['user_id', 'server_id', 'secret', 'expires_at'];
/**
* @var array
*/
public static $validationRules = [
'user_id' => 'required|numeric|exists:users,id',
'server_id' => 'required|numeric|exists:servers,id',
'secret' => 'required|string|min:20',
'expires_at' => 'required|date',
];
/**
* Return the server relation.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function server()
{
return $this->belongsTo(Server::class);
}
/**
* Return the node relation.
*
* @return \Znck\Eloquent\Relations\BelongsToThrough
* @throws \Exception
*/
public function node()
{
return $this->belongsToThrough(Node::class, Server::class);
}
/**
* Return the user relation.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function user()
{
return $this->belongsTo(User::class);
}
}

View file

@ -47,8 +47,6 @@ use Znck\Eloquent\Traits\BelongsToThrough;
* @property \Pterodactyl\Models\Schedule[]|\Illuminate\Database\Eloquent\Collection $schedule * @property \Pterodactyl\Models\Schedule[]|\Illuminate\Database\Eloquent\Collection $schedule
* @property \Pterodactyl\Models\Database[]|\Illuminate\Database\Eloquent\Collection $databases * @property \Pterodactyl\Models\Database[]|\Illuminate\Database\Eloquent\Collection $databases
* @property \Pterodactyl\Models\Location $location * @property \Pterodactyl\Models\Location $location
* @property \Pterodactyl\Models\DaemonKey $key
* @property \Pterodactyl\Models\DaemonKey[]|\Illuminate\Database\Eloquent\Collection $keys
* @property \Pterodactyl\Models\ServerTransfer $transfer * @property \Pterodactyl\Models\ServerTransfer $transfer
* @property \Pterodactyl\Models\Backup[]|\Illuminate\Database\Eloquent\Collection $backups * @property \Pterodactyl\Models\Backup[]|\Illuminate\Database\Eloquent\Collection $backups
* @property \Pterodactyl\Models\Mount[]|\Illuminate\Database\Eloquent\Collection $mounts * @property \Pterodactyl\Models\Mount[]|\Illuminate\Database\Eloquent\Collection $mounts
@ -294,26 +292,6 @@ class Server extends Model
return $this->belongsToThrough(Location::class, Node::class); return $this->belongsToThrough(Location::class, Node::class);
} }
/**
* Return the key belonging to the server owner.
*
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function key()
{
return $this->hasOne(DaemonKey::class, 'user_id', 'owner_id');
}
/**
* Returns all of the daemon keys belonging to this server.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function keys()
{
return $this->hasMany(DaemonKey::class);
}
/** /**
* Returns the associated server transfer. * Returns the associated server transfer.
* *

View file

@ -99,14 +99,4 @@ class Subuser extends Model
{ {
return $this->hasMany(Permission::class); return $this->hasMany(Permission::class);
} }
/**
* Return the key that belongs to this subuser for the server.
*
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function key()
{
return $this->hasOne(DaemonKey::class, 'server_id', 'server_id')->where('daemon_keys.user_id', '=', $this->user_id);
}
} }

View file

@ -38,7 +38,6 @@ use Pterodactyl\Notifications\SendPasswordReset as ResetPasswordNotification;
* @property string $name * @property string $name
* @property \Pterodactyl\Models\ApiKey[]|\Illuminate\Database\Eloquent\Collection $apiKeys * @property \Pterodactyl\Models\ApiKey[]|\Illuminate\Database\Eloquent\Collection $apiKeys
* @property \Pterodactyl\Models\Server[]|\Illuminate\Database\Eloquent\Collection $servers * @property \Pterodactyl\Models\Server[]|\Illuminate\Database\Eloquent\Collection $servers
* @property \Pterodactyl\Models\DaemonKey[]|\Illuminate\Database\Eloquent\Collection $keys
* @property \Pterodactyl\Models\RecoveryToken[]|\Illuminate\Database\Eloquent\Collection $recoveryTokens * @property \Pterodactyl\Models\RecoveryToken[]|\Illuminate\Database\Eloquent\Collection $recoveryTokens
*/ */
class User extends Model implements class User extends Model implements
@ -214,16 +213,6 @@ class User extends Model implements
return $this->hasMany(Server::class, 'owner_id'); return $this->hasMany(Server::class, 'owner_id');
} }
/**
* Return all of the daemon keys that a user belongs to.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function keys()
{
return $this->hasMany(DaemonKey::class);
}
/** /**
* @return \Illuminate\Database\Eloquent\Relations\HasMany * @return \Illuminate\Database\Eloquent\Relations\HasMany
*/ */

View file

@ -16,7 +16,6 @@ use Pterodactyl\Repositories\Eloquent\DatabaseRepository;
use Pterodactyl\Repositories\Eloquent\LocationRepository; use Pterodactyl\Repositories\Eloquent\LocationRepository;
use Pterodactyl\Repositories\Eloquent\ScheduleRepository; use Pterodactyl\Repositories\Eloquent\ScheduleRepository;
use Pterodactyl\Repositories\Eloquent\SettingsRepository; use Pterodactyl\Repositories\Eloquent\SettingsRepository;
use Pterodactyl\Repositories\Eloquent\DaemonKeyRepository;
use Pterodactyl\Repositories\Eloquent\AllocationRepository; use Pterodactyl\Repositories\Eloquent\AllocationRepository;
use Pterodactyl\Contracts\Repository\EggRepositoryInterface; use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
use Pterodactyl\Repositories\Eloquent\EggVariableRepository; use Pterodactyl\Repositories\Eloquent\EggVariableRepository;
@ -34,7 +33,6 @@ use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
use Pterodactyl\Contracts\Repository\LocationRepositoryInterface; use Pterodactyl\Contracts\Repository\LocationRepositoryInterface;
use Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface; use Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface;
use Pterodactyl\Contracts\Repository\SettingsRepositoryInterface; use Pterodactyl\Contracts\Repository\SettingsRepositoryInterface;
use Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface;
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface; use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
use Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface; use Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface;
use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface; use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface;
@ -50,7 +48,6 @@ class RepositoryServiceProvider extends ServiceProvider
// Eloquent Repositories // Eloquent Repositories
$this->app->bind(AllocationRepositoryInterface::class, AllocationRepository::class); $this->app->bind(AllocationRepositoryInterface::class, AllocationRepository::class);
$this->app->bind(ApiKeyRepositoryInterface::class, ApiKeyRepository::class); $this->app->bind(ApiKeyRepositoryInterface::class, ApiKeyRepository::class);
$this->app->bind(DaemonKeyRepositoryInterface::class, DaemonKeyRepository::class);
$this->app->bind(DatabaseRepositoryInterface::class, DatabaseRepository::class); $this->app->bind(DatabaseRepositoryInterface::class, DatabaseRepository::class);
$this->app->bind(DatabaseHostRepositoryInterface::class, DatabaseHostRepository::class); $this->app->bind(DatabaseHostRepositoryInterface::class, DatabaseHostRepository::class);
$this->app->bind(EggRepositoryInterface::class, EggRepository::class); $this->app->bind(EggRepositoryInterface::class, EggRepository::class);

View file

@ -1,87 +0,0 @@
<?php
namespace Pterodactyl\Repositories\Eloquent;
use Pterodactyl\Models\User;
use Webmozart\Assert\Assert;
use Pterodactyl\Models\DaemonKey;
use Illuminate\Support\Collection;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
use Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface;
class DaemonKeyRepository extends EloquentRepository implements DaemonKeyRepositoryInterface
{
/**
* Return the model backing this repository.
*
* @return string
*/
public function model()
{
return DaemonKey::class;
}
/**
* Load the server and user relations onto a key model.
*
* @param \Pterodactyl\Models\DaemonKey $key
* @param bool $refresh
* @return \Pterodactyl\Models\DaemonKey
*/
public function loadServerAndUserRelations(DaemonKey $key, bool $refresh = false): DaemonKey
{
if (! $key->relationLoaded('server') || $refresh) {
$key->load('server');
}
if (! $key->relationLoaded('user') || $refresh) {
$key->load('user');
}
return $key;
}
/**
* Return a daemon key with the associated server relation attached.
*
* @param string $key
* @return \Pterodactyl\Models\DaemonKey
*
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function getKeyWithServer(string $key): DaemonKey
{
Assert::notEmpty($key, 'Expected non-empty string as first argument passed to ' . __METHOD__);
try {
return $this->getBuilder()->with('server')->where('secret', '=', $key)->firstOrFail($this->getColumns());
} catch (ModelNotFoundException $exception) {
throw new RecordNotFoundException;
}
}
/**
* Get all of the keys for a specific user including the information needed
* from their server relation for revocation on the daemon.
*
* @param \Pterodactyl\Models\User $user
* @return \Illuminate\Support\Collection
*/
public function getKeysForRevocation(User $user): Collection
{
return $this->getBuilder()->with('node')->where('user_id', $user->id)->get($this->getColumns());
}
/**
* Delete an array of daemon keys from the database. Used primarily in
* conjunction with getKeysForRevocation.
*
* @param array $ids
* @return bool|int
*/
public function deleteKeys(array $ids)
{
return $this->getBuilder()->whereIn('id', $ids)->delete();
}
}

View file

@ -1,76 +0,0 @@
<?php
namespace Pterodactyl\Transformers\Daemon;
use Carbon\Carbon;
use Pterodactyl\Models\DaemonKey;
use Pterodactyl\Models\Permission;
use League\Fractal\TransformerAbstract;
use Pterodactyl\Contracts\Repository\SubuserRepositoryInterface;
use Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface;
class ApiKeyTransformer extends TransformerAbstract
{
/**
* @var \Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface
*/
private $keyRepository;
/**
* @var \Pterodactyl\Contracts\Repository\SubuserRepositoryInterface
*/
private $repository;
/**
* ApiKeyTransformer constructor.
*
* @param \Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface $keyRepository
* @param \Pterodactyl\Contracts\Repository\SubuserRepositoryInterface $repository
*/
public function __construct(DaemonKeyRepositoryInterface $keyRepository, SubuserRepositoryInterface $repository)
{
$this->repository = $repository;
$this->keyRepository = $keyRepository;
}
/**
* Return a listing of servers that a daemon key can access.
*
* @param \Pterodactyl\Models\DaemonKey $key
* @return array
*
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function transform(DaemonKey $key)
{
$this->keyRepository->loadServerAndUserRelations($key);
if ($key->user_id === $key->getRelation('server')->owner_id || $key->getRelation('user')->root_admin) {
return [
'id' => $key->getRelation('server')->uuid,
'is_temporary' => true,
'expires_in' => max(Carbon::now()->diffInSeconds($key->expires_at, false), 0),
'permissions' => ['s:*'],
];
}
$subuser = $this->repository->getWithPermissionsUsingUserAndServer($key->user_id, $key->server_id);
$permissions = $subuser->getRelation('permissions')->pluck('permission')->toArray();
$mappings = Permission::getPermissions(true);
$daemonPermissions = ['s:console'];
foreach ($permissions as $permission) {
if (! is_null(array_get($mappings, $permission))) {
$daemonPermissions[] = array_get($mappings, $permission);
}
}
return [
'id' => $key->getRelation('server')->uuid,
'is_temporary' => true,
'expires_in' => max(Carbon::now()->diffInSeconds($key->expires_at, false), 0),
'permissions' => $daemonPermissions,
];
}
}

View file

@ -181,13 +181,6 @@ $factory->define(Pterodactyl\Models\Task::class, function (Faker $faker) {
]; ];
}); });
$factory->define(Pterodactyl\Models\DaemonKey::class, function (Faker $faker) {
return [
'secret' => 'i_' . str_random(40),
'expires_at' => \Carbon\Carbon::now()->addMinutes(10)->toDateTimeString(),
];
});
$factory->define(Pterodactyl\Models\ApiKey::class, function (Faker $faker) { $factory->define(Pterodactyl\Models\ApiKey::class, function (Faker $faker) {
static $token; static $token;

View file

@ -0,0 +1,40 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class DropDaemonKeyTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::dropIfExists('daemon_keys');
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::create('daemon_keys', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('server_id');
$table->unsignedInteger('user_id');
$table->string('secret')->unique();
$table->timestamp('expires_at');
$table->timestamps();
});
Schema::table('daemon_keys', function (Blueprint $table) {
$table->foreign('server_id')->references('id')->on('servers')->cascadeOnDelete();
$table->foreign('user_id')->references('id')->on('users')->cascadeOnDelete();
});
}
}

View file

@ -11,9 +11,7 @@ use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Exceptions\DisplayException; use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Services\Users\UserCreationService; use Pterodactyl\Services\Users\UserCreationService;
use Pterodactyl\Services\Subusers\SubuserCreationService; use Pterodactyl\Services\Subusers\SubuserCreationService;
use Pterodactyl\Services\Subusers\PermissionCreationService;
use Pterodactyl\Contracts\Repository\UserRepositoryInterface; use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
use Pterodactyl\Services\DaemonKeys\DaemonKeyCreationService;
use Pterodactyl\Exceptions\Repository\RecordNotFoundException; use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
use Pterodactyl\Contracts\Repository\SubuserRepositoryInterface; use Pterodactyl\Contracts\Repository\SubuserRepositoryInterface;
@ -28,17 +26,7 @@ class SubuserCreationServiceTest extends TestCase
protected $connection; protected $connection;
/** /**
* @var \Pterodactyl\Services\DaemonKeys\DaemonKeyCreationService|\Mockery\Mock * @var \Pterodactyl\Repositories\Eloquent\SubuserRepository|\Mockery\Mock
*/
protected $keyCreationService;
/**
* @var \Pterodactyl\Services\Subusers\PermissionCreationService|\Mockery\Mock
*/
protected $permissionService;
/**
* @var \Pterodactyl\Contracts\Repository\SubuserRepositoryInterface|\Mockery\Mock
*/ */
protected $subuserRepository; protected $subuserRepository;
@ -70,8 +58,6 @@ class SubuserCreationServiceTest extends TestCase
parent::setUp(); parent::setUp();
$this->connection = m::mock(ConnectionInterface::class); $this->connection = m::mock(ConnectionInterface::class);
$this->keyCreationService = m::mock(DaemonKeyCreationService::class);
$this->permissionService = m::mock(PermissionCreationService::class);
$this->subuserRepository = m::mock(SubuserRepositoryInterface::class); $this->subuserRepository = m::mock(SubuserRepositoryInterface::class);
$this->serverRepository = m::mock(ServerRepositoryInterface::class); $this->serverRepository = m::mock(ServerRepositoryInterface::class);
$this->userCreationService = m::mock(UserCreationService::class); $this->userCreationService = m::mock(UserCreationService::class);
@ -107,8 +93,6 @@ class SubuserCreationServiceTest extends TestCase
$this->subuserRepository->shouldReceive('create')->with(['user_id' => $user->id, 'server_id' => $server->id]) $this->subuserRepository->shouldReceive('create')->with(['user_id' => $user->id, 'server_id' => $server->id])
->once()->andReturn($subuser); ->once()->andReturn($subuser);
$this->keyCreationService->shouldReceive('handle')->with($server->id, $user->id)->once()->andReturnNull();
$this->permissionService->shouldReceive('handle')->with($subuser->id, array_keys($permissions))->once()->andReturnNull();
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull(); $this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$response = $this->getService()->handle($server, $user->email, array_keys($permissions)); $response = $this->getService()->handle($server, $user->email, array_keys($permissions));
@ -136,8 +120,6 @@ class SubuserCreationServiceTest extends TestCase
$this->subuserRepository->shouldReceive('create')->with(['user_id' => $user->id, 'server_id' => $server->id]) $this->subuserRepository->shouldReceive('create')->with(['user_id' => $user->id, 'server_id' => $server->id])
->once()->andReturn($subuser); ->once()->andReturn($subuser);
$this->keyCreationService->shouldReceive('handle')->with($server->id, $user->id)->once()->andReturnNull();
$this->permissionService->shouldReceive('handle')->with($subuser->id, $permissions)->once()->andReturnNull();
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull(); $this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$response = $this->getService()->handle($server->id, $user->email, $permissions); $response = $this->getService()->handle($server->id, $user->email, $permissions);
@ -196,9 +178,6 @@ class SubuserCreationServiceTest extends TestCase
{ {
return new SubuserCreationService( return new SubuserCreationService(
$this->connection, $this->connection,
$this->keyCreationService,
$this->permissionService,
$this->serverRepository,
$this->subuserRepository, $this->subuserRepository,
$this->userCreationService, $this->userCreationService,
$this->userRepository $this->userRepository

View file

@ -1,147 +0,0 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Tests\Unit\Services\Subusers;
use Mockery as m;
use Tests\TestCase;
use Pterodactyl\Models\User;
use GuzzleHttp\Psr7\Response;
use Pterodactyl\Models\Server;
use Pterodactyl\Models\Subuser;
use Tests\Traits\MocksRequestException;
use GuzzleHttp\Exception\RequestException;
use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Exceptions\PterodactylException;
use Pterodactyl\Services\Subusers\SubuserUpdateService;
use Pterodactyl\Services\Subusers\PermissionCreationService;
use Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService;
use Pterodactyl\Contracts\Repository\SubuserRepositoryInterface;
use Pterodactyl\Contracts\Repository\PermissionRepositoryInterface;
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface;
class SubuserUpdateServiceTest extends TestCase
{
use MocksRequestException;
/**
* @var \Illuminate\Database\ConnectionInterface|\Mockery\Mock
*/
private $connection;
/**
* @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface|\Mockery\Mock
*/
private $daemonRepository;
/**
* @var \Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService|\Mockery\Mock
*/
private $keyProviderService;
/**
* @var \Pterodactyl\Contracts\Repository\PermissionRepositoryInterface|\Mockery\Mock
*/
private $permissionRepository;
/**
* @var \Pterodactyl\Services\Subusers\PermissionCreationService|\Mockery\Mock
*/
private $permissionService;
/**
* @var \Pterodactyl\Contracts\Repository\SubuserRepositoryInterface|\Mockery\Mock
*/
private $repository;
/**
* Setup tests.
*/
public function setUp(): void
{
parent::setUp();
$this->connection = m::mock(ConnectionInterface::class);
$this->daemonRepository = m::mock(DaemonServerRepositoryInterface::class);
$this->keyProviderService = m::mock(DaemonKeyProviderService::class);
$this->permissionRepository = m::mock(PermissionRepositoryInterface::class);
$this->permissionService = m::mock(PermissionCreationService::class);
$this->repository = m::mock(SubuserRepositoryInterface::class);
}
/**
* Test that permissions are updated in the database.
*/
public function testPermissionsAreUpdated()
{
$subuser = factory(Subuser::class)->make();
$subuser->setRelation('server', factory(Server::class)->make());
$subuser->setRelation('user', factory(User::class)->make());
$this->repository->shouldReceive('loadServerAndUserRelations')->with($subuser)->once()->andReturn($subuser);
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->permissionRepository->shouldReceive('deleteWhere')->with([['subuser_id', '=', $subuser->id]])->once()->andReturn(1);
$this->permissionService->shouldReceive('handle')->with($subuser->id, ['some-permission'])->once()->andReturnNull();
$this->keyProviderService->shouldReceive('handle')->with($subuser->server, $subuser->user, false)->once()->andReturn('test123');
$this->daemonRepository->shouldReceive('setServer')->with($subuser->server)->once()->andReturnSelf();
$this->daemonRepository->shouldReceive('revokeAccessKey')->with('test123')->once()->andReturn(new Response);
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$this->getService()->handle($subuser, ['some-permission']);
$this->assertTrue(true);
}
/**
* Test that an exception is thrown if the daemon connection fails.
*/
public function testExceptionIsThrownIfDaemonConnectionFails()
{
$this->configureExceptionMock();
$subuser = factory(Subuser::class)->make();
$subuser->setRelation('server', factory(Server::class)->make());
$subuser->setRelation('user', factory(User::class)->make());
$this->repository->shouldReceive('loadServerAndUserRelations')->with($subuser)->once()->andReturn($subuser);
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->permissionRepository->shouldReceive('deleteWhere')->with([['subuser_id', '=', $subuser->id]])->once()->andReturn(1);
$this->permissionService->shouldReceive('handle')->with($subuser->id, [])->once()->andReturnNull();
$this->keyProviderService->shouldReceive('handle')->with($subuser->server, $subuser->user, false)->once()->andReturn('test123');
$this->daemonRepository->shouldReceive('setServer')->with($subuser->server)->once()->andThrow($this->getExceptionMock());
$this->connection->shouldReceive('rollBack')->withNoArgs()->once()->andReturnNull();
try {
$this->getService()->handle($subuser, []);
} catch (PterodactylException $exception) {
$this->assertInstanceOf(DaemonConnectionException::class, $exception);
$this->assertInstanceOf(RequestException::class, $exception->getPrevious());
}
}
/**
* Return an instance of the service with mocked dependencies for testing.
*
* @return \Pterodactyl\Services\Subusers\SubuserUpdateService
*/
private function getService(): SubuserUpdateService
{
return new SubuserUpdateService(
$this->connection,
$this->keyProviderService,
$this->daemonRepository,
$this->permissionService,
$this->permissionRepository,
$this->repository
);
}
}

View file

@ -9,7 +9,6 @@ use Illuminate\Support\Collection;
use Illuminate\Contracts\Hashing\Hasher; use Illuminate\Contracts\Hashing\Hasher;
use Pterodactyl\Services\Users\UserUpdateService; use Pterodactyl\Services\Users\UserUpdateService;
use Pterodactyl\Contracts\Repository\UserRepositoryInterface; use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
use Pterodactyl\Services\DaemonKeys\RevokeMultipleDaemonKeysService;
class UserUpdateServiceTest extends TestCase class UserUpdateServiceTest extends TestCase
{ {
@ -19,15 +18,10 @@ class UserUpdateServiceTest extends TestCase
private $hasher; private $hasher;
/** /**
* @var \Pterodactyl\Contracts\Repository\UserRepositoryInterface|\Mockery\Mock * @var \Pterodactyl\Repositories\Eloquent\UserRepository|\Mockery\Mock
*/ */
private $repository; private $repository;
/**
* @var \Pterodactyl\Services\DaemonKeys\RevokeMultipleDaemonKeysService|\Mockery\Mock
*/
private $revocationService;
/** /**
* Setup tests. * Setup tests.
*/ */
@ -37,7 +31,6 @@ class UserUpdateServiceTest extends TestCase
$this->hasher = m::mock(Hasher::class); $this->hasher = m::mock(Hasher::class);
$this->repository = m::mock(UserRepositoryInterface::class); $this->repository = m::mock(UserRepositoryInterface::class);
$this->revocationService = m::mock(RevokeMultipleDaemonKeysService::class);
} }
/** /**
@ -49,7 +42,6 @@ class UserUpdateServiceTest extends TestCase
public function testUpdateUserWithoutTouchingHasherIfNoPasswordPassed(array $data) public function testUpdateUserWithoutTouchingHasherIfNoPasswordPassed(array $data)
{ {
$user = factory(User::class)->make(); $user = factory(User::class)->make();
$this->revocationService->shouldReceive('getExceptions')->withNoArgs()->once()->andReturn([]);
$this->repository->shouldReceive('update')->with($user->id, ['test-data' => 'value'])->once()->andReturnNull(); $this->repository->shouldReceive('update')->with($user->id, ['test-data' => 'value'])->once()->andReturnNull();
$response = $this->getService()->handle($user, $data); $response = $this->getService()->handle($user, $data);
@ -80,7 +72,6 @@ class UserUpdateServiceTest extends TestCase
{ {
$user = factory(User::class)->make(); $user = factory(User::class)->make();
$this->hasher->shouldReceive('make')->with('raw_pass')->once()->andReturn('enc_pass'); $this->hasher->shouldReceive('make')->with('raw_pass')->once()->andReturn('enc_pass');
$this->revocationService->shouldReceive('getExceptions')->withNoArgs()->once()->andReturn([]);
$this->repository->shouldReceive('update')->with($user->id, ['password' => 'enc_pass'])->once()->andReturnNull(); $this->repository->shouldReceive('update')->with($user->id, ['password' => 'enc_pass'])->once()->andReturnNull();
$response = $this->getService()->handle($user, ['password' => 'raw_pass']); $response = $this->getService()->handle($user, ['password' => 'raw_pass']);
@ -98,8 +89,6 @@ class UserUpdateServiceTest extends TestCase
$service = $this->getService(); $service = $this->getService();
$service->setUserLevel(User::USER_LEVEL_ADMIN); $service->setUserLevel(User::USER_LEVEL_ADMIN);
$this->revocationService->shouldReceive('handle')->with($user, false)->once()->andReturnNull();
$this->revocationService->shouldReceive('getExceptions')->withNoArgs()->once()->andReturn([]);
$this->repository->shouldReceive('update')->with($user->id, ['root_admin' => false])->once()->andReturnNull(); $this->repository->shouldReceive('update')->with($user->id, ['root_admin' => false])->once()->andReturnNull();
$response = $service->handle($user, ['root_admin' => false]); $response = $service->handle($user, ['root_admin' => false]);
@ -117,7 +106,6 @@ class UserUpdateServiceTest extends TestCase
$service = $this->getService(); $service = $this->getService();
$service->setUserLevel(User::USER_LEVEL_USER); $service->setUserLevel(User::USER_LEVEL_USER);
$this->revocationService->shouldReceive('getExceptions')->withNoArgs()->once()->andReturn([]);
$this->repository->shouldReceive('update')->with($user->id, [])->once()->andReturnNull(); $this->repository->shouldReceive('update')->with($user->id, [])->once()->andReturnNull();
$response = $service->handle($user, ['root_admin' => true]); $response = $service->handle($user, ['root_admin' => true]);
@ -133,6 +121,6 @@ class UserUpdateServiceTest extends TestCase
*/ */
private function getService(): UserUpdateService private function getService(): UserUpdateService
{ {
return new UserUpdateService($this->hasher, $this->revocationService, $this->repository); return new UserUpdateService($this->hasher, $this->repository);
} }
} }