~ubuntu-branches/ubuntu/trusty/ldap-account-manager/trusty-proposed

« back to all changes in this revision

Viewing changes to lib/modules/windowsHost.inc

  • Committer: Package Import Robot
  • Author(s): Roland Gruber
  • Date: 2013-07-21 11:15:58 UTC
  • mfrom: (1.2.21)
  • Revision ID: package-import@ubuntu.com-20130721111558-ex49xgcpo71fh0xg
Tags: 4.2.1-2
* Apache 2.4 support
* Fix "transition towards Apache 2.4" 
  support conf-enabled (Closes: #669824)
* Fix "[ldap-account-manager] Can't install ldap-account-manager"
  support conf-enabled (Closes: #711778)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/*
 
3
$Id: windowsHost.inc 4907 2013-05-10 16:20:37Z gruberroland $
 
4
 
 
5
  This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
 
6
  Copyright (C) 2013  Roland Gruber
 
7
 
 
8
  This program is free software; you can redistribute it and/or modify
 
9
  it under the terms of the GNU General Public License as published by
 
10
  the Free Software Foundation; either version 2 of the License, or
 
11
  (at your option) any later version.
 
12
 
 
13
  This program is distributed in the hope that it will be useful,
 
14
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
  GNU General Public License for more details.
 
17
 
 
18
  You should have received a copy of the GNU General Public License
 
19
  along with this program; if not, write to the Free Software
 
20
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
21
*/
 
22
 
 
23
/**
 
24
* Manages Windows AD (e.g. Samba 4) hosts.
 
25
*
 
26
* @package modules
 
27
* @author Roland Gruber
 
28
*/
 
29
 
 
30
/**
 
31
* Manages Windows AD (e.g. Samba 4) hosts.
 
32
*
 
33
* @package modules
 
34
*/
 
35
class windowsHost extends baseModule {
 
36
        
 
37
        /**
 
38
        * Returns meta data that is interpreted by parent class
 
39
        *
 
40
        * @return array array with meta data
 
41
        * 
 
42
        * @see baseModule::get_metaData()
 
43
        */
 
44
        public function get_metaData() {
 
45
                $return = array();
 
46
                // icon
 
47
                $return['icon'] = 'samba.png';
 
48
                // manages host accounts
 
49
                $return["account_types"] = array('host');
 
50
                // this is a base module
 
51
                $return["is_base"] = true;
 
52
                // RDN attribute
 
53
                $return["RDN"] = array("cn" => "high");
 
54
                // LDAP filter
 
55
                $return["ldap_filter"] = array('and' => "", 'or' => '(objectClass=computer)');
 
56
                // alias name
 
57
                $return["alias"] = _("Windows");
 
58
                // module dependencies
 
59
                $return['dependencies'] = array('depends' => array(), 'conflicts' => array());
 
60
                // managed object classes
 
61
                $return['objectClasses'] = array('computer', 'securityPrincipal');
 
62
                // managed attributes
 
63
                $return['attributes'] = array('cn', 'description', 'location', 'sAMAccountName', 'managedBy', 'operatingSystem', 'operatingSystemVersion', 'dNSHostName');
 
64
                // help Entries
 
65
                $return['help'] = array(
 
66
                        'cn' => array(
 
67
                                "Headline" => _('Host name'), 'attr' => 'cn, sAMAccountName',
 
68
                                "Text" => _('Please enter the host name.')
 
69
                        ),
 
70
                        'description' => array(
 
71
                                "Headline" => _('Description'), 'attr' => 'description',
 
72
                                "Text" => _('Please enter a descriptive text for this host.')
 
73
                        ),
 
74
                        'location' => array(
 
75
                                "Headline" => _('Location'), 'attr' => 'location',
 
76
                                "Text" => _('This is the host\'s location (e.g. Munich, server room 3).')
 
77
                        ),
 
78
                        'managedBy' => array(
 
79
                                "Headline" => _('Managed by'), 'attr' => 'managedBy',
 
80
                                "Text" => _('The host is managed by this contact person.')
 
81
                        ),
 
82
                );
 
83
                // upload fields
 
84
                $return['upload_columns'] = array(
 
85
                        array(
 
86
                                'name' => 'windowsHost_name',
 
87
                                'description' => _('Host name'),
 
88
                                'help' => 'cn',
 
89
                                'example' => _('PC01'),
 
90
                                'required' => true
 
91
                        ),
 
92
                        array(
 
93
                                'name' => 'windowsHost_description',
 
94
                                'description' => _('Description'),
 
95
                                'help' => 'description',
 
96
                        ),
 
97
                        array(
 
98
                                'name' => 'windowsHost_location',
 
99
                                'description' => _('Location'),
 
100
                                'help' => 'location',
 
101
                                'example' => _('MyCity'),
 
102
                        ),
 
103
                        array(
 
104
                                'name' => 'windowsHost_managedBy',
 
105
                                'description' => _('Managed by'),
 
106
                                'help' => 'managedBy',
 
107
                                'example' => 'cn=user1,o=test',
 
108
                        ),
 
109
                );
 
110
                // available PDF fields
 
111
                $return['PDF_fields'] = array(
 
112
                        'cn' => _('Host name'),
 
113
                        'description' => _('Description'),
 
114
                        'location' => _('Location'),
 
115
                        'managedBy' => _('Managed by'),
 
116
                );
 
117
                return $return;
 
118
        }
 
119
 
 
120
        /**
 
121
        * This function fills the $messages variable with output messages from this module.
 
122
        */
 
123
        public function load_Messages() {
 
124
                $this->messages['cn'][0] = array('ERROR', _('Host name'), _('Host name contains invalid characters. Valid characters are: a-z, A-Z, 0-9 and .-_ !'));
 
125
                $this->messages['cn'][1] = array('ERROR', _('Account %s:') . ' windowsHost_cn', _('Host name contains invalid characters. Valid characters are: a-z, A-Z, 0-9 and .-_ !'));
 
126
        }
 
127
 
 
128
        /**
 
129
         * Returns the HTML meta data for the main account page.
 
130
         * 
 
131
         * @return htmlElement HTML meta data
 
132
         */
 
133
        public function display_html_attributes() {
 
134
                $container = new htmlTable();
 
135
                $this->addSimpleInputTextField($container, 'cn', _('Host name'), true);
 
136
                $this->addSimpleInputTextField($container, 'description', _('Description'), false);
 
137
                $this->addSimpleInputTextField($container, 'location', _('Location'), false);
 
138
                // managed by
 
139
                $container->addElement(new htmlOutputText(_('Managed by')));
 
140
                $managedBy = '-';
 
141
                if (isset($this->attributes['managedBy'][0])) {
 
142
                        $managedBy = $this->attributes['managedBy'][0];
 
143
                }
 
144
                $container->addElement(new htmlOutputText(getAbstractDN($managedBy)));
 
145
                $container->addElement(new htmlHelpLink('managedBy'), true);
 
146
                $container->addElement(new htmlOutputText(''));
 
147
                $managedByButtons = new htmlGroup();
 
148
                $managedByButtons->addElement(new htmlAccountPageButton(get_class($this), 'managedBy', 'edit', _('Change')));
 
149
                if (isset($this->attributes['managedBy'][0])) {
 
150
                        $managedByButtons->addElement(new htmlSpacer('5px', null));
 
151
                        $managedByButtons->addElement(new htmlAccountPageButton(get_class($this), 'attributes', 'removeManagedBy', _('Remove')));
 
152
                }
 
153
                $container->addElement($managedByButtons, true);
 
154
                return $container;
 
155
        }
 
156
 
 
157
        /**
 
158
        * Processes user input of the primary module page.
 
159
        * It checks if all input values are correct and updates the associated LDAP attributes.
 
160
        *
 
161
        * @return array list of info/error messages
 
162
        */
 
163
        public function process_attributes() {
 
164
                $return = array();
 
165
                // cn
 
166
                $this->attributes['cn'][0] = $_POST['cn'];
 
167
                $this->attributes['sAMAccountName'][0] = $_POST['cn'] . '$';
 
168
                if (!get_preg($_POST['cn'], 'hostname')) {
 
169
                        $return[] = $this->messages['cn'][0];
 
170
                }
 
171
                // description
 
172
                $this->attributes['description'][0] = $_POST['description'];
 
173
                // location
 
174
                $this->attributes['location'][0] = $_POST['location'];
 
175
                // managed by
 
176
                if (isset($_POST['form_subpage_' . get_class($this) . '_attributes_removeManagedBy'])) {
 
177
                        unset($this->attributes['managedBy']);
 
178
                }
 
179
                if ($this->getAccountContainer()->isNewAccount) {
 
180
                        $this->attributes['userAccountControl'][0] = 4128; // machine trust account, no password required
 
181
                }
 
182
                return $return;
 
183
        }
 
184
        
 
185
        /**
 
186
        * This function will create the meta HTML code to show a page to change the member attribute.
 
187
        * 
 
188
        * @return htmlElement HTML meta data
 
189
        */
 
190
        function display_html_managedBy() {
 
191
                $return = new htmlTable();
 
192
                // show possible managers
 
193
                $options = array();
 
194
                $filter = get_ldap_filter('user');
 
195
                $entries = searchLDAPByFilter($filter, array('dn'), array('user'));
 
196
                for ($i = 0; $i < sizeof($entries); $i++) {
 
197
                        $entries[$i] = $entries[$i]['dn'];
 
198
                }
 
199
                // sort by DN
 
200
                usort($entries, 'compareDN');
 
201
                for ($i = 0; $i < sizeof($entries); $i++) {
 
202
                        $options[getAbstractDN($entries[$i])] = $entries[$i];
 
203
                }
 
204
                $selected = array();
 
205
                if (isset($this->attributes['managedBy'][0])) {
 
206
                        $selected = array($this->attributes['managedBy'][0]);
 
207
                        if (!in_array($selected[0], $options)) {
 
208
                                $options[getAbstractDN($selected[0])] = $selected[0];
 
209
                        }
 
210
                }
 
211
                $membersSelect = new htmlSelect('managedBy', $options, $selected);
 
212
                $membersSelect->setHasDescriptiveElements(true);
 
213
                $membersSelect->setRightToLeftTextDirection(true);
 
214
                $membersSelect->setSortElements(false);
 
215
                $membersSelect->setTransformSingleSelect(false);
 
216
                $return->addElement($membersSelect, true);
 
217
                $buttonTable = new htmlTable();
 
218
                $buttonTable->addElement(new htmlAccountPageButton(get_class($this), 'attributes', 'set', _('Change')));
 
219
                $buttonTable->addElement(new htmlAccountPageButton(get_class($this), 'attributes', 'cancel', _('Cancel')));
 
220
                $return->addElement($buttonTable);
 
221
                return $return;
 
222
        }
 
223
        
 
224
        /**
 
225
        * Processes user input of the members page.
 
226
        * It checks if all input values are correct and updates the associated LDAP attributes.
 
227
        *
 
228
        * @return array list of info/error messages
 
229
        */
 
230
        function process_managedBy() {
 
231
                $return = array();
 
232
                if (isset($_POST['form_subpage_' . get_class($this) . '_attributes_set'])) {
 
233
                        $this->attributes['managedBy'][] = $_POST['managedBy'];
 
234
                }
 
235
                return $return;
 
236
        }
 
237
 
 
238
        /**
 
239
        * In this function the LDAP account is built up.
 
240
        *
 
241
        * @param array $rawAccounts list of hash arrays (name => value) from user input
 
242
        * @param array $ids list of IDs for column position (e.g. "posixAccount_uid" => 5)
 
243
        * @param array $partialAccounts list of hash arrays (name => value) which are later added to LDAP
 
244
        * @param array $selectedModules list of selected account modules
 
245
        * @return array list of error messages if any
 
246
        */
 
247
        public function build_uploadAccounts($rawAccounts, $ids, &$partialAccounts, $selectedModules) {
 
248
                $errors = array();
 
249
                for ($i = 0; $i < sizeof($rawAccounts); $i++) {
 
250
                        // add object class
 
251
                        if (!in_array('computer', $partialAccounts[$i]['objectClass'])) $partialAccounts[$i]['objectClass'][] = 'computer';
 
252
                        // cn + sAMAccountName
 
253
                        if ($rawAccounts[$i][$ids['windowsHost_name']] != "") {
 
254
                                if (get_preg($rawAccounts[$i][$ids['windowsHost_name']], 'hostname')) {
 
255
                                        $partialAccounts[$i]['cn'] = $rawAccounts[$i][$ids['windowsHost_name']];
 
256
                                        $partialAccounts[$i]['sAMAccountName'] = $rawAccounts[$i][$ids['windowsHost_name']] . '$';
 
257
                                }
 
258
                                else {
 
259
                                        $errMsg = $this->messages['cn'][1];
 
260
                                        array_push($errMsg, array($i));
 
261
                                        $errors[] = $errMsg;
 
262
                                }
 
263
                        }
 
264
                        // description
 
265
                        if ($rawAccounts[$i][$ids['windowsHost_description']] != "") {
 
266
                                $partialAccounts[$i]['description'] = $rawAccounts[$i][$ids['windowsHost_description']];
 
267
                        }
 
268
                        // location
 
269
                        if ($rawAccounts[$i][$ids['windowsHost_location']] != "") {
 
270
                                $partialAccounts[$i]['location'] = $rawAccounts[$i][$ids['windowsHost_location']];
 
271
                        }
 
272
                        // managed by
 
273
                        if ($rawAccounts[$i][$ids['windowsHost_managedBy']] != "") {
 
274
                                $partialAccounts[$i]['managedBy'] = $rawAccounts[$i][$ids['windowsHost_managedBy']];
 
275
                        }
 
276
                        // machine trust account, no password required
 
277
                        $partialAccounts[$i]['userAccountControl'][0] = 4128;
 
278
                }
 
279
                return $errors;
 
280
        }
 
281
        
 
282
        /**
 
283
        * Returns a list of PDF entries
 
284
        */
 
285
        public function get_pdfEntries() {
 
286
                $return = array();
 
287
                $this->addSimplePDFField($return, 'cn', _('Host name'));
 
288
                $this->addSimplePDFField($return, 'description', _('Description'));
 
289
                $this->addSimplePDFField($return, 'location', _('Location'));
 
290
                // managed by
 
291
                $managedBy = '';
 
292
                if (isset($this->attributes['managedBy'][0])) {
 
293
                        $managedBy = getAbstractDN($this->attributes['managedBy'][0]);
 
294
                        $return[get_class($this) . '_managedBy'] = array('<block><key>' . _('Managed by') . '</key><value>' . $managedBy . '</value></block>');
 
295
                }
 
296
                return $return;
 
297
        }
 
298
 
 
299
}
 
300
 
 
301
 
 
302
?>