admin(roles): add has one on User -> AdminRole

This commit is contained in:
Matthew Penner 2021-01-16 13:24:27 -07:00
parent 9d005b5fd2
commit 1e61fd161c
4 changed files with 70 additions and 1 deletions

View File

@ -7,6 +7,7 @@ namespace Pterodactyl\Models;
* @property string $name * @property string $name
* @property string|null $description * @property string|null $description
* @property int $sort_id * @property int $sort_id
* @property array $permissions
*/ */
class AdminRole extends Model class AdminRole extends Model
{ {
@ -33,6 +34,16 @@ class AdminRole extends Model
'description', 'description',
]; ];
/**
* Cast values to correct type.
*
* @var array
*/
protected $casts = [
'sort_id' => 'int',
'permissions' => 'array',
];
/** /**
* @var array * @var array
*/ */
@ -45,4 +56,14 @@ class AdminRole extends Model
* @var bool * @var bool
*/ */
public $timestamps = false; public $timestamps = false;
/**
* Gets the permissions associated with a admin role.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function permissions()
{
return $this->hasMany(Permission::class);
}
} }

View File

@ -227,6 +227,16 @@ class User extends Model implements
return $this->root_admin ? 'Super Administrator' : null; return $this->root_admin ? 'Super Administrator' : null;
} }
/**
* Gets the admin role associated with a user.
*
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function adminRole()
{
return $this->hasOne(AdminRole::class, 'id', 'admin_role_id');
}
/** /**
* Returns all servers that a user owns. * Returns all servers that a user owns.
* *

View File

@ -14,11 +14,13 @@ class CreateAdminRolesTable extends Migration
public function up() public function up()
{ {
Schema::create('admin_roles', function (Blueprint $table) { Schema::create('admin_roles', function (Blueprint $table) {
$table->id(); $table->integer('id')->unsigned();
$table->string('name', 64); $table->string('name', 64);
$table->string('description', 255)->nullable(); $table->string('description', 255)->nullable();
$table->integer('sort_id'); $table->integer('sort_id');
$table->json('permissions')->nullable(); $table->json('permissions')->nullable();
$table->unique(['id']);
}); });
} }

View File

@ -0,0 +1,36 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddAdminRoleIdColumnToUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->integer('admin_role_id')->nullable()->unsigned()->after('language');
$table->index('admin_role_id')->nullable();
$table->foreign('admin_role_id')->references('id')->on('admin_roles')->onDelete('set null');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->dropForeign(['admin_role_id']);
$table->dropIndex('admin_role_id');
$table->dropColumn('admin_role_id');
});
}
}