Skip to content

An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.

feat(auth): implement authorization for PrivateKey management

Andras Bacsaid8d01e6886fa
diff --git a/app/Livewire/Security/PrivateKey/Create.php b/app/Livewire/Security/PrivateKey/Create.php
index ff196bd355..0f36037ff1 100644
--- a/app/Livewire/Security/PrivateKey/Create.php
+++ b/app/Livewire/Security/PrivateKey/Create.php
@@ -4,10 +4,13 @@ namespace App\Livewire\Security\PrivateKey;
use App\Models\PrivateKey;
use App\Support\ValidationPatterns;
+use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Livewire\Component;
class Create extends Component
{
+ use AuthorizesRequests;
+
public string $name = '';
public string $value = '';
@@ -66,6 +69,7 @@ class Create extends Component
$this->validate();
try {
+ $this->authorize('create', PrivateKey::class);
$privateKey = PrivateKey::createAndStore([
'name' => $this->name,
'description' => $this->description,
diff --git a/app/Livewire/Security/PrivateKey/Show.php b/app/Livewire/Security/PrivateKey/Show.php
index b783674648..2ff06c3492 100644
--- a/app/Livewire/Security/PrivateKey/Show.php
+++ b/app/Livewire/Security/PrivateKey/Show.php
@@ -4,10 +4,13 @@ namespace App\Livewire\Security\PrivateKey;
use App\Models\PrivateKey;
use App\Support\ValidationPatterns;
+use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Livewire\Component;
class Show extends Component
{
+ use AuthorizesRequests;
+
public PrivateKey $private_key;
public $public_key = 'Loading...';
@@ -62,6 +65,7 @@ class Show extends Component
public function delete()
{
try {
+ $this->authorize('delete', $this->private_key);
$this->private_key->safeDelete();
currentTeam()->privateKeys = PrivateKey::where('team_id', currentTeam()->id)->get();
@@ -76,6 +80,7 @@ class Show extends Component
public function changePrivateKey()
{
try {
+ $this->authorize('update', $this->private_key);
$this->private_key->updatePrivateKey([
'private_key' => formatPrivateKey($this->private_key->private_key),
]);
diff --git a/app/Policies/PrivateKeyPolicy.php b/app/Policies/PrivateKeyPolicy.php
new file mode 100644
index 0000000000..6b9fd2171d
--- /dev/null
+++ b/app/Policies/PrivateKeyPolicy.php
@@ -0,0 +1,65 @@
+<?php
+
+namespace App\Policies;
+
+use App\Models\PrivateKey;
+use App\Models\User;
+
+class PrivateKeyPolicy
+{
+ /**
+ * Determine whether the user can view any models.
+ */
+ public function viewAny(User $user): bool
+ {
+ return true;
+ }
+
+ /**
+ * Determine whether the user can view the model.
+ */
+ public function view(User $user, PrivateKey $privateKey): bool
+ {
+ return $user->teams()->get()->firstWhere('id', $privateKey->team_id) !== null;
+ }
+
+ /**
+ * Determine whether the user can create models.
+ */
+ public function create(User $user): bool
+ {
+ return $user->isAdmin();
+ }
+
+ /**
+ * Determine whether the user can update the model.
+ */
+ public function update(User $user, PrivateKey $privateKey): bool
+ {
+ return $user->isAdmin() && $user->teams()->get()->firstWhere('id', $privateKey->team_id) !== null;
+ }
+
+ /**
+ * Determine whether the user can delete the model.
+ */
+ public function delete(User $user, PrivateKey $privateKey): bool
+ {
+ return $user->isAdmin() && $user->teams()->get()->firstWhere('id', $privateKey->team_id) !== null;
+ }
+
+ /**
+ * Determine whether the user can restore the model.
+ */
+ public function restore(User $user, PrivateKey $privateKey): bool
+ {
+ return false;
+ }
+
+ /**
+ * Determine whether the user can permanently delete the model.
+ */
+ public function forceDelete(User $user, PrivateKey $privateKey): bool
+ {
+ return false;
+ }
+}
diff --git a/app/Providers/AuthServiceProvider.php b/app/Providers/AuthServiceProvider.php
index dafcbee79f..476e064d60 100644
--- a/app/Providers/AuthServiceProvider.php
+++ b/app/Providers/AuthServiceProvider.php
@@ -13,7 +13,8 @@ class AuthServiceProvider extends ServiceProvider
* @var array<class-string, class-string>
*/
protected $policies = [
- // 'App\Models\Model' => 'App\Policies\ModelPolicy',
+ \App\Models\Server::class => \App\Policies\ServerPolicy::class,
+ \App\Models\PrivateKey::class => \App\Policies\PrivateKeyPolicy::class,
];
/**