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

108 lines
3 KiB
PHP
Raw Normal View History

<?php
2016-12-07 22:46:38 +00:00
2017-08-24 02:34:11 +00:00
namespace Pterodactyl\Repositories\Eloquent;
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
/**
* Return the model backing this repository.
*
* @return string
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
2019-11-03 20:20:11 +00:00
/**
* Returns a subuser model for the given user and server combination. If no record
* exists an exception will be thrown.
2019-11-03 20:20:11 +00:00
*
* @param int $server
* @param string $uuid
* @return \Pterodactyl\Models\Subuser
*
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
2019-11-03 20:20:11 +00:00
*/
public function getUserForServer(int $server, string $uuid): Subuser
2019-11-03 20:20:11 +00:00
{
/** @var \Pterodactyl\Models\Subuser $model */
$model = $this->getBuilder()
->with('server', 'user')
->select('subusers.*')
->join('users', 'users.id', '=', 'subusers.user_id')
->where('subusers.server_id', $server)
->where('users.uuid', $uuid)
->firstOrFail();
return $model;
2019-11-03 20:20:11 +00:00
}
/**
* Return a subuser with the associated server relationship.
*
* @param \Pterodactyl\Models\Subuser $subuser
2019-09-06 04:32:57 +00:00
* @param bool $refresh
* @return \Pterodactyl\Models\Subuser
*/
public function loadServerAndUserRelations(Subuser $subuser, bool $refresh = false): Subuser
{
if (! $subuser->relationLoaded('server') || $refresh) {
$subuser->load('server');
2017-09-04 23:12:13 +00:00
}
if (! $subuser->relationLoaded('user') || $refresh) {
$subuser->load('user');
}
return $subuser;
2017-09-04 23:12:13 +00:00
}
/**
* Return a subuser with the associated permissions relationship.
*
* @param \Pterodactyl\Models\Subuser $subuser
2019-09-06 04:32:57 +00:00
* @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;
}
/**
* Return a subuser and associated permissions given a user_id and server_id.
*
* @param int $user
* @param int $server
* @return \Pterodactyl\Models\Subuser
*
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function getWithPermissionsUsingUserAndServer(int $user, int $server): Subuser
{
$instance = $this->getBuilder()->with('permissions')->where([
['user_id', '=', $user],
['server_id', '=', $server],
])->first();
if (is_null($instance)) {
throw new RecordNotFoundException;
}
return $instance;
}
}