Skip to content

Commit

Permalink
Teams support on permission:show
Browse files Browse the repository at this point in the history
  • Loading branch information
erikn69 committed Aug 6, 2021
1 parent 6dd191a commit e728a5e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 9 deletions.
2 changes: 1 addition & 1 deletion database/migrations/create_permission_tables.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class CreatePermissionTables extends Migration
$table->string('name'); // For MySQL 8.0 use string('name', 125);
$table->string('guard_name'); // For MySQL 8.0 use string('guard_name', 125);
$table->timestamps();
if ($teams) {
if ($teams || config('permission.testing')) {
$table->unique([$columnNames['team_foreign_key'], 'name', 'guard_name']);
} else {
$table->unique(['name', 'guard_name']);
Expand Down
35 changes: 27 additions & 8 deletions src/Commands/Show.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Support\Collection;
use Spatie\Permission\Contracts\Permission as PermissionContract;
use Spatie\Permission\Contracts\Role as RoleContract;
use Symfony\Component\Console\Helper\TableCell;

class Show extends Command
{
Expand All @@ -19,6 +20,7 @@ public function handle()
{
$permissionClass = app(PermissionContract::class);
$roleClass = app(RoleContract::class);
$team_key = config('permission.column_names.team_foreign_key');

$style = $this->argument('style') ?? 'default';
$guard = $this->argument('guard');
Expand All @@ -32,20 +34,37 @@ public function handle()
foreach ($guards as $guard) {
$this->info("Guard: $guard");

$roles = $roleClass::whereGuardName($guard)->orderBy('name')->get()->mapWithKeys(function ($role) {
return [$role->name => $role->permissions->pluck('name')];
});
$roles = $roleClass::whereGuardName($guard)
->when(config('permission.teams'), function ($q) use ($team_key) {
$q->orderBy($team_key);
})
->orderBy('name')->get()->mapWithKeys(function ($role) use ($team_key) {
return [$role->name.'_'.($role->$team_key ?: '') => ['permissions' => $role->permissions->pluck('id'), $team_key => $role->$team_key ]];
});

$permissions = $permissionClass::whereGuardName($guard)->orderBy('name')->pluck('name');
$permissions = $permissionClass::whereGuardName($guard)->orderBy('name')->pluck('name', 'id');

$body = $permissions->map(function ($permission) use ($roles) {
return $roles->map(function (Collection $role_permissions) use ($permission) {
return $role_permissions->contains($permission) ? '' : ' ·';
$body = $permissions->map(function ($permission, $id) use ($roles) {
return $roles->map(function (array $role_data) use ($id) {
return $role_data['permissions']->contains($id) ? '' : ' ·';
})->prepend($permission);
});

if (config('permission.teams')) {
$teams = $roles->groupBy($team_key)->values()->map(function ($group, $id) {
return new TableCell('Team ID: '.($id ?: 'NULL'), ['colspan' => $group->count()]);
});
}

$this->table(
$roles->keys()->prepend('')->toArray(),
array_merge([
config('permission.teams') ? $teams->prepend('')->toArray() : [],
$roles->keys()->map(function ($val) {
$name = explode('_', $val);
return $name[0];
})
->prepend('')->toArray()
]),
$body->toArray(),
$style
);
Expand Down
23 changes: 23 additions & 0 deletions tests/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,27 @@ public function it_can_setup_teams_upgrade()
unlink($file);
}
}

/** @test */
public function it_can_show_roles_by_teams()
{
config()->set('permission.teams', true);
app(\Spatie\Permission\PermissionRegistrar::class)->initializeCache();

Role::create(['name' => 'testRoleTeam', 'team_test_id' => 1]);
Role::create(['name' => 'testRoleTeam', 'team_test_id' => 2]); // same name diferent team
Artisan::call('permission:show');

$output = Artisan::output();

// | | Team ID: NULL | Team ID: 1 | Team ID: 2 |
// | | testRole | testRole2 | testRoleTeam | testRoleTeam |
if (method_exists($this, 'assertMatchesRegularExpression')) {
$this->assertMatchesRegularExpression('/\|\s+\|\s+Team ID: NULL\s+\|\s+Team ID: 1\s+\|\s+Team ID: 2\s+\|/', $output);
$this->assertMatchesRegularExpression('/\|\s+\|\s+testRole\s+\|\s+testRole2\s+\|\s+testRoleTeam\s+\|\s+testRoleTeam\s+\|/', $output);
} else { // phpUnit 9/8
$this->assertRegExp('/\|\s+\|\s+Team ID: NULL\s+\|\s+Team ID: 1\s+\|\s+Team ID: 2\s+\|/', $output);
$this->assertRegExp('/\|\s+\|\s+testRole\s+\|\s+testRole2\s+\|\s+testRoleTeam\s+\|\s+testRoleTeam\s+\|/', $output);
}
}
}

0 comments on commit e728a5e

Please sign in to comment.