|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LeKoala\Admini; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\ORM\DB; |
|
6
|
|
|
use SilverStripe\Forms\Form; |
|
7
|
|
|
use SilverStripe\Forms\TabSet; |
|
8
|
|
|
use SilverStripe\Security\Group; |
|
9
|
|
|
use SilverStripe\View\ArrayData; |
|
10
|
|
|
use SilverStripe\Forms\FieldList; |
|
11
|
|
|
use SilverStripe\Security\Member; |
|
12
|
|
|
use LeKoala\Tabulator\TabulatorGrid; |
|
13
|
|
|
use SilverStripe\Security\Permission; |
|
14
|
|
|
use SilverStripe\Security\PermissionRole; |
|
15
|
|
|
use SilverStripe\Security\PermissionProvider; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Security section of the CMS |
|
19
|
|
|
*/ |
|
20
|
|
|
class SecurityAdmin extends LeftAndMain implements PermissionProvider |
|
21
|
|
|
{ |
|
22
|
|
|
|
|
23
|
|
|
private static $url_segment = 'security'; |
|
24
|
|
|
|
|
25
|
|
|
private static $url_rule = '/$Action/$ID/$OtherID'; |
|
26
|
|
|
|
|
27
|
|
|
private static $menu_title = 'Security'; |
|
28
|
|
|
|
|
29
|
|
|
private static $tree_class = Group::class; |
|
30
|
|
|
|
|
31
|
|
|
private static $subitem_class = Member::class; |
|
32
|
|
|
|
|
33
|
|
|
private static $required_permission_codes = 'CMS_ACCESS_SecurityAdmin'; |
|
34
|
|
|
|
|
35
|
|
|
private static $menu_icon = MaterialIcons::SECURITY; |
|
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
private static $allowed_actions = [ |
|
38
|
|
|
'EditForm', |
|
39
|
|
|
]; |
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @return array |
|
44
|
|
|
*/ |
|
45
|
|
|
public static function getMembersFromSecurityGroupsIDs() |
|
46
|
|
|
{ |
|
47
|
|
|
$sql = 'SELECT DISTINCT MemberID FROM Group_Members INNER JOIN Permission ON Permission.GroupID = Group_Members.GroupID WHERE Code LIKE \'CMS_%\' OR Code = \'ADMIN\''; |
|
48
|
|
|
return DB::query($sql)->column(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function getEditForm($id = null, $fields = null) |
|
52
|
|
|
{ |
|
53
|
|
|
$fields = new FieldList(); |
|
54
|
|
|
$fields->push(new TabSet('Root')); |
|
55
|
|
|
|
|
56
|
|
|
// Build member fields (display only relevant security members) |
|
57
|
|
|
$membersOfGroups = self::getMembersFromSecurityGroupsIDs(); |
|
58
|
|
|
$memberField = new TabulatorGrid( |
|
59
|
|
|
'Members', |
|
60
|
|
|
false, |
|
61
|
|
|
Member::get()->filter("ID", $membersOfGroups), |
|
62
|
|
|
); |
|
63
|
|
|
$membersTab = $fields->findOrMakeTab('Root.Users', _t(__CLASS__ . '.TABUSERS', 'Users')); |
|
64
|
|
|
$membersTab->push($memberField); |
|
65
|
|
|
|
|
66
|
|
|
// Build group fields |
|
67
|
|
|
$groupField = new TabulatorGrid( |
|
68
|
|
|
'Groups', |
|
69
|
|
|
false, |
|
70
|
|
|
Group::get(), |
|
71
|
|
|
); |
|
72
|
|
|
$groupsTab = $fields->findOrMakeTab('Root.Groups', Group::singleton()->i18n_plural_name()); |
|
73
|
|
|
$groupsTab->push($groupField); |
|
74
|
|
|
|
|
75
|
|
|
// Add roles editing interface |
|
76
|
|
|
$rolesTab = null; |
|
|
|
|
|
|
77
|
|
|
if (Permission::check('APPLY_ROLES')) { |
|
78
|
|
|
$rolesField = new TabulatorGrid('Roles', false, PermissionRole::get()); |
|
79
|
|
|
$rolesTab = $fields->findOrMakeTab('Root.Roles', PermissionRole::singleton()->i18n_plural_name()); |
|
80
|
|
|
$rolesTab->push($rolesField); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
// Build replacement form |
|
84
|
|
|
$form = Form::create( |
|
85
|
|
|
$this, |
|
86
|
|
|
'EditForm', |
|
87
|
|
|
$fields, |
|
88
|
|
|
new FieldList() |
|
89
|
|
|
)->setHTMLID('Form_EditForm'); |
|
90
|
|
|
$form->setTemplate($this->getTemplatesWithSuffix('_EditForm')); |
|
91
|
|
|
$this->setCMSTabset($form); |
|
92
|
|
|
|
|
93
|
|
|
$this->extend('updateEditForm', $form); |
|
94
|
|
|
|
|
95
|
|
|
return $form; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function Breadcrumbs($unlinked = false) |
|
99
|
|
|
{ |
|
100
|
|
|
$crumbs = parent::Breadcrumbs($unlinked); |
|
101
|
|
|
|
|
102
|
|
|
// Name root breadcrumb based on which record is edited, |
|
103
|
|
|
// which can only be determined by looking for the fieldname of the GridField. |
|
104
|
|
|
// Note: Titles should be same titles as tabs in RootForm(). |
|
105
|
|
|
$params = $this->getRequest()->allParams(); |
|
106
|
|
|
if (isset($params['FieldName'])) { |
|
107
|
|
|
// TODO FieldName param gets overwritten by nested GridFields, |
|
108
|
|
|
// so shows "Members" rather than "Groups" for the following URL: |
|
109
|
|
|
// admin/security/EditForm/field/Groups/item/2/ItemEditForm/field/Members/item/1/edit |
|
110
|
|
|
$firstCrumb = $crumbs->shift(); |
|
111
|
|
|
if ($params['FieldName'] == 'Groups') { |
|
112
|
|
|
$crumbs->unshift(new ArrayData(array( |
|
113
|
|
|
'Title' => Group::singleton()->i18n_plural_name(), |
|
114
|
|
|
'Link' => $this->Link() . '#Root_Groups' |
|
115
|
|
|
))); |
|
116
|
|
|
} elseif ($params['FieldName'] == 'Users') { |
|
117
|
|
|
$crumbs->unshift(new ArrayData(array( |
|
118
|
|
|
'Title' => _t(__CLASS__ . '.TABUSERS', 'Users'), |
|
119
|
|
|
'Link' => $this->Link() . '#Root_Users' |
|
120
|
|
|
))); |
|
121
|
|
|
} elseif ($params['FieldName'] == 'Roles') { |
|
122
|
|
|
$crumbs->unshift(new ArrayData(array( |
|
123
|
|
|
'Title' => PermissionRole::singleton()->i18n_plural_name(), |
|
124
|
|
|
'Link' => $this->Link() . '#Root_Roles' |
|
125
|
|
|
))); |
|
126
|
|
|
} |
|
127
|
|
|
$crumbs->unshift($firstCrumb); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
return $crumbs; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
public function providePermissions() |
|
134
|
|
|
{ |
|
135
|
|
|
$title = $this->menu_title(); |
|
136
|
|
|
return array( |
|
137
|
|
|
"CMS_ACCESS_SecurityAdmin" => [ |
|
138
|
|
|
'name' => _t( |
|
139
|
|
|
'LeKoala\\Admini\\LeftAndMain.ACCESS', |
|
140
|
|
|
"Access to '{title}' section", |
|
141
|
|
|
['title' => $title] |
|
142
|
|
|
), |
|
143
|
|
|
'category' => _t('SilverStripe\\Security\\Permission.CMS_ACCESS_CATEGORY', 'CMS Access'), |
|
144
|
|
|
'help' => _t( |
|
145
|
|
|
__CLASS__ . '.ACCESS_HELP', |
|
146
|
|
|
'Allow viewing, adding and editing users, as well as assigning permissions and roles to them.' |
|
147
|
|
|
) |
|
148
|
|
|
], |
|
149
|
|
|
'EDIT_PERMISSIONS' => array( |
|
150
|
|
|
'name' => _t(__CLASS__ . '.EDITPERMISSIONS', 'Manage permissions for groups'), |
|
151
|
|
|
'category' => _t( |
|
152
|
|
|
'SilverStripe\\Security\\Permission.PERMISSIONS_CATEGORY', |
|
153
|
|
|
'Roles and access permissions' |
|
154
|
|
|
), |
|
155
|
|
|
'help' => _t( |
|
156
|
|
|
__CLASS__ . '.EDITPERMISSIONS_HELP', |
|
157
|
|
|
'Ability to edit Permissions and IP Addresses for a group.' |
|
158
|
|
|
. ' Requires the "Access to \'Security\' section" permission.' |
|
159
|
|
|
), |
|
160
|
|
|
'sort' => 0 |
|
161
|
|
|
), |
|
162
|
|
|
'APPLY_ROLES' => array( |
|
163
|
|
|
'name' => _t(__CLASS__ . '.APPLY_ROLES', 'Apply roles to groups'), |
|
164
|
|
|
'category' => _t( |
|
165
|
|
|
'SilverStripe\\Security\\Permission.PERMISSIONS_CATEGORY', |
|
166
|
|
|
'Roles and access permissions' |
|
167
|
|
|
), |
|
168
|
|
|
'help' => _t( |
|
169
|
|
|
__CLASS__ . '.APPLY_ROLES_HELP', |
|
170
|
|
|
'Ability to edit the roles assigned to a group.' |
|
171
|
|
|
. ' Requires the "Access to \'Users\' section" permission.' |
|
172
|
|
|
), |
|
173
|
|
|
'sort' => 0 |
|
174
|
|
|
) |
|
175
|
|
|
); |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|