🚀 مُصادَق: تحديث برمجي جديد 2026-05-03 13:39
This commit is contained in:
50
app/Modules/Admin/AdminController.php
Normal file
50
app/Modules/Admin/AdminController.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Admin;
|
||||
|
||||
use App\Core\{Request, Response, Database};
|
||||
|
||||
final class AdminController
|
||||
{
|
||||
public function getSystemStats(Request $request): void
|
||||
{
|
||||
// Must be super_admin
|
||||
if (($request->user->role ?? '') !== 'super_admin') {
|
||||
Response::error('غير مصرح', 'FORBIDDEN', 403);
|
||||
return;
|
||||
}
|
||||
|
||||
$db = Database::getInstance();
|
||||
|
||||
$stmt = $db->prepare("SELECT COUNT(*) as count FROM tenants");
|
||||
$stmt->execute();
|
||||
$totalTenants = $stmt->fetch()['count'];
|
||||
|
||||
$stmt = $db->prepare("SELECT COUNT(*) as count FROM invoices");
|
||||
$stmt->execute();
|
||||
$totalInvoices = $stmt->fetch()['count'];
|
||||
|
||||
// Simple Health Check
|
||||
$redisHealth = 'ok';
|
||||
try {
|
||||
$redis = \App\Core\Redis::getInstance();
|
||||
$redis->ping();
|
||||
} catch (\Throwable $e) {
|
||||
$redisHealth = 'failed';
|
||||
}
|
||||
|
||||
Response::json([
|
||||
'success' => true,
|
||||
'data' => [
|
||||
'total_tenants' => $totalTenants,
|
||||
'total_invoices' => $totalInvoices,
|
||||
'system_health' => [
|
||||
'database' => 'ok',
|
||||
'redis' => $redisHealth
|
||||
]
|
||||
]
|
||||
]);
|
||||
}
|
||||
}
|
||||
60
app/Modules/ApiKeys/ApiKeyController.php
Normal file
60
app/Modules/ApiKeys/ApiKeyController.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\ApiKeys;
|
||||
|
||||
use App\Core\{Request, Response};
|
||||
use App\Modules\ApiKeys\ApiKeyModel;
|
||||
|
||||
final class ApiKeyController
|
||||
{
|
||||
public function __construct(private readonly ApiKeyModel $apiKeyModel) {}
|
||||
|
||||
public function list(Request $request): void
|
||||
{
|
||||
$tenantId = $request->tenantId;
|
||||
$keys = $this->apiKeyModel->findAllByTenant($tenantId);
|
||||
|
||||
Response::json([
|
||||
'success' => true,
|
||||
'data' => $keys
|
||||
]);
|
||||
}
|
||||
|
||||
public function create(Request $request): void
|
||||
{
|
||||
$tenantId = $request->tenantId;
|
||||
$data = $request->getBody();
|
||||
|
||||
if (empty($data['name'])) {
|
||||
Response::error('اسم المفتاح مطلوب', 'VALIDATION_ERROR', 422);
|
||||
return;
|
||||
}
|
||||
|
||||
$id = \Ramsey\Uuid\Uuid::uuid4()->toString();
|
||||
// Generate a random key
|
||||
$rawKey = bin2hex(random_bytes(32));
|
||||
$prefix = substr($rawKey, 0, 8);
|
||||
$hashedKey = hash('sha256', $rawKey);
|
||||
|
||||
$this->apiKeyModel->create([
|
||||
'id' => $id,
|
||||
'tenant_id' => $tenantId,
|
||||
'name' => $data['name'],
|
||||
'key_hash' => $hashedKey,
|
||||
'prefix' => $prefix,
|
||||
'is_active' => 1
|
||||
]);
|
||||
|
||||
Response::json([
|
||||
'success' => true,
|
||||
'message' => 'تم إنشاء مفتاح API بنجاح',
|
||||
'data' => [
|
||||
'id' => $id,
|
||||
'name' => $data['name'],
|
||||
'key' => $rawKey // Only shown once!
|
||||
]
|
||||
], 201);
|
||||
}
|
||||
}
|
||||
19
app/Modules/ApiKeys/ApiKeyModel.php
Normal file
19
app/Modules/ApiKeys/ApiKeyModel.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\ApiKeys;
|
||||
|
||||
use App\Models\BaseModel;
|
||||
|
||||
final class ApiKeyModel extends BaseModel
|
||||
{
|
||||
protected string $table = 'api_keys';
|
||||
|
||||
public function findAllByTenant(string $tenantId): array
|
||||
{
|
||||
$stmt = $this->db()->prepare("SELECT id, name, prefix, expires_at, last_used_at, is_active, created_at FROM {$this->table} WHERE tenant_id = ? AND deleted_at IS NULL");
|
||||
$stmt->execute([$tenantId]);
|
||||
return $stmt->fetchAll();
|
||||
}
|
||||
}
|
||||
@@ -25,6 +25,16 @@ final class AuthController
|
||||
try {
|
||||
$result = $this->authService->login($email, $password);
|
||||
|
||||
// 2FA Check
|
||||
if ($result['user']->totp_enabled) {
|
||||
Response::json([
|
||||
'success' => true,
|
||||
'requires_2fa' => true,
|
||||
'temp_token' => $result['access_token']
|
||||
]);
|
||||
return;
|
||||
}
|
||||
|
||||
// Set refresh token in HttpOnly cookie
|
||||
setcookie('refresh_token', $result['refresh_token'], [
|
||||
'expires' => time() + (60 * 60 * 24 * 7),
|
||||
@@ -128,4 +138,47 @@ final class AuthController
|
||||
Response::error($e->getMessage(), 'REGISTRATION_FAILED', 400);
|
||||
}
|
||||
}
|
||||
|
||||
public function enable2FA(Request $request): void
|
||||
{
|
||||
$user = $request->user;
|
||||
$totpService = new \App\Services\TotpService();
|
||||
$secret = $totpService->generateSecret();
|
||||
$qrUrl = $totpService->getQrCodeUrl($user->email, $secret);
|
||||
|
||||
Response::json([
|
||||
'success' => true,
|
||||
'data' => [
|
||||
'secret' => $secret,
|
||||
'qr_url' => $qrUrl
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
public function verify2FA(Request $request): void
|
||||
{
|
||||
$data = $request->getBody();
|
||||
$code = $data['code'] ?? '';
|
||||
$secret = $data['secret'] ?? '';
|
||||
|
||||
$totpService = new \App\Services\TotpService();
|
||||
if ($totpService->verify($secret, $code)) {
|
||||
$db = \App\Core\Database::getInstance();
|
||||
$stmt = $db->prepare("UPDATE users SET totp_secret = ?, totp_enabled = 1 WHERE id = ?");
|
||||
$stmt->execute([$secret, $request->user->user_id]);
|
||||
|
||||
Response::json(['success' => true, 'message' => 'تم تفعيل التحقق الثنائي بنجاح']);
|
||||
} else {
|
||||
Response::error('رمز التحقق غير صحيح', 'INVALID_CODE', 400);
|
||||
}
|
||||
}
|
||||
|
||||
public function disable2FA(Request $request): void
|
||||
{
|
||||
$db = \App\Core\Database::getInstance();
|
||||
$stmt = $db->prepare("UPDATE users SET totp_secret = NULL, totp_enabled = 0 WHERE id = ?");
|
||||
$stmt->execute([$request->user->user_id]);
|
||||
|
||||
Response::json(['success' => true, 'message' => 'تم تعطيل التحقق الثنائي']);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,4 +89,53 @@ final class InvoiceController
|
||||
Response::error($e->getMessage(), 'UPLOAD_FAILED', 500);
|
||||
}
|
||||
}
|
||||
|
||||
public function detail(Request $request, array $vars): void
|
||||
{
|
||||
$tenantId = $request->tenantId;
|
||||
$invoiceId = $vars['id'] ?? null;
|
||||
|
||||
$db = \App\Core\Database::getInstance();
|
||||
$stmt = $db->prepare("SELECT * FROM invoices WHERE id = ? AND tenant_id = ? AND deleted_at IS NULL LIMIT 1");
|
||||
$stmt->execute([$invoiceId, $tenantId]);
|
||||
$invoice = $stmt->fetch();
|
||||
|
||||
if (!$invoice) {
|
||||
Response::error('الفاتورة غير موجودة', 'NOT_FOUND', 404);
|
||||
return;
|
||||
}
|
||||
|
||||
// Additional authorization check based on assigned company if needed
|
||||
$role = $request->user->role ?? 'viewer';
|
||||
if ($role !== 'super_admin' && $invoice['company_id'] !== $request->user->assigned_company_id) {
|
||||
Response::error('غير مصرح لك بمشاهدة هذه الفاتورة', 'FORBIDDEN', 403);
|
||||
return;
|
||||
}
|
||||
|
||||
// Fetch lines
|
||||
$stmt = $db->prepare("SELECT * FROM invoice_lines WHERE invoice_id = ? ORDER BY id ASC");
|
||||
$stmt->execute([$invoiceId]);
|
||||
$invoice['lines'] = $stmt->fetchAll();
|
||||
|
||||
Response::json([
|
||||
'success' => true,
|
||||
'data' => $invoice
|
||||
]);
|
||||
}
|
||||
|
||||
public function submit(Request $request, array $vars): void
|
||||
{
|
||||
$tenantId = $request->tenantId;
|
||||
$invoiceId = $vars['id'];
|
||||
|
||||
// Push to Queue for JoFotara Submission
|
||||
\App\Services\QueueService::push('submit_jofotara', [
|
||||
'invoice_id' => $invoiceId
|
||||
]);
|
||||
|
||||
Response::json([
|
||||
'success' => true,
|
||||
'message' => 'Invoice submission queued.'
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,5 +38,38 @@ final class UserController
|
||||
'success' => true,
|
||||
'data' => $user
|
||||
]);
|
||||
public function create(Request $request): void
|
||||
{
|
||||
$tenantId = $request->tenantId;
|
||||
$data = $request->getBody();
|
||||
|
||||
if (empty($data['email']) || empty($data['password']) || empty($data['name']) || empty($data['role'])) {
|
||||
Response::error('جميع الحقول مطلوبة', 'VALIDATION_ERROR', 422);
|
||||
return;
|
||||
}
|
||||
|
||||
if ($this->userModel->findByEmail($data['email'])) {
|
||||
Response::error('البريد الإلكتروني مستخدم مسبقاً', 'DUPLICATE_EMAIL', 409);
|
||||
return;
|
||||
}
|
||||
|
||||
$userId = \Ramsey\Uuid\Uuid::uuid4()->toString();
|
||||
|
||||
$this->userModel->create([
|
||||
'id' => $userId,
|
||||
'tenant_id' => $tenantId,
|
||||
'name' => $data['name'],
|
||||
'email' => $data['email'],
|
||||
'password_hash' => password_hash($data['password'], PASSWORD_ARGON2ID),
|
||||
'role' => $data['role'],
|
||||
'assigned_company_id' => $data['assigned_company_id'] ?? null,
|
||||
'is_active' => 1
|
||||
]);
|
||||
|
||||
Response::json([
|
||||
'success' => true,
|
||||
'message' => 'تم إضافة المستخدم بنجاح',
|
||||
'data' => ['id' => $userId]
|
||||
], 201);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user