Browse code

Add popup user privileges

Benjamin Roth authored on12/01/2023 22:23:51
Showing7 changed files
... ...
@@ -19,3 +19,6 @@ $GLOBALS['BE_MOD']['content']['modals'] = array
19 19
 
20 20
 
21 21
 $GLOBALS['TL_MODELS']['tl_vr_modal'] = ModalModel::class;
22
+
23
+// Add permissions
24
+$GLOBALS['TL_PERMISSIONS'][] = 'modalp';
22 25
new file mode 100644
... ...
@@ -0,0 +1,32 @@
1
+<?php
2
+
3
+declare(strict_types=1);
4
+
5
+/*
6
+ * This file is part of modal bundle for Contao.
7
+ *
8
+ * (c) Benjamin Roth
9
+ *
10
+ * @license LGPL-3.0-or-later
11
+ */
12
+
13
+use Contao\CoreBundle\DataContainer\PaletteManipulator;
14
+
15
+// Extend the default palette
16
+PaletteManipulator::create()
17
+    ->addLegend('modal_legend', 'amg_legend', PaletteManipulator::POSITION_BEFORE)
18
+    ->addField(array('modalp'), 'modal_legend', PaletteManipulator::POSITION_APPEND)
19
+    ->applyToPalette('extend', 'tl_user')
20
+    ->applyToPalette('custom', 'tl_user')
21
+;
22
+
23
+// Add fields to tl_user_group
24
+$GLOBALS['TL_DCA']['tl_user']['fields']['modalp'] = array
25
+(
26
+    'exclude'                 => true,
27
+    'inputType'               => 'checkbox',
28
+    'options'                 => array('create', 'delete'),
29
+    'reference'               => &$GLOBALS['TL_LANG']['MSC'],
30
+    'eval'                    => array('multiple'=>true),
31
+    'sql'                     => "blob NULL"
32
+);
0 33
new file mode 100644
... ...
@@ -0,0 +1,31 @@
1
+<?php
2
+
3
+declare(strict_types=1);
4
+
5
+/*
6
+ * This file is part of modal bundle for Contao.
7
+ *
8
+ * (c) Benjamin Roth
9
+ *
10
+ * @license LGPL-3.0-or-later
11
+ */
12
+
13
+use Contao\CoreBundle\DataContainer\PaletteManipulator;
14
+
15
+// Extend the default palette
16
+PaletteManipulator::create()
17
+    ->addLegend('modal_legend', 'amg_legend', PaletteManipulator::POSITION_BEFORE)
18
+    ->addField(array('modalp'), 'modal_legend', PaletteManipulator::POSITION_APPEND)
19
+    ->applyToPalette('default', 'tl_user_group')
20
+;
21
+
22
+// Add fields to tl_user_group
23
+$GLOBALS['TL_DCA']['tl_user_group']['fields']['modalp'] = array
24
+(
25
+    'exclude'                 => true,
26
+    'inputType'               => 'checkbox',
27
+    'options'                 => array('create', 'delete'),
28
+    'reference'               => &$GLOBALS['TL_LANG']['MSC'],
29
+    'eval'                    => array('multiple'=>true),
30
+    'sql'                     => "blob NULL"
31
+);
0 32
new file mode 100644
... ...
@@ -0,0 +1,19 @@
1
+<?php
2
+
3
+declare(strict_types=1);
4
+
5
+/*
6
+ * This file is part of modal bundle for Contao.
7
+ *
8
+ * (c) Benjamin Roth
9
+ *
10
+ * @license LGPL-3.0-or-later
11
+ */
12
+
13
+use Contao\DC_Table;
14
+use Contao\DataContainer;
15
+
16
+$GLOBALS['TL_LANG']['tl_user']['modalp'][0] = 'PopUp-Rechte';
17
+$GLOBALS['TL_LANG']['tl_user']['modalp'][1] = 'Hier können Sie PopUp-Rechte festlegen.';
18
+
19
+$GLOBALS['TL_LANG']['tl_user']['modal_legend'] = 'PopUp-Rechte';
0 20
new file mode 100644
... ...
@@ -0,0 +1,19 @@
1
+<?php
2
+
3
+declare(strict_types=1);
4
+
5
+/*
6
+ * This file is part of modal bundle for Contao.
7
+ *
8
+ * (c) Benjamin Roth
9
+ *
10
+ * @license LGPL-3.0-or-later
11
+ */
12
+
13
+use Contao\DC_Table;
14
+use Contao\DataContainer;
15
+
16
+$GLOBALS['TL_LANG']['tl_user_group']['modalp'][0] = 'PopUp-Rechte';
17
+$GLOBALS['TL_LANG']['tl_user_group']['modalp'][1] = 'Hier können Sie PopUp-Rechte festlegen.';
18
+
19
+$GLOBALS['TL_LANG']['tl_user_group']['modal_legend'] = 'PopUp-Rechte';
0 20
new file mode 100644
... ...
@@ -0,0 +1,132 @@
1
+<?php
2
+
3
+declare(strict_types=1);
4
+
5
+/*
6
+ * This file is part of modal bundle for Contao.
7
+ *
8
+ * (c) Benjamin Roth
9
+ *
10
+ * @license LGPL-3.0-or-later
11
+ */
12
+
13
+namespace vonRotenberg\ModalBundle\EventListener\DataContainer;
14
+
15
+use Contao\Backend;
16
+use Contao\BackendUser;
17
+use Contao\CoreBundle\Exception\AccessDeniedException;
18
+use Contao\CoreBundle\ServiceAnnotation\Callback;
19
+use Contao\DataContainer;
20
+use Contao\Image;
21
+use Contao\Input;
22
+use Contao\StringUtil;
23
+use Symfony\Component\HttpFoundation\Session\SessionInterface;
24
+use Symfony\Component\Security\Core\Security;
25
+use vonRotenberg\ModalBundle\Security\ModalPermissions;
26
+
27
+class ModalDataContainerListener
28
+{
29
+    private Security $security;
30
+    private SessionInterface $session;
31
+
32
+    public function __construct(Security $security, SessionInterface $session)
33
+    {
34
+        $this->security = $security;
35
+        $this->session = $session;
36
+    }
37
+
38
+    /**
39
+     * @Callback(table="tl_vr_modal", target="config.onload")
40
+     */
41
+    public function checkPermission(DataContainer $dc = null): void
42
+    {
43
+        $user = $this->security->getUser();
44
+        $userId = $user instanceof BackendUser ? (int)$user->id : 0;
45
+
46
+        if ($user->isAdmin)
47
+        {
48
+            return;
49
+        }
50
+
51
+        // Check permissions to add modals
52
+        if (!$this->security->isGranted(ModalPermissions::USER_CAN_CREATE_MODALS))
53
+        {
54
+            $GLOBALS['TL_DCA']['tl_vr_modal']['config']['closed'] = true;
55
+            $GLOBALS['TL_DCA']['tl_vr_modal']['config']['notCreatable'] = true;
56
+            $GLOBALS['TL_DCA']['tl_vr_modal']['config']['notCopyable'] = true;
57
+        }
58
+
59
+        // Check permissions to delete modals
60
+        if (!$this->security->isGranted(ModalPermissions::USER_CAN_DELETE_MODALS))
61
+        {
62
+            $GLOBALS['TL_DCA']['tl_vr_modal']['config']['notDeletable'] = true;
63
+        }
64
+
65
+        // Check current action
66
+        switch (Input::get('act'))
67
+        {
68
+            case 'overrideAll':
69
+            case 'editAll':
70
+            case 'show':
71
+            case 'edit':
72
+            case 'select':
73
+                // Allow
74
+                break;
75
+
76
+            case 'copy':
77
+            case 'create':
78
+                if (!$this->security->isGranted(ModalPermissions::USER_CAN_CREATE_MODALS))
79
+                {
80
+                    throw new AccessDeniedException('Not enough permissions to ' . Input::get('act') . ' modals.');
81
+                }
82
+                break;
83
+
84
+            case 'delete':
85
+                if (!$this->security->isGranted(ModalPermissions::USER_CAN_DELETE_MODALS))
86
+                {
87
+                    throw new AccessDeniedException('Not enough permissions to ' . Input::get('act') . ' modal ID ' . Input::get('id') . '.');
88
+                }
89
+                break;
90
+
91
+            case 'deleteAll':
92
+            case 'copyAll':
93
+                $session = $this->session->all();
94
+
95
+                if (Input::get('act') == 'deleteAll' && !$this->security->isGranted(ModalPermissions::USER_CAN_DELETE_MODALS))
96
+                {
97
+                    $session['CURRENT']['IDS'] = array();
98
+                } else
99
+                {
100
+                    if (Input::get('act') == 'copyAll' && !$this->security->isGranted(ModalPermissions::USER_CAN_CREATE_MODALS))
101
+                    {
102
+                        $session['CURRENT']['IDS'] = array();
103
+                    }
104
+                }
105
+                $this->session->replace($session);
106
+                break;
107
+
108
+            default:
109
+                if (Input::get('act'))
110
+                {
111
+                    throw new AccessDeniedException('Not enough permissions to ' . Input::get('act') . ' modals.');
112
+                }
113
+                break;
114
+        }
115
+    }
116
+
117
+    /**
118
+     * @Callback(table="tl_vr_modal", target="list.operations.copy.button")
119
+     */
120
+    public function copyModal($row, $href, $label, $title, $icon, $attributes): string
121
+    {
122
+        return $this->security->isGranted(ModalPermissions::USER_CAN_CREATE_MODALS) ? '<a href="' . Backend::addToUrl($href . '&amp;id=' . $row['id']) . '" title="' . StringUtil::specialchars($title) . '"' . $attributes . '>' . Image::getHtml($icon, $label) . '</a> ' : Image::getHtml(preg_replace('/\.svg$/i', '_.svg', $icon)) . ' ';
123
+    }
124
+
125
+    /**
126
+     * @Callback(table="tl_vr_modal", target="list.operations.delete.button")
127
+     */
128
+    public function deleteModal($row, $href, $label, $title, $icon, $attributes): string
129
+    {
130
+        return $this->security->isGranted(ModalPermissions::USER_CAN_DELETE_MODALS) ? '<a href="' . Backend::addToUrl($href . '&amp;id=' . $row['id']) . '" title="' . StringUtil::specialchars($title) . '"' . $attributes . '>' . Image::getHtml($icon, $label) . '</a> ' : Image::getHtml(preg_replace('/\.svg$/i', '_.svg', $icon)) . ' ';
131
+    }
132
+}
0 133
new file mode 100644
... ...
@@ -0,0 +1,19 @@
1
+<?php
2
+
3
+declare(strict_types=1);
4
+
5
+/*
6
+ * This file is part of modal bundle for Contao.
7
+ *
8
+ * (c) Benjamin Roth
9
+ *
10
+ * @license LGPL-3.0-or-later
11
+ */
12
+
13
+namespace vonRotenberg\ModalBundle\Security;
14
+
15
+final class ModalPermissions
16
+{
17
+    public const USER_CAN_CREATE_MODALS = 'contao_user.modalp.create';
18
+    public const USER_CAN_DELETE_MODALS = 'contao_user.modalp.delete';
19
+}