1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\SiteConfig; |
4
|
|
|
|
5
|
|
|
use SilverStripe\CMS\Model\SiteTree; |
6
|
|
|
use SilverStripe\Forms\FieldList; |
7
|
|
|
use SilverStripe\Forms\FormAction; |
8
|
|
|
use SilverStripe\Forms\HiddenField; |
9
|
|
|
use SilverStripe\Forms\ListboxField; |
10
|
|
|
use SilverStripe\Forms\LiteralField; |
11
|
|
|
use SilverStripe\Forms\OptionsetField; |
12
|
|
|
use SilverStripe\Forms\Tab; |
13
|
|
|
use SilverStripe\Forms\TabSet; |
14
|
|
|
use SilverStripe\Forms\TextField; |
15
|
|
|
use SilverStripe\ORM\DB; |
16
|
|
|
use SilverStripe\ORM\DataObject; |
17
|
|
|
use SilverStripe\ORM\ManyManyList; |
18
|
|
|
use SilverStripe\Security\Group; |
19
|
|
|
use SilverStripe\Security\Member; |
20
|
|
|
use SilverStripe\Security\Permission; |
21
|
|
|
use SilverStripe\Security\PermissionProvider; |
22
|
|
|
use SilverStripe\Security\Security; |
23
|
|
|
use SilverStripe\View\TemplateGlobalProvider; |
24
|
|
|
use SilverStripe\CMS\Controllers\CMSMain; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* SiteConfig |
28
|
|
|
* |
29
|
|
|
* @property string Title Title of the website. |
30
|
|
|
* @property string Tagline Tagline of the website. |
31
|
|
|
* @property string CanViewType Type of restriction used for view permissions. |
32
|
|
|
* @property string CanEditType Type of restriction used for edit permissions. |
33
|
|
|
* @property string CanCreateTopLevelType Type of restriction used for creation of root-level pages. |
34
|
|
|
* @method ManyManyList ViewerGroups() List of groups that can view SiteConfig. |
35
|
|
|
* @method ManyManyList EditorGroups() List of groups that can edit SiteConfig. |
36
|
|
|
* @method ManyManyList CreateTopLevelGroups() List of groups that can create root-level pages. |
37
|
|
|
*/ |
38
|
|
|
class SiteConfig extends DataObject implements PermissionProvider, TemplateGlobalProvider |
39
|
|
|
{ |
40
|
|
|
private static $db = [ |
|
|
|
|
41
|
|
|
"Title" => "Varchar(255)", |
42
|
|
|
"Tagline" => "Varchar(255)", |
43
|
|
|
"CanViewType" => "Enum('Anyone, LoggedInUsers, OnlyTheseUsers', 'Anyone')", |
44
|
|
|
"CanEditType" => "Enum('LoggedInUsers, OnlyTheseUsers', 'LoggedInUsers')", |
45
|
|
|
"CanCreateTopLevelType" => "Enum('LoggedInUsers, OnlyTheseUsers', 'LoggedInUsers')", |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
private static $many_many = [ |
|
|
|
|
49
|
|
|
"ViewerGroups" => Group::class, |
50
|
|
|
"EditorGroups" => Group::class, |
51
|
|
|
"CreateTopLevelGroups" => Group::class, |
52
|
|
|
]; |
53
|
|
|
|
54
|
|
|
private static $defaults = [ |
|
|
|
|
55
|
|
|
"CanViewType" => "Anyone", |
56
|
|
|
"CanEditType" => "LoggedInUsers", |
57
|
|
|
"CanCreateTopLevelType" => "LoggedInUsers", |
58
|
|
|
]; |
59
|
|
|
|
60
|
|
|
private static $table_name = 'SiteConfig'; |
|
|
|
|
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Default permission to check for 'LoggedInUsers' to create or edit pages |
64
|
|
|
* |
65
|
|
|
* @var array |
66
|
|
|
* @config |
67
|
|
|
*/ |
68
|
|
|
private static $required_permission = [ |
|
|
|
|
69
|
|
|
'CMS_ACCESS_CMSMain', |
70
|
|
|
'CMS_ACCESS_LeftAndMain' |
71
|
|
|
]; |
72
|
|
|
|
73
|
|
|
public function populateDefaults() |
74
|
|
|
{ |
75
|
|
|
$this->Title = _t(self::class . '.SITENAMEDEFAULT', "Your Site Name"); |
76
|
|
|
$this->Tagline = _t(self::class . '.TAGLINEDEFAULT', "your tagline here"); |
77
|
|
|
|
78
|
|
|
// Allow these defaults to be overridden |
79
|
|
|
parent::populateDefaults(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get the fields that are sent to the CMS. |
84
|
|
|
* |
85
|
|
|
* In your extensions: updateCMSFields($fields). |
86
|
|
|
* |
87
|
|
|
* @return FieldList |
88
|
|
|
*/ |
89
|
|
|
public function getCMSFields() |
90
|
|
|
{ |
91
|
|
|
$mapFn = function ($groups = []) { |
92
|
|
|
$map = []; |
93
|
|
|
foreach ($groups as $group) { |
94
|
|
|
// Listboxfield values are escaped, use ASCII char instead of » |
95
|
|
|
$map[$group->ID] = $group->getBreadcrumbs(' > '); |
96
|
|
|
} |
97
|
|
|
asort($map); |
98
|
|
|
return $map; |
99
|
|
|
}; |
100
|
|
|
$groupsMap = $mapFn(Group::get()); |
101
|
|
|
$viewAllGroupsMap = $mapFn(Permission::get_groups_by_permission(['SITETREE_VIEW_ALL', 'ADMIN'])); |
102
|
|
|
$editAllGroupsMap = $mapFn(Permission::get_groups_by_permission(['SITETREE_EDIT_ALL', 'ADMIN'])); |
103
|
|
|
|
104
|
|
|
$fields = new FieldList( |
105
|
|
|
new TabSet( |
106
|
|
|
"Root", |
107
|
|
|
$tabMain = new Tab( |
108
|
|
|
'Main', |
109
|
|
|
$titleField = new TextField("Title", _t(self::class . '.SITETITLE', "Site title")), |
110
|
|
|
$taglineField = new TextField("Tagline", _t(self::class . '.SITETAGLINE', "Site Tagline/Slogan")) |
111
|
|
|
), |
112
|
|
|
$tabAccess = new Tab( |
113
|
|
|
'Access', |
114
|
|
|
$viewersOptionsField = new OptionsetField( |
115
|
|
|
"CanViewType", |
116
|
|
|
_t(self::class . '.VIEWHEADER', "Who can view pages on this site?") |
117
|
|
|
), |
118
|
|
|
$viewerGroupsField = ListboxField::create( |
119
|
|
|
"ViewerGroups", |
120
|
|
|
_t(SiteTree::class . '.VIEWERGROUPS', "Viewer Groups") |
121
|
|
|
) |
122
|
|
|
->setSource($groupsMap) |
123
|
|
|
->setAttribute( |
124
|
|
|
'data-placeholder', |
125
|
|
|
_t(SiteTree::class . '.GroupPlaceholder', 'Click to select group') |
126
|
|
|
), |
127
|
|
|
$editorsOptionsField = new OptionsetField( |
128
|
|
|
"CanEditType", |
129
|
|
|
_t(self::class . '.EDITHEADER', "Who can edit pages on this site?") |
130
|
|
|
), |
131
|
|
|
$editorGroupsField = ListboxField::create( |
132
|
|
|
"EditorGroups", |
133
|
|
|
_t(SiteTree::class . '.EDITORGROUPS', "Editor Groups") |
134
|
|
|
) |
135
|
|
|
->setSource($groupsMap) |
136
|
|
|
->setAttribute( |
137
|
|
|
'data-placeholder', |
138
|
|
|
_t(SiteTree::class . '.GroupPlaceholder', 'Click to select group') |
139
|
|
|
), |
140
|
|
|
$topLevelCreatorsOptionsField = new OptionsetField( |
141
|
|
|
"CanCreateTopLevelType", |
142
|
|
|
_t(self::class . '.TOPLEVELCREATE', "Who can create pages in the root of the site?") |
143
|
|
|
), |
144
|
|
|
$topLevelCreatorsGroupsField = ListboxField::create( |
145
|
|
|
"CreateTopLevelGroups", |
146
|
|
|
_t(self::class . '.TOPLEVELCREATORGROUPS', "Top level creators") |
147
|
|
|
) |
148
|
|
|
->setSource($groupsMap) |
149
|
|
|
->setAttribute( |
150
|
|
|
'data-placeholder', |
151
|
|
|
_t(SiteTree::class . '.GroupPlaceholder', 'Click to select group') |
152
|
|
|
) |
153
|
|
|
) |
154
|
|
|
), |
155
|
|
|
new HiddenField('ID') |
156
|
|
|
); |
157
|
|
|
|
158
|
|
|
$viewersOptionsSource = []; |
159
|
|
|
$viewersOptionsSource["Anyone"] = _t(SiteTree::class . '.ACCESSANYONE', "Anyone"); |
160
|
|
|
$viewersOptionsSource["LoggedInUsers"] = _t( |
161
|
|
|
SiteTree::class . '.ACCESSLOGGEDIN', |
162
|
|
|
"Logged-in users" |
163
|
|
|
); |
164
|
|
|
$viewersOptionsSource["OnlyTheseUsers"] = _t( |
165
|
|
|
SiteTree::class . '.ACCESSONLYTHESE', |
166
|
|
|
"Only these groups (choose from list)" |
167
|
|
|
); |
168
|
|
|
$viewersOptionsField->setSource($viewersOptionsSource); |
169
|
|
|
|
170
|
|
View Code Duplication |
if ($viewAllGroupsMap) { |
|
|
|
|
171
|
|
|
$viewerGroupsField->setDescription(_t( |
172
|
|
|
SiteTree::class . '.VIEWER_GROUPS_FIELD_DESC', |
173
|
|
|
'Groups with global view permissions: {groupList}', |
174
|
|
|
['groupList' => implode(', ', array_values($viewAllGroupsMap))] |
175
|
|
|
)); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
View Code Duplication |
if ($editAllGroupsMap) { |
|
|
|
|
179
|
|
|
$editorGroupsField->setDescription(_t( |
180
|
|
|
SiteTree::class . '.EDITOR_GROUPS_FIELD_DESC', |
181
|
|
|
'Groups with global edit permissions: {groupList}', |
182
|
|
|
['groupList' => implode(', ', array_values($editAllGroupsMap))] |
183
|
|
|
)); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
$editorsOptionsSource = []; |
187
|
|
|
$editorsOptionsSource["LoggedInUsers"] = _t( |
188
|
|
|
SiteTree::class . '.EDITANYONE', |
189
|
|
|
"Anyone who can log-in to the CMS" |
190
|
|
|
); |
191
|
|
|
$editorsOptionsSource["OnlyTheseUsers"] = _t( |
192
|
|
|
SiteTree::class . '.EDITONLYTHESE', |
193
|
|
|
"Only these groups (choose from list)" |
194
|
|
|
); |
195
|
|
|
$editorsOptionsField->setSource($editorsOptionsSource); |
196
|
|
|
|
197
|
|
|
$topLevelCreatorsOptionsField->setSource($editorsOptionsSource); |
198
|
|
|
|
199
|
|
|
if (!Permission::check('EDIT_SITECONFIG')) { |
200
|
|
|
$fields->makeFieldReadonly($viewersOptionsField); |
201
|
|
|
$fields->makeFieldReadonly($viewerGroupsField); |
202
|
|
|
$fields->makeFieldReadonly($editorsOptionsField); |
203
|
|
|
$fields->makeFieldReadonly($editorGroupsField); |
204
|
|
|
$fields->makeFieldReadonly($topLevelCreatorsOptionsField); |
205
|
|
|
$fields->makeFieldReadonly($topLevelCreatorsGroupsField); |
206
|
|
|
$fields->makeFieldReadonly($taglineField); |
207
|
|
|
$fields->makeFieldReadonly($titleField); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
if (file_exists(BASE_PATH . '/install.php')) { |
211
|
|
|
$fields->addFieldToTab( |
212
|
|
|
"Root.Main", |
213
|
|
|
new LiteralField( |
214
|
|
|
"InstallWarningHeader", |
215
|
|
|
"<p class=\"message warning\">" . _t( |
216
|
|
|
SiteTree::class . 'REMOVE_INSTALL_WARNING', |
217
|
|
|
'Warning: You should remove install.php from this SilverStripe install for security reasons.' |
218
|
|
|
) . "</p>" |
219
|
|
|
), |
220
|
|
|
"Title" |
221
|
|
|
); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
$tabMain->setTitle(_t(self::class . '.TABMAIN', "Main")); |
225
|
|
|
$tabAccess->setTitle(_t(self::class . '.TABACCESS', "Access")); |
226
|
|
|
$this->extend('updateCMSFields', $fields); |
227
|
|
|
|
228
|
|
|
return $fields; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Get the actions that are sent to the CMS. |
233
|
|
|
* |
234
|
|
|
* In your extensions: updateEditFormActions($actions) |
235
|
|
|
* |
236
|
|
|
* @return FieldList |
237
|
|
|
*/ |
238
|
|
|
public function getCMSActions() |
239
|
|
|
{ |
240
|
|
|
if (Permission::check('ADMIN') || Permission::check('EDIT_SITECONFIG')) { |
241
|
|
|
$actions = new FieldList( |
242
|
|
|
FormAction::create( |
243
|
|
|
'save_siteconfig', |
244
|
|
|
_t(CMSMain::class . '.SAVE', 'Save') |
245
|
|
|
)->addExtraClass('btn-primary font-icon-save') |
246
|
|
|
); |
247
|
|
|
} else { |
248
|
|
|
$actions = new FieldList(); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
$this->extend('updateCMSActions', $actions); |
252
|
|
|
|
253
|
|
|
return $actions; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* @return string |
258
|
|
|
*/ |
259
|
|
|
public function CMSEditLink() |
260
|
|
|
{ |
261
|
|
|
return SiteConfigLeftAndMain::singleton()->Link(); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Get the current sites SiteConfig, and creates a new one through |
266
|
|
|
* {@link make_site_config()} if none is found. |
267
|
|
|
* |
268
|
|
|
* @return SiteConfig |
269
|
|
|
*/ |
270
|
|
|
public static function current_site_config() |
271
|
|
|
{ |
272
|
|
|
/** @var SiteConfig $siteConfig */ |
273
|
|
|
$siteConfig = DataObject::get_one(SiteConfig::class); |
274
|
|
|
if ($siteConfig) { |
275
|
|
|
return $siteConfig; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
return self::make_site_config(); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Setup a default SiteConfig record if none exists. |
283
|
|
|
*/ |
284
|
|
|
public function requireDefaultRecords() |
285
|
|
|
{ |
286
|
|
|
parent::requireDefaultRecords(); |
287
|
|
|
|
288
|
|
|
$config = DataObject::get_one(SiteConfig::class); |
289
|
|
|
|
290
|
|
|
if (!$config) { |
291
|
|
|
self::make_site_config(); |
292
|
|
|
|
293
|
|
|
DB::alteration_message("Added default site config", "created"); |
294
|
|
|
} |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Create SiteConfig with defaults from language file. |
299
|
|
|
* |
300
|
|
|
* @return SiteConfig |
301
|
|
|
*/ |
302
|
|
|
public static function make_site_config() |
303
|
|
|
{ |
304
|
|
|
$config = SiteConfig::create(); |
305
|
|
|
$config->write(); |
306
|
|
|
|
307
|
|
|
return $config; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Can a user view this SiteConfig instance? |
312
|
|
|
* |
313
|
|
|
* @param Member $member |
314
|
|
|
* @return boolean |
315
|
|
|
*/ |
316
|
|
View Code Duplication |
public function canView($member = null) |
|
|
|
|
317
|
|
|
{ |
318
|
|
|
if (!$member) { |
319
|
|
|
$member = Security::getCurrentUser(); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
$extended = $this->extendedCan('canView', $member); |
|
|
|
|
323
|
|
|
if ($extended !== null) { |
324
|
|
|
return $extended; |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
// Assuming all that can edit this object can also view it |
328
|
|
|
return $this->canEdit($member); |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* Can a user view pages on this site? This method is only |
333
|
|
|
* called if a page is set to Inherit, but there is nothing |
334
|
|
|
* to inherit from. |
335
|
|
|
* |
336
|
|
|
* @param Member $member |
337
|
|
|
* @return boolean |
338
|
|
|
*/ |
339
|
|
|
public function canViewPages($member = null) |
340
|
|
|
{ |
341
|
|
|
if (!$member) { |
342
|
|
|
$member = Security::getCurrentUser(); |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
if ($member && Permission::checkMember($member, "ADMIN")) { |
346
|
|
|
return true; |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
$extended = $this->extendedCan('canViewPages', $member); |
|
|
|
|
350
|
|
|
if ($extended !== null) { |
351
|
|
|
return $extended; |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
if (!$this->CanViewType || $this->CanViewType == 'Anyone') { |
355
|
|
|
return true; |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
// check for any logged-in users |
359
|
|
|
if ($this->CanViewType === 'LoggedInUsers' && $member) { |
360
|
|
|
return true; |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
// check for specific groups |
364
|
|
|
if ($this->CanViewType === 'OnlyTheseUsers' && $member && $member->inGroups($this->ViewerGroups())) { |
365
|
|
|
return true; |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
return false; |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
/** |
372
|
|
|
* Can a user edit pages on this site? This method is only |
373
|
|
|
* called if a page is set to Inherit, but there is nothing |
374
|
|
|
* to inherit from, or on new records without a parent. |
375
|
|
|
* |
376
|
|
|
* @param Member $member |
377
|
|
|
* @return boolean |
378
|
|
|
*/ |
379
|
|
View Code Duplication |
public function canEditPages($member = null) |
|
|
|
|
380
|
|
|
{ |
381
|
|
|
if (!$member) { |
382
|
|
|
$member = Security::getCurrentUser(); |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
if ($member && Permission::checkMember($member, "ADMIN")) { |
386
|
|
|
return true; |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
$extended = $this->extendedCan('canEditPages', $member); |
|
|
|
|
390
|
|
|
if ($extended !== null) { |
391
|
|
|
return $extended; |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
// check for any logged-in users with CMS access |
395
|
|
|
if ($this->CanEditType === 'LoggedInUsers' |
396
|
|
|
&& Permission::checkMember($member, $this->config()->get('required_permission')) |
397
|
|
|
) { |
398
|
|
|
return true; |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
// check for specific groups |
402
|
|
|
if ($this->CanEditType === 'OnlyTheseUsers' && $member && $member->inGroups($this->EditorGroups())) { |
403
|
|
|
return true; |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
return false; |
407
|
|
|
} |
408
|
|
|
|
409
|
|
View Code Duplication |
public function canEdit($member = null) |
|
|
|
|
410
|
|
|
{ |
411
|
|
|
if (!$member) { |
412
|
|
|
$member = Security::getCurrentUser(); |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
$extended = $this->extendedCan('canEdit', $member); |
416
|
|
|
if ($extended !== null) { |
417
|
|
|
return $extended; |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
return Permission::checkMember($member, "EDIT_SITECONFIG"); |
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
/** |
424
|
|
|
* @return array |
425
|
|
|
*/ |
426
|
|
|
public function providePermissions() |
427
|
|
|
{ |
428
|
|
|
return [ |
429
|
|
|
'EDIT_SITECONFIG' => [ |
430
|
|
|
'name' => _t(self::class . '.EDIT_PERMISSION', 'Manage site configuration'), |
431
|
|
|
'category' => _t(Permission::class . '.PERMISSIONS_CATEGORY', 'Roles and access permissions'), |
432
|
|
|
'help' => _t( |
433
|
|
|
self::class . '.EDIT_PERMISSION_HELP', |
434
|
|
|
'Ability to edit global access settings/top-level page permissions.' |
435
|
|
|
), |
436
|
|
|
'sort' => 400 |
437
|
|
|
] |
438
|
|
|
]; |
439
|
|
|
} |
440
|
|
|
|
441
|
|
|
/** |
442
|
|
|
* Can a user create pages in the root of this site? |
443
|
|
|
* |
444
|
|
|
* @param Member $member |
445
|
|
|
* @return boolean |
446
|
|
|
*/ |
447
|
|
View Code Duplication |
public function canCreateTopLevel($member = null) |
|
|
|
|
448
|
|
|
{ |
449
|
|
|
if (!$member) { |
450
|
|
|
$member = Security::getCurrentUser(); |
451
|
|
|
} |
452
|
|
|
|
453
|
|
|
if ($member && Permission::checkMember($member, "ADMIN")) { |
454
|
|
|
return true; |
455
|
|
|
} |
456
|
|
|
|
457
|
|
|
$extended = $this->extendedCan('canCreateTopLevel', $member); |
|
|
|
|
458
|
|
|
if ($extended !== null) { |
459
|
|
|
return $extended; |
460
|
|
|
} |
461
|
|
|
|
462
|
|
|
// check for any logged-in users with CMS permission |
463
|
|
|
if ($this->CanCreateTopLevelType === 'LoggedInUsers' |
464
|
|
|
&& Permission::checkMember($member, $this->config()->get('required_permission')) |
465
|
|
|
) { |
466
|
|
|
return true; |
467
|
|
|
} |
468
|
|
|
|
469
|
|
|
// check for specific groups |
470
|
|
|
if ($this->CanCreateTopLevelType === 'OnlyTheseUsers' |
471
|
|
|
&& $member |
472
|
|
|
&& $member->inGroups($this->CreateTopLevelGroups()) |
473
|
|
|
) { |
474
|
|
|
return true; |
475
|
|
|
} |
476
|
|
|
|
477
|
|
|
return false; |
478
|
|
|
} |
479
|
|
|
|
480
|
|
|
/** |
481
|
|
|
* Add $SiteConfig to all SSViewers |
482
|
|
|
*/ |
483
|
|
|
public static function get_template_global_variables() |
484
|
|
|
{ |
485
|
|
|
return [ |
486
|
|
|
'SiteConfig' => 'current_site_config', |
487
|
|
|
]; |
488
|
|
|
} |
489
|
|
|
} |
490
|
|
|
|