🎨 add command GroupAdd

for rbac-with-domains
This commit is contained in:
osi 2019-05-31 01:14:47 +08:00
parent 8edf70e280
commit 7430c78a0f
2 changed files with 81 additions and 38 deletions

46
src/Commands/GroupAdd.php Normal file
View File

@ -0,0 +1,46 @@
<?php
namespace Lauthz\Commands;
use Illuminate\Console\Command;
use Lauthz\Facades\Enforcer;
/**
* PolicyAdd class.
*/
class GroupAdd extends Command {
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'group:add
{policy : the rule separated by commas}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Adds a role inheritance rule to the current policy.';
/**
* Execute the console command.
*
* @return mixed
*/
public function handle() {
$params = explode(',', $this->argument('policy'));
array_walk($params, function (&$value) {
$value = trim($value);
});
$ret = Enforcer::addGroupingPolicy(...$params);
if ($ret) {
$this->info('Grouping `' . implode(', ', $params) . '` created');
} else {
$this->error('Grouping `' . implode(', ', $params) . '` creation failed');
}
return $ret ? 0 : 1;
}
}

View File

@ -2,29 +2,28 @@
namespace Lauthz;
use Illuminate\Support\ServiceProvider;
use Lauthz\Models\Rule;
use Lauthz\Observers\RuleObserver;
use Illuminate\Support\ServiceProvider;
class LauthzServiceProvider extends ServiceProvider
{
class LauthzServiceProvider extends ServiceProvider {
/**
* Perform post-registration booting of services.
*/
public function boot()
{
public function boot() {
if ($this->app->runningInConsole()) {
$this->publishes([__DIR__.'/../database/migrations' => database_path('migrations')], 'laravel-lauthz-migrations');
$this->publishes([__DIR__.'/../config/lauthz-rbac-model.conf' => config_path('lauthz-rbac-model.conf')], 'laravel-lauthz-config');
$this->publishes([__DIR__.'/../config/lauthz.php' => config_path('lauthz.php')], 'laravel-lauthz-config');
$this->publishes([__DIR__ . '/../database/migrations' => database_path('migrations')], 'laravel-lauthz-migrations');
$this->publishes([__DIR__ . '/../config/lauthz-rbac-model.conf' => config_path('lauthz-rbac-model.conf')], 'laravel-lauthz-config');
$this->publishes([__DIR__ . '/../config/lauthz.php' => config_path('lauthz.php')], 'laravel-lauthz-config');
$this->commands([
Commands\GroupAdd::class,
Commands\PolicyAdd::class,
Commands\RoleAssign::class,
]);
}
$this->mergeConfigFrom(__DIR__.'/../config/lauthz.php', 'lauthz');
$this->mergeConfigFrom(__DIR__ . '/../config/lauthz.php', 'lauthz');
$this->bootObserver();
}
@ -34,16 +33,14 @@ class LauthzServiceProvider extends ServiceProvider
*
* @return void
*/
protected function bootObserver()
{
protected function bootObserver() {
Rule::observe(new RuleObserver());
}
/**
* Register bindings in the container.
*/
public function register()
{
public function register() {
$this->app->singleton('enforcer', function ($app) {
return new EnforcerManager($app);
});