Skip to content

Commit 7430c78

Browse files
committed
🎨 add command GroupAdd
for rbac-with-domains
1 parent 8edf70e commit 7430c78

File tree

2 files changed

+86
-43
lines changed

2 files changed

+86
-43
lines changed

src/Commands/GroupAdd.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Lauthz\Commands;
4+
5+
use Illuminate\Console\Command;
6+
use Lauthz\Facades\Enforcer;
7+
8+
/**
9+
* PolicyAdd class.
10+
*/
11+
class GroupAdd extends Command {
12+
/**
13+
* The name and signature of the console command.
14+
*
15+
* @var string
16+
*/
17+
protected $signature = 'group:add
18+
{policy : the rule separated by commas}';
19+
20+
/**
21+
* The console command description.
22+
*
23+
* @var string
24+
*/
25+
protected $description = 'Adds a role inheritance rule to the current policy.';
26+
27+
/**
28+
* Execute the console command.
29+
*
30+
* @return mixed
31+
*/
32+
public function handle() {
33+
$params = explode(',', $this->argument('policy'));
34+
array_walk($params, function (&$value) {
35+
$value = trim($value);
36+
});
37+
$ret = Enforcer::addGroupingPolicy(...$params);
38+
if ($ret) {
39+
$this->info('Grouping `' . implode(', ', $params) . '` created');
40+
} else {
41+
$this->error('Grouping `' . implode(', ', $params) . '` creation failed');
42+
}
43+
44+
return $ret ? 0 : 1;
45+
}
46+
}

src/LauthzServiceProvider.php

Lines changed: 40 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,50 +2,47 @@
22

33
namespace Lauthz;
44

5+
use Illuminate\Support\ServiceProvider;
56
use Lauthz\Models\Rule;
67
use Lauthz\Observers\RuleObserver;
7-
use Illuminate\Support\ServiceProvider;
88

9-
class LauthzServiceProvider extends ServiceProvider
10-
{
11-
/**
12-
* Perform post-registration booting of services.
13-
*/
14-
public function boot()
15-
{
16-
if ($this->app->runningInConsole()) {
17-
$this->publishes([__DIR__.'/../database/migrations' => database_path('migrations')], 'laravel-lauthz-migrations');
18-
$this->publishes([__DIR__.'/../config/lauthz-rbac-model.conf' => config_path('lauthz-rbac-model.conf')], 'laravel-lauthz-config');
19-
$this->publishes([__DIR__.'/../config/lauthz.php' => config_path('lauthz.php')], 'laravel-lauthz-config');
20-
21-
$this->commands([
22-
Commands\PolicyAdd::class,
23-
Commands\RoleAssign::class,
24-
]);
25-
}
26-
27-
$this->mergeConfigFrom(__DIR__.'/../config/lauthz.php', 'lauthz');
28-
29-
$this->bootObserver();
30-
}
31-
32-
/**
33-
* Boot Observer.
34-
*
35-
* @return void
36-
*/
37-
protected function bootObserver()
38-
{
39-
Rule::observe(new RuleObserver());
40-
}
41-
42-
/**
43-
* Register bindings in the container.
44-
*/
45-
public function register()
46-
{
47-
$this->app->singleton('enforcer', function ($app) {
48-
return new EnforcerManager($app);
49-
});
50-
}
9+
class LauthzServiceProvider extends ServiceProvider {
10+
/**
11+
* Perform post-registration booting of services.
12+
*/
13+
public function boot() {
14+
if ($this->app->runningInConsole()) {
15+
$this->publishes([__DIR__ . '/../database/migrations' => database_path('migrations')], 'laravel-lauthz-migrations');
16+
$this->publishes([__DIR__ . '/../config/lauthz-rbac-model.conf' => config_path('lauthz-rbac-model.conf')], 'laravel-lauthz-config');
17+
$this->publishes([__DIR__ . '/../config/lauthz.php' => config_path('lauthz.php')], 'laravel-lauthz-config');
18+
19+
$this->commands([
20+
Commands\GroupAdd::class,
21+
Commands\PolicyAdd::class,
22+
Commands\RoleAssign::class,
23+
]);
24+
}
25+
26+
$this->mergeConfigFrom(__DIR__ . '/../config/lauthz.php', 'lauthz');
27+
28+
$this->bootObserver();
29+
}
30+
31+
/**
32+
* Boot Observer.
33+
*
34+
* @return void
35+
*/
36+
protected function bootObserver() {
37+
Rule::observe(new RuleObserver());
38+
}
39+
40+
/**
41+
* Register bindings in the container.
42+
*/
43+
public function register() {
44+
$this->app->singleton('enforcer', function ($app) {
45+
return new EnforcerManager($app);
46+
});
47+
}
5148
}

0 commit comments

Comments
 (0)