Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[7.x] Add option to specify a custom guard for the policy #33210

Merged
merged 1 commit into from
Jun 15, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/Illuminate/Foundation/Console/PolicyMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Foundation\Console;

use Illuminate\Console\GeneratorCommand;
use Illuminate\Contracts\Auth\Access\Authorizable;
use Illuminate\Support\Str;
use Symfony\Component\Console\Input\InputOption;

Expand Down Expand Up @@ -67,6 +68,28 @@ protected function replaceUserNamespace($stub)
);
}

/**
* Get the model for the guard's user provider.
*
* @return string|null
*/
protected function userProviderModel()
{
$config = $this->laravel['config'];

if ($this->option('any-guard')) {
return Authorizable::class;
}

$guard = $this->option('guard')
? $this->option('guard')
: $config->get('auth.defaults.guard');

$provider = $config->get('auth.guards.'.$guard.'.provider');

return $config->get("auth.providers.{$provider}.model");
}

/**
* Replace the model for the given stub.
*
Expand Down Expand Up @@ -160,6 +183,8 @@ protected function getOptions()
{
return [
['model', 'm', InputOption::VALUE_OPTIONAL, 'The model that the policy applies to'],
['guard', 'g', InputOption::VALUE_OPTIONAL, 'The guard that the policy relies on'],
['any-guard', null, InputOption::VALUE_NONE, 'Allow use with any authorizable model (overrides --guard)'],
];
}
}