Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
bobimicroweber committed Sep 13, 2024
1 parent 28647b3 commit 4a773e1
Show file tree
Hide file tree
Showing 12 changed files with 121 additions and 61 deletions.
9 changes: 2 additions & 7 deletions web/app/Models/CronJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Models;

use App\Models\Scopes\CustomerHostingSubscriptionScope;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
Expand All @@ -17,13 +18,7 @@ class CronJob extends Model

protected static function booted(): void
{
static::addGlobalScope('customer', function (Builder $query) {
if (auth()->check() && auth()->guard()->name == 'web_customer') {
$query->whereHas('hostingSubscription', function ($query) {
$query->where('customer_id', auth()->user()->id);
});
}
});
static::addGlobalScope(new CustomerHostingSubscriptionScope());
}

public function hostingSubscription()
Expand Down
11 changes: 11 additions & 0 deletions web/app/Models/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\ApiSDK\PhyreApiSDK;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Facades\Hash;
use Laravel\Sanctum\HasApiTokens;

class Customer extends Authenticatable
Expand All @@ -31,7 +32,17 @@ public static function boot()
{
parent::boot();

static::updating(function ($model) {

$model->password = Hash::make($model->password);

});


static::creating(function ($model) {

$model->password = Hash::make($model->password);

if ($model->phyre_server_id > 0) {
$phyreServer = PhyreServer::where('id', $model->phyre_server_id)->first();
if ($phyreServer) {
Expand Down
9 changes: 2 additions & 7 deletions web/app/Models/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Models;

use App\Models\Scopes\CustomerHostingSubscriptionScope;
use App\PhyreConfig;
use App\Services\RemoteDatabaseService;
use App\UniversalDatabaseExecutor;
Expand All @@ -25,13 +26,7 @@ class Database extends Model

protected static function booted(): void
{
static::addGlobalScope('customer', function (Builder $query) {
if (auth()->check() && auth()->guard()->name == 'web_customer') {
$query->whereHas('hostingSubscription', function ($query) {
$query->where('customer_id', auth()->user()->id);
});
}
});
static::addGlobalScope(new CustomerHostingSubscriptionScope());
}

public static function boot()
Expand Down
1 change: 1 addition & 0 deletions web/app/Models/DatabaseUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Models;

use App\Models\Scopes\CustomerHostingSubscriptionScope;
use App\PhyreConfig;
use App\UniversalDatabaseExecutor;
use Illuminate\Database\Eloquent\Factories\HasFactory;
Expand Down
9 changes: 2 additions & 7 deletions web/app/Models/Domain.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Events\DomainIsCreated;
use App\Events\ModelDomainDeleting;
use App\Jobs\ApacheBuild;
use App\Models\Scopes\CustomerHostingSubscriptionScope;
use App\ShellApi;
use App\VirtualHosts\DTO\ApacheVirtualHostSettings;
use Illuminate\Database\Eloquent\Builder;
Expand Down Expand Up @@ -38,13 +39,7 @@ class Domain extends Model

protected static function booted(): void
{
static::addGlobalScope('customer', function (Builder $query) {
if (auth()->check() && auth()->guard()->name == 'web_customer') {
$query->whereHas('hostingSubscription', function ($query) {
$query->where('customer_id', auth()->user()->id);
});
}
});
static::addGlobalScope(new CustomerHostingSubscriptionScope());
}

public static function boot()
Expand Down
43 changes: 11 additions & 32 deletions web/app/Models/GitRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ public function clone()

$projectDir = $findDomain->domain_root . '/' . $this->dir;

$privateKeyFile = null;

$gitSSHKey = GitSshKey::find($this->git_ssh_key_id);
if ($gitSSHKey) {
$sshPath = '/home/'.$findHostingSubscription->system_username .'/.ssh';
Expand Down Expand Up @@ -135,46 +137,23 @@ public function clone()
return;
}

$gitProvider = $gitSSHUrl['provider'];

$shellCommand = [];
$shellCommand[] = 'echo "Cloning started at $(date)"';

$exportCommand = 'export HOME=/home/'.$findHostingSubscription->system_username;
$shellCommand[] = 'su -m '.$findHostingSubscription->system_username.' -c "'.$exportCommand.'"';


if ($gitSSHKey) {

$shellCommand[] = "ssh-keyscan $gitProvider >> /home/$findHostingSubscription->system_username/.ssh/known_hosts";
$shellCommand[] = 'chmod 0600 /home/'.$findHostingSubscription->system_username.'/.ssh/known_hosts';
$shellCommand[] = 'chown '.$findHostingSubscription->system_username.':'.$findHostingSubscription->system_username.' /home/'.$findHostingSubscription->system_username.'/.ssh/known_hosts';

$cloneUrl = 'git@'.$gitSSHUrl['provider'].':'.$gitSSHUrl['owner'].'/'.$gitSSHUrl['name'].'.git';
$cloneCommand = 'git -c core.sshCommand="ssh -i '.$privateKeyFile .'" clone '.$cloneUrl.' '.$projectDir . ' 2>&1';
} else {
$cloneCommand = 'git clone '.$this->url.' '.$projectDir . ' 2>&1';
}

$shellCommand[] = 'su -m '.$findHostingSubscription->system_username." -c '".$cloneCommand."'";

$shellCommand[] = 'phyre-php /usr/local/phyre/web/artisan git-repository:mark-as-cloned '.$this->id;
$cloneUrl = 'git@'.$gitSSHUrl['provider'].':'.$gitSSHUrl['owner'].'/'.$gitSSHUrl['name'].'.git';

$shellFile = '/tmp/git-clone-' . $this->id . '.sh';
$shellLog = '/tmp/git-clone-' . $this->id . '.log';

$shellCommand[] = 'rm -rf ' . $shellFile;
$shellContent = view('actions.git.clone-repo', [
'gitProvider' => $gitSSHUrl['provider'],
'systemUsername' => $findHostingSubscription->system_username,
'gitRepositoryId' => $this->id,
'cloneUrl' => $cloneUrl,
'projectDir' => $projectDir,
'privateKeyFile' => $privateKeyFile,
])->render();

$shellContent = '';
foreach ($shellCommand as $command) {
$shellContent .= $command . "\n";
}

shell_exec('rm -rf ' . $shellFile);
file_put_contents($shellFile, $shellContent);

shell_exec('chmod +x ' . $shellFile);

shell_exec('bash '.$shellFile.' >> ' . $shellLog . ' &');

}
Expand Down
8 changes: 5 additions & 3 deletions web/app/Models/Scopes/CustomerDomainScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Models\Domain;
use App\Models\HostingSubscription;
use Filament\Facades\Filament;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;
Expand All @@ -15,9 +16,10 @@ class CustomerDomainScope implements Scope
*/
public function apply(Builder $builder, Model $model): void
{
if (auth()->check() && auth()->guard()->name == 'web_customer') {

$findHostingSubscriptionIds = HostingSubscription::where('customer_id', auth()->user()->id)->pluck('id');
$guard = Filament::auth();
if ($guard->check() && $guard->name == 'web_customer') {

$findHostingSubscriptionIds = HostingSubscription::where('customer_id', $guard->user()->id)->pluck('id');
$findDomainIds = Domain::whereIn('hosting_subscription_id', $findHostingSubscriptionIds)->pluck('id');

$builder->whereIn('domain_id', $findDomainIds);
Expand Down
10 changes: 7 additions & 3 deletions web/app/Models/Scopes/CustomerHostingSubscriptionScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Models\Scopes;

use Filament\Facades\Filament;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;
Expand All @@ -13,9 +14,12 @@ class CustomerHostingSubscriptionScope implements Scope
*/
public function apply(Builder $builder, Model $model): void
{
if (auth()->check() && auth()->guard()->name == 'web_customer') {
$builder->whereHas('hostingSubscription', function ($query) {
$query->where('customer_id', auth()->user()->id);

$guard = Filament::auth();

if ($guard->check() && $guard->name == 'web_customer') {
$builder->whereHas('hostingSubscription', function ($query) use($guard) {
$query->where('customer_id', $guard->user()->id);
});
}
}
Expand Down
7 changes: 5 additions & 2 deletions web/app/Models/Scopes/CustomerScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Models\Scopes;

use Filament\Facades\Filament;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;
Expand All @@ -13,8 +14,10 @@ class CustomerScope implements Scope
*/
public function apply(Builder $builder, Model $model): void
{
if (auth()->check() && auth()->guard()->name == 'web_customer') {
$builder->where('customer_id', auth()->user()->id);
$guard = Filament::auth();

if ($guard->check() && $guard->name == 'web_customer') {
$builder->where('customer_id', $guard->user()->id);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

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

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('sessions', function (Blueprint $table) {
$table->string('id')->primary();
$table->foreignId('user_id')->nullable()->index();
$table->string('ip_address', 45)->nullable();
$table->text('user_agent')->nullable();
$table->longText('payload');
$table->integer('last_activity')->index();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('sessions');
}
};
22 changes: 22 additions & 0 deletions web/resources/views/actions/git/clone-repo.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
echo "Cloning started at {{ date('Y-m-d H:i:s') }}

su -m {{$systemUsername}} -c "export HOME=/home/{{$systemUsername}}"

@if($privateKeyFile)

ssh-keyscan {{$gitProvider}} >> /home/{{$systemUsername}}/.ssh/known_hosts
chmod 0600 /home/{{$systemUsername}}/.ssh/known_hosts
chown {{$systemUsername}}:{{$systemUsername}} /home/{{$systemUsername}}/.ssh/known_hosts

su -m {{$systemUsername}} -c 'git -c core.sshCommand="ssh -i {{$privateKeyFile}}" clone {{$cloneUrl}} {{$projectDir}}'

@else

su -m {{$systemUsername}} -c 'git clone {{$cloneUrl}} {{$projectDir}}'

@endif

phyre-php /usr/local/phyre/web/artisan git-repository:mark-as-cloned {{$gitRepositoryId}}


rm -rf /tmp/git-clone-{{$gitRepositoryId}}.sh
22 changes: 22 additions & 0 deletions web/resources/views/actions/git/pull-repo.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
echo "Pull started at {{ date('Y-m-d H:i:s') }}

su -m {{$systemUsername}} -c "export HOME=/home/{{$systemUsername}}"

@if($privateKeyFile)

ssh-keyscan {{$gitProvider}} >> /home/{{$systemUsername}}/.ssh/known_hosts
chmod 0600 /home/{{$systemUsername}}/.ssh/known_hosts
chown {{$systemUsername}}:{{$systemUsername}} /home/{{$systemUsername}}/.ssh/known_hosts

su -m {{$systemUsername}} -c 'cd {{$projectDir}} && git -c core.sshCommand="ssh -i {{$privateKeyFile}}" pull'

@else

su -m {{$systemUsername}} -c 'cd {{$projectDir}} && git pull'

@endif

phyre-php /usr/local/phyre/web/artisan git-repository:mark-as-pulled {{$gitRepositoryId}}


rm -rf /tmp/git-pull-{{$gitRepositoryId}}.sh

0 comments on commit 4a773e1

Please sign in to comment.