164 lines
5.8 KiB
PHP
164 lines
5.8 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
namespace App\Modules\Users;
|
|
|
|
use App\Core\{Request, Response, Database};
|
|
use Ramsey\Uuid\Uuid;
|
|
|
|
final class UsersController
|
|
{
|
|
public function __construct(private readonly UserModel $userModel) {}
|
|
|
|
public function list(Request $request): void
|
|
{
|
|
if (!in_array($request->user->role, ['admin', 'super_admin'])) {
|
|
throw new \App\Core\Exceptions\HttpException("غير مصرح لك بالوصول", "FORBIDDEN", 403);
|
|
}
|
|
$tenantId = $request->tenantId;
|
|
$users = $this->userModel->findAllByTenant($tenantId);
|
|
|
|
Response::json([
|
|
'success' => true,
|
|
'data' => $users
|
|
]);
|
|
}
|
|
|
|
public function create(Request $request): void
|
|
{
|
|
if (!in_array($request->user->role, ['admin', 'super_admin'])) {
|
|
throw new \App\Core\Exceptions\HttpException("غير مصرح لك بالوصول", "FORBIDDEN", 403);
|
|
}
|
|
$tenantId = $request->tenantId;
|
|
$data = $request->getBody();
|
|
|
|
if (empty($data['email']) || empty($data['password']) || empty($data['name']) || empty($data['role'])) {
|
|
throw new \App\Core\Exceptions\HttpException('جميع الحقول مطلوبة', 'VALIDATION_ERROR', 422);
|
|
}
|
|
|
|
// Fix: Check email uniqueness WITHIN THE TENANT
|
|
if ($this->userModel->findByEmail($data['email'], $tenantId)) {
|
|
throw new \App\Core\Exceptions\HttpException('البريد الإلكتروني مستخدم مسبقاً في هذه الشركة', 'DUPLICATE_EMAIL', 409);
|
|
}
|
|
|
|
$userId = 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);
|
|
}
|
|
|
|
public function update(Request $request, string $id): void
|
|
{
|
|
if (!in_array($request->user->role, ['admin', 'super_admin'])) {
|
|
throw new \App\Core\Exceptions\HttpException("غير مصرح لك بالوصول", "FORBIDDEN", 403);
|
|
}
|
|
$tenantId = $request->tenantId;
|
|
$user = $this->userModel->findById($id, $tenantId);
|
|
if (!$user) {
|
|
throw new \App\Core\Exceptions\HttpException('المستخدم غير موجود', 'NOT_FOUND', 404);
|
|
}
|
|
|
|
$data = $request->getBody();
|
|
$updateData = [];
|
|
if (isset($data['name'])) $updateData['name'] = $data['name'];
|
|
if (isset($data['role'])) $updateData['role'] = $data['role'];
|
|
if (isset($data['is_active'])) $updateData['is_active'] = $data['is_active'];
|
|
if (isset($data['assigned_company_id'])) $updateData['assigned_company_id'] = $data['assigned_company_id'];
|
|
|
|
if (empty($updateData)) {
|
|
Response::error('لا توجد بيانات لتحديثها', 'VALIDATION_ERROR', 422);
|
|
return;
|
|
}
|
|
|
|
$this->userModel->update($id, $updateData);
|
|
|
|
Response::json([
|
|
'success' => true,
|
|
'message' => 'تم تحديث بيانات المستخدم بنجاح'
|
|
]);
|
|
}
|
|
|
|
public function destroy(Request $request, string $id): void
|
|
{
|
|
if (!in_array($request->user->role, ['admin', 'super_admin'])) {
|
|
throw new \App\Core\Exceptions\HttpException("غير مصرح لك بالوصول", "FORBIDDEN", 403);
|
|
}
|
|
$tenantId = $request->tenantId;
|
|
$user = $this->userModel->findById($id, $tenantId);
|
|
if (!$user) {
|
|
throw new \App\Core\Exceptions\HttpException('المستخدم غير موجود', 'NOT_FOUND', 404);
|
|
}
|
|
|
|
if ($user['id'] === $request->user->user_id) {
|
|
Response::error('لا يمكنك حذف حسابك الشخصي', 'FORBIDDEN', 403);
|
|
return;
|
|
}
|
|
|
|
$this->userModel->delete($id);
|
|
|
|
Response::json([
|
|
'success' => true,
|
|
'message' => 'تم حذف المستخدم بنجاح'
|
|
]);
|
|
}
|
|
|
|
public function updateProfile(Request $request): void
|
|
{
|
|
$userId = $request->user->user_id;
|
|
$data = $request->getBody();
|
|
|
|
if (empty($data['name'])) {
|
|
Response::error('الاسم مطلوب', 'VALIDATION_ERROR', 422);
|
|
return;
|
|
}
|
|
|
|
$this->userModel->update($userId, [
|
|
'name' => $data['name']
|
|
]);
|
|
|
|
Response::json([
|
|
'success' => true,
|
|
'message' => 'تم تحديث الملف الشخصي بنجاح'
|
|
]);
|
|
}
|
|
|
|
public function changePassword(Request $request): void
|
|
{
|
|
$userId = $request->user->user_id;
|
|
$data = $request->getBody();
|
|
|
|
if (empty($data['current_password']) || empty($data['new_password'])) {
|
|
Response::error('كلمة المرور الحالية والجديدة مطلوبة', 'VALIDATION_ERROR', 422);
|
|
return;
|
|
}
|
|
|
|
$user = $this->userModel->find($userId);
|
|
if (!password_verify($data['current_password'], $user['password_hash'])) {
|
|
Response::error('كلمة المرور الحالية غير صحيحة', 'UNAUTHORIZED', 401);
|
|
return;
|
|
}
|
|
|
|
$this->userModel->update($userId, [
|
|
'password_hash' => password_hash($data['new_password'], PASSWORD_ARGON2ID)
|
|
]);
|
|
|
|
Response::json([
|
|
'success' => true,
|
|
'message' => 'تم تغيير كلمة المرور بنجاح'
|
|
]);
|
|
}
|
|
}
|