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 16, 2021
1 parent 6dd191a commit c0e768d
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/run-tests-L7.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:

runs-on: ubuntu-latest
strategy:
fail-fast: true
fail-fast: false
matrix:
php: [8.0, 7.4, 7.3, 7.2]
laravel: [7.*, 6.*]
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/run-tests-L8.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:

runs-on: ubuntu-latest
strategy:
fail-fast: true
fail-fast: false
matrix:
php: [8.0, 7.4, 7.3]
laravel: [8.*]
Expand Down
4 changes: 2 additions & 2 deletions database/migrations/add_teams_fields.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ class AddTeamsFields extends Migration
}
});

Schema::table($tableNames['model_has_permissions'], function (Blueprint $table) use ($tableNames, $columnNames) {
if (! Schema::hasColumn($tableNames['model_has_permissions'], $columnNames['team_foreign_key'])) {
Schema::table($tableNames['model_has_roles'], function (Blueprint $table) use ($tableNames, $columnNames) {
if (! Schema::hasColumn($tableNames['model_has_roles'], $columnNames['team_foreign_key'])) {
$table->unsignedBigInteger($columnNames['team_foreign_key'])->default('1');;
$table->index($columnNames['team_foreign_key'], 'model_has_roles_team_foreign_key_index');

Expand Down
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
26 changes: 25 additions & 1 deletion tests/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ public function it_can_setup_teams_upgrade()
config()->set('permission.teams', true);

$this->artisan('permission:setup-teams')
->expectsConfirmation('Proceed with the migration creation?', 'yes');
->expectsQuestion('Proceed with the migration creation?', 'yes')
->assertExitCode(0);

$matchingFiles = glob(database_path('migrations/*_add_teams_fields.php'));
$this->assertTrue(count($matchingFiles) > 0);
Expand All @@ -156,4 +157,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 c0e768d

Please sign in to comment.