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