wip: boarding

This commit is contained in:
Andras Bacsai
2023-08-22 17:44:49 +02:00
parent 2414ddd360
commit b39ca51d41
71 changed files with 694 additions and 137 deletions

View File

@@ -12,7 +12,7 @@ class ApplicationController extends Controller
public function configuration()
{
$project = auth()->user()->currentTeam()->load(['projects'])->projects->where('uuid', request()->route('project_uuid'))->first();
$project = currentTeam()->load(['projects'])->projects->where('uuid', request()->route('project_uuid'))->first();
if (!$project) {
return redirect()->route('dashboard');
}
@@ -29,7 +29,7 @@ class ApplicationController extends Controller
public function deployments()
{
$project = auth()->user()->currentTeam()->load(['projects'])->projects->where('uuid', request()->route('project_uuid'))->first();
$project = currentTeam()->load(['projects'])->projects->where('uuid', request()->route('project_uuid'))->first();
if (!$project) {
return redirect()->route('dashboard');
}
@@ -49,7 +49,7 @@ class ApplicationController extends Controller
{
$deploymentUuid = request()->route('deployment_uuid');
$project = auth()->user()->currentTeam()->load(['projects'])->projects->where('uuid', request()->route('project_uuid'))->first();
$project = currentTeam()->load(['projects'])->projects->where('uuid', request()->route('project_uuid'))->first();
if (!$project) {
return redirect()->route('dashboard');
}

View File

@@ -58,7 +58,6 @@ class Controller extends BaseController
$resources += $project->applications->count();
$resources += $project->postgresqls->count();
}
return view('dashboard', [
'servers' => $servers->count(),
'projects' => $projects->count(),
@@ -66,10 +65,17 @@ class Controller extends BaseController
's3s' => $s3s,
]);
}
public function boarding() {
if (currentTeam()->boarding || is_dev()) {
return view('boarding');
} else {
return redirect()->route('dashboard');
}
}
public function settings()
{
if (is_instance_admin()) {
if (isInstanceAdmin()) {
$settings = InstanceSettings::get();
$database = StandalonePostgresql::whereName('coolify-db')->first();
if ($database) {
@@ -89,7 +95,7 @@ class Controller extends BaseController
{
$invitations = [];
if (auth()->user()->isAdminFromSession()) {
$invitations = TeamInvitation::whereTeamId(auth()->user()->currentTeam()->id)->get();
$invitations = TeamInvitation::whereTeamId(currentTeam()->id)->get();
}
return view('team.show', [
'invitations' => $invitations,
@@ -116,7 +122,7 @@ class Controller extends BaseController
{
$invitations = [];
if (auth()->user()->isAdminFromSession()) {
$invitations = TeamInvitation::whereTeamId(auth()->user()->currentTeam()->id)->get();
$invitations = TeamInvitation::whereTeamId(currentTeam()->id)->get();
}
return view('team.members', [
'invitations' => $invitations,

View File

@@ -11,7 +11,7 @@ class DatabaseController extends Controller
public function configuration()
{
$project = auth()->user()->currentTeam()->load(['projects'])->projects->where('uuid', request()->route('project_uuid'))->first();
$project = currentTeam()->load(['projects'])->projects->where('uuid', request()->route('project_uuid'))->first();
if (!$project) {
return redirect()->route('dashboard');
}
@@ -29,7 +29,7 @@ class DatabaseController extends Controller
public function executions()
{
$backup_uuid = request()->route('backup_uuid');
$project = auth()->user()->currentTeam()->load(['projects'])->projects->where('uuid', request()->route('project_uuid'))->first();
$project = currentTeam()->load(['projects'])->projects->where('uuid', request()->route('project_uuid'))->first();
if (!$project) {
return redirect()->route('dashboard');
}
@@ -50,13 +50,13 @@ class DatabaseController extends Controller
'database' => $database,
'backup' => $backup,
'executions' => $executions,
's3s' => auth()->user()->currentTeam()->s3s,
's3s' => currentTeam()->s3s,
]);
}
public function backups()
{
$project = auth()->user()->currentTeam()->load(['projects'])->projects->where('uuid', request()->route('project_uuid'))->first();
$project = currentTeam()->load(['projects'])->projects->where('uuid', request()->route('project_uuid'))->first();
if (!$project) {
return redirect()->route('dashboard');
}
@@ -70,7 +70,7 @@ class DatabaseController extends Controller
}
return view('project.database.backups.all', [
'database' => $database,
's3s' => auth()->user()->currentTeam()->s3s,
's3s' => currentTeam()->s3s,
]);
}
}

View File

@@ -41,7 +41,7 @@ class MagicController extends Controller
{
$project = Project::firstOrCreate(
['name' => request()->query('name') ?? generate_random_name()],
['team_id' => auth()->user()->currentTeam()->id]
['team_id' => currentTeam()->id]
);
return response()->json([
'project_uuid' => $project->uuid
@@ -68,7 +68,7 @@ class MagicController extends Controller
],
);
auth()->user()->teams()->attach($team, ['role' => 'admin']);
session(['currentTeam' => $team]);
refreshSession();
return redirect(request()->header('Referer'));
}
}

View File

@@ -18,7 +18,7 @@ class ProjectController extends Controller
public function edit()
{
$projectUuid = request()->route('project_uuid');
$teamId = auth()->user()->currentTeam()->id;
$teamId = currentTeam()->id;
$project = Project::where('team_id', $teamId)->where('uuid', $projectUuid)->first();
if (!$project) {
return redirect()->route('dashboard');
@@ -29,7 +29,7 @@ class ProjectController extends Controller
public function show()
{
$projectUuid = request()->route('project_uuid');
$teamId = auth()->user()->currentTeam()->id;
$teamId = currentTeam()->id;
$project = Project::where('team_id', $teamId)->where('uuid', $projectUuid)->first();
if (!$project) {
@@ -44,7 +44,7 @@ class ProjectController extends Controller
$type = request()->query('type');
$destination_uuid = request()->query('destination');
$project = auth()->user()->currentTeam()->load(['projects'])->projects->where('uuid', request()->route('project_uuid'))->first();
$project = currentTeam()->load(['projects'])->projects->where('uuid', request()->route('project_uuid'))->first();
if (!$project) {
return redirect()->route('dashboard');
}
@@ -67,7 +67,7 @@ class ProjectController extends Controller
public function resources()
{
$project = auth()->user()->currentTeam()->load(['projects'])->projects->where('uuid', request()->route('project_uuid'))->first();
$project = currentTeam()->load(['projects'])->projects->where('uuid', request()->route('project_uuid'))->first();
if (!$project) {
return redirect()->route('dashboard');
}

View File

@@ -12,14 +12,14 @@ class ServerController extends Controller
public function new_server()
{
if (!is_cloud() || is_instance_admin()) {
if (!is_cloud() || isInstanceAdmin()) {
return view('server.create', [
'limit_reached' => false,
'private_keys' => PrivateKey::ownedByCurrentTeam()->get(),
]);
}
$servers = auth()->user()->currentTeam()->servers->count();
$subscription = auth()->user()->currentTeam()?->subscription->type();
$servers = currentTeam()->servers->count();
$subscription = currentTeam()?->subscription->type();
$your_limit = config('constants.limits.server')[strtolower($subscription)];
$limit_reached = $servers >= $your_limit;

View File

@@ -39,6 +39,7 @@ class Kernel extends HttpKernel
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\App\Http\Middleware\CheckForcePasswordReset::class,
\App\Http\Middleware\SubscriptionValid::class,
\App\Http\Middleware\IsBoardingFlow::class,
],

View File

@@ -0,0 +1,147 @@
<?php
namespace App\Http\Livewire;
use App\Models\PrivateKey;
use App\Models\Project;
use App\Models\Server;
use Livewire\Component;
class Boarding extends Component
{
public string $currentState = 'create-private-key';
// public ?string $serverType = null;
public ?string $privateKeyType = null;
public ?string $privateKey = null;
public ?string $privateKeyName = null;
public ?string $privateKeyDescription = null;
public ?PrivateKey $createdPrivateKey = null;
public ?string $remoteServerName = null;
public ?string $remoteServerDescription = null;
public ?string $remoteServerHost = null;
public ?int $remoteServerPort = 22;
public ?string $remoteServerUser = 'root';
public ?Server $createdServer = null;
public function mount()
{
$this->privateKeyName = generate_random_name();
$this->remoteServerName = generate_random_name();
if (is_dev()) {
$this->privateKey = '-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW
QyNTUxOQAAACBbhpqHhqv6aI67Mj9abM3DVbmcfYhZAhC7ca4d9UCevAAAAJi/QySHv0Mk
hwAAAAtzc2gtZWQyNTUxOQAAACBbhpqHhqv6aI67Mj9abM3DVbmcfYhZAhC7ca4d9UCevA
AAAECBQw4jg1WRT2IGHMncCiZhURCts2s24HoDS0thHnnRKVuGmoeGq/pojrsyP1pszcNV
uZx9iFkCELtxrh31QJ68AAAAEXNhaWxANzZmZjY2ZDJlMmRkAQIDBA==
-----END OPENSSH PRIVATE KEY-----';
$this->privateKeyDescription = 'Created by Coolify';
$this->remoteServerDescription = 'Created by Coolify';
$this->remoteServerHost = 'coolify-testing-host';
}
}
public function restartBoarding()
{
if ($this->createdServer) {
$this->createdServer->delete();
}
if ($this->createdPrivateKey) {
$this->createdPrivateKey->delete();
}
return redirect()->route('boarding');
}
public function skipBoarding()
{
currentTeam()->update([
'show_boarding' => false
]);
refreshSession();
return redirect()->route('dashboard');
}
public function setServer(string $type)
{
if ($type === 'localhost') {
$this->currentState = 'create-project';
} elseif ($type === 'remote') {
$this->currentState = 'private-key';
}
}
public function setPrivateKey(string $type)
{
$this->privateKeyType = $type;
$this->currentState = 'create-private-key';
}
public function savePrivateKey()
{
$this->validate([
'privateKeyName' => 'required',
'privateKey' => 'required',
]);
$this->currentState = 'create-server';
}
public function saveServer()
{
$this->validate([
'remoteServerName' => 'required',
'remoteServerHost' => 'required',
'remoteServerPort' => 'required',
'remoteServerUser' => 'required',
]);
if ($this->privateKeyType === 'create') {
$this->createNewPrivateKey();
}
$this->privateKey = formatPrivateKey($this->privateKey);
$this->createdPrivateKey = PrivateKey::create([
'name' => $this->privateKeyName,
'description' => $this->privateKeyDescription,
'private_key' => $this->privateKey,
'team_id' => currentTeam()->id
]);
$this->createdServer = Server::create([
'name' => $this->remoteServerName,
'ip' => $this->remoteServerHost,
'port' => $this->remoteServerPort,
'user' => $this->remoteServerUser,
'description' => $this->remoteServerDescription,
'private_key_id' => $this->createdPrivateKey->id,
'team_id' => currentTeam()->id
]);
try {
['uptime' => $uptime, 'dockerVersion' => $dockerVersion] = validateServer($this->createdServer);
if (!$uptime) {
$this->createdServer->delete();
$this->createdPrivateKey->delete();
throw new \Exception('Server is not reachable.');
} else {
$this->createdServer->settings->update([
'is_reachable' => true,
]);
$this->emit('success', 'Server is reachable.');
}
if ($dockerVersion) {
$this->emit('error', 'Docker is not installed on the server.');
$this->currentState = 'install-docker';
return;
}
ray($uptime, $dockerVersion);
} catch (\Exception $e) {
return general_error_handler(customErrorMessage: "Server is not reachable. Reason: {$e->getMessage()}", that: $this);
}
}
private function createNewPrivateKey()
{
$this->privateKeyName = generate_random_name();
$this->privateKeyDescription = 'Created by Coolify';
$this->privateKey = generateSSHKey();
}
public function createNewProject()
{
Project::create([
'name' => generate_random_name(),
'team_id' => currentTeam()->id
]);
}
}

View File

@@ -67,7 +67,7 @@ class StandaloneDocker extends Component
'name' => $this->name,
'network' => $this->network,
'server_id' => $this->server_id,
'team_id' => auth()->user()->currentTeam()->id
'team_id' => currentTeam()->id
]);
}
$this->createNetworkAndAttachToProxy();

View File

@@ -41,10 +41,9 @@ class DiscordSettings extends Component
public function saveModel()
{
ray($this->model);
$this->model->save();
if (is_a($this->model, Team::class)) {
session(['currentTeam' => $this->model]);
refreshSession();
}
$this->emit('success', 'Settings saved.');
}

View File

@@ -59,7 +59,7 @@ class EmailSettings extends Component
{
$settings = InstanceSettings::get();
if ($settings->smtp_enabled) {
$team = auth()->user()->currentTeam();
$team = currentTeam();
$team->update([
'smtp_enabled' => $settings->smtp_enabled,
'smtp_from_address' => $settings->smtp_from_address,
@@ -74,7 +74,7 @@ class EmailSettings extends Component
]);
$this->decrypt();
if (is_a($team, Team::class)) {
session(['currentTeam' => $team]);
refreshSession();
}
$this->model = $team;
$this->emit('success', 'Settings saved.');
@@ -119,7 +119,7 @@ class EmailSettings extends Component
$this->model->save();
$this->decrypt();
if (is_a($this->model, Team::class)) {
session(['currentTeam' => $this->model]);
refreshSession();
}
$this->emit('success', 'Settings saved.');
}

View File

@@ -26,7 +26,7 @@ class Change extends Component
try {
if ($this->private_key->isEmpty()) {
$this->private_key->delete();
auth()->user()->currentTeam()->privateKeys = PrivateKey::where('team_id', auth()->user()->currentTeam()->id)->get();
currentTeam()->privateKeys = PrivateKey::where('team_id', currentTeam()->id)->get();
return redirect()->route('private-key.all');
}
$this->emit('error', 'This private key is in use and cannot be deleted. Please delete all servers, applications, and GitHub/GitLab apps that use this private key before deleting it.');
@@ -38,10 +38,7 @@ class Change extends Component
public function changePrivateKey()
{
try {
$this->private_key->private_key = trim($this->private_key->private_key);
if (!str_ends_with($this->private_key->private_key, "\n")) {
$this->private_key->private_key .= "\n";
}
$this->private_key->private_key = formatPrivateKey($this->private_key->private_key);
$this->private_key->save();
refresh_server_connection($this->private_key);
} catch (\Exception $e) {

View File

@@ -32,7 +32,7 @@ class Create extends Component
'name' => $this->name,
'description' => $this->description,
'private_key' => $this->value,
'team_id' => auth()->user()->currentTeam()->id
'team_id' => currentTeam()->id
]);
if ($this->from === 'server') {
return redirect()->route('server.create');

View File

@@ -25,7 +25,7 @@ class AddEmpty extends Component
$project = Project::create([
'name' => $this->name,
'description' => $this->description,
'team_id' => auth()->user()->currentTeam()->id,
'team_id' => currentTeam()->id,
]);
return redirect()->route('project.show', $project->uuid);
} catch (\Exception $e) {

View File

@@ -29,7 +29,7 @@ class Source extends Component
private function get_private_keys()
{
$this->private_keys = PrivateKey::whereTeamId(auth()->user()->currentTeam()->id)->get()->reject(function ($key) {
$this->private_keys = PrivateKey::whereTeamId(currentTeam()->id)->get()->reject(function ($key) {
return $key->id == $this->application->private_key_id;
});
}

View File

@@ -39,7 +39,7 @@ class CreateScheduledBackup extends Component
's3_storage_id' => $this->s3_storage_id,
'database_id' => $this->database->id,
'database_type' => $this->database->getMorphClass(),
'team_id' => auth()->user()->currentTeam()->id,
'team_id' => currentTeam()->id,
]);
$this->emit('refreshScheduledBackups');
} catch (\Exception $e) {

View File

@@ -11,7 +11,7 @@ class EmptyProject extends Component
{
$project = Project::create([
'name' => generate_random_name(),
'team_id' => auth()->user()->currentTeam()->id,
'team_id' => currentTeam()->id,
]);
return redirect()->route('project.show', ['project_uuid' => $project->uuid, 'environment_name' => 'production']);
}

View File

@@ -55,7 +55,7 @@ class GithubPrivateRepositoryDeployKey extends Component
}
$this->parameters = get_route_parameters();
$this->query = request()->query();
$this->private_keys = PrivateKey::where('team_id', auth()->user()->currentTeam()->id)->where('id', '!=', 0)->get();
$this->private_keys = PrivateKey::where('team_id', currentTeam()->id)->where('id', '!=', 0)->get();
}
public function instantSave()

View File

@@ -42,7 +42,7 @@ class Form extends Component
public function installDocker()
{
$activity = resolve(InstallDocker::class)($this->server, auth()->user()->currentTeam());
$activity = resolve(InstallDocker::class)($this->server, currentTeam());
$this->emit('newMonitorActivity', $activity->id);
}

View File

@@ -65,7 +65,7 @@ class ByIp extends Component
'ip' => $this->ip,
'user' => $this->user,
'port' => $this->port,
'team_id' => auth()->user()->currentTeam()->id,
'team_id' => currentTeam()->id,
'private_key_id' => $this->private_key_id,
]);
$server->settings->is_part_of_swarm = $this->is_part_of_swarm;

View File

@@ -65,7 +65,7 @@ class Backup extends Component
'frequency' => '0 0 * * *',
'database_id' => $this->database->id,
'database_type' => 'App\Models\StandalonePostgresql',
'team_id' => auth()->user()->currentTeam()->id,
'team_id' => currentTeam()->id,
]);
$this->database->refresh();
$this->backup->refresh();

View File

@@ -40,7 +40,7 @@ class Create extends Component
'custom_user' => $this->custom_user,
'custom_port' => $this->custom_port,
'is_system_wide' => $this->is_system_wide,
'team_id' => auth()->user()->currentTeam()->id,
'team_id' => currentTeam()->id,
]);
redirect()->route('source.github.show', ['github_app_uuid' => $github_app->uuid]);
} catch (\Exception $e) {

View File

@@ -10,7 +10,7 @@ class Actions extends Component
public function cancel()
{
try {
$subscription_id = auth()->user()->currentTeam()->subscription->lemon_subscription_id;
$subscription_id = currentTeam()->subscription->lemon_subscription_id;
if (!$subscription_id) {
throw new \Exception('No subscription found');
}
@@ -37,7 +37,7 @@ class Actions extends Component
public function resume()
{
try {
$subscription_id = auth()->user()->currentTeam()->subscription->lemon_subscription_id;
$subscription_id = currentTeam()->subscription->lemon_subscription_id;
if (!$subscription_id) {
throw new \Exception('No subscription found');
}

View File

@@ -29,7 +29,7 @@ class Create extends Component
'personal_team' => false,
]);
auth()->user()->teams()->attach($team, ['role' => 'admin']);
session(['currentTeam' => $team]);
refreshSession();
return redirect()->route('team.show');
} catch (\Throwable $th) {
return general_error_handler($th, $this);

View File

@@ -9,7 +9,7 @@ class Delete extends Component
{
public function delete()
{
$currentTeam = auth()->user()->currentTeam();
$currentTeam = currentTeam();
$currentTeam->delete();
$team = auth()->user()->teams()->first();
@@ -24,7 +24,7 @@ class Delete extends Component
}
});
session(['currentTeam' => $team]);
refreshSession();
return redirect()->route('team.show');
}
}

View File

@@ -19,7 +19,7 @@ class Form extends Component
public function mount()
{
$this->team = auth()->user()->currentTeam();
$this->team = currentTeam();
}
public function submit()
@@ -27,7 +27,7 @@ class Form extends Component
$this->validate();
try {
$this->team->save();
session(['currentTeam' => $this->team]);
refreshSession();
$this->emit('reloadWindow');
} catch (\Throwable $th) {
return general_error_handler($th, $this);

View File

@@ -18,6 +18,6 @@ class Invitations extends Component
public function refreshInvitations()
{
$this->invitations = TeamInvitation::whereTeamId(auth()->user()->currentTeam()->id)->get();
$this->invitations = TeamInvitation::whereTeamId(currentTeam()->id)->get();
}
}

View File

@@ -35,9 +35,9 @@ class InviteLink extends Component
return general_error_handler(that: $this, customErrorMessage: "$this->email must be registered first (or activate transactional emails to invite via email).");
}
$member_emails = auth()->user()->currentTeam()->members()->get()->pluck('email');
$member_emails = currentTeam()->members()->get()->pluck('email');
if ($member_emails->contains($this->email)) {
return general_error_handler(that: $this, customErrorMessage: "$this->email is already a member of " . auth()->user()->currentTeam()->name . ".");
return general_error_handler(that: $this, customErrorMessage: "$this->email is already a member of " . currentTeam()->name . ".");
}
$invitation = TeamInvitation::whereEmail($this->email);
@@ -53,7 +53,7 @@ class InviteLink extends Component
}
TeamInvitation::firstOrCreate([
'team_id' => auth()->user()->currentTeam()->id,
'team_id' => currentTeam()->id,
'uuid' => $uuid,
'email' => $this->email,
'role' => $this->role,

View File

@@ -11,19 +11,19 @@ class Member extends Component
public function makeAdmin()
{
$this->member->teams()->updateExistingPivot(auth()->user()->currentTeam()->id, ['role' => 'admin']);
$this->member->teams()->updateExistingPivot(currentTeam()->id, ['role' => 'admin']);
$this->emit('reloadWindow');
}
public function makeReadonly()
{
$this->member->teams()->updateExistingPivot(auth()->user()->currentTeam()->id, ['role' => 'member']);
$this->member->teams()->updateExistingPivot(currentTeam()->id, ['role' => 'member']);
$this->emit('reloadWindow');
}
public function remove()
{
$this->member->teams()->detach(auth()->user()->currentTeam());
$this->member->teams()->detach(currentTeam());
$this->emit('reloadWindow');
}
}

View File

@@ -62,7 +62,7 @@ class Create extends Component
} else {
$this->storage->endpoint = $this->endpoint;
}
$this->storage->team_id = auth()->user()->currentTeam()->id;
$this->storage->team_id = currentTeam()->id;
$this->storage->testConnection();
$this->emit('success', 'Connection is working. Tested with "ListObjectsV2" action.');
$this->storage->save();

View File

@@ -0,0 +1,28 @@
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
class IsBoardingFlow
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next): Response
{
$allowed_paths = [
'subscription',
'boarding',
'livewire/message/boarding'
];
if (showBoarding() && !in_array($request->path(), $allowed_paths)) {
return redirect('boarding');
}
return $next($request);
}
}

View File

@@ -17,8 +17,7 @@ class SubscriptionValid
return $next($request);
}
}
$is_instance_admin = is_instance_admin();
if ($is_instance_admin) {
if (isInstanceAdmin()) {
return $next($request);
}