fix: add middleware to new abilities, better ux for selecting permissions, etc.

This commit is contained in:
Andras Bacsai
2024-12-09 10:28:34 +01:00
parent 78f0ac80c1
commit 5bbcd7bf76
7 changed files with 191 additions and 136 deletions

View File

@@ -2,8 +2,6 @@
use App\Models\PersonalAccessToken;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
@@ -12,14 +10,24 @@ return new class extends Migration
*/
public function up(): void
{
$tokens = PersonalAccessToken::all();
foreach ($tokens as $token) {
$abilities = collect();
if (in_array('*', $token->abilities)) $abilities->push('write', 'read', 'read:sensitive');
if (in_array('read-only', $token->abilities)) $abilities->push('read');
if (in_array('view:sensitive', $token->abilities)) $abilities->push('read', 'read:sensitive');
$token->abilities = $abilities->unique()->values()->all();
$token->save();
try {
$tokens = PersonalAccessToken::all();
foreach ($tokens as $token) {
$abilities = collect();
if (in_array('*', $token->abilities)) {
$abilities->push('write', 'deploy', 'read', 'read:sensitive');
}
if (in_array('read-only', $token->abilities)) {
$abilities->push('read');
}
if (in_array('view:sensitive', $token->abilities)) {
$abilities->push('read', 'read:sensitive');
}
$token->abilities = $abilities->unique()->values()->all();
$token->save();
}
} catch (\Exception $e) {
\Log::error('Error renaming token permissions: '.$e->getMessage());
}
}
@@ -28,17 +36,25 @@ return new class extends Migration
*/
public function down(): void
{
$tokens = PersonalAccessToken::all();
foreach ($tokens as $token) {
$abilities = collect();
if (in_array('write', $token->abilities)) {
$abilities->push('*');
} else {
if (in_array('read', $token->abilities)) $abilities->push('read-only');
if (in_array('read:sensitive', $token->abilities)) $abilities->push('view:sensitive');
try {
$tokens = PersonalAccessToken::all();
foreach ($tokens as $token) {
$abilities = collect();
if (in_array('write', $token->abilities)) {
$abilities->push('*');
} else {
if (in_array('read', $token->abilities)) {
$abilities->push('read-only');
}
if (in_array('read:sensitive', $token->abilities)) {
$abilities->push('view:sensitive');
}
}
$token->abilities = $abilities->unique()->values()->all();
$token->save();
}
$token->abilities = $abilities->unique()->values()->all();
$token->save();
} catch (\Exception $e) {
\Log::error('Error renaming token permissions: '.$e->getMessage());
}
}
};