Last active
February 21, 2022 14:57
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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