Skip to content

Commit

Permalink
Use casts in UserRole
Browse files Browse the repository at this point in the history
  • Loading branch information
ttimot24 committed Dec 26, 2024
1 parent 29dd478 commit ec605d6
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 15 deletions.
19 changes: 4 additions & 15 deletions app/Model/UserRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,22 @@ 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);
$this->setRightsAttribute($all_rights);
}

public function isAdminRole(){
$roles = $this->getRightsAttribute();
$roles = $this->attributes['rights'];

return $roles? in_array('admin_area',$roles) : false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function up()
$table->string('name');
$table->integer('permission')->nullable();
$table->text('rights')->nullable();
$table->timestamps();
});
}

Expand Down

0 comments on commit ec605d6

Please sign in to comment.