Skip to content

Commit c8f6c93

Browse files
committed
Modify subusers model setup
1 parent 4f61637 commit c8f6c93

File tree

5 files changed

+124
-29
lines changed

5 files changed

+124
-29
lines changed

app/Models/Permission.php

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,35 @@ class Permission extends Model
4242
*/
4343
protected $guarded = ['id', 'created_at', 'updated_at'];
4444

45-
/**
46-
* Cast values to correct type.
47-
*
48-
* @var array
49-
*/
50-
protected $casts = [
51-
'user_id' => 'integer',
52-
'server_id' => 'integer',
53-
];
45+
/**
46+
* Cast values to correct type.
47+
*
48+
* @var array
49+
*/
50+
protected $casts = [
51+
'subuser_id' => 'integer',
52+
];
5453

54+
/**
55+
* Find permission by permission node.
56+
*
57+
* @param \Illuminate\Database\Query\Builder $query
58+
* @param string $permission
59+
* @return \Illuminate\Database\Query\Builder
60+
*/
5561
public function scopePermission($query, $permission)
5662
{
5763
return $query->where('permission', $permission);
5864
}
5965

60-
public function scopeServer($query, $server)
66+
/**
67+
* Filter permission by server.
68+
*
69+
* @param \Illuminate\Database\Query\Builder $query
70+
* @param \Pterodactyl\Models\Server $server
71+
* @return \Illuminate\Database\Query\Builder
72+
*/
73+
public function scopeServer($query, Server $server)
6174
{
6275
return $query->where('server_id', $server->id);
6376
}

app/Models/Subuser.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,14 @@ public function user()
7979
{
8080
return $this->belongsTo(User::class);
8181
}
82+
83+
/**
84+
* Gets the permissions associated with a subuser.
85+
*
86+
* @return \Illuminate\Database\Eloquent\Relations\HasMany
87+
*/
88+
public function permissions()
89+
{
90+
return $this->hasMany(Pemission::class);
91+
}
8292
}

app/Models/User.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,11 +199,11 @@ public function serverAccessCollection($paginate = null)
199199
/**
200200
* Returns all permissions that a user has.
201201
*
202-
* @return \Illuminate\Database\Eloquent\Relations\HasMany
202+
* @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
203203
*/
204204
public function permissions()
205205
{
206-
return $this->hasMany(Permission::class);
206+
return $this->hasManyThrough(Permission::class, Subuser::class);
207207
}
208208

209209
/**

app/Policies/ServerPolicy.php

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,23 @@ public function __construct()
3939
//
4040
}
4141

42+
/**
43+
* Checks if the user has the given permission on/for the server.
44+
*
45+
* @param \Pterodactyl\Models\User $user
46+
* @param \Pterodactyl\Models\Server $server
47+
* @param $permission
48+
* @return bool
49+
*/
50+
private function checkPermission(User $user, Server $server, $permission)
51+
{
52+
if ($this->isOwner($user, $server)) {
53+
return true;
54+
}
55+
56+
return $user->permissions()->server($server)->permission($permission)->exists();
57+
}
58+
4259
/**
4360
* Determine if current user is the owner of a server.
4461
*
@@ -521,21 +538,4 @@ public function setAllocation(User $user, Server $server)
521538
{
522539
return $this->checkPermission($user, $server, 'set-allocation');
523540
}
524-
525-
/**
526-
* Checks if the user has the given permission on/for the server.
527-
*
528-
* @param \Pterodactyl\Models\User $user
529-
* @param \Pterodactyl\Models\Server $server
530-
* @param $permission
531-
* @return bool
532-
*/
533-
private function checkPermission(User $user, Server $server, $permission)
534-
{
535-
if ($this->isOwner($user, $server)) {
536-
return true;
537-
}
538-
539-
return $user->permissions()->server($server)->permission($permission)->exists();
540-
}
541541
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
use Pterodactyl\Models\Subuser;
7+
use Pterodactyl\Models\Permission;
8+
9+
class SetupPermissionsPivotTable extends Migration
10+
{
11+
/**
12+
* Run the migrations.
13+
*
14+
* @return void
15+
*/
16+
public function up()
17+
{
18+
Schema::table('permissions', function (Blueprint $table) {
19+
$table->unsignedInteger('subuser_id')->after('id');
20+
});
21+
22+
DB::transaction(function () {
23+
foreach(Subuser::all() as &$subuser) {
24+
Permission::where('user_id', $subuser->user_id)->where('server_id', $subuser->server_id)->update([
25+
'subuser_id' => $subuser->id,
26+
]);
27+
}
28+
});
29+
30+
Schema::table('permissions', function (Blueprint $table) {
31+
$table->dropForeign('permissions_server_id_foreign');
32+
$table->dropIndex('permissions_server_id_foreign');
33+
$table->dropForeign('permissions_user_id_foreign');
34+
$table->dropIndex('permissions_user_id_foreign');
35+
36+
$table->dropColumn('server_id');
37+
$table->dropColumn('user_id');
38+
$table->foreign('subuser_id')->references('id')->on('subusers');
39+
});
40+
}
41+
42+
/**
43+
* Reverse the migrations.
44+
*
45+
* @return void
46+
*/
47+
public function down()
48+
{
49+
Schema::table('permissions', function (Blueprint $table) {
50+
$table->unsignedInteger('server_id')->after('subuser_id');
51+
$table->unsignedInteger('user_id')->after('server_id');
52+
});
53+
54+
DB::transaction(function () {
55+
foreach(Subuser::all() as &$subuser) {
56+
Permission::where('subuser_id', $subuser->id)->update([
57+
'user_id' => $subuser->user_id,
58+
'server_id' => $subuser->server_id,
59+
]);
60+
}
61+
});
62+
63+
Schema::table('permissions', function (Blueprint $table) {
64+
$table->dropForeign('permissions_subuser_id_foreign');
65+
$table->dropIndex('permissions_subuser_id_foreign');
66+
$table->dropColumn('subuser_id');
67+
68+
$table->foreign('server_id')->references('id')->on('servers');
69+
$table->foreign('user_id')->references('id')->on('users');
70+
});
71+
}
72+
}

0 commit comments

Comments
 (0)