Modify subusers model setup
This commit is contained in:
parent
4f61637284
commit
c8f6c93d26
5 changed files with 124 additions and 29 deletions
|
@ -48,16 +48,29 @@ class Permission extends Model
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected $casts = [
|
protected $casts = [
|
||||||
'user_id' => 'integer',
|
'subuser_id' => 'integer',
|
||||||
'server_id' => 'integer',
|
|
||||||
];
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find permission by permission node.
|
||||||
|
*
|
||||||
|
* @param \Illuminate\Database\Query\Builder $query
|
||||||
|
* @param string $permission
|
||||||
|
* @return \Illuminate\Database\Query\Builder
|
||||||
|
*/
|
||||||
public function scopePermission($query, $permission)
|
public function scopePermission($query, $permission)
|
||||||
{
|
{
|
||||||
return $query->where('permission', $permission);
|
return $query->where('permission', $permission);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function scopeServer($query, $server)
|
/**
|
||||||
|
* Filter permission by server.
|
||||||
|
*
|
||||||
|
* @param \Illuminate\Database\Query\Builder $query
|
||||||
|
* @param \Pterodactyl\Models\Server $server
|
||||||
|
* @return \Illuminate\Database\Query\Builder
|
||||||
|
*/
|
||||||
|
public function scopeServer($query, Server $server)
|
||||||
{
|
{
|
||||||
return $query->where('server_id', $server->id);
|
return $query->where('server_id', $server->id);
|
||||||
}
|
}
|
||||||
|
|
|
@ -79,4 +79,14 @@ class Subuser extends Model
|
||||||
{
|
{
|
||||||
return $this->belongsTo(User::class);
|
return $this->belongsTo(User::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the permissions associated with a subuser.
|
||||||
|
*
|
||||||
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
||||||
|
*/
|
||||||
|
public function permissions()
|
||||||
|
{
|
||||||
|
return $this->hasMany(Pemission::class);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -199,11 +199,11 @@ class User extends Model implements AuthenticatableContract, AuthorizableContrac
|
||||||
/**
|
/**
|
||||||
* Returns all permissions that a user has.
|
* Returns all permissions that a user has.
|
||||||
*
|
*
|
||||||
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
* @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
|
||||||
*/
|
*/
|
||||||
public function permissions()
|
public function permissions()
|
||||||
{
|
{
|
||||||
return $this->hasMany(Permission::class);
|
return $this->hasManyThrough(Permission::class, Subuser::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -39,6 +39,23 @@ class ServerPolicy
|
||||||
//
|
//
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if the user has the given permission on/for the server.
|
||||||
|
*
|
||||||
|
* @param \Pterodactyl\Models\User $user
|
||||||
|
* @param \Pterodactyl\Models\Server $server
|
||||||
|
* @param $permission
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
private function checkPermission(User $user, Server $server, $permission)
|
||||||
|
{
|
||||||
|
if ($this->isOwner($user, $server)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $user->permissions()->server($server)->permission($permission)->exists();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Determine if current user is the owner of a server.
|
* Determine if current user is the owner of a server.
|
||||||
*
|
*
|
||||||
|
@ -521,21 +538,4 @@ class ServerPolicy
|
||||||
{
|
{
|
||||||
return $this->checkPermission($user, $server, 'set-allocation');
|
return $this->checkPermission($user, $server, 'set-allocation');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Checks if the user has the given permission on/for the server.
|
|
||||||
*
|
|
||||||
* @param \Pterodactyl\Models\User $user
|
|
||||||
* @param \Pterodactyl\Models\Server $server
|
|
||||||
* @param $permission
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
private function checkPermission(User $user, Server $server, $permission)
|
|
||||||
{
|
|
||||||
if ($this->isOwner($user, $server)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $user->permissions()->server($server)->permission($permission)->exists();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,72 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Pterodactyl\Models\Subuser;
|
||||||
|
use Pterodactyl\Models\Permission;
|
||||||
|
|
||||||
|
class SetupPermissionsPivotTable extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function up()
|
||||||
|
{
|
||||||
|
Schema::table('permissions', function (Blueprint $table) {
|
||||||
|
$table->unsignedInteger('subuser_id')->after('id');
|
||||||
|
});
|
||||||
|
|
||||||
|
DB::transaction(function () {
|
||||||
|
foreach(Subuser::all() as &$subuser) {
|
||||||
|
Permission::where('user_id', $subuser->user_id)->where('server_id', $subuser->server_id)->update([
|
||||||
|
'subuser_id' => $subuser->id,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Schema::table('permissions', function (Blueprint $table) {
|
||||||
|
$table->dropForeign('permissions_server_id_foreign');
|
||||||
|
$table->dropIndex('permissions_server_id_foreign');
|
||||||
|
$table->dropForeign('permissions_user_id_foreign');
|
||||||
|
$table->dropIndex('permissions_user_id_foreign');
|
||||||
|
|
||||||
|
$table->dropColumn('server_id');
|
||||||
|
$table->dropColumn('user_id');
|
||||||
|
$table->foreign('subuser_id')->references('id')->on('subusers');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function down()
|
||||||
|
{
|
||||||
|
Schema::table('permissions', function (Blueprint $table) {
|
||||||
|
$table->unsignedInteger('server_id')->after('subuser_id');
|
||||||
|
$table->unsignedInteger('user_id')->after('server_id');
|
||||||
|
});
|
||||||
|
|
||||||
|
DB::transaction(function () {
|
||||||
|
foreach(Subuser::all() as &$subuser) {
|
||||||
|
Permission::where('subuser_id', $subuser->id)->update([
|
||||||
|
'user_id' => $subuser->user_id,
|
||||||
|
'server_id' => $subuser->server_id,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Schema::table('permissions', function (Blueprint $table) {
|
||||||
|
$table->dropForeign('permissions_subuser_id_foreign');
|
||||||
|
$table->dropIndex('permissions_subuser_id_foreign');
|
||||||
|
$table->dropColumn('subuser_id');
|
||||||
|
|
||||||
|
$table->foreign('server_id')->references('id')->on('servers');
|
||||||
|
$table->foreign('user_id')->references('id')->on('users');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue