From ec605d636ff4f9cc71fe279ea111561e71c7dc48 Mon Sep 17 00:00:00 2001 From: Timot Tarjani Date: Thu, 26 Dec 2024 10:34:10 +0100 Subject: [PATCH] Use casts in UserRole --- app/Model/UserRole.php | 19 ++++--------------- ...6_10_20_000000_create_user_roles_table.php | 1 + 2 files changed, 5 insertions(+), 15 deletions(-) diff --git a/app/Model/UserRole.php b/app/Model/UserRole.php index 7ac7111e9..c0866fa23 100644 --- a/app/Model/UserRole.php +++ b/app/Model/UserRole.php @@ -17,25 +17,14 @@ class UserRole extends Model { 'name', 'rights' ]; + protected $casts = [ + 'rights' => 'array', + ]; public function users(){ return $this->hasMany(\App\Model\User::class,'role_id','id'); } - /** - * Accessor for rights - */ - public function getRightsAttribute(){ - return json_decode($this->attributes['rights']); - } - - /** - * Mutator for rights - */ - public function setRightsAttribute($value){ - $this->attributes['rights'] = json_encode($value); - } - public function addRight($right){ $all_rights = $this->getRightsAttribute(); array_push($all_rights, $right); @@ -43,7 +32,7 @@ public function addRight($right){ } public function isAdminRole(){ - $roles = $this->getRightsAttribute(); + $roles = $this->attributes['rights']; return $roles? in_array('admin_area',$roles) : false; } diff --git a/database/migrations/2016_10_20_000000_create_user_roles_table.php b/database/migrations/2016_10_20_000000_create_user_roles_table.php index 3f7b2e4c6..5aa6c4f28 100644 --- a/database/migrations/2016_10_20_000000_create_user_roles_table.php +++ b/database/migrations/2016_10_20_000000_create_user_roles_table.php @@ -22,6 +22,7 @@ public function up() $table->string('name'); $table->integer('permission')->nullable(); $table->text('rights')->nullable(); + $table->timestamps(); }); }