diff --git a/web/app/Models/CronJob.php b/web/app/Models/CronJob.php index 5250195..c8b09fd 100644 --- a/web/app/Models/CronJob.php +++ b/web/app/Models/CronJob.php @@ -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; @@ -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() diff --git a/web/app/Models/Customer.php b/web/app/Models/Customer.php index 3631c0b..48c1f25 100644 --- a/web/app/Models/Customer.php +++ b/web/app/Models/Customer.php @@ -5,6 +5,7 @@ namespace App\Models; 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 @@ -31,7 +32,17 @@ class Customer extends Authenticatable { 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) { diff --git a/web/app/Models/Database.php b/web/app/Models/Database.php index e079cbb..ec8d3b6 100644 --- a/web/app/Models/Database.php +++ b/web/app/Models/Database.php @@ -2,6 +2,7 @@ namespace App\Models; +use App\Models\Scopes\CustomerHostingSubscriptionScope; use App\PhyreConfig; use App\Services\RemoteDatabaseService; use App\UniversalDatabaseExecutor; @@ -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() diff --git a/web/app/Models/DatabaseUser.php b/web/app/Models/DatabaseUser.php index b66e1e6..9893bea 100644 --- a/web/app/Models/DatabaseUser.php +++ b/web/app/Models/DatabaseUser.php @@ -2,6 +2,7 @@ namespace App\Models; +use App\Models\Scopes\CustomerHostingSubscriptionScope; use App\PhyreConfig; use App\UniversalDatabaseExecutor; use Illuminate\Database\Eloquent\Factories\HasFactory; diff --git a/web/app/Models/Domain.php b/web/app/Models/Domain.php index ac4f6ff..3307d3c 100644 --- a/web/app/Models/Domain.php +++ b/web/app/Models/Domain.php @@ -6,6 +6,7 @@ use App\Actions\ApacheWebsiteDelete; 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; @@ -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() diff --git a/web/app/Models/GitRepository.php b/web/app/Models/GitRepository.php index 9a333d8..e4108bb 100644 --- a/web/app/Models/GitRepository.php +++ b/web/app/Models/GitRepository.php @@ -100,6 +100,8 @@ class GitRepository extends Model $projectDir = $findDomain->domain_root . '/' . $this->dir; + $privateKeyFile = null; + $gitSSHKey = GitSshKey::find($this->git_ssh_key_id); if ($gitSSHKey) { $sshPath = '/home/'.$findHostingSubscription->system_username .'/.ssh'; @@ -135,46 +137,23 @@ class GitRepository extends Model 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 . ' &'); } diff --git a/web/app/Models/Scopes/CustomerDomainScope.php b/web/app/Models/Scopes/CustomerDomainScope.php index 16948fb..0cecee0 100644 --- a/web/app/Models/Scopes/CustomerDomainScope.php +++ b/web/app/Models/Scopes/CustomerDomainScope.php @@ -4,6 +4,7 @@ namespace App\Models\Scopes; 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; @@ -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); diff --git a/web/app/Models/Scopes/CustomerHostingSubscriptionScope.php b/web/app/Models/Scopes/CustomerHostingSubscriptionScope.php index d3103c7..491e25a 100644 --- a/web/app/Models/Scopes/CustomerHostingSubscriptionScope.php +++ b/web/app/Models/Scopes/CustomerHostingSubscriptionScope.php @@ -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; @@ -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); }); } } diff --git a/web/app/Models/Scopes/CustomerScope.php b/web/app/Models/Scopes/CustomerScope.php index 5768021..d3f9123 100644 --- a/web/app/Models/Scopes/CustomerScope.php +++ b/web/app/Models/Scopes/CustomerScope.php @@ -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; @@ -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); } } } diff --git a/web/database/migrations/2024_09_13_113429_create_sessions_table.php b/web/database/migrations/2024_09_13_113429_create_sessions_table.php new file mode 100644 index 0000000..f60625b --- /dev/null +++ b/web/database/migrations/2024_09_13_113429_create_sessions_table.php @@ -0,0 +1,31 @@ +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'); + } +}; diff --git a/web/resources/views/actions/git/clone-repo.blade.php b/web/resources/views/actions/git/clone-repo.blade.php new file mode 100644 index 0000000..7b8df1c --- /dev/null +++ b/web/resources/views/actions/git/clone-repo.blade.php @@ -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 diff --git a/web/resources/views/actions/git/pull-repo.blade.php b/web/resources/views/actions/git/pull-repo.blade.php new file mode 100644 index 0000000..ccbe99e --- /dev/null +++ b/web/resources/views/actions/git/pull-repo.blade.php @@ -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