misc_pterodactyl-panel/app/Repositories/Eloquent/SubuserRepository.php

118 lines
3.3 KiB
PHP
Raw Normal View History

<?php
2017-09-26 02:43:01 +00:00
/**
* Pterodactyl - Panel
2017-01-24 22:57:08 +00:00
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
2017-09-26 02:43:01 +00:00
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
2016-12-07 22:46:38 +00:00
2017-08-24 02:34:11 +00:00
namespace Pterodactyl\Repositories\Eloquent;
2017-08-26 18:31:18 +00:00
use Webmozart\Assert\Assert;
2017-08-27 19:55:25 +00:00
use Pterodactyl\Models\Subuser;
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
use Pterodactyl\Contracts\Repository\SubuserRepositoryInterface;
2017-08-24 02:34:11 +00:00
class SubuserRepository extends EloquentRepository implements SubuserRepositoryInterface
{
2017-03-19 23:36:50 +00:00
/**
2017-08-24 02:34:11 +00:00
* {@inheritdoc}
2017-03-19 23:36:50 +00:00
*/
2017-08-24 02:34:11 +00:00
public function model()
{
2017-08-24 02:34:11 +00:00
return Subuser::class;
}
2017-08-26 18:31:18 +00:00
/**
* Return a subuser with the associated server relationship.
*
* @param \Pterodactyl\Models\Subuser $subuser
* @param bool $refresh
* @return \Pterodactyl\Models\Subuser
*/
public function getWithServer(Subuser $subuser, bool $refresh = false): Subuser
{
if (! $subuser->relationLoaded('server') || $refresh) {
$subuser->load('server');
2017-09-04 23:12:13 +00:00
}
return $subuser;
2017-09-04 23:12:13 +00:00
}
/**
* Return a subuser with the associated permissions relationship.
*
* @param \Pterodactyl\Models\Subuser $subuser
* @param bool $refresh
* @return \Pterodactyl\Models\Subuser
2017-09-04 23:12:13 +00:00
*/
public function getWithPermissions(Subuser $subuser, bool $refresh = false): Subuser
2017-09-04 23:12:13 +00:00
{
if (! $subuser->relationLoaded('permissions') || $refresh) {
$subuser->load('permissions');
}
2017-09-04 23:12:13 +00:00
if (! $subuser->relationLoaded('user') || $refresh) {
$subuser->load('user');
}
return $subuser;
}
/**
* {@inheritdoc}
*/
public function getWithPermissionsUsingUserAndServer($user, $server)
{
Assert::integerish($user, 'First argument passed to getWithPermissionsUsingUserAndServer must be integer, received %s.');
Assert::integerish($server, 'Second argument passed to getWithPermissionsUsingUserAndServer must be integer, received %s.');
$instance = $this->getBuilder()->with('permissions')->where([
['user_id', '=', $user],
['server_id', '=', $server],
])->first();
if (is_null($instance)) {
throw new RecordNotFoundException;
}
return $instance;
}
2017-08-26 18:31:18 +00:00
/**
* {@inheritdoc}
*/
public function getWithServerAndPermissions($id)
{
Assert::numeric($id, 'First argument passed to getWithServerAndPermissions must be numeric, received %s.');
2017-09-04 23:12:13 +00:00
$instance = $this->getBuilder()->with('server', 'permission', 'user')->find($id, $this->getColumns());
2017-08-26 18:31:18 +00:00
if (! $instance) {
throw new RecordNotFoundException;
}
return $instance;
}
/**
* {@inheritdoc}
*/
public function getWithKey($user, $server)
{
Assert::integerish($user, 'First argument passed to getWithKey must be integer, received %s.');
Assert::integerish($server, 'Second argument passed to getWithKey must be integer, received %s.');
$instance = $this->getBuilder()->with('key')->where([
['user_id', '=', $user],
['server_id', '=', $server],
])->first();
if (is_null($instance)) {
throw new RecordNotFoundException;
}
return $instance;
}
}