Skip to content

Commit

Permalink
feat: add ptype parameter to artisan commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Dobmod committed May 23, 2024
1 parent 124ab91 commit 3f54397
Show file tree
Hide file tree
Showing 3 changed files with 14 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
8 changes: 8 additions & 0 deletions tests/Commands/RoleAssignTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,13 @@ public function testHandle()
$exitCode = Artisan::call('role:assign', ['user' => 'eve', 'role' => 'writer']);
$this->assertFalse(0 === $exitCode);
$this->assertTrue(Enforcer::hasRoleForUser('eve', 'writer'));

Enforcer::getModel()->addDef('g', 'g2', '_,_');
$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 3f54397

Please sign in to comment.