Skip to content

Commit

Permalink
Merge pull request #68 from Dobmod/master
Browse files Browse the repository at this point in the history
feat: add ptype parameter to artisan commands
  • Loading branch information
leeqvip authored May 24, 2024
2 parents 124ab91 + 718f846 commit 783c401
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,8 @@ Adds a role for a user:

```bash
php artisan role:assign eve writer
# Specify the ptype of the role assignment by using the --ptype option.
php artisan role:assign eve writer --ptype=g2
```

### Using cache
Expand Down
6 changes: 4 additions & 2 deletions src/Commands/RoleAssign.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ class RoleAssign extends Command
*/
protected $signature = 'role:assign
{user : the identifier of user}
{role : the name of role}';
{role : the name of role}
{--ptype= : the ptype of role}';

/**
* The console command description.
Expand All @@ -35,8 +36,9 @@ public function handle()
{
$user = $this->argument('user');
$role = $this->argument('role');
$ptype = $this->option('ptype') ?: 'g';

$ret = Enforcer::addRoleForUser($user, $role);
$ret = Enforcer::addNamedGroupingPolicy($ptype, $user, $role);
if ($ret) {
$this->info('Added `'.$role.'` role to `'.$user.'` successfully');
} else {
Expand Down
17 changes: 17 additions & 0 deletions tests/Commands/RoleAssignTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Lauthz\Tests\Commands;

use Casbin\Model\Model;
use Lauthz\Facades\Enforcer;
use Lauthz\Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseMigrations;
Expand All @@ -19,5 +20,21 @@ public function testHandle()
$exitCode = Artisan::call('role:assign', ['user' => 'eve', 'role' => 'writer']);
$this->assertFalse(0 === $exitCode);
$this->assertTrue(Enforcer::hasRoleForUser('eve', 'writer'));

$model = Model::newModel();
$model->addDef('r', 'r', 'sub, obj, act');
$model->addDef('p', 'p', 'sub, obj, act');
$model->addDef('g', 'g', '_, _');
$model->addDef('g', 'g2', '_, _');
$model->addDef('e', 'e', 'some(where (p.eft == allow))');
$model->addDef('m', 'm', 'g(r.sub, p.sub) && g2(r.obj, p.obj) && r.act == p.act');
Enforcer::setModel($model);
Enforcer::loadPolicy();
$this->assertFalse(Enforcer::hasNamedGroupingPolicy('g2', 'eve', 'writer'));
$exitCode = Artisan::call('role:assign', ['user' => 'eve', 'role' => 'writer', '--ptype' => 'g2']);
$this->assertTrue(0 === $exitCode);
$exitCode = Artisan::call('role:assign', ['user' => 'eve', 'role' => 'writer', '--ptype' => 'g2']);
$this->assertFalse(0 === $exitCode);
$this->assertTrue(Enforcer::hasNamedGroupingPolicy('g2', 'eve', 'writer'));
}
}

0 comments on commit 783c401

Please sign in to comment.