From 1e61fd161c22261d031d2dc211786c85277011d8 Mon Sep 17 00:00:00 2001 From: Matthew Penner Date: Sat, 16 Jan 2021 13:24:27 -0700 Subject: [PATCH] admin(roles): add has one on User -> AdminRole --- app/Models/AdminRole.php | 21 +++++++++++ app/Models/User.php | 10 ++++++ ..._09_25_021109_create_admin_roles_table.php | 4 ++- ...dd_admin_role_id_column_to_users_table.php | 36 +++++++++++++++++++ 4 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 database/migrations/2021_01_16_201057_add_admin_role_id_column_to_users_table.php diff --git a/app/Models/AdminRole.php b/app/Models/AdminRole.php index 001810cee..d022eb7f4 100644 --- a/app/Models/AdminRole.php +++ b/app/Models/AdminRole.php @@ -7,6 +7,7 @@ namespace Pterodactyl\Models; * @property string $name * @property string|null $description * @property int $sort_id + * @property array $permissions */ class AdminRole extends Model { @@ -33,6 +34,16 @@ class AdminRole extends Model 'description', ]; + /** + * Cast values to correct type. + * + * @var array + */ + protected $casts = [ + 'sort_id' => 'int', + 'permissions' => 'array', + ]; + /** * @var array */ @@ -45,4 +56,14 @@ class AdminRole extends Model * @var bool */ 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); + } } diff --git a/app/Models/User.php b/app/Models/User.php index 109700f28..7f2c8675d 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -227,6 +227,16 @@ class User extends Model implements 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. * diff --git a/database/migrations/2020_09_25_021109_create_admin_roles_table.php b/database/migrations/2020_09_25_021109_create_admin_roles_table.php index a2cad02e1..058c00b9a 100644 --- a/database/migrations/2020_09_25_021109_create_admin_roles_table.php +++ b/database/migrations/2020_09_25_021109_create_admin_roles_table.php @@ -14,11 +14,13 @@ class CreateAdminRolesTable extends Migration public function up() { Schema::create('admin_roles', function (Blueprint $table) { - $table->id(); + $table->integer('id')->unsigned(); $table->string('name', 64); $table->string('description', 255)->nullable(); $table->integer('sort_id'); $table->json('permissions')->nullable(); + + $table->unique(['id']); }); } diff --git a/database/migrations/2021_01_16_201057_add_admin_role_id_column_to_users_table.php b/database/migrations/2021_01_16_201057_add_admin_role_id_column_to_users_table.php new file mode 100644 index 000000000..1bc4ebe01 --- /dev/null +++ b/database/migrations/2021_01_16_201057_add_admin_role_id_column_to_users_table.php @@ -0,0 +1,36 @@ +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'); + }); + } +}