Skip to content

Commit 2e35811

Browse files
committed
fix: permessi segmenti
1 parent f4abfa2 commit 2e35811

2 files changed

Lines changed: 36 additions & 0 deletions

File tree

modules/utenti/actions.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@
4747
'idmodule' => $id_module_start,
4848
'permessi' => 'r',
4949
]);
50+
51+
// Sincronizza i permessi delle viste e dei segmenti per il modulo di partenza
52+
$group->syncModulePermissions($id_module_start, 'r');
5053
}
5154

5255
flash()->info(tr('Gruppo aggiunto!'));

src/Models/Group.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ public static function getTranslatedFields()
7676
* Sincronizza i permessi delle viste e dei segmenti per un modulo specifico.
7777
* Se il gruppo non ha permessi sul modulo, rimuove i permessi sulle viste e segmenti.
7878
* Se il gruppo ha permessi sul modulo, aggiunge i permessi alle viste predefinite e al segmento predefinito.
79+
* Se non ci sono viste o segmenti predefiniti, aggiunge il gruppo ad almeno uno di essi.
7980
* Gli Amministratori mantengono sempre accesso completo.
8081
*
8182
* @param int $id_module ID del modulo
@@ -109,6 +110,22 @@ public function syncModulePermissions($id_module, $permessi)
109110
}
110111
}
111112

113+
// Se non ci sono viste predefinite, aggiungi il gruppo ad almeno una vista (la prima disponibile)
114+
if (empty($default_views)) {
115+
$first_view = $database->fetchArray('SELECT `id` FROM `zz_views` WHERE `id_module`='.prepare($id_module).' ORDER BY `order` ASC LIMIT 1');
116+
117+
if (!empty($first_view)) {
118+
$has_view_access = $database->fetchArray('SELECT COUNT(*) as cont FROM `zz_group_view` WHERE `id_gruppo` = '.prepare($this->id).' AND `id_vista` = '.prepare($first_view[0]['id']))['cont'];
119+
120+
if ($has_view_access == 0) {
121+
$database->insert('zz_group_view', [
122+
'id_gruppo' => $this->id,
123+
'id_vista' => $first_view[0]['id'],
124+
]);
125+
}
126+
}
127+
}
128+
112129
// Aggiungi accesso ai segmenti predefiniti se non ce l'ha già
113130
$default_segments = $database->fetchArray('SELECT `id` FROM `zz_segments` WHERE `id_module`='.prepare($id_module).' AND `predefined` = 1');
114131

@@ -123,6 +140,22 @@ public function syncModulePermissions($id_module, $permessi)
123140
]);
124141
}
125142
}
143+
144+
// Se non ci sono segmenti predefiniti, aggiungi il gruppo ad almeno un segmento (il primo disponibile)
145+
if (empty($default_segments)) {
146+
$first_segment = $database->fetchArray('SELECT `id` FROM `zz_segments` WHERE `id_module`='.prepare($id_module).' ORDER BY `id` ASC LIMIT 1');
147+
148+
if (!empty($first_segment)) {
149+
$has_segment_access = $database->fetchArray('SELECT COUNT(*) as cont FROM `zz_group_segment` WHERE `id_gruppo` = '.prepare($this->id).' AND `id_segment` = '.prepare($first_segment[0]['id']))['cont'];
150+
151+
if ($has_segment_access == 0) {
152+
$database->insert('zz_group_segment', [
153+
'id_gruppo' => $this->id,
154+
'id_segment' => $first_segment[0]['id'],
155+
]);
156+
}
157+
}
158+
}
126159
} else {
127160
// Se i permessi sono stati rimossi, rimuovi tutti i permessi su viste e segmenti del modulo
128161
$database->query('DELETE FROM `zz_group_view` WHERE `id_gruppo` = '.prepare($this->id).' AND `id_vista` IN (SELECT `id` FROM `zz_views` WHERE `id_module`='.prepare($id_module).')');

0 commit comments

Comments
 (0)