Non puoi selezionare più di 25 argomenti
Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
1028 righe
43 KiB
1028 righe
43 KiB
<? |
|
|
|
function sortUtenti($a, $b) |
|
{ |
|
return $a["cognome"] > $b["cognome"]; |
|
} |
|
|
|
class Utente { |
|
|
|
public $codice; |
|
public $botVerify; |
|
public $gerarchia; |
|
public $collaborazione; |
|
public $readOnly; |
|
public $codice_ente; |
|
public $settings; |
|
public $oe; |
|
public $enteInnovativo; |
|
public $ruolo; |
|
private $codice_sessione; |
|
public $collaborazioni; |
|
public $qualificato; |
|
public $publicSSO; |
|
private $lockPath; |
|
public $twoFactorVerification; |
|
public $apiUser; |
|
public $superPowers; |
|
public $authenticationLevel; |
|
|
|
public static function ruoli() { |
|
$ruoli = []; |
|
$ruoli["SAD"] = ["nome"=>"Root","descrizione"=>"Amministratore di Sistema","ente"=>false,"admin"=>true,"gerarchia"=>1]; |
|
$ruoli["SUP"] = ["nome"=>"Help desk","descrizione"=>"Utente di help desk","ente"=>false,"admin"=>true,"gerarchia"=>5]; |
|
$ruoli["CON"] = ["nome"=>"Consulente","descrizione"=>"Consulente esterno","ente"=>false,"admin"=>true,"gerarchia"=>99]; |
|
$ruoli["ADM"] = ["nome"=>"Amministratore","descrizione"=>"Amministratore di piattaforma","ente"=>true,"admin"=>false,"gerarchia"=>10]; |
|
$ruoli["USR"] = ["nome"=>"Utente","descrizione"=>"Utente di piattaforma","ente"=>true,"admin"=>true,"gerarchia"=>20]; |
|
$ruoli["COM"] = ["nome"=>"Commissario","descrizione"=>"Commissario valutatore","ente"=>true,"admin"=>false,"gerarchia"=>30]; |
|
$ruoli["OPE"] = ["nome"=>"Operatore Economico","descrizione"=>"","ente"=>false,"admin"=>false,"gerarchia"=>100]; |
|
$ruoli["ENT"] = ["nome"=>"Operatore Fabbisogno","descrizione"=>"Utente che esprime un fabbisogno di innovazione","ente"=>false,"admin"=>false,"gerarchia"=>101]; |
|
return $ruoli; |
|
} |
|
|
|
public static function removeLock($codice) { |
|
global $config; |
|
$path = $config["user_log"] . "/lock/{$codice}"; |
|
if (file_exists($path)) { unlink($path); } |
|
} |
|
|
|
public static function touchLock($codice) { |
|
global $config; |
|
$path = $config["user_log"] . "/lock/{$codice}"; |
|
touch($path); |
|
} |
|
|
|
public function __construct() { |
|
$this->settings = self::permissionSettings(); |
|
} |
|
|
|
public static function permissionSettings() { |
|
global $config; |
|
$return = jsonToArray($config["jsonFolder"]."/permission.json"); |
|
if (empty($return)) { |
|
$return = ["default"=>["permissionRequired"=>false]]; |
|
} |
|
return $return; |
|
} |
|
|
|
public static function publicSSOLogin(Array $attributes, String $email) { |
|
global $pdo; |
|
$return = false; |
|
$emailAddress = false; |
|
$_tokenSPID = generateStrongPassword(); |
|
$ris = $pdo->go("SELECT b_utenti.email FROM b_utenti WHERE cf LIKE :fiscalNumber AND email = :email AND attivo = 'S'",[":email"=>$email,":fiscalNumber"=>$attributes["fiscalNumber"]]); |
|
if ($ris->rowCount() === 1) { |
|
$emailAddress = $ris->fetch(PDO::FETCH_COLUMN); |
|
} |
|
if (!empty($emailAddress)) { |
|
$return = Utente::login($emailAddress,"__SPIDFORCE".$_tokenSPID,true,$_tokenSPID); |
|
$level = 2; |
|
if (isset($_SESSION['spidSession']->level) && $_SESSION['spidSession']->level == 3) { |
|
$level = 3; |
|
} |
|
$return->authenticationLevel = $level; |
|
} |
|
return $return; |
|
} |
|
|
|
|
|
public static function printLoginForm() { |
|
include __DIR__ . "/login-view/form.php"; |
|
} |
|
|
|
/** |
|
* getUsersFromCF function |
|
* |
|
* La funzione consente di individuare tutte le utenze collegate ad un codice Fiscale |
|
* |
|
* @param String $cf |
|
* @return Array |
|
*/ |
|
public static function getUsersFromCF(String $cf) : Array { |
|
global $pdo; |
|
$risultato = $pdo->go("SELECT codice,email,cognome,nome,ruolo FROM b_utenti WHERE cf = :cf AND attivo = 'S'",[":cf" => $cf]); |
|
$enti = []; |
|
if (isset($_SESSION["ente"])) { |
|
$enti = $_SESSION["ente"]->entiBeneficiari(); |
|
} |
|
$return = []; |
|
if ($risultato->rowCount() > 0) { |
|
$ruoli = self::ruoli(); |
|
while($utente = $risultato->fetch(PDO::FETCH_ASSOC)) { |
|
|
|
// Se si tratta di un operatore economico |
|
if ($utente["ruolo"] == "OPE") { |
|
$utente["oe"] = self::getOEFromUser($utente["codice"]); |
|
$return[$utente["codice"]] = $utente; |
|
} |
|
|
|
// Se l'utente è standard, amministratore, supporto o root |
|
elseif (! empty($ruoli[$utente["ruolo"]]) && $ruoli[$utente["ruolo"]]["gerarchia"] <= 20) { |
|
$utente["collaborazioni"] = []; |
|
if (! empty($enti)) { |
|
$utente["collaborazioni"] = self::getCollaborazioniFromUtente($utente["codice"], $enti); |
|
} |
|
if (! empty($utente["collaborazioni"])) { |
|
$return[$utente["codice"]] = $utente; |
|
} |
|
} |
|
} |
|
} |
|
return $return; |
|
} |
|
|
|
/** |
|
* getCollaborazioniFromUtente function |
|
* |
|
* Restituisce le collaborazioni disponibili per un utente |
|
* |
|
* @param Int $utente |
|
* @param Array $enti |
|
* @return Array |
|
*/ |
|
private static function getCollaborazioniFromUtente(Int $utente, Array $enti) : Array { |
|
global $pdo; |
|
$check = $pdo->prepare("SELECT * FROM r_collaborazioni WHERE codice_utente = :codice_utente AND codice_ente = :codice_ente AND attivo = 'S' "); |
|
$check->bindValue(":codice_utente",$utente); |
|
$collaborazioni = []; |
|
foreach($enti AS $codice_ente => $ente) { |
|
$check->bindValue(":codice_ente",$codice_ente); |
|
$check->execute(); |
|
if ($check->rowCount() == 1) { |
|
$tmp = $check->fetch(PDO::FETCH_ASSOC); |
|
if (self::ruoli()[$tmp["gruppo"]]["ente"]) { |
|
$tmp["denominazione"] = $ente["denominazione"]; |
|
$collaborazioni[$tmp["codice"]] = $tmp; |
|
} |
|
} |
|
} |
|
return $collaborazioni; |
|
} |
|
|
|
private static function getOEFromUser(Int $utente) { |
|
global $pdo; |
|
return $pdo->go("SELECT b_operatori_economici.* FROM b_operatori_economici |
|
JOIN r_oe_utenti ON b_operatori_economici.codice = r_oe_utenti.codice_operatore |
|
WHERE r_oe_utenti.codice_utente = :codice_utente ",[":codice_utente"=>$utente])->fetchAll(PDO::FETCH_ASSOC); |
|
} |
|
|
|
private static function getEnteInnovativoFromUser(Int $utente) { |
|
global $pdo; |
|
return $pdo->go("SELECT b_enti_innovazione.* FROM b_enti_innovazione |
|
JOIN r_utenti_enti_innovazione ON b_enti_innovazione.codice = r_utenti_enti_innovazione.codice_ente |
|
WHERE r_utenti_enti_innovazione.codice_utente = :codice_utente ",[":codice_utente"=>$utente])->fetchAll(PDO::FETCH_ASSOC); |
|
} |
|
|
|
public static function cleanPublicSSOFiscalCode(String $cf) { |
|
return str_replace("TINIT-","",$cf); |
|
} |
|
|
|
public static function login($email,$password,$force = false,$_tokenSPID = false) { |
|
$return = 0; |
|
if (!empty($email) && !empty($password)) { |
|
global $pdo, $config; |
|
$abilitato = false; |
|
$bind = array(":email"=>$email); |
|
$strsql = "SELECT * FROM b_utenti WHERE email = :email "; |
|
$risultato = $pdo->go($strsql,$bind); |
|
if ($risultato->rowCount() === 1) { |
|
$utente = $risultato->fetch(PDO::FETCH_ASSOC); |
|
if ($utente["attivo"] == "S") { |
|
$continua = false; |
|
$spid = false; |
|
if (!empty($_tokenSPID)) { |
|
$checkToken = str_replace("__SPIDFORCE","",$password); |
|
if ($checkToken == $_tokenSPID) { |
|
$continua = true; |
|
$spid = true; |
|
} |
|
} else { |
|
$cryptpassw = md5($password); |
|
if (password_verify($cryptpassw,$utente["password"]) && $utente["tentativi"] < 5 && $utente["scaduto"] != "S") { |
|
$continua = true; |
|
} |
|
} |
|
if ($continua) { |
|
if ($utente["ruolo"] == "SAD" || $utente["ruolo"] == "SUP") { |
|
$abilitato = true; |
|
} else if ($utente["ruolo"] == "CON") { |
|
if (!isset($_SESSION["ente"])) { |
|
$abilitato = true; |
|
} |
|
} else { |
|
if (isset($_SESSION["ente"])) { |
|
if ($utente["ruolo"] == "OPE") { |
|
$oe = self::getOEFromUser($utente["codice"]); |
|
if (count($oe) === 1) { |
|
$oe = reset($oe); |
|
$bind=array(":codice_ente"=>$_SESSION["ente"]->codice,":codice_operatore"=>$oe["codice"]); |
|
$rel = $pdo->go("SELECT r_enti_operatori.codice FROM r_enti_operatori JOIN b_enti ON r_enti_operatori.cod_ente = b_enti.codice |
|
WHERE b_enti.attivo = 'S' AND b_enti.codice = :codice_ente |
|
AND r_enti_operatori.codice_operatore = :codice_operatore ",$bind); |
|
if ($rel->rowCount() > 0) { |
|
$abilitato = true; |
|
} else { |
|
$salva = new salva(); |
|
$salva->debug = false; |
|
|
|
$salva->nome_tabella = "r_enti_operatori"; |
|
$salva->operazione = "INSERT"; |
|
$salva->oggetto = array("cod_ente"=>$_SESSION["ente"]->codice,"codice_operatore"=>$oe["codice"]); |
|
if ($salva->save() !== false) { |
|
$abilitato = true; |
|
} |
|
} |
|
} |
|
} else if ($utente["ruolo"] == "ENT" && isset($_SESSION["ente"]) && $_SESSION["ente"]->hasModulo("matchmaking")) { |
|
$enteInnovativo = self::getEnteInnovativoFromUser($utente["codice"]); |
|
if (count($enteInnovativo) === 1) { |
|
$enteInnovativo = reset($enteInnovativo); |
|
$bind=array(":codice_ente"=>$_SESSION["ente"]->codice,":cod_innovativo"=>$enteInnovativo["codice"]); |
|
$rel = $pdo->go("SELECT r_enti_innovazione.codice FROM r_enti_innovazione JOIN b_enti ON r_enti_innovazione.cod_ente = b_enti.codice |
|
WHERE b_enti.attivo = 'S' AND b_enti.codice = :codice_ente |
|
AND r_enti_innovazione.cod_innovativo = :cod_innovativo ",$bind); |
|
if ($rel->rowCount() > 0) { |
|
$abilitato = true; |
|
} else { |
|
$salva = new salva(); |
|
$salva->debug = false; |
|
|
|
$salva->nome_tabella = "r_enti_operatori"; |
|
$salva->operazione = "INSERT"; |
|
$salva->oggetto = array("cod_ente"=>$_SESSION["ente"]->codice,"cod_innovativo"=>$enteInnovativo["codice"]); |
|
if ($salva->save() !== false) { $abilitato = true; } |
|
} |
|
} |
|
} else { |
|
$enti = $_SESSION["ente"]->entiBeneficiari(); |
|
if (!empty($enti)) { |
|
$collaborazioni = self::getCollaborazioniFromUtente($utente["codice"],$enti); |
|
if (!empty($collaborazioni)) { |
|
$sql = "SELECT timestamp FROM b_login_hash WHERE codice_utente = :codice_utente "; |
|
$ris_hash = $pdo->go($sql,array(":codice_utente"=>$utente["codice"])); |
|
if ($ris_hash->rowCount() > 0) { |
|
$last_login = $ris_hash->fetch(PDO::FETCH_ASSOC); |
|
if ((strtotime($last_login["timestamp"]) < strtotime('-60 minutes', time())) || $force || $force === "api") { |
|
$abilitato = true; |
|
if ($force !== "api") { |
|
$bind = array(":codice_utente"=>$utente["codice"]); |
|
$pdo->go("DELETE FROM b_login_hash WHERE codice_utente = :codice_utente",$bind); |
|
} |
|
} |
|
$return = -1; |
|
} else { |
|
$abilitato = true; |
|
} |
|
if ($abilitato && $force !== "api") { |
|
$_SESSION["loginHash"] = sha1($utente["codice"].time()); |
|
$bind = array(":codice_utente"=>$utente["codice"],":hash"=>$_SESSION["loginHash"]); |
|
$pdo->go("INSERT INTO b_login_hash (codice_utente, hash) VALUES (:codice_utente, :hash)",$bind); |
|
} |
|
} |
|
} |
|
} |
|
} |
|
} |
|
} |
|
} |
|
} |
|
if ($abilitato) { |
|
global $config; |
|
$return = new Utente(); |
|
$return->codice = $utente["codice"]; |
|
$return->lockPath = $config["user_log"] . "/lock/{$utente["codice"]}"; |
|
$return->readOnly = false; |
|
$return->authenticationLevel = 1; |
|
self::touchLock($return->codice); |
|
if (!empty($collaborazioni)) { |
|
$return->collaborazioni = $collaborazioni; |
|
if (count($collaborazioni) === 1){ |
|
$codice_collaborazione = array_keys($collaborazioni)[0]; |
|
$collaborazione = $collaborazioni[$codice_collaborazione]; |
|
$return->codice_ente = $collaborazione["codice_ente"]; |
|
$utente["ruolo"] = $collaborazione["gruppo"]; |
|
$return->collaborazione = $codice_collaborazione; |
|
if ($collaborazione["readOnly"] == "S") { |
|
$return->readOnly = true; |
|
} |
|
} |
|
} else if (!empty($oe)) { |
|
$return->oe = $oe; |
|
$return->verifyTempInviti(); |
|
} else if (!empty($enteInnovativo)) { |
|
$return->enteInnovativo = $enteInnovativo; |
|
$return->verifyTempInviti(); |
|
} else if (self::ruoli()[$utente["ruolo"]]["gerarchia"] > 10) { |
|
return 0; |
|
} |
|
$return->gerarchia = self::ruoli()[$utente["ruolo"]]["gerarchia"]; |
|
$return->botVerify = $utente["bot_verify"]; |
|
$return->ruolo = $utente["ruolo"]; |
|
$return->qualificato = true; |
|
$return->apiUser = ($force==="api") ? true : false; |
|
$bind = array(":utente_modifica"=>$utente["codice"],":codice_ente"=>($_SESSION["ente"]->codice??0),":ip"=>$_SERVER["REMOTE_ADDR"]); |
|
$pdo->go("INSERT INTO b_log_accessi (utente_modifica, codice_ente, ip) VALUES (:utente_modifica, :codice_ente, :ip)",$bind); |
|
$sql = "UPDATE b_utenti SET tentativi = 0, bot_verify = 'S' WHERE codice = :codice_utente "; |
|
$pdo->go($sql,array(":codice_utente"=>$utente["codice"])); |
|
$sql = "INSERT INTO b_check_sessions (codice_utente, sessionID, agent, ip) VALUES (:codice_utente, :session_id, :agent, :ip)"; |
|
$bind = [':codice_utente' => $utente["codice"], ':session_id' => simple_encrypt(session_id(),$config["simple_encrypt"]["session"]), ':agent' => $_SERVER['HTTP_USER_AGENT'], ':ip' => $_SERVER["REMOTE_ADDR"]]; |
|
$pdo->go($sql, $bind); |
|
$pdo->go("UPDATE b_utenti SET last_login = NOW() WHERE codice = :codice_utente",array(":codice_utente"=>$utente["codice"])); |
|
} else { |
|
if (!empty($utente) && $return !== -1) { |
|
$sql = "UPDATE b_utenti SET tentativi = :tentativi WHERE attivo = 'S' AND codice = :codice_utente "; |
|
$tentativi = $utente["tentativi"] + 1; |
|
$return = array("tentativi"=>$tentativi,"scaduto"=>$utente["scaduto"]); |
|
$pdo->go($sql,array(":codice_utente"=>$utente["codice"],":tentativi"=>$tentativi)); |
|
} |
|
$sql_injection = "/(ALTER|CREATE|DELETE|DROP|EXEC(UTE)|INSERT|MERGE|SELECT|UPDATE|UNION|HEX)+/i"; |
|
$char_injection = "/(((\%27)|(\'))|((\%47)|(\/))|((\#)|(\%43)))/"; |
|
if (preg_match($sql_injection,$email) && preg_match($char_injection,$email)) { |
|
$email = "INJECTION"; |
|
} |
|
$bind = array(":username"=>$email,":codice_ente"=>(!empty($_SESSION["ente"])) ? $_SESSION["ente"]->codice : 0,":ip"=>$_SERVER["REMOTE_ADDR"]); |
|
$pdo->go("INSERT INTO b_log_tentativi (username, codice_ente, ip) VALUES (:username, :codice_ente, :ip)",$bind); |
|
} |
|
} |
|
return $return; |
|
} |
|
|
|
public function fromHere() { |
|
if (!empty($_SESSION["ente"]) && $this->gerarchia > 9 && $_SESSION["ente"]->codice != $this->codice_ente) { |
|
return false; |
|
} |
|
return true; |
|
} |
|
|
|
public function getCollaborazioneAttiva() { |
|
if (isset($this->collaborazioni)) { |
|
return $this->collaborazioni[$this->collaborazione]; |
|
} |
|
} |
|
|
|
public function check($interno=false) { |
|
global $pdo; |
|
$abilitato = false; |
|
if (!empty($this->gerarchia)) { |
|
$bind = array(":codice"=>$this->codice); |
|
$sql = "SELECT b_utenti.* FROM b_utenti WHERE b_utenti.codice = :codice AND b_utenti.attivo='S'"; |
|
$risultato = $pdo->go($sql,$bind); |
|
if ($risultato->rowCount() === 1 && $utente = $risultato->fetch(PDO::FETCH_ASSOC)) { |
|
$sql = "SELECT * FROM b_check_sessions WHERE codice_utente = :codice_utente"; |
|
$check_session = $pdo->go($sql, [':codice_utente' => $utente["codice"]]); |
|
$current_session = ["sessionID" => session_id(), "agent" => purify($_SERVER['HTTP_USER_AGENT']), "ip" => $_SERVER["REMOTE_ADDR"]]; |
|
$is_a_valid_session = false; |
|
while($stored_session = $check_session->fetch(PDO::FETCH_ASSOC)) { |
|
$current_check = true; |
|
$stored_session["sessionID"] = simple_decrypt($stored_session["sessionID"], $config["simple_decrypt"]["session"]); |
|
foreach ($current_session as $key => $value) { |
|
if($stored_session[$key] != $value) { |
|
$current_check = false; |
|
} |
|
} |
|
if($current_check) { |
|
$this->codice_sessione = $stored_session["id"]; |
|
$is_a_valid_session = true; break; |
|
} |
|
} |
|
if($is_a_valid_session) { |
|
$abilitato = true; |
|
if ($this->ruolo == "CON") { |
|
$abilitato = false; |
|
if (!isset($_SESSION["ente"])) { |
|
$abilitato = true; |
|
} |
|
} else if ($this->gerarchia >= 10 && $this->gerarchia < 100) { |
|
$abilitato = false; |
|
if (!empty($this->collaborazione) && !empty($this->collaborazioni)) { |
|
$this->codice_ente = ""; |
|
foreach($this->collaborazioni AS $id_coll => $collaborazione) { |
|
if ($id_coll == $this->collaborazione) { |
|
if (self::ruoli()[$collaborazione["gruppo"]]["ente"]) { |
|
$utente["ruolo"] = $collaborazione["gruppo"]; |
|
$this->codice_ente = $collaborazione["codice_ente"]; |
|
$this->collaborazione = $id_coll; |
|
$this->readOnly = ($collaborazione["readOnly"] == "S") ? true : false; |
|
} |
|
} |
|
} |
|
if (!empty($this->collaborazione)) { |
|
$check = $pdo->go("SELECT codice FROM r_collaborazioni WHERE codice = :codice AND attivo = 'S'",[":codice"=>$this->collaborazione]); |
|
if ($check->rowCount() == 0) { |
|
$this->gerarchia = null; |
|
$this->ruolo = null; |
|
unset($this->collaborazioni[$this->collaborazione]); |
|
$this->collaborazione = null; |
|
} |
|
} |
|
if ($this->apiUser === false && !empty($this->codice_ente) && !empty($utente["ruolo"])) { |
|
$sql = "SELECT b_enti.codice FROM b_enti LEFT JOIN b_enti AS sua ON b_enti.sua = sua.codice |
|
WHERE b_enti.codice = :codice_ente AND (b_enti.attivo = 'S' OR sua.attivo = 'S')"; |
|
$ris_ente = $pdo->go($sql,array(":codice_ente"=>$this->codice_ente)); |
|
if ($ris_ente->rowCount() === 1) { |
|
$bind = array(":codice_utente"=>$utente["codice"],":hash"=>$_SESSION["loginHash"]); |
|
$sql = "SELECT codice FROM b_login_hash WHERE codice_utente = :codice_utente AND hash = :hash"; |
|
$ris_hash = $pdo->go($sql,$bind); |
|
if ($ris_hash->rowCount() > 0) { |
|
$pdo->go("UPDATE b_login_hash SET timestamp = NOW() WHERE codice_utente = :codice_utente AND hash = :hash",$bind); |
|
$abilitato = true; |
|
} |
|
} |
|
} else if ($this->apiUser) { |
|
$abilitato = true; |
|
} |
|
} else if (!empty($this->collaborazioni) && empty($this->collaborazione)) { |
|
$mustSelectCollaborazione = true; |
|
} |
|
} else if (!isset($_SESSION["ente"]) && $this->gerarchia > 9) { |
|
$abilitato = false; |
|
} |
|
} |
|
} |
|
} |
|
|
|
if ($abilitato) { |
|
$this->gerarchia = self::ruoli()[$utente["ruolo"]]["gerarchia"]; |
|
$this->ruolo = $utente["ruolo"]; |
|
$_SESSION["utente"] = $this; |
|
return true; |
|
} else { |
|
if (!$interno && empty($mustSelectCollaborazione)) { |
|
session_destroy(); |
|
echo '<meta http-equiv="refresh" content="0;URL=/index.php">'; |
|
die(); |
|
} else { |
|
return false; |
|
} |
|
} |
|
} |
|
|
|
public function regenerateID() { |
|
global $pdo, $config; |
|
if ($this->check() && !empty($this->codice_sessione)) { |
|
$bindUPD = array(); |
|
session_regenerate_id(true); |
|
$bindUPD[":sessionID"] = simple_encrypt(session_id(),$config["simple_encrypt"]["session"]); |
|
$bindUPD[":codice_utente"] = $this->codice; |
|
$bindUPD[":codice_sessione"] = $this->codice_sessione; |
|
$bindUPD[":agent"] = $_SERVER['HTTP_USER_AGENT']; |
|
$bindUPD[":ip"] = $_SERVER["REMOTE_ADDR"]; |
|
$pdo->go("UPDATE b_check_sessions SET sessionID = :sessionID, agent = :agent, ip = :ip WHERE id = :codice_sessione AND codice_utente = :codice_utente ",$bindUPD); |
|
} |
|
} |
|
|
|
public function hasSuperPowers() { |
|
if (!empty($this->superPowers) && is_numeric($this->superPowers) && $this->superPowers > time()) { |
|
return true; |
|
} |
|
return false; |
|
} |
|
|
|
public function unlockSupporto() { |
|
if ($this->isSupportoOrRoot()) { |
|
if (!$this->isSupporto() || $this->hasSuperPowers()) { |
|
return true; |
|
} else { |
|
return false; |
|
} |
|
} |
|
return true; |
|
} |
|
|
|
public function isSupportoRole() { |
|
$types = ["SUP"]; |
|
return (in_array($this->ruolo,$types)!==false) ? true : false; |
|
} |
|
|
|
public function isSupporto() { |
|
if ($this->isSupportoRole()) { |
|
return true; |
|
} |
|
return false; |
|
} |
|
|
|
public function isSupportoOrRoot() { |
|
return ($this->isSupporto() || $this->ruolo == "SAD"); |
|
} |
|
|
|
public function canAssignPermissions() { |
|
if ($this->gerarchia <= 10) { |
|
return true; |
|
} else { |
|
if (isset($_SESSION["ente"])) { |
|
return (settingsManager::getValue("assegnazionePermessi",$_SESSION["ente"]->codice) == "S"); |
|
} |
|
} |
|
return false; |
|
} |
|
|
|
public function specificPermissions($modulo,$id,$specific,$skipVerify = false) { |
|
global $root; |
|
global $pdo; |
|
$return = false; |
|
$settingsModulo = $this->settings[$modulo] ?? $this->settings["default"]; |
|
$ignoreTable = false; |
|
if (!empty($modulo) && !empty($id)) { |
|
$path = $root."/backend/".$modulo."/_verify.php"; |
|
if (file_exists($path) && !$skipVerify) { |
|
include($path); |
|
} else { |
|
$return = true; |
|
} |
|
if ($return && !$ignoreTable && $this->gerarchia > 10) { |
|
$check = $pdo->prepare("SELECT codice_utente, funzione FROM r_permessi WHERE sezione = :sezione AND codice_gestore = :codice_gestore AND codice_elemento = :codice_elemento "); |
|
$check->bindValue(":sezione",$modulo); |
|
$check->bindValue(":codice_elemento",$id); |
|
$check->bindValue(":codice_gestore",$_SESSION["ente"]->codice); |
|
$check->execute(); |
|
if($check->rowCount() > 0) { |
|
$return = false; |
|
while($utente = $check->FETCH(PDO::FETCH_ASSOC)) { |
|
if ($utente["codice_utente"] == $this->codice && (empty($specific) || empty($utente["funzione"]) || $specific == $utente["funzione"])) { |
|
$return = true; break; |
|
} |
|
} |
|
} else if ($settingsModulo["permissionRequired"]) { $return = false; } |
|
} |
|
} |
|
return $return; |
|
} |
|
|
|
public static function getPermissionsInModulo(int $codice_gestore, int $codice_utente, string $sezione) { |
|
$return = []; |
|
global $pdo; |
|
$bind = []; |
|
$bind[":codice_gestore"] = $codice_gestore; |
|
$bind[":codice_utente"] = $codice_utente; |
|
$bind[":sezione"] = $sezione; |
|
$ids = $pdo->go("SELECT codice_elemento FROM r_permessi WHERE codice_gestore = :codice_gestore AND codice_utente = :codice_utente AND sezione = :sezione",$bind)->fetchAll(PDO::FETCH_COLUMN); |
|
if (!empty($ids)) { |
|
foreach($ids AS $id) { |
|
$return[] = (int) $id; |
|
} |
|
} |
|
return $return; |
|
} |
|
|
|
public function permission($radice,$id=0,$specific="",$skipVerify = false) { |
|
$return = false; |
|
$permission_string = "{$this->codice}"; |
|
if (!empty($this->collaborazione)) { |
|
$permission_string .= $this->collaborazione . "-"; |
|
} |
|
$permission_string .= "-{$radice}-{$id}-{$specific}"; |
|
$permission_id = hash("md5",$permission_string); |
|
$check = true; |
|
if (!empty($radice) && $this->check(true)) { |
|
if (isset($_SESSION["verified-permission"][$permission_id])) { |
|
if (file_exists($this->lockPath)) { |
|
if (time() < $_SESSION["verified-permission"][$permission_id]["scadenza"]) { |
|
$check = false; |
|
$return = $_SESSION["verified-permission"][$permission_id]["esito"]; |
|
} |
|
} else { |
|
unset($_SESSION["verified-permission"]); |
|
} |
|
} |
|
if ($check) { |
|
global $pdo; |
|
global $config; |
|
if (!empty($config["moduli"][$radice])) { |
|
$modulo = $config["moduli"][$radice]; |
|
if (isset($_SESSION["ente"]) && $_SESSION["ente"]->getInfo()["permit_cross"] == "S") { |
|
$modulo["cross_p"] = "S"; |
|
} |
|
if ($modulo["gerarchia"] >= $this->gerarchia) { |
|
if (empty($this->codice_ente) && $this->gerarchia <= 9) { |
|
if ($modulo["admin"] || (!empty($_SESSION["ente"]) && ($modulo["all_ente"] || $_SESSION["ente"]->hasModulo($radice)))) { |
|
$return = true; |
|
} |
|
} else { |
|
if ($modulo["ente"]) { |
|
$eccezioni = []; |
|
if (isset($_SESSION["ente"])) { |
|
$eccezioni = $_SESSION["ente"]->getEccezioniCross(); |
|
} |
|
if ($modulo["all_ente"]) { |
|
$return = true; |
|
} else { |
|
$sql = "SELECT codice FROM r_moduli_ente WHERE id_modulo = :id_modulo AND cod_ente = :codice_ente "; |
|
$ris = $pdo->go($sql,array(":id_modulo"=>$radice,":codice_ente"=>$this->codice_ente)); |
|
if ($ris->rowCount() > 0) { |
|
$return = true; |
|
} else { |
|
if (!$this->fromHere()) { |
|
$ris = $pdo->go($sql,array(":id_modulo"=>$radice,":codice_ente"=>$_SESSION["ente"]->codice)); |
|
if ($ris->rowCount() > 0 && ($modulo["cross_p"] == "S" || in_array($radice,$eccezioni) !== false)) { |
|
$return = true; |
|
} |
|
} |
|
} |
|
} |
|
if ($return && ($_SESSION["ente"]->codice != $this->codice_ente) && !$modulo["cross_p"] && $_SESSION["ente"]->getInfo()["permit_cross"] == "N" && in_array($radice,$eccezioni) === false) { |
|
$return = false; |
|
} |
|
} |
|
if ($return && !$modulo["all_utente"]) { |
|
$return = false; |
|
$sql = "SELECT codice FROM r_permessi_collaborazioni WHERE id_modulo = :id_modulo AND cod_relazione = :cod_relazione "; |
|
$ris = $pdo->go($sql,array(":id_modulo"=>$radice,":cod_relazione"=>$this->collaborazione)); |
|
if ($ris->rowCount() > 0) { |
|
$return = true; |
|
} |
|
} |
|
} |
|
} |
|
} |
|
if ($return && !empty($id)) { |
|
$return = $this->specificPermissions($radice,$id,$specific,$skipVerify); |
|
} |
|
$_SESSION["verified-permission"][$permission_id] = ["esito"=>$return,"scadenza"=>strtotime("+1 hour")]; |
|
self::touchLock($this->codice); |
|
} |
|
} |
|
return $return; |
|
} |
|
|
|
public function TwoFactorVerify($getToken=false) { |
|
$return = false; |
|
global $pdo; |
|
if (!empty($this->twoFactorVerification)) { |
|
$return = true; |
|
} else { |
|
$info = $pdo->go("SELECT twoFactor_token FROM b_utenti WHERE codice = :codice_utente",[":codice_utente"=>$this->codice])->fetch(PDO::FETCH_ASSOC); |
|
if (!empty($info)) { |
|
if (!empty($info["twoFactor_token"])) { |
|
if ($getToken) { $return = $info["twoFactor_token"]; } |
|
} else { |
|
$return = true; |
|
} |
|
} |
|
} |
|
return $return; |
|
} |
|
|
|
public static function getRuoloFromID($ids,$codice_ente) { |
|
global $pdo; |
|
$ris_user = $pdo->prepare("SELECT * FROM r_collaborazioni WHERE codice_utente = :codice_utente AND codice_ente = :codice_ente"); |
|
$ris_user->bindValue(":codice_ente",$codice_ente); |
|
$returnSingle = false; |
|
$return = []; |
|
if (!is_array($ids)) { |
|
$ids = [$ids]; |
|
$returnSingle = true; |
|
} |
|
foreach($ids AS $id) { |
|
$ris_user->bindValue(":codice_utente",$id); |
|
$ris_user->execute(); |
|
if ($ris_user->rowCount() > 0) { |
|
$user = $ris_user->fetch(PDO::FETCH_ASSOC); |
|
$return[$user["codice_utente"]] = $user; |
|
} |
|
} |
|
if ($returnSingle) { |
|
if (count($return) === 1) { |
|
$return = reset($return[0]); |
|
} else { |
|
$return = false; |
|
} |
|
} |
|
if (empty($return)) { $return = false; } |
|
return $return; |
|
} |
|
|
|
public function canCertify(?int $utente = null) : Bool { |
|
if (isset($_SESSION["ente"])) { |
|
if ($_SESSION["ente"]->certificazioneManualeUtenti() && ($this->ruolo == "ADM" || $this->hasSuperPowers()) && (empty($utente) || ($utente != $this->codice && $this->permission("utenti",$utente)))) { |
|
return true; |
|
} |
|
} |
|
return false; |
|
} |
|
|
|
public static function getInfoFromID($ids) { |
|
global $pdo; |
|
$ris_user = $pdo->prepare("SELECT * FROM b_utenti WHERE codice = :codice_utente "); |
|
$returnSingle = false; |
|
$return = []; |
|
if (!is_array($ids)) { |
|
$ids = [$ids]; |
|
$returnSingle = true; |
|
} |
|
foreach($ids AS $id) { |
|
$ris_user->bindValue(":codice_utente",$id); |
|
$ris_user->execute(); |
|
if ($ris_user->rowCount() > 0) { |
|
$user = $ris_user->fetch(PDO::FETCH_ASSOC); |
|
$return[$user["codice"]] = $user; |
|
} |
|
} |
|
if ($returnSingle) { |
|
if (count($return) === 1) { |
|
$return = reset($return); |
|
} else { |
|
$return = false; |
|
} |
|
} else { |
|
usort($return,"sortUtenti"); |
|
} |
|
if (empty($return)) { $return = false; } |
|
return $return; |
|
} |
|
|
|
public static function checkEmail($email,$codice = 0) { |
|
global $pdo; |
|
$bind = [":valore"=>$email]; |
|
$sql = "SELECT codice FROM b_utenti WHERE email = :valore "; |
|
if (!empty($codice)) { |
|
$bind[":codice"] = $codice; |
|
$sql .= " AND codice <> :codice "; |
|
} |
|
$ris = $pdo->go($sql,$bind); |
|
return ($ris->rowCount() === 0) ? true : false; |
|
} |
|
|
|
public static function checkSpidCode($spidCode) { |
|
global $pdo; |
|
$bind = [":valore"=>$spidCode]; |
|
$sql = "SELECT codice FROM b_utenti WHERE spidCode = :valore "; |
|
$ris = $pdo->go($sql,$bind); |
|
return ($ris->rowCount() === 0) ? true : false; |
|
} |
|
|
|
|
|
public function getInfo() { |
|
return self::getInfoFromID($this->codice); |
|
} |
|
|
|
public function moduliDisponibili($filtri = []) { |
|
global $pdo; |
|
$moduli = []; |
|
if (empty($filtri)) { $filtri = ["attivo"=>true,"nascosto"=>false]; } |
|
if (empty($this->codice_ente) && empty($_SESSION["ente"])) { $filtri["admin"] = true; } |
|
if (!empty($this->codice_ente) || !empty($_SESSION["ente"])) { |
|
$cod_ente = (!empty($this->codice_ente)) ? $this->codice_ente : $_SESSION["ente"]->codice; |
|
$filtri["ente"] = true; |
|
$ck_ente = $pdo->prepare("SELECT codice FROM r_moduli_ente WHERE cod_ente = :cod_ente AND id_modulo = :id_modulo"); |
|
$ck_ente->bindValue(":cod_ente",$cod_ente); |
|
if (!$this->fromHere() && $_SESSION["ente"]->getInfo()["permit_cross"] != "S") { |
|
$filtri["cross_p"] = true; |
|
} |
|
} |
|
$tmpModuli = Modulo::getModuli($filtri); |
|
if (!$this->fromHere()) { |
|
$eccezioni = $_SESSION["ente"]->getEccezioniCross(); |
|
if (!empty($tmpModuli)) { |
|
foreach($eccezioni AS $eccezione) { |
|
$tmpModuli[$eccezione] = Modulo::getInfo($eccezione); |
|
} |
|
} |
|
} |
|
$ck_utente = $pdo->prepare("SELECT codice FROM r_permessi_collaborazioni WHERE id_modulo = :id_modulo AND cod_relazione = :cod_relazione "); |
|
$ck_utente->bindValue(":cod_relazione",$this->collaborazione); |
|
foreach($tmpModuli AS $id_modulo => $modulo) { |
|
$insert = true; |
|
if ($modulo["gerarchia"] >= $this->gerarchia) { |
|
if ($this->gerarchia > 9 && !$modulo["all_utente"]) { |
|
$ck_utente->bindValue(":id_modulo",$id_modulo); |
|
$ck_utente->execute(); |
|
if ($ck_utente->rowCount() !== 1) { $insert = false; } |
|
} |
|
if ($insert && !$modulo["all_ente"] && isset($ck_ente)) { |
|
$ck_ente->bindValue(":id_modulo",$id_modulo); |
|
$ck_ente->execute(); |
|
if ($ck_ente->rowCount() !== 1) { |
|
$insert = false; |
|
if (!$this->fromHere()) { |
|
$ck_platform = $ck_ente; |
|
$ck_platform->bindValue(":cod_ente",$_SESSION["ente"]->codice); |
|
$ck_platform->execute(); |
|
if ($ck_platform->rowCount() > 0 && ($modulo["cross_p"] == "S" || in_array($id_modulo,$eccezioni) !== false)) { |
|
$insert = true; |
|
} |
|
} |
|
} |
|
} |
|
} else { |
|
$insert = false; |
|
} |
|
if ($insert) { |
|
if (!isset($moduli[$modulo["tipo"]])) { $moduli[$modulo["tipo"]] = []; } |
|
$moduli[$modulo["tipo"]][$id_modulo] = $modulo; |
|
} |
|
} |
|
return $moduli; |
|
} |
|
|
|
public function getEntiBeneficiari(bool $includePrivati = true) { |
|
$return = false; |
|
if ($this->ruolo != "OPE" && $this->ruolo != "ENT" && !empty($_SESSION["ente"])) { |
|
$return = $_SESSION["ente"]->entiBeneficiari($includePrivati); |
|
if ($this->gerarchia > 9 && !$this->fromHere()) { |
|
$return = []; |
|
$return[$this->codice_ente] = ente::getInfoFromID($this->codice_ente)[0]; |
|
} |
|
} |
|
return $return; |
|
} |
|
|
|
|
|
public function getTasks($codice = null,$dateFrom = null,$dateTo = null) { |
|
global $pdo; |
|
$bind = [":codice_utente"=>$this->codice]; |
|
$sql = "SELECT * FROM b_tasks WHERE codice_utente = :codice_utente"; |
|
if (!empty($codice)) { |
|
$sql .= " AND codice = :codice"; |
|
$bind[":codice"] = $codice; |
|
} |
|
if (!empty($dateFrom)) { |
|
$sql .= " AND data >= :dateFrom "; |
|
$bind[":dateFrom"] = $dateFrom; |
|
} |
|
if (!empty($dateTo)) { |
|
$sql .= " AND data <= :dateTo "; |
|
$bind[":dateTo"] = $dateTo; |
|
} |
|
if (empty($codice)) { |
|
return $pdo->go($sql,$bind)->fetchAll(PDO::FETCH_ASSOC); |
|
} else { |
|
return $pdo->go($sql,$bind)->fetch(PDO::FETCH_ASSOC); |
|
} |
|
} |
|
|
|
public function getScadenze($fromDate,$toDate,$moduloSelezionato=null,$codiceElementoScadenze=null) { |
|
global $pdo; |
|
global $root; |
|
$scadenze = []; |
|
$filtri = ["attivo"=>true]; |
|
$moduli = $this->moduliDisponibili($filtri); |
|
$fromDate .= " 00:00:00"; |
|
$toDate .= " 23:59:59"; |
|
foreach($moduli AS $gruppo) { |
|
if (!empty($moduloSelezionato)) { |
|
if (isset($gruppo[$moduloSelezionato])) { |
|
$gruppo = [$moduloSelezionato=>$gruppo[$moduloSelezionato]]; |
|
} else { |
|
$gruppo = []; |
|
} |
|
} |
|
if (!empty($gruppo)) { |
|
foreach($gruppo AS $idModulo => $modulo) { |
|
$path = $root.DIRECTORY_SEPARATOR."backend".DIRECTORY_SEPARATOR.$idModulo.DIRECTORY_SEPARATOR."_scadenze.php"; |
|
if (file_exists($path)) { |
|
include($path); |
|
} |
|
} |
|
} |
|
} |
|
return $scadenze; |
|
} |
|
|
|
|
|
public function verifyTempInviti() { |
|
if (!empty($this->oe) && !empty($_SESSION["ente"]->codice)) { |
|
global $pdo; |
|
$inviti = $pdo->go("SELECT * FROM temp_inviti WHERE pec = :pec AND attivo = 'S'",[":pec"=>$this->oe["pec"]])->fetchAll(PDO::FETCH_ASSOC); |
|
if (!empty($inviti)) { |
|
foreach($inviti AS $invito) { |
|
$delete = false; |
|
if ($invito["contesto"] == "gare") { |
|
$insert = []; |
|
$insert["codice_operatore"] = $this->oe["codice"]; |
|
$insert["codice_round"] = $invito["codice_elemento"]; |
|
$insert["codice_lotto"] = $invito["sub_elemento"]; |
|
$insert["sorteggio"] = "N"; |
|
$salva = new salva(); |
|
$salva->debug = false; |
|
$salva->nome_tabella = "r_inviti_gare"; |
|
$salva->operazione = "INSERT"; |
|
$salva->oggetto = $insert; |
|
if ($salva->save()) { |
|
$delete = true; |
|
} |
|
} |
|
if ($delete) { |
|
$pdo->go("DELETE FROM temp_inviti WHERE codice = :codice",[":codice"=>$invito["codice"]]); |
|
} |
|
} |
|
} |
|
} |
|
} |
|
|
|
public function myVR() { |
|
if (!empty($this->oe) && !empty($_SESSION["ente"]->codice)) { |
|
global $pdo; |
|
$bind=array(":codice_ente"=>$_SESSION["ente"]->codice,":codice_operatore"=>$this->oe["codice"]); |
|
return $pdo->go("SELECT r_enti_operatori.feedback FROM r_enti_operatori JOIN b_enti ON r_enti_operatori.cod_ente = b_enti.codice |
|
WHERE b_enti.attivo = 'S' AND b_enti.codice = :codice_ente |
|
AND r_enti_operatori.codice_operatore = :codice_operatore ",$bind)->fetch(PDO::FETCH_COLUMN); |
|
} |
|
} |
|
|
|
public static function getConsulenti($gruppo) { |
|
global $pdo; |
|
return $pdo->go("SELECT email FROM b_utenti WHERE attivo = 'S' AND scaduto = 'N' AND ruolo IN ('SAD','SUP','CON') AND gruppo_consulenza = :gruppo",[":gruppo"=>$gruppo])->fetchAll(PDO::FETCH_COLUMN) ?? NULL; |
|
} |
|
|
|
/** |
|
* Restituisce un array con gli ID degli utenti con collaborazioni di monitoraggio |
|
* |
|
* @return Array |
|
*/ |
|
public static function getUtentiMonitoraggio() : Array { |
|
global $pdo; |
|
return $pdo->go("SELECT b_utenti.codice FROM b_utenti JOIN r_collaborazioni ON b_utenti.codice = r_collaborazioni.codice_utente WHERE monitoraggio = 'S' GROUP BY b_utenti.codice")->fetchAll(PDO::FETCH_COLUMN); |
|
} |
|
|
|
/** |
|
* Attribuisce automaticamente i permessi su una procedura all'RP |
|
* |
|
* @param String $codice_fiscale: Codice fiscale dell'utente |
|
* @param Int $ente_riferimento: Ente di riferimento dell'elemento |
|
* @param String $sezione: Modulo della piattaforma |
|
* @param String $elemento: Elemento nel modulo |
|
* @return Bool |
|
*/ |
|
public static function addPermissionForRP(String $codice_fiscale,Int $ente_riferimento, String $sezione, String $elemento) : Bool { |
|
if (isset($_SESSION["ente"])) { |
|
$utenze = self::getUsersFromCF($codice_fiscale); |
|
if (!empty($utenze)) { |
|
$utenze = array_filter($utenze,function($utente) { |
|
return $utente["ruolo"] === "USR"; |
|
}); |
|
if (!empty($utenze)) { |
|
$disponibili = $_SESSION["ente"]->getUtentiFromEnte(array_column($utenze,"codice"),$ente_riferimento); |
|
if (!empty($disponibili)) { |
|
$utente = reset($disponibili); |
|
return self::savePermission($utente["codice"],$sezione,$elemento); |
|
} |
|
} |
|
} |
|
} |
|
return false; |
|
} |
|
|
|
/** |
|
* Salva i permessi per un elemento |
|
* |
|
* @param Array | Int $utenti Array di ID utenti o ID del singolo utente su cui agire |
|
* @param String $sezione |
|
* @param String $elemento |
|
* @param string $funzione |
|
* @return Bool |
|
*/ |
|
public static function savePermission($utenti,String $sezione,String $elemento, String $funzione = "0") : Bool { |
|
global $pdo; |
|
$clean = (is_array($utenti)); |
|
if ($clean) { |
|
$pdo->go("DELETE FROM r_permessi WHERE sezione = :sezione AND codice_elemento = :codice_elemento AND codice_gestore = :codice_gestore AND funzione = :funzione", |
|
[ |
|
":sezione"=>$sezione, |
|
":codice_elemento"=>$elemento, |
|
":funzione"=>$funzione, |
|
":codice_gestore"=>$_SESSION["ente"]->codice |
|
]); |
|
} else { |
|
$checkPermission = $pdo->go("SELECT FROM r_permessi WHERE sezione = :sezione AND codice_elemento = :codice_elemento AND codice_gestore = :codice_gestore AND codice_utente = :codice_utente AND funzione :funzione", |
|
[ |
|
":utente" => $utenti, |
|
":sezione" => $sezione, |
|
":codice_elemento" => $elemento, |
|
":codice_gestore" => $_SESSION["ente"]->codice |
|
]); |
|
if ($checkPermission->rowCount() > 0) { |
|
return true; |
|
} else { |
|
$utenti = [$utenti]; |
|
} |
|
} |
|
if (empty($funzione)) { |
|
$deleteOthers = $pdo->prepare("DELETE FROM r_permessi WHERE sezione = :sezione AND codice_elemento = :codice_elemento AND codice_gestore = :codice_gestore AND codice_utente = :codice_utente AND funzione <> '0'"); |
|
$deleteOthers->bindValue(":sezione",$sezione); |
|
$deleteOthers->bindValue(":codice_elemento",$elemento); |
|
$deleteOthers->bindValue(":codice_gestore",$_SESSION["ente"]->codice); |
|
} |
|
if (!empty($utenti)) { |
|
foreach($utenti AS $utente) { |
|
$permesso = []; |
|
$permesso["sezione"] = $sezione; |
|
$permesso["codice_elemento"] = $elemento; |
|
$permesso["codice_gestore"] = $_SESSION["ente"]->codice; |
|
$permesso["funzione"] = $funzione; |
|
$permesso["codice_utente"] = $utente; |
|
self::removeLock($utente); |
|
$salva = new salva(); |
|
$salva->debug = false; |
|
$salva->nome_tabella = "r_permessi"; |
|
$salva->operazione = "INSERT"; |
|
$salva->oggetto = $permesso; |
|
$salva->save(); |
|
if (isset($deleteOthers)) { |
|
$deleteOthers->bindValue(":codice_utente",$utente); |
|
$deleteOthers->execute(); |
|
} |
|
} |
|
} |
|
return true; |
|
} |
|
}
|
|
|