-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRestrictTabView.module.php
More file actions
234 lines (196 loc) · 8.48 KB
/
RestrictTabView.module.php
File metadata and controls
234 lines (196 loc) · 8.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
/**
* ProcessWire module for restricting access to Page Edit tabs via permissions
* by Adrian Jones
*
* Determine how pages are renamed when the title is changed
*
* Copyright (C) 2023 by Adrian Jones
*
*/
class RestrictTabView extends WireData implements Module, ConfigurableModule {
public static function getModuleInfo() {
return array(
'title' => 'Restrict Tab View',
'summary' => 'Restrict access to Page Edit tabs via permissions',
'author' => 'Adrian Jones',
'href' => 'http://modules.processwire.com/modules/restrict-tab-view/',
'version' => '1.3.1',
'autoload' => 'template=admin',
'requires' => 'ProcessWire>=2.5.16',
'icon' => 'toggle-on'
);
}
protected $data = array();
protected $hiddenTabs = array();
/**
* Default configuration for module
*
*/
static public function getDefaultData() {
return array(
"viewTabs" => array(),
"hideTabs" => array(),
"specifiedTemplates" => array(),
"exemptRoles" => array(),
"showNameInContentTab" => false
);
}
/**
* Populate the default config data
*
*/
public function __construct() {
foreach(self::getDefaultData() as $key => $value) {
$this->$key = $value;
}
}
public function init() {
if($this->wire('user')->isSuperuser()) return;
foreach($this->data['exemptRoles'] as $role) {
if($this->wire('user')->hasRole($role)) return;
}
$this->wire()->addHookAfter('ProcessPageEdit::loadPage', function(HookEvent $event) {
$pid = $event->arguments[0];
$this->getHiddenTabs($pid);
});
$this->wire()->addHookBefore('ProcessPageEdit::buildFormContent', $this, "beforeBuildFormContent");
$this->wire()->addHookAfter('ProcessPageEdit::buildForm', $this, "afterBuildForm");
$this->wire()->addHookAfter('ProcessPageEdit::execute', function(HookEvent $event) {
if(!$this->wire('config')->ajax) {
if(in_array('View', $this->hiddenTabs)) {
$event->return .= '
<script>
$(document).ready(function() {
$("#_ProcessPageEditViewDropdown").remove();
});
</script>';
}
}
});
}
protected function afterBuildForm(HookEvent $event) {
foreach($this->hiddenTabs as $tab) {
$this->removeTabs($tab, $event);
}
}
protected function beforeBuildFormContent(HookEvent $event) {
// if settings tab is hidden for this user and name field is not set to be in the content tab, then we need to add it hidden
if(
(in_array("Settings", $this->data['viewTabs']) && !$this->wire('user')->hasPermission("tab-settings-view")) ||
(in_array("Settings", $this->data['hideTabs']) && $this->wire('user')->hasPermission("tab-settings-hide"))
) {
$p = $event->object->getPage();
if(!$this->data['specifiedTemplates'] || in_array($p->template->id, $this->data['specifiedTemplates'])) {
if(!$p->template->nameContentTab) $p->template->nameContentTab = 1;
}
}
}
private function getHiddenTabs($pid) {
$p = $this->wire('pages')->get($pid);
if(!$this->data['specifiedTemplates'] || in_array($p->template->id, $this->data['specifiedTemplates'])) {
foreach($this->data['viewTabs'] as $tab) {
if(!$this->wire('user')->hasPermission("tab-".strtolower($tab)."-view")) {
$this->hiddenTabs[] = $tab;
}
}
foreach($this->data['hideTabs'] as $tab) {
if($this->wire('user')->hasPermission("tab-".strtolower($tab)."-hide")) {
$this->hiddenTabs[] = $tab;
}
}
}
}
private function removeTabs($tab, $event) {
$form = $event->return;
if($tab == "Settings") {
if(!$this->data['showNameInContentTab']) {
$pn = $form->getChildByName('_pw_page_name');
if($pn instanceof Inputfield) {
$pn->wrapAttr('style', 'display:none;');
}
}
}
if($tab == "Settings" || $tab == "Children" || $tab == "Restore" || $tab == "Delete") {
$fieldset = $form->find("id=ProcessPageEdit".$tab)->first();
}
elseif($tab == "View") {
$fieldset = $form->find("id=ProcessPageEdit".$tab);
}
else {
$fieldset = $form->find("id=Inputfield_".$tab)->first();
}
if(!is_object($fieldset)) return;
$form->remove($fieldset);
if($tab == "Settings" || $tab == "Children" || $tab == "Restore" || $tab == "Delete" || $tab == "View") {
$event->object->removeTab("ProcessPageEdit".$tab);
}
else {
$event->object->removeTab("Inputfield_".$tab);
}
}
/**
* Return an InputfieldsWrapper of Inputfields used to configure the class
*
* @param array $data Array of config values indexed by field name
* @return InputfieldsWrapper
*
*/
public function getModuleConfigInputfields(array $data) {
$data = array_merge(self::getDefaultData(), $data);
$wrapper = new InputfieldWrapper();
$f = $this->wire('modules')->get('InputfieldCheckboxes');
$f->attr('name+id', 'viewTabs');
$f->label = __('View Tabs');
$f->description = __("For non-superusers, the selected tabs will not be viewable unless they have a permission named tab-tabname-view, eg: tab-settings-view");
$f->addOption("Children");
$f->addOption("Settings");
$f->addOption("Restore");
$f->addOption("Delete");
$f->addOption("View");
foreach($this->wire('fields')->find("type=FieldtypeFieldsetTabOpen") as $tab) {
$f->addOption($tab->name, ($tab->label ?: $tab->name));
}
if(isset($data['viewTabs'])) $f->value = $data['viewTabs'];
$wrapper->add($f);
$f = $this->wire('modules')->get('InputfieldCheckboxes');
$f->attr('name+id', 'hideTabs');
$f->label = __('Hide Tabs');
$f->description = __("For non-superusers, the selected tabs will be hidden if they have a permission named tab-tabname-hide, eg: tab-settings-hide");
$f->addOption("Children");
$f->addOption("Settings");
$f->addOption("Restore");
$f->addOption("Delete");
$f->addOption("View");
foreach($this->wire('fields')->find("type=FieldtypeFieldsetTabOpen") as $tab) {
$f->addOption($tab->name, ($tab->label ?: $tab->name));
}
if(isset($data['hideTabs'])) $f->value = $data['hideTabs'];
$wrapper->add($f);
$f = $this->wire('modules')->get('InputfieldAsmSelect');
$f->attr('name+id', 'specifiedTemplates');
$f->label = __('Specified Templates');
$f->description = __("If any templates are selected, then only these templates will be affected. If none selected, then all will be affected.");
$f->setAsmSelectOption('sortable', false);
// populate with all available templates
foreach($this->wire('templates') as $t) $f->addOption($t->id,$t->name);
if(isset($data['specifiedTemplates'])) $f->value = $data['specifiedTemplates'];
$wrapper->add($f);
$f = $this->wire('modules')->get('InputfieldAsmSelect');
$f->attr('name+id', 'exemptRoles');
$f->label = __('Exempt Roles');
$f->description = __("Any selected roles will be exempt from all restrictions (same as the superuser is)");
$f->setAsmSelectOption('sortable', false);
// populate with all available templates
foreach($this->wire('roles') as $t) $f->addOption($t->id,$t->name);
if(isset($data['exemptRoles'])) $f->value = $data['exemptRoles'];
$wrapper->add($f);
$f = $this->wire('modules')->get('InputfieldCheckbox');
$f->attr('name+id', 'showNameInContentTab');
$f->label = __('Show page name in content tab');
$f->description = __("If settings tabs is hidden, do you want the name field to be visible in the content tab?");
$f->attr('checked', $data['showNameInContentTab'] == '1' ? 'checked' : '');
$wrapper->add($f);
return $wrapper;
}
}