Skip to content

Instantly share code, notes, and snippets.

@VaguelyOnline
Last active February 21, 2022 14:57
Show Gist options
  • Save VaguelyOnline/5a94d38b9418e90226d7fa5318433f91 to your computer and use it in GitHub Desktop.
Save VaguelyOnline/5a94d38b9418e90226d7fa5318433f91 to your computer and use it in GitHub Desktop.
Easily serialise Laravel policy method permissions that a given user has, for a given model.
/**
* Automatically generates an array detailing the permissions that this User has over the given target model.
* Usage: $user->getPolicyPermissions($modelWithPolicy)
*
* @param $model
* @return array
* @throws \ReflectionException
*/
public function getPolicyPermissions($model): array
{
$perms = [];
if ($policy = policy($model))
foreach((new ReflectionClass($policy))->getMethods(ReflectionMethod::IS_PUBLIC) as $method)
$perms[$method->getName()] = $method->invoke($policy, $this, $model);
return $perms;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment