Skip to content

Commit

Permalink
global storage-providers, notification channels and server providers (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
saeedvaziry authored Jun 29, 2024
1 parent ad027eb commit 11e3b16
Show file tree
Hide file tree
Showing 44 changed files with 678 additions and 77 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();
}
}
1 change: 1 addition & 0 deletions app/Actions/ServerProvider/CreateServerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function create(User $user, array $input): ServerProvider
$serverProvider->profile = $input['name'];
$serverProvider->provider = $input['provider'];
$serverProvider->credentials = $provider->credentialData($input);
$serverProvider->project_id = isset($input['global']) && $input['global'] ? null : $user->current_project_id;
$serverProvider->save();

return $serverProvider;
Expand Down
34 changes: 34 additions & 0 deletions app/Actions/ServerProvider/EditServerProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace App\Actions\ServerProvider;

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

class EditServerProvider
{
public function edit(ServerProvider $serverProvider, User $user, array $input): void
{
$this->validate($input);

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

$serverProvider->save();
}

/**
* @throws ValidationException
*/
private function validate(array $input): void
{
$rules = [
'name' => [
'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
4 changes: 3 additions & 1 deletion app/Http/Controllers/ServerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ public function create(Request $request): View
$this->authorize('create', [Server::class, $user->currentProject]);

$provider = $request->query('provider', old('provider', \App\Enums\ServerProvider::CUSTOM));
$serverProviders = ServerProvider::query()->where('provider', $provider)->get();
$serverProviders = ServerProvider::getByProjectId(auth()->user()->current_project_id)
->where('provider', $provider)
->get();

return view('servers.create', [
'serverProviders' => $serverProviders,
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)->get(),
];

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
7 changes: 6 additions & 1 deletion app/Http/Controllers/Settings/ProjectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function delete(Project $project): RedirectResponse
return back();
}

public function switch($projectId): RedirectResponse
public function switch(Request $request, $projectId): RedirectResponse
{
/** @var User $user */
$user = auth()->user();
Expand All @@ -81,6 +81,11 @@ public function switch($projectId): RedirectResponse
$user->current_project_id = $project->id;
$user->save();

// check if the referer is settings/*
if (str_contains($request->headers->get('referer'), 'settings')) {
return redirect()->to($request->headers->get('referer'));
}

return redirect()->route('servers');
}
}
28 changes: 24 additions & 4 deletions app/Http/Controllers/Settings/ServerProviderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Actions\ServerProvider\CreateServerProvider;
use App\Actions\ServerProvider\DeleteServerProvider;
use App\Actions\ServerProvider\EditServerProvider;
use App\Facades\Toast;
use App\Helpers\HtmxResponse;
use App\Http\Controllers\Controller;
Expand All @@ -14,11 +15,17 @@

class ServerProviderController extends Controller
{
public function index(): View
public function index(Request $request): View
{
return view('settings.server-providers.index', [
'providers' => auth()->user()->serverProviders,
]);
$data = [
'providers' => ServerProvider::getByProjectId(auth()->user()->current_project_id)->get(),
];

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

return view('settings.server-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.server-providers'));
}

public function update(ServerProvider $serverProvider, Request $request): HtmxResponse
{
app(EditServerProvider::class)->edit(
$serverProvider,
$request->user(),
$request->input(),
);

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

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

public function delete(ServerProvider $serverProvider): RedirectResponse
{
try {
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)->get(),
];

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)->get(),
];

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\Builder;
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): Builder
{
return self::query()
->where('project_id', $projectId)
->orWhereNull('project_id');
}
}
Loading

0 comments on commit 11e3b16

Please sign in to comment.