Skip to content

Commit a304fa0

Browse files
committed
feat: sistema di redirect post-login
1 parent 49c79f6 commit a304fa0

3 files changed

Lines changed: 155 additions & 0 deletions

File tree

core.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,15 @@
183183
Auth::logout();
184184
}
185185

186+
// Memorizza l'URL corrente per il redirect post-login
187+
if (!Auth::check() && !API\Response::isAPIRequest()) {
188+
$current_url = $_SERVER['REQUEST_URI'];
189+
// Evita di memorizzare URL di logout o login per prevenire loop
190+
if (strpos($current_url, 'op=logout') === false && strpos($current_url, 'op=login') === false) {
191+
Auth::setIntended($current_url);
192+
}
193+
}
194+
186195
redirect(base_path().'/index.php');
187196
exit;
188197
}

index.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,39 @@
7272

7373
case 'logout':
7474
Auth::logout();
75+
// Pulisce anche l'intended URL al logout
76+
Auth::clearIntended();
7577

7678
redirect(base_path().'/index.php');
7779
exit;
7880
}
7981

8082
if (Auth::check() && isset($dbo) && $dbo->isConnected() && $dbo->isInstalled()) {
83+
// Priorità 1: Token access (sistema esistente)
8184
if (Permissions::isTokenAccess()) {
8285
if (!empty($_SESSION['token_access']['id_module_target']) && !empty($_SESSION['token_access']['id_record_target'])) {
8386
redirect(base_path().'/shared_editor.php?id_module='.$_SESSION['token_access']['id_module_target'].'&id_record='.$_SESSION['token_access']['id_record_target']);
8487
exit;
8588
}
8689
}
8790

91+
// Priorità 2: Intended URL (nuovo sistema di redirect post-login)
92+
if (Auth::hasIntended()) {
93+
$intended_url = Auth::getIntended();
94+
95+
// Verifica i permessi per l'URL intended
96+
if (Auth::canAccessIntended()) {
97+
Auth::clearIntended();
98+
redirect($intended_url);
99+
exit;
100+
} else {
101+
// L'utente non ha i permessi per accedere alla pagina richiesta
102+
Auth::clearIntended();
103+
flash()->warning(tr('Non hai i permessi necessari per accedere alla pagina richiesta.'));
104+
}
105+
}
106+
107+
// Priorità 3: Primo modulo (sistema esistente come fallback)
88108
$module = Auth::firstModule();
89109

90110
if (!empty($module)) {

src/Auth.php

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -996,4 +996,130 @@ private function checkTokenValidity($token_record)
996996

997997
return $is_not_active;
998998
}
999+
1000+
/**
1001+
* Memorizza l'URL di destinazione nella sessione per il redirect post-login.
1002+
*
1003+
* @param string $url URL di destinazione
1004+
*/
1005+
public function setIntendedUrl($url)
1006+
{
1007+
if ($this->isValidInternalUrl($url)) {
1008+
$_SESSION['intended_url'] = $url;
1009+
}
1010+
}
1011+
1012+
/**
1013+
* Recupera l'URL di destinazione dalla sessione.
1014+
*
1015+
* @return string|null
1016+
*/
1017+
public function getIntendedUrl()
1018+
{
1019+
return $_SESSION['intended_url'] ?? null;
1020+
}
1021+
1022+
/**
1023+
* Verifica se esiste un URL di destinazione memorizzato.
1024+
*
1025+
* @return bool
1026+
*/
1027+
public function hasIntendedUrl()
1028+
{
1029+
return !empty($_SESSION['intended_url']);
1030+
}
1031+
1032+
/**
1033+
* Pulisce l'URL di destinazione dalla sessione.
1034+
*/
1035+
public function clearIntendedUrl()
1036+
{
1037+
unset($_SESSION['intended_url']);
1038+
}
1039+
1040+
/**
1041+
* Verifica se l'utente ha i permessi per accedere all'URL intended.
1042+
*
1043+
* @return bool
1044+
*/
1045+
public function canAccessIntendedUrl()
1046+
{
1047+
if (!$this->hasIntendedUrl()) {
1048+
return false;
1049+
}
1050+
1051+
$url = $this->getIntendedUrl();
1052+
1053+
// Estrae l'id_module dall'URL
1054+
if (preg_match('/[?&]id_module=(\d+)/', $url, $matches)) {
1055+
$id_module = $matches[1];
1056+
1057+
// Verifica i permessi per il modulo
1058+
$permission = \Modules::getPermission($id_module);
1059+
return in_array($permission, ['r', 'rw']);
1060+
}
1061+
1062+
return true; // Per URL senza modulo specifico
1063+
}
1064+
1065+
/**
1066+
* Valida che l'URL sia interno al sistema e sicuro.
1067+
*
1068+
* @param string $url URL da validare
1069+
*
1070+
* @return bool
1071+
*/
1072+
private function isValidInternalUrl($url)
1073+
{
1074+
if (empty($url)) {
1075+
return false;
1076+
}
1077+
1078+
// Verifica che non contenga protocolli pericolosi
1079+
if (strpos($url, 'javascript:') !== false || strpos($url, 'data:') !== false) {
1080+
return false;
1081+
}
1082+
1083+
// Verifica che non sia un URL esterno (con protocollo http/https)
1084+
if (strpos($url, 'http://') === 0 || strpos($url, 'https://') === 0) {
1085+
return false;
1086+
}
1087+
1088+
$base_path = base_path();
1089+
1090+
// L'URL deve iniziare con il base_path del sistema o essere relativo
1091+
if (strpos($url, $base_path) === 0 || strpos($url, '/') === 0) {
1092+
return true;
1093+
}
1094+
1095+
return false;
1096+
}
1097+
1098+
/**
1099+
* Metodi statici per l'accesso ai metodi di intended URL.
1100+
*/
1101+
public static function setIntended($url)
1102+
{
1103+
return self::getInstance()->setIntendedUrl($url);
1104+
}
1105+
1106+
public static function getIntended()
1107+
{
1108+
return self::getInstance()->getIntendedUrl();
1109+
}
1110+
1111+
public static function hasIntended()
1112+
{
1113+
return self::getInstance()->hasIntendedUrl();
1114+
}
1115+
1116+
public static function clearIntended()
1117+
{
1118+
return self::getInstance()->clearIntendedUrl();
1119+
}
1120+
1121+
public static function canAccessIntended()
1122+
{
1123+
return self::getInstance()->canAccessIntendedUrl();
1124+
}
9991125
}

0 commit comments

Comments
 (0)