This commit is contained in:
Bozhidar 2024-09-13 14:52:48 +03:00
parent 28647b38fd
commit 4a773e1f1d
12 changed files with 121 additions and 61 deletions

View file

@ -2,6 +2,7 @@
namespace App\Models; namespace App\Models;
use App\Models\Scopes\CustomerHostingSubscriptionScope;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str; use Illuminate\Support\Str;
@ -17,13 +18,7 @@ class CronJob extends Model
protected static function booted(): void protected static function booted(): void
{ {
static::addGlobalScope('customer', function (Builder $query) { static::addGlobalScope(new CustomerHostingSubscriptionScope());
if (auth()->check() && auth()->guard()->name == 'web_customer') {
$query->whereHas('hostingSubscription', function ($query) {
$query->where('customer_id', auth()->user()->id);
});
}
});
} }
public function hostingSubscription() public function hostingSubscription()

View file

@ -5,6 +5,7 @@ namespace App\Models;
use App\ApiSDK\PhyreApiSDK; use App\ApiSDK\PhyreApiSDK;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Facades\Hash;
use Laravel\Sanctum\HasApiTokens; use Laravel\Sanctum\HasApiTokens;
class Customer extends Authenticatable class Customer extends Authenticatable
@ -31,7 +32,17 @@ class Customer extends Authenticatable
{ {
parent::boot(); parent::boot();
static::updating(function ($model) {
$model->password = Hash::make($model->password);
});
static::creating(function ($model) { static::creating(function ($model) {
$model->password = Hash::make($model->password);
if ($model->phyre_server_id > 0) { if ($model->phyre_server_id > 0) {
$phyreServer = PhyreServer::where('id', $model->phyre_server_id)->first(); $phyreServer = PhyreServer::where('id', $model->phyre_server_id)->first();
if ($phyreServer) { if ($phyreServer) {

View file

@ -2,6 +2,7 @@
namespace App\Models; namespace App\Models;
use App\Models\Scopes\CustomerHostingSubscriptionScope;
use App\PhyreConfig; use App\PhyreConfig;
use App\Services\RemoteDatabaseService; use App\Services\RemoteDatabaseService;
use App\UniversalDatabaseExecutor; use App\UniversalDatabaseExecutor;
@ -25,13 +26,7 @@ class Database extends Model
protected static function booted(): void protected static function booted(): void
{ {
static::addGlobalScope('customer', function (Builder $query) { static::addGlobalScope(new CustomerHostingSubscriptionScope());
if (auth()->check() && auth()->guard()->name == 'web_customer') {
$query->whereHas('hostingSubscription', function ($query) {
$query->where('customer_id', auth()->user()->id);
});
}
});
} }
public static function boot() public static function boot()

View file

@ -2,6 +2,7 @@
namespace App\Models; namespace App\Models;
use App\Models\Scopes\CustomerHostingSubscriptionScope;
use App\PhyreConfig; use App\PhyreConfig;
use App\UniversalDatabaseExecutor; use App\UniversalDatabaseExecutor;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;

View file

@ -6,6 +6,7 @@ use App\Actions\ApacheWebsiteDelete;
use App\Events\DomainIsCreated; use App\Events\DomainIsCreated;
use App\Events\ModelDomainDeleting; use App\Events\ModelDomainDeleting;
use App\Jobs\ApacheBuild; use App\Jobs\ApacheBuild;
use App\Models\Scopes\CustomerHostingSubscriptionScope;
use App\ShellApi; use App\ShellApi;
use App\VirtualHosts\DTO\ApacheVirtualHostSettings; use App\VirtualHosts\DTO\ApacheVirtualHostSettings;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
@ -38,13 +39,7 @@ class Domain extends Model
protected static function booted(): void protected static function booted(): void
{ {
static::addGlobalScope('customer', function (Builder $query) { static::addGlobalScope(new CustomerHostingSubscriptionScope());
if (auth()->check() && auth()->guard()->name == 'web_customer') {
$query->whereHas('hostingSubscription', function ($query) {
$query->where('customer_id', auth()->user()->id);
});
}
});
} }
public static function boot() public static function boot()

View file

@ -100,6 +100,8 @@ class GitRepository extends Model
$projectDir = $findDomain->domain_root . '/' . $this->dir; $projectDir = $findDomain->domain_root . '/' . $this->dir;
$privateKeyFile = null;
$gitSSHKey = GitSshKey::find($this->git_ssh_key_id); $gitSSHKey = GitSshKey::find($this->git_ssh_key_id);
if ($gitSSHKey) { if ($gitSSHKey) {
$sshPath = '/home/'.$findHostingSubscription->system_username .'/.ssh'; $sshPath = '/home/'.$findHostingSubscription->system_username .'/.ssh';
@ -135,46 +137,23 @@ class GitRepository extends Model
return; return;
} }
$gitProvider = $gitSSHUrl['provider']; $cloneUrl = 'git@'.$gitSSHUrl['provider'].':'.$gitSSHUrl['owner'].'/'.$gitSSHUrl['name'].'.git';
$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;
$shellFile = '/tmp/git-clone-' . $this->id . '.sh'; $shellFile = '/tmp/git-clone-' . $this->id . '.sh';
$shellLog = '/tmp/git-clone-' . $this->id . '.log'; $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); file_put_contents($shellFile, $shellContent);
shell_exec('chmod +x ' . $shellFile); shell_exec('chmod +x ' . $shellFile);
shell_exec('bash '.$shellFile.' >> ' . $shellLog . ' &'); shell_exec('bash '.$shellFile.' >> ' . $shellLog . ' &');
} }

View file

@ -4,6 +4,7 @@ namespace App\Models\Scopes;
use App\Models\Domain; use App\Models\Domain;
use App\Models\HostingSubscription; use App\Models\HostingSubscription;
use Filament\Facades\Filament;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope; use Illuminate\Database\Eloquent\Scope;
@ -15,9 +16,10 @@ class CustomerDomainScope implements Scope
*/ */
public function apply(Builder $builder, Model $model): void public function apply(Builder $builder, Model $model): void
{ {
if (auth()->check() && auth()->guard()->name == 'web_customer') { $guard = Filament::auth();
if ($guard->check() && $guard->name == 'web_customer') {
$findHostingSubscriptionIds = HostingSubscription::where('customer_id', auth()->user()->id)->pluck('id');
$findHostingSubscriptionIds = HostingSubscription::where('customer_id', $guard->user()->id)->pluck('id');
$findDomainIds = Domain::whereIn('hosting_subscription_id', $findHostingSubscriptionIds)->pluck('id'); $findDomainIds = Domain::whereIn('hosting_subscription_id', $findHostingSubscriptionIds)->pluck('id');
$builder->whereIn('domain_id', $findDomainIds); $builder->whereIn('domain_id', $findDomainIds);

View file

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

View file

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

View file

@ -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');
}
};

View file

@ -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

View file

@ -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