laravel-permission
laravel-permission copied to clipboard
feat(Roles): Support for casting role names to enums
fixes #2609
It's more like a new feature because @schnetzi extended base Role model and did:
protected $casts = [
'name' => UserRoleEnum::class,
];
I used a custom accesor to dynamically cast it into the corresponding enum. Otherwise, it returns the original name attribute value.
/**
* @return string|\BackedEnum
*/
public function getNameAttribute()
{
$name = $this->attributes['name'];
if (str_contains($name, 'casted_enum')) {
return TestRolePermissionsEnum::from($name);
}
return $name;
}
Probably need to add tests with string parameters too. Actually I don't know.. for sure it works if the given parameters are enums as well the casts.
Thanks!