From 99812b040731b90854598ce8c48c3a2fc0572b97 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Thu, 9 Feb 2017 19:38:54 -0500 Subject: [PATCH] More subuser Model updates. --- .../Controllers/Server/SubuserController.php | 45 +++++-------------- app/Models/Permission.php | 8 ++++ app/Models/Server.php | 10 +++++ app/Models/Subuser.php | 2 +- app/Repositories/SubuserRepository.php | 21 +++++---- ...2_09_174834_SetupPermissionsPivotTable.php | 3 ++ .../pterodactyl/server/users/index.blade.php | 16 +++---- .../pterodactyl/server/users/view.blade.php | 4 +- 8 files changed, 54 insertions(+), 55 deletions(-) diff --git a/app/Http/Controllers/Server/SubuserController.php b/app/Http/Controllers/Server/SubuserController.php index ed25d4a2c..76735e351 100644 --- a/app/Http/Controllers/Server/SubuserController.php +++ b/app/Http/Controllers/Server/SubuserController.php @@ -49,7 +49,7 @@ class SubuserController extends Controller public function getIndex(Request $request, $uuid) { - $server = Models\Server::byUuid($uuid); + $server = Models\Server::byUuid($uuid)->load('subusers.user'); $this->authorize('list-subusers', $server); $server->js(); @@ -57,35 +57,17 @@ class SubuserController extends Controller return view('server.users.index', [ 'server' => $server, 'node' => $server->node, - 'subusers' => Models\Subuser::select('subusers.*', 'users.email', 'users.username', 'users.use_totp') - ->join('users', 'users.id', '=', 'subusers.user_id') - ->where('server_id', $server->id) - ->get(), + 'subusers' => $server->subusers, ]); } public function getView(Request $request, $uuid, $id) { - $server = Models\Server::byUuid($uuid); + $server = Models\Server::byUuid($uuid)->load('node'); $this->authorize('view-subuser', $server); - $subuser = Models\Subuser::select('subusers.*', 'users.email as a_userEmail') - ->join('users', 'users.id', '=', 'subusers.user_id') - ->where(DB::raw('md5(subusers.id)'), $id)->where('subusers.server_id', $server->id) - ->first(); - - if (! $subuser) { - abort(404); - } - - $permissions = []; - $modelPermissions = Models\Permission::select('permission') - ->where('user_id', $subuser->user_id)->where('server_id', $server->id) - ->get(); - - foreach ($modelPermissions as &$perm) { - $permissions[$perm->permission] = true; - } + $subuser = Models\Subuser::with('permissions', 'user') + ->where('server_id', $server->id)->findOrFail($id); $server->js(); @@ -93,7 +75,9 @@ class SubuserController extends Controller 'server' => $server, 'node' => $server->node, 'subuser' => $subuser, - 'permissions' => $permissions, + 'permissions' => $subuser->permissions->mapWithKeys(function ($item, $key) { + return [$item->permission => true]; + }), ]); } @@ -102,12 +86,10 @@ class SubuserController extends Controller $server = Models\Server::byUuid($uuid); $this->authorize('edit-subuser', $server); - $subuser = Models\Subuser::where(DB::raw('md5(id)'), $id)->where('server_id', $server->id)->first(); + $subuser = Models\Subuser::where('server_id', $server->id)->findOrFail($id); try { - if (! $subuser) { - throw new DisplayException('Unable to locate a subuser by that ID.'); - } elseif ($subuser->user_id === Auth::user()->id) { + if ($subuser->user_id === Auth::user()->id) { throw new DisplayException('You are not authorized to edit you own account.'); } @@ -163,7 +145,7 @@ class SubuserController extends Controller return redirect()->route('server.subusers.view', [ 'uuid' => $uuid, - 'id' => md5($subuser->id), + 'id' => $subuser->id, ]); } catch (DisplayValidationException $ex) { return redirect()->route('server.subusers.new', $uuid)->withErrors(json_decode($ex->getMessage()))->withInput(); @@ -183,10 +165,7 @@ class SubuserController extends Controller $this->authorize('delete-subuser', $server); try { - $subuser = Models\Subuser::select('id')->where(DB::raw('md5(id)'), $id)->where('server_id', $server->id)->first(); - if (! $subuser) { - throw new DisplayException('No subuser by that ID was found on the system.'); - } + $subuser = Models\Subuser::where('server_id', $server->id)->findOrFail($id); $repo = new SubuserRepository; $repo->delete($subuser->id); diff --git a/app/Models/Permission.php b/app/Models/Permission.php index 391f5e8d2..fe7221719 100644 --- a/app/Models/Permission.php +++ b/app/Models/Permission.php @@ -28,6 +28,14 @@ use Illuminate\Database\Eloquent\Model; class Permission extends Model { + + /** + * Should timestamps be used on this model. + * + * @var boolean + */ + public $timestamps = false; + /** * The table associated with the model. * diff --git a/app/Models/Server.php b/app/Models/Server.php index 3ea72e2b9..1ce041ca7 100644 --- a/app/Models/Server.php +++ b/app/Models/Server.php @@ -196,6 +196,16 @@ class Server extends Model return $this->belongsTo(User::class, 'owner_id'); } + /** + * Gets the subusers associated with a server. + * + * @return \Illuminate\Database\Eloquent\Relations\HasMany + */ + public function subusers() + { + return $this->hasMany(Subuser::class); + } + /** * Gets the default allocation for a server. * diff --git a/app/Models/Subuser.php b/app/Models/Subuser.php index 6d390df95..25a82f615 100644 --- a/app/Models/Subuser.php +++ b/app/Models/Subuser.php @@ -87,6 +87,6 @@ class Subuser extends Model */ public function permissions() { - return $this->hasMany(Pemission::class); + return $this->hasMany(Permission::class); } } diff --git a/app/Repositories/SubuserRepository.php b/app/Repositories/SubuserRepository.php index 94a8ccecb..4e4691dda 100644 --- a/app/Repositories/SubuserRepository.php +++ b/app/Repositories/SubuserRepository.php @@ -167,8 +167,7 @@ class SubuserRepository } Models\Permission::create([ - 'user_id' => $user->id, - 'server_id' => $server->id, + 'subuser_id' => $subuser->id, 'permission' => $permission, ]); } @@ -213,14 +212,12 @@ class SubuserRepository */ public function delete($id) { - $subuser = Models\Subuser::with('server.node', 'permissions')->findOrFail($id); + $subuser = Models\Subuser::with('server.node')->findOrFail($id); $server = $subuser->server; DB::beginTransaction(); try { - Models\Permission::where('user_id', $subuser->user_id)->where('server_id', $subuser->server_id)->delete(); - $server->node->guzzleClient([ 'X-Access-Server' => $server->uuid, 'X-Access-Token' => $server->node->daemonSecret, @@ -232,6 +229,9 @@ class SubuserRepository ], ]); + foreach($subuser->permissions as &$permission) { + $permission->delete(); + } $subuser->delete(); DB::commit(); @@ -273,7 +273,9 @@ class SubuserRepository DB::beginTransaction(); try { - Models\Permission::where('user_id', $subuser->user_id)->where('server_id', $subuser->server_id)->delete(); + foreach($subuser->permissions as &$permission) { + $permission->delete(); + } $daemonPermissions = $this->coreDaemonPermissions; foreach ($data['permissions'] as $permission) { @@ -282,13 +284,10 @@ class SubuserRepository if (! is_null($this->permissions[$permission])) { array_push($daemonPermissions, $this->permissions[$permission]); } - $model = new Models\Permission; - $model->fill([ - 'user_id' => $data['user'], - 'server_id' => $data['server'], + Models\Permission::create([ + 'subuser_id' => $subuser->id, 'permission' => $permission, ]); - $model->save(); } } diff --git a/database/migrations/2017_02_09_174834_SetupPermissionsPivotTable.php b/database/migrations/2017_02_09_174834_SetupPermissionsPivotTable.php index fe87c4f77..ce5e3ae89 100644 --- a/database/migrations/2017_02_09_174834_SetupPermissionsPivotTable.php +++ b/database/migrations/2017_02_09_174834_SetupPermissionsPivotTable.php @@ -35,6 +35,8 @@ class SetupPermissionsPivotTable extends Migration $table->dropColumn('server_id'); $table->dropColumn('user_id'); + $table->dropColumn('created_at'); + $table->dropColumn('updated_at'); $table->foreign('subuser_id')->references('id')->on('subusers'); }); } @@ -49,6 +51,7 @@ class SetupPermissionsPivotTable extends Migration Schema::table('permissions', function (Blueprint $table) { $table->unsignedInteger('server_id')->after('subuser_id'); $table->unsignedInteger('user_id')->after('server_id'); + $table->timestamps(); }); DB::transaction(function () { diff --git a/resources/themes/pterodactyl/server/users/index.blade.php b/resources/themes/pterodactyl/server/users/index.blade.php index f44849758..daffcb611 100644 --- a/resources/themes/pterodactyl/server/users/index.blade.php +++ b/resources/themes/pterodactyl/server/users/index.blade.php @@ -51,29 +51,29 @@ @can('view-subuser', $server)@endcan @can('delete-subuser', $server)@endcan - @foreach($subusers as $user) + @foreach($subusers as $subuser) - User Image - {{ $user->username }} - {{ $user->email }} + User Image + {{ $subuser->user->username }} + {{ $subuser->user->email }} - @if($user->use_totp) + @if($subuser->user->use_totp) @else @endif - {{ $user->created_at }} + {{ $subuser->user->created_at }} @can('view-subuser', $server) - + @endcan @can('delete-subuser', $server) - + diff --git a/resources/themes/pterodactyl/server/users/view.blade.php b/resources/themes/pterodactyl/server/users/view.blade.php index b697edc34..925fbdf05 100644 --- a/resources/themes/pterodactyl/server/users/view.blade.php +++ b/resources/themes/pterodactyl/server/users/view.blade.php @@ -35,7 +35,7 @@ @section('content') @can('edit-subuser', $server) -
+ @endcan
@@ -45,7 +45,7 @@
{!! csrf_field() !!} - +