Le Mie Aziende
+Gestisci il portafoglio clienti e monitora la compliance NIS2
+diff --git a/application/controllers/AuthController.php b/application/controllers/AuthController.php index fa378ac..ed2b5f9 100644 --- a/application/controllers/AuthController.php +++ b/application/controllers/AuthController.php @@ -26,6 +26,8 @@ class AuthController extends BaseController $password = $this->getParam('password'); $fullName = trim($this->getParam('full_name')); $phone = $this->getParam('phone'); + $userType = $this->getParam('user_type', 'azienda'); // 'azienda' | 'consultant' + $role = ($userType === 'consultant') ? 'consultant' : 'employee'; // Validazione email if (!$this->validateEmail($email)) { @@ -54,7 +56,7 @@ class AuthController extends BaseController 'password_hash' => password_hash($password, PASSWORD_DEFAULT), 'full_name' => $fullName, 'phone' => $phone, - 'role' => 'employee', + 'role' => $role, 'is_active' => 1, ]); @@ -64,14 +66,14 @@ class AuthController extends BaseController // Audit log $this->currentUser = ['id' => $userId]; - $this->logAudit('user_registered', 'user', $userId); + $this->logAudit('user_registered', 'user', $userId, ['user_type' => $userType]); $this->jsonSuccess([ 'user' => [ 'id' => $userId, 'email' => $email, 'full_name' => $fullName, - 'role' => 'employee', + 'role' => $role, ], 'access_token' => $accessToken, 'refresh_token' => $refreshToken, diff --git a/application/controllers/OnboardingController.php b/application/controllers/OnboardingController.php index 9b5cb02..51ac626 100644 --- a/application/controllers/OnboardingController.php +++ b/application/controllers/OnboardingController.php @@ -132,17 +132,34 @@ class OnboardingController extends BaseController $this->validateRequired(['name', 'sector']); $userId = $this->getCurrentUserId(); + $currentUser = $this->getCurrentUser(); + $isConsultant = ($currentUser['role'] === 'consultant'); - // Check if user already has an organization + // Check if user already has an organization (blocca solo per non-consulenti) $existingOrg = Database::fetchOne( 'SELECT organization_id FROM user_organizations WHERE user_id = ? AND is_primary = 1', [$userId] ); - if ($existingOrg) { + if ($existingOrg && !$isConsultant) { $this->jsonError('Hai già un\'organizzazione configurata', 409, 'ORG_EXISTS'); } + // Per consulenti: verifica che la P.IVA non sia già associata al loro account + $vatNumber = $this->getParam('vat_number'); + if ($isConsultant && $vatNumber) { + $cleanVat = preg_replace('/^IT/i', '', preg_replace('/\s+/', '', $vatNumber)); + $dupOrg = Database::fetchOne( + 'SELECT o.id FROM organizations o + JOIN user_organizations uo ON uo.organization_id = o.id + WHERE uo.user_id = ? AND o.vat_number = ?', + [$userId, $cleanVat] + ); + if ($dupOrg) { + $this->jsonError('Questa azienda è già presente nel tuo portafoglio clienti', 409, 'ORG_DUPLICATE_VAT'); + } + } + Database::beginTransaction(); try { // Create organization @@ -178,12 +195,14 @@ class OnboardingController extends BaseController 'voluntary_compliance' => $voluntaryCompliance, ], 'id = ?', [$orgId]); - // Link user as org_admin + // Link user: consulente → ruolo 'consultant', is_primary solo se prima org + $orgRole = $isConsultant ? 'consultant' : 'org_admin'; + $isPrimary = $existingOrg ? 0 : 1; Database::insert('user_organizations', [ 'user_id' => $userId, 'organization_id' => $orgId, - 'role' => 'org_admin', - 'is_primary' => 1, + 'role' => $orgRole, + 'is_primary' => $isPrimary, ]); // Update user profile if provided @@ -207,7 +226,8 @@ class OnboardingController extends BaseController $this->logAudit('onboarding_completed', 'organization', $orgId, [ 'name' => $orgData['name'], 'sector' => $orgData['sector'], - 'entity_type' => $entityType + 'entity_type' => $entityType, + 'is_consultant' => $isConsultant, ]); $this->jsonSuccess([ diff --git a/docs/sql/005_consultant_support.sql b/docs/sql/005_consultant_support.sql new file mode 100644 index 0000000..5d2ce08 --- /dev/null +++ b/docs/sql/005_consultant_support.sql @@ -0,0 +1,8 @@ +-- ============================================= +-- Migration 005: Supporto ruolo Consulente +-- Aggiunge 'consultant' all'enum di user_organizations.role +-- ============================================= + +ALTER TABLE user_organizations +MODIFY COLUMN role ENUM('org_admin','compliance_manager','board_member','auditor','employee','consultant') +NOT NULL DEFAULT 'employee'; diff --git a/public/companies.html b/public/companies.html new file mode 100644 index 0000000..2af94cb --- /dev/null +++ b/public/companies.html @@ -0,0 +1,548 @@ + + +
+ + +Gestisci il portafoglio clienti e monitora la compliance NIS2
+