1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\LDAP\Extensions; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Forms\FieldList; |
6
|
|
|
use SilverStripe\Forms\GridField\GridField; |
7
|
|
|
use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor; |
8
|
|
|
use SilverStripe\Forms\LiteralField; |
9
|
|
|
use SilverStripe\Forms\ReadonlyField; |
10
|
|
|
use SilverStripe\LDAP\Model\LDAPGroupMapping; |
11
|
|
|
use SilverStripe\ORM\DataExtension; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class LDAPGroupExtension |
15
|
|
|
* |
16
|
|
|
* Adds a field to map an LDAP group to a SilverStripe {@link Group} |
17
|
|
|
*/ |
18
|
|
|
class LDAPGroupExtension extends DataExtension |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
private static $db = [ |
|
|
|
|
24
|
|
|
// Unique user identifier, same field is used by SAMLMemberExtension |
25
|
|
|
'GUID' => 'Varchar(50)', |
26
|
|
|
'DN' => 'Text', |
27
|
|
|
'LastSynced' => 'DBDatetime' |
28
|
|
|
]; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* A SilverStripe group can have several mappings to LDAP groups. |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
private static $has_many = [ |
|
|
|
|
35
|
|
|
'LDAPGroupMappings' => LDAPGroupMapping::class, |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Add a field to the Group_Members join table so we can keep track |
40
|
|
|
* of Members added to a mapped Group. |
41
|
|
|
* |
42
|
|
|
* See {@link LDAPService::updateMemberFromLDAP()} for more details |
43
|
|
|
* on how this gets used. |
44
|
|
|
* |
45
|
|
|
* @var array |
46
|
|
|
*/ |
47
|
|
|
private static $many_many_extraFields = [ |
|
|
|
|
48
|
|
|
'Members' => [ |
49
|
|
|
'IsImportedFromLDAP' => 'Boolean' |
50
|
|
|
] |
51
|
|
|
]; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritDoc} |
55
|
|
|
* @param FieldList $fields |
56
|
|
|
*/ |
57
|
|
|
public function updateCMSFields(FieldList $fields) |
58
|
|
|
{ |
59
|
|
|
// Add read-only LDAP metadata fields. |
60
|
|
|
$fields->addFieldToTab('Root.LDAP', ReadonlyField::create('GUID')); |
|
|
|
|
61
|
|
|
$fields->addFieldToTab('Root.LDAP', ReadonlyField::create('DN')); |
62
|
|
|
$fields->addFieldToTab( |
63
|
|
|
'Root.LDAP', |
64
|
|
|
ReadonlyField::create( |
65
|
|
|
'LastSynced', |
66
|
|
|
_t(__CLASS__ . '.LASTSYNCED', 'Last synced') |
67
|
|
|
) |
68
|
|
|
); |
69
|
|
|
|
70
|
|
|
if ($this->owner->GUID) { |
71
|
|
|
$fields->replaceField('Title', ReadonlyField::create('Title')); |
72
|
|
|
$fields->replaceField('Description', ReadonlyField::create('Description')); |
73
|
|
|
// Surface the code which is normally hidden from the CMS user. |
74
|
|
|
$fields->addFieldToTab('Root.Members', ReadonlyField::create('Code'), 'Members'); |
75
|
|
|
|
76
|
|
|
$message = _t( |
77
|
|
|
__CLASS__ . '.INFOIMPORTED', |
78
|
|
|
'This group is automatically imported from LDAP.' |
79
|
|
|
); |
80
|
|
|
$fields->addFieldToTab( |
81
|
|
|
'Root.Members', |
82
|
|
|
LiteralField::create( |
83
|
|
|
'Info', |
84
|
|
|
sprintf('<p class="message warning">%s</p>', $message) |
85
|
|
|
), |
86
|
|
|
'Title' |
87
|
|
|
); |
88
|
|
|
|
89
|
|
|
$fields->addFieldToTab('Root.LDAP', ReadonlyField::create( |
90
|
|
|
'LDAPGroupMappingsRO', |
91
|
|
|
_t(__CLASS__ . '.AUTOMAPPEDGROUPS', 'Automatically mapped LDAP Groups'), |
92
|
|
|
implode('; ', $this->owner->LDAPGroupMappings()->column('DN')) |
93
|
|
|
)); |
94
|
|
|
} else { |
95
|
|
|
$field = GridField::create( |
96
|
|
|
'LDAPGroupMappings', |
97
|
|
|
_t('LDAPGroupExtension.MAPPEDGROUPS', 'Mapped LDAP Groups'), |
98
|
|
|
$this->owner->LDAPGroupMappings() |
99
|
|
|
); |
100
|
|
|
$config = GridFieldConfig_RecordEditor::create(); |
101
|
|
|
$config->getComponentByType('GridFieldAddNewButton') |
102
|
|
|
->setButtonName(_t(__CLASS__ . '.ADDMAPPEDGROUP', 'Add LDAP group mapping')); |
103
|
|
|
|
104
|
|
|
$field->setConfig($config); |
105
|
|
|
$fields->addFieldToTab('Root.LDAP', $field); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* LDAPGroupMappings are inherently relying on groups and can be removed now. |
111
|
|
|
*/ |
112
|
|
|
public function onBeforeDelete() |
113
|
|
|
{ |
114
|
|
|
foreach ($this->owner->LDAPGroupMappings() as $mapping) { |
115
|
|
|
$mapping->delete(); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.