docs/basic-usage/enums.md
Internally, Enums implicitly implement \BackedEnum, which is how this package recognizes that you're passing an Enum.
NOTE: Presently (versions 6, 7) this package does not support using $casts to specify enums on the Permission model. You can still use enums to reference things as shown below, just without declaring it in a $casts property.
You can create your Enum object for use with Roles and/or Permissions. You will probably create separate Enums for Roles and for Permissions, although if your application needs are simple you might choose a single Enum for both.
Usually the list of application Roles is much shorter than the list of Permissions, so having separate objects for them can make them easier to manage.
Here is an example Enum for Roles. You would do similarly for Permissions.
namespace App\Enums;
enum RolesEnum: string
{
// case NAMEINAPP = 'name-in-database';
case WRITER = 'writer';
case EDITOR = 'editor';
case USERMANAGER = 'user-manager';
// extra helper to allow for greater customization of displayed values, without disclosing the name/value data directly
public function label(): string
{
return match ($this) {
static::WRITER => 'Writers',
static::EDITOR => 'Editors',
static::USERMANAGER => 'User Managers',
};
}
}
When creating roles/permissions, you cannot pass an Enum name directly, because Eloquent expects a string for the name.
Use Laravel's enum_value() function for simplicity:
$role = app(Role::class)->findOrCreate(enum_value(RolesEnum::WRITER), 'web');
Same with creating Permissions.
In your application code, when checking for authorization using features of this package, you can use MyEnum::NAME directly in most cases, without passing ->value to convert to a string.
There may occasionally be times where you will need to manually fallback to adding ->value (eg: MyEnum::NAME->value) when using features that aren't aware of Enum support, such as when you need to pass string values instead of an Enum to a function that doesn't recognize Enums. Again, wrap it with enum_value() as a simple workaround.
Examples:
// the following are identical because `hasPermissionTo` is aware of `BackedEnum` support:
$user->hasPermissionTo(PermissionsEnum::VIEWPOSTS);
$user->hasPermissionTo(PermissionsEnum::VIEWPOSTS->value);
$user->hasPermissionTo(enum_value(PermissionsEnum::VIEWPOSTS));
// Blade directives:
@can(enum_value(PermissionsEnum::VIEWPOSTS))
The following methods of this package support passing BackedEnum parameters directly:
$user->assignRole(RolesEnum::WRITER);
$user->removeRole(RolesEnum::EDITOR);
$role->givePermissionTo(PermissionsEnum::EDITPOSTS);
$role->revokePermissionTo(PermissionsEnum::EDITPOSTS);
$user->givePermissionTo(PermissionsEnum::EDITPOSTS);
$user->revokePermissionTo(PermissionsEnum::EDITPOSTS);
$user->hasPermissionTo(PermissionsEnum::EDITPOSTS);
$user->hasAnyPermission([PermissionsEnum::EDITPOSTS, PermissionsEnum::VIEWPOSTS]);
$user->hasDirectPermission(PermissionsEnum::EDITPOSTS);
$user->hasRole(RolesEnum::WRITER);
$user->hasAllRoles([RolesEnum::WRITER, RolesEnum::EDITOR]);
$user->hasExactRoles([RolesEnum::WRITER, RolesEnum::EDITOR, RolesEnum::MANAGER]);