Skip to content

Commit

Permalink
global storage-providers and notification channels
Browse files Browse the repository at this point in the history
  • Loading branch information
saeedvaziry committed Jun 25, 2024
1 parent e031baf commit 7ae619e
Show file tree
Hide file tree
Showing 26 changed files with 388 additions and 41 deletions.
1 change: 1 addition & 0 deletions app/Actions/NotificationChannels/AddChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public function add(User $user, array $input): void
'user_id' => $user->id,
'provider' => $input['provider'],
'label' => $input['label'],
'project_id' => isset($input['global']) && $input['global'] ? null : $user->current_project_id,
]);
$this->validateType($channel, $input);
$channel->data = $channel->provider()->createData($input);
Expand Down
34 changes: 34 additions & 0 deletions app/Actions/NotificationChannels/EditChannel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace App\Actions\NotificationChannels;

use App\Models\NotificationChannel;
use App\Models\User;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\ValidationException;

class EditChannel
{
public function edit(NotificationChannel $notificationChannel, User $user, array $input): void
{
$this->validate($input);

$notificationChannel->label = $input['label'];
$notificationChannel->project_id = isset($input['global']) && $input['global'] ? null : $user->current_project_id;

$notificationChannel->save();
}

/**
* @throws ValidationException
*/
private function validate(array $input): void
{
$rules = [
'label' => [
'required',
],
];
Validator::make($input, $rules)->validate();
}
}
2 changes: 1 addition & 1 deletion app/Actions/SourceControl/EditSourceControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function edit(SourceControl $sourceControl, User $user, array $input): vo
$this->validate($input);

$sourceControl->profile = $input['name'];
$sourceControl->url = isset($input['url']) ? $input['url'] : null;
$sourceControl->url = $input['url'] ?? null;
$sourceControl->project_id = isset($input['global']) && $input['global'] ? null : $user->current_project_id;

$this->validateProvider($sourceControl, $input);
Expand Down
1 change: 1 addition & 0 deletions app/Actions/StorageProvider/CreateStorageProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function create(User $user, array $input): void
'user_id' => $user->id,
'provider' => $input['provider'],
'profile' => $input['name'],
'project_id' => isset($input['global']) && $input['global'] ? null : $user->current_project_id,
]);

$this->validateProvider($input, $storageProvider->provider()->validationRules());
Expand Down
34 changes: 34 additions & 0 deletions app/Actions/StorageProvider/EditStorageProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace App\Actions\StorageProvider;

use App\Models\StorageProvider;
use App\Models\User;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\ValidationException;

class EditStorageProvider
{
public function edit(StorageProvider $storageProvider, User $user, array $input): void
{
$this->validate($input);

$storageProvider->profile = $input['name'];
$storageProvider->project_id = isset($input['global']) && $input['global'] ? null : $user->current_project_id;

$storageProvider->save();
}

/**
* @throws ValidationException
*/
private function validate(array $input): void
{
$rules = [
'name' => [
'required',
],
];
Validator::make($input, $rules)->validate();
}
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/SSHKeyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function store(Server $server, Request $request): HtmxResponse
{
$this->authorize('manage', $server);

/** @var \App\Models\SshKey $key */
/** @var SshKey $key */
$key = app(CreateSshKey::class)->create(
$request->user(),
$request->input()
Expand Down
28 changes: 24 additions & 4 deletions app/Http/Controllers/Settings/NotificationChannelController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Http\Controllers\Settings;

use App\Actions\NotificationChannels\AddChannel;
use App\Actions\NotificationChannels\EditChannel;
use App\Facades\Toast;
use App\Helpers\HtmxResponse;
use App\Http\Controllers\Controller;
Expand All @@ -13,11 +14,17 @@

class NotificationChannelController extends Controller
{
public function index(): View
public function index(Request $request): View
{
return view('settings.notification-channels.index', [
'channels' => NotificationChannel::query()->latest()->get(),
]);
$data = [
'channels' => NotificationChannel::getByProjectId(auth()->user()->current_project_id),
];

if ($request->has('edit')) {
$data['editChannel'] = NotificationChannel::find($request->input('edit'));
}

return view('settings.notification-channels.index', $data);
}

public function add(Request $request): HtmxResponse
Expand All @@ -32,6 +39,19 @@ public function add(Request $request): HtmxResponse
return htmx()->redirect(route('settings.notification-channels'));
}

public function update(NotificationChannel $notificationChannel, Request $request): HtmxResponse
{
app(EditChannel::class)->edit(
$notificationChannel,
$request->user(),
$request->input(),
);

Toast::success('Channel updated.');

return htmx()->redirect(route('settings.notification-channels'));
}

public function delete(int $id): RedirectResponse
{
$channel = NotificationChannel::query()->findOrFail($id);
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Settings/SourceControlController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class SourceControlController extends Controller
public function index(Request $request): View
{
$data = [
'sourceControls' => SourceControl::getByCurrentProject(),
'sourceControls' => SourceControl::getByProjectId(auth()->user()->current_project_id),
];

if ($request->has('edit')) {
Expand Down
28 changes: 24 additions & 4 deletions app/Http/Controllers/Settings/StorageProviderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Actions\StorageProvider\CreateStorageProvider;
use App\Actions\StorageProvider\DeleteStorageProvider;
use App\Actions\StorageProvider\EditStorageProvider;
use App\Facades\Toast;
use App\Helpers\HtmxResponse;
use App\Http\Controllers\Controller;
Expand All @@ -14,11 +15,17 @@

class StorageProviderController extends Controller
{
public function index(): View
public function index(Request $request): View
{
return view('settings.storage-providers.index', [
'providers' => auth()->user()->storageProviders,
]);
$data = [
'providers' => StorageProvider::getByProjectId(auth()->user()->current_project_id),
];

if ($request->has('edit')) {
$data['editProvider'] = StorageProvider::find($request->input('edit'));
}

return view('settings.storage-providers.index', $data);
}

public function connect(Request $request): HtmxResponse
Expand All @@ -33,6 +40,19 @@ public function connect(Request $request): HtmxResponse
return htmx()->redirect(route('settings.storage-providers'));
}

public function update(StorageProvider $storageProvider, Request $request): HtmxResponse
{
app(EditStorageProvider::class)->edit(
$storageProvider,
$request->user(),
$request->input(),
);

Toast::success('Provider updated.');

return htmx()->redirect(route('settings.storage-providers'));
}

public function delete(StorageProvider $storageProvider): RedirectResponse
{
try {
Expand Down
16 changes: 16 additions & 0 deletions app/Models/NotificationChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace App\Models;

use App\Notifications\NotificationInterface;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Notifications\Notifiable;

/**
Expand All @@ -12,6 +14,7 @@
* @property array data
* @property string label
* @property bool connected
* @property int $project_id
*/
class NotificationChannel extends AbstractModel
{
Expand All @@ -24,6 +27,7 @@ class NotificationChannel extends AbstractModel
'data',
'connected',
'is_default',
'project_id',
];

protected $casts = [
Expand All @@ -47,4 +51,16 @@ public static function notifyAll(NotificationInterface $notification): void
$channel->notify($notification);
}
}

public function project(): BelongsTo
{
return $this->belongsTo(Project::class);
}

public static function getByProjectId(int $projectId): Collection
{
return self::query()
->where('project_id', $projectId)
->orWhereNull('project_id')->get();
}
}
4 changes: 2 additions & 2 deletions app/Models/SourceControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ public function project(): BelongsTo
return $this->belongsTo(Project::class);
}

public static function getByCurrentProject(): Collection
public static function getByProjectId(int $projectId): Collection
{
return self::query()
->where('project_id', auth()->user()->current_project_id)
->where('project_id', $projectId)
->orWhereNull('project_id')->get();
}
}
16 changes: 16 additions & 0 deletions app/Models/StorageProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Models;

use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
Expand All @@ -12,6 +13,7 @@
* @property string $provider
* @property array $credentials
* @property User $user
* @property int $project_id
*/
class StorageProvider extends AbstractModel
{
Expand All @@ -22,11 +24,13 @@ class StorageProvider extends AbstractModel
'profile',
'provider',
'credentials',
'project_id',
];

protected $casts = [
'user_id' => 'integer',
'credentials' => 'encrypted:array',
'project_id' => 'integer',
];

public function user(): BelongsTo
Expand All @@ -45,4 +49,16 @@ public function backups(): HasMany
{
return $this->hasMany(Backup::class, 'storage_id');
}

public function project(): BelongsTo
{
return $this->belongsTo(Project::class);
}

public static function getByProjectId(int $projectId): Collection
{
return self::query()
->where('project_id', $projectId)
->orWhereNull('project_id')->get();
}
}
23 changes: 0 additions & 23 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,24 +106,6 @@ public function storageProvider(string $provider): HasOne
return $this->hasOne(StorageProvider::class)->where('provider', $provider);
}

public function connectedStorageProviders(): HasMany
{
return $this->storageProviders()->where('connected', true);
}

public function connectedSourceControls(): array
{
$connectedSourceControls = [];
$sourceControls = $this->sourceControls()
->where('connected', 1)
->get(['provider']);
foreach ($sourceControls as $sourceControl) {
$connectedSourceControls[] = $sourceControl->provider;
}

return $connectedSourceControls;
}

public function projects(): BelongsToMany
{
return $this->belongsToMany(Project::class, 'user_project')->withTimestamps();
Expand All @@ -134,11 +116,6 @@ public function currentProject(): HasOne
return $this->HasOne(Project::class, 'id', 'current_project_id');
}

public function isMemberOfProject(Project $project): bool
{
return $project->user_id === $this->id;
}

public function createDefaultProject(): Project
{
$project = $this->projects()->first();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
public function up(): void
{
Schema::table('notification_channels', function (Blueprint $table) {
$table->unsignedBigInteger('project_id')->nullable();
});
}

public function down(): void
{
Schema::table('notification_channels', function (Blueprint $table) {
$table->dropColumn('project_id');
});
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
public function up(): void
{
Schema::table('storage_providers', function (Blueprint $table) {
$table->unsignedBigInteger('project_id')->nullable();
});
}

public function down(): void
{
Schema::table('storage_providers', function (Blueprint $table) {
$table->dropColumn('project_id');
});
}
};
Loading

0 comments on commit 7ae619e

Please sign in to comment.