db = Database::connect(); } /** * Corps SELECT des acces actifs (remplace l'ancienne vue v_eve_acces_actif). * Pas de vue stockee : evite le DEFINER non copiable par Navicat / releve. */ protected function actifBody(): string { return "SELECT ea.ea_id, ea.com_id, ea.eve_id, ea.role_id, r.role_code, r.role_label_fr, r.role_label_en, ea.ea_statut, ea.ea_expires_at, ea.ea_expire_days, ea.ea_granted_by, ea.ea_created_at FROM inscriptions_eve_acces ea INNER JOIN inscriptions_eve_roles r ON r.role_id = ea.role_id AND r.role_actif = 1 WHERE ea.ea_statut = 'actif' AND (ea.ea_expires_at IS NULL OR ea.ea_expires_at > NOW())"; } /** Sous-requete aliasee, equivalente a l'ancienne vue v_eve_acces_actif. */ protected function actifSql(): string { return "(" . $this->actifBody() . ") v_eve_acces_actif"; } /** Sous-requete aliasee, equivalente a l'ancienne vue v_eve_acces_permissions. */ protected function permSql(): string { return "((SELECT v.ea_id, v.com_id, v.eve_id, v.role_id, v.role_code, erp.perm_key, p.perm_group, p.perm_scope, p.perm_label_fr, p.perm_phase FROM (" . $this->actifBody() . ") v INNER JOIN inscriptions_eve_role_permissions erp ON erp.role_id = v.role_id INNER JOIN inscriptions_eve_permissions p ON p.perm_key = erp.perm_key AND p.perm_actif = 1) UNION (SELECT NULL AS ea_id, ae.com_id, ae.eve_id, NULL AS role_id, 'extra' AS role_code, ae.perm_key, p.perm_group, p.perm_scope, p.perm_label_fr, p.perm_phase FROM inscriptions_eve_acces_extra ae INNER JOIN inscriptions_eve_permissions p ON p.perm_key = ae.perm_key AND p.perm_actif = 1 WHERE ae.ae_statut = 'actif')) v_eve_acces_permissions"; } public function isEnabled(): bool { static $enabled = null; if ($enabled !== null) { return $enabled; } try { $row = $this->db->query("SHOW TABLES LIKE 'inscriptions_eve_acces'")->getRow(); $enabled = ($row !== null); } catch (\Throwable $e) { $enabled = false; } return $enabled; } public function hasV2Access(int $comId): bool { if (!$this->isEnabled()) { return false; } $sql = "SELECT COUNT(*) AS n FROM " . $this->actifSql() . " WHERE com_id = ?"; $row = $this->db->query($sql, [$comId])->getRow(); return $row !== null && (int) $row->n > 0; } public function getEventIds(int $comId): array { if (!$this->isEnabled()) { return []; } $sql = "SELECT eve_id FROM " . $this->actifSql() . " WHERE com_id = ?"; $rows = $this->db->query($sql, [$comId])->getResultArray(); return array_map(static fn($r) => (string) $r['eve_id'], $rows); } public function hasPermission(int $comId, int $eveId, string $permKey): bool { if (!$this->isEnabled()) { return false; } $sql = "SELECT COUNT(*) AS n FROM " . $this->permSql() . " WHERE com_id = ? AND eve_id = ? AND perm_key = ?"; $row = $this->db->query($sql, [$comId, $eveId, $permKey])->getRow(); return $row !== null && (int) $row->n > 0; } public function canAccessEvent(int $comId, int $eveId): bool { if (!$this->isEnabled()) { return false; } $sql = "SELECT COUNT(*) AS n FROM " . $this->actifSql() . " WHERE com_id = ? AND eve_id = ?"; $row = $this->db->query($sql, [$comId, $eveId])->getRow(); return $row !== null && (int) $row->n > 0; } /** * Legacy : com_eve_promoteur CSV */ public function getLegacyEventIds(int $comId): array { $rowVar = $this->db->table('variables') ->select('var_valeur') ->where('var_nom', 'promoteur_legacy_actif') ->get() ->getRow(); if ($rowVar !== null && (string) $rowVar->var_valeur === '0') { return []; } $compte = $this->db->table('inscriptions_comptes') ->select('com_eve_promoteur') ->where('com_id', $comId) ->get() ->getRow(); if (!$compte || empty(trim($compte->com_eve_promoteur ?? ''))) { return []; } $ids = array_filter(array_map('trim', explode(',', $compte->com_eve_promoteur))); return array_values($ids); } public function getAuthorizedEventIds(int $comId): array { if ($this->hasV2Access($comId)) { return $this->getEventIds($comId); } return $this->getLegacyEventIds($comId); } public function assertEventPermission(int $comId, int $eveId, ?string $permKey = null): bool { if ($this->hasV2Access($comId)) { if ($permKey !== null) { return $this->hasPermission($comId, $eveId, $permKey); } return $this->canAccessEvent($comId, $eveId); } return in_array((string) $eveId, $this->getLegacyEventIds($comId), true); } }