Skip to content

Commit 0ee21ed

Browse files
committed
Add option to specify a custom guard for the policy
1 parent 0280ddc commit 0ee21ed

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/Illuminate/Foundation/Console/PolicyMakeCommand.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Illuminate\Foundation\Console;
44

55
use Illuminate\Console\GeneratorCommand;
6+
use Illuminate\Contracts\Auth\Access\Authorizable;
67
use Illuminate\Support\Str;
78
use Symfony\Component\Console\Input\InputOption;
89

@@ -67,6 +68,28 @@ protected function replaceUserNamespace($stub)
6768
);
6869
}
6970

71+
/**
72+
* Get the model for the guard's user provider.
73+
*
74+
* @return string|null
75+
*/
76+
protected function userProviderModel()
77+
{
78+
$config = $this->laravel['config'];
79+
80+
if ($this->option('any-guard')) {
81+
return Authorizable::class;
82+
}
83+
84+
$guard = $this->option('guard')
85+
? $this->option('guard')
86+
: $config->get('auth.defaults.guard');
87+
88+
$provider = $config->get('auth.guards.'.$guard.'.provider');
89+
90+
return $config->get("auth.providers.{$provider}.model");
91+
}
92+
7093
/**
7194
* Replace the model for the given stub.
7295
*
@@ -160,6 +183,8 @@ protected function getOptions()
160183
{
161184
return [
162185
['model', 'm', InputOption::VALUE_OPTIONAL, 'The model that the policy applies to'],
186+
['guard', 'g', InputOption::VALUE_OPTIONAL, 'The guard that the policy relies on'],
187+
['any-guard', null, InputOption::VALUE_NONE, 'Allow use with any authorizable model (overrides --guard)'],
163188
];
164189
}
165190
}

0 commit comments

Comments
 (0)