mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 09:54:05 +00:00
Add jobs for creating/deleting Postgres roles
This commit is contained in:
parent
758fbc8a75
commit
e6a0717c84
2 changed files with 91 additions and 0 deletions
45
src/Jobs/CreatePostgresRoleForTenant.php
Normal file
45
src/Jobs/CreatePostgresRoleForTenant.php
Normal file
|
|
@ -0,0 +1,45 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Stancl\Tenancy\Jobs;
|
||||||
|
|
||||||
|
use Illuminate\Bus\Queueable;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use Illuminate\Queue\SerializesModels;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Queue\InteractsWithQueue;
|
||||||
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||||
|
use Illuminate\Foundation\Bus\Dispatchable;
|
||||||
|
use Illuminate\Database\QueryException;
|
||||||
|
use Stancl\Tenancy\Database\Contracts\TenantWithDatabase;
|
||||||
|
|
||||||
|
class CreatePostgresRoleForTenant implements ShouldQueue
|
||||||
|
{
|
||||||
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new job instance.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __construct(
|
||||||
|
protected TenantWithDatabase&Model $tenant,
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute the job.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function handle()
|
||||||
|
{
|
||||||
|
$name = $this->tenant->getTenantKey();
|
||||||
|
$password = $this->tenant->database()->getPassword() ?? 'password';
|
||||||
|
|
||||||
|
try {
|
||||||
|
DB::statement("CREATE ROLE \"$name\" LOGIN PASSWORD '$password';");
|
||||||
|
} catch (QueryException $exception) {
|
||||||
|
// Skip creating Postgres role if it already exists
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
46
src/Jobs/DeleteTenantsPostgresRole.php
Normal file
46
src/Jobs/DeleteTenantsPostgresRole.php
Normal file
|
|
@ -0,0 +1,46 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Stancl\Tenancy\Jobs;
|
||||||
|
|
||||||
|
use Illuminate\Bus\Queueable;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use Illuminate\Queue\SerializesModels;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Queue\InteractsWithQueue;
|
||||||
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||||
|
use Illuminate\Foundation\Bus\Dispatchable;
|
||||||
|
use Illuminate\Database\QueryException;
|
||||||
|
use Stancl\Tenancy\Database\Contracts\TenantWithDatabase;
|
||||||
|
|
||||||
|
class DeleteTenantsPostgresRole implements ShouldQueue
|
||||||
|
{
|
||||||
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new job instance.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __construct(
|
||||||
|
protected TenantWithDatabase&Model $tenant,
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute the job.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function handle()
|
||||||
|
{
|
||||||
|
$tenantKey = $this->tenant->getTenantKey();
|
||||||
|
|
||||||
|
// Revoke all permissions of a role before dropping it
|
||||||
|
try {
|
||||||
|
DB::statement("DROP OWNED BY \"{$tenantKey}\";");
|
||||||
|
DB::statement("DROP ROLE \"{$tenantKey}\";");
|
||||||
|
} catch (QueryException $exception) {
|
||||||
|
// Skip dropping permissions if the role doesn't exist
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue