51
common/Auth/Jobs/ExportRolesCsv.php
Executable file
51
common/Auth/Jobs/ExportRolesCsv.php
Executable file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace Common\Auth\Jobs;
|
||||
|
||||
use Common\Auth\Roles\Role;
|
||||
use Common\Csv\BaseCsvExportJob;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
class ExportRolesCsv extends BaseCsvExportJob
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected $requesterId;
|
||||
|
||||
public function __construct(int $requesterId)
|
||||
{
|
||||
$this->requesterId = $requesterId;
|
||||
}
|
||||
|
||||
public function cacheName(): string
|
||||
{
|
||||
return 'roles';
|
||||
}
|
||||
|
||||
protected function generateLines()
|
||||
{
|
||||
$selectCols = [
|
||||
'id',
|
||||
'name',
|
||||
'description',
|
||||
'type',
|
||||
'internal',
|
||||
'created_at',
|
||||
'updated_at',
|
||||
];
|
||||
|
||||
Role::select($selectCols)->chunkById(100, function (Collection $chunk) {
|
||||
$chunk->each(function (Role $role) {
|
||||
$data = $role->toArray();
|
||||
$this->writeLineToCsv($data);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
55
common/Auth/Jobs/ExportUsersCsv.php
Executable file
55
common/Auth/Jobs/ExportUsersCsv.php
Executable file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace Common\Auth\Jobs;
|
||||
|
||||
use App\Models\User;
|
||||
use Common\Csv\BaseCsvExportJob;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
class ExportUsersCsv extends BaseCsvExportJob
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected $requesterId;
|
||||
|
||||
public function __construct(int $requesterId)
|
||||
{
|
||||
$this->requesterId = $requesterId;
|
||||
}
|
||||
|
||||
public function cacheName(): string
|
||||
{
|
||||
return 'users';
|
||||
}
|
||||
|
||||
protected function generateLines()
|
||||
{
|
||||
$selectCols = [
|
||||
'id',
|
||||
'email',
|
||||
'username',
|
||||
'first_name',
|
||||
'last_name',
|
||||
'avatar',
|
||||
'created_at',
|
||||
'language',
|
||||
'country',
|
||||
'timezone',
|
||||
];
|
||||
|
||||
User::select($selectCols)->chunkById(100, function (Collection $chunk) {
|
||||
$chunk->each(function (User $user) {
|
||||
$data = $user->toArray();
|
||||
unset($data['display_name'], $data['has_password']);
|
||||
$this->writeLineToCsv($data);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
51
common/Auth/Jobs/LogActiveSessionJob.php
Executable file
51
common/Auth/Jobs/LogActiveSessionJob.php
Executable file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace Common\Auth\Jobs;
|
||||
|
||||
use Common\Auth\ActiveSession;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
class LogActiveSessionJob implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
public function __construct(protected array $data)
|
||||
{
|
||||
}
|
||||
|
||||
public function handle(): void
|
||||
{
|
||||
$sessionId = $this->data['session_id'] ?? null;
|
||||
$token = $this->data['token'] ?? null;
|
||||
|
||||
$existingSession = app(ActiveSession::class)
|
||||
->when(
|
||||
$sessionId,
|
||||
fn($query) => $query->where('session_id', $sessionId),
|
||||
)
|
||||
->when($token, fn($query) => $query->where('token', $token))
|
||||
->where('user_id', $this->data['user_id'])
|
||||
->first();
|
||||
|
||||
if ($existingSession) {
|
||||
$existingSession->touch('updated_at');
|
||||
} else {
|
||||
$this->createNewSession();
|
||||
}
|
||||
}
|
||||
|
||||
protected function createNewSession()
|
||||
{
|
||||
ActiveSession::create([
|
||||
'session_id' => $this->data['session_id'] ?? null,
|
||||
'token' => $this->data['token'] ?? null,
|
||||
'user_id' => $this->data['user_id'],
|
||||
'ip_address' => $this->data['ip_address'] ?? null,
|
||||
'user_agent' => $this->data['user_agent'] ?? null,
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user