From 718f846e77d95537e3e1bc08e89f4a6780ac0cdb Mon Sep 17 00:00:00 2001 From: Dobando <1692898084@qq.com> Date: Fri, 24 May 2024 12:13:57 +0800 Subject: [PATCH] feat: add ptype parameter to artisan commands and updated unit test --- README.md | 2 ++ src/Commands/RoleAssign.php | 6 ++++-- tests/Commands/RoleAssignTest.php | 17 +++++++++++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index de8a830..cd2b3ed 100755 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/Commands/RoleAssign.php b/src/Commands/RoleAssign.php index cf87244..232c883 100644 --- a/src/Commands/RoleAssign.php +++ b/src/Commands/RoleAssign.php @@ -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. @@ -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 { diff --git a/tests/Commands/RoleAssignTest.php b/tests/Commands/RoleAssignTest.php index b53eb21..ca40812 100644 --- a/tests/Commands/RoleAssignTest.php +++ b/tests/Commands/RoleAssignTest.php @@ -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; @@ -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')); } }