source: branches/2.2/setup/ldapmodify.php @ 3607

Revision 3607, 14.0 KB checked in by rodsouza, 13 years ago (diff)

Ticket #707 - Adicionando classes e atributos minimos para o ExpressoLivre?

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1<?php
2  /**************************************************************************\
3  * eGroupWare - Setup                                                       *
4  * http://www.egroupware.org                                                *
5  * --------------------------------------------                             *
6  *  This program is free software; you can redistribute it and/or modify it *
7  *  under the terms of the GNU General Public License as published by the   *
8  *  Free Software Foundation; either version 2 of the License, or (at your  *
9  *  option) any later version.                                              *
10  \**************************************************************************/
11
12
13        $phpgw_info = array();
14        $phpgw_info["flags"] = array(
15                'noheader'   => True,
16                'nonavbar'   => True,
17                'currentapp' => 'home',
18                'noapi'      => True
19        );
20        include('./inc/functions.inc.php');
21
22        /* Authorize the user to use setup app and load the database */
23        if(!$GLOBALS['phpgw_setup']->auth('Config'))
24        {
25                Header('Location: index.php');
26                exit;
27        }
28        /* Does not return unless user is authorized */
29
30        class phpgw
31        {
32                var $common;
33                var $accounts;
34                var $applications;
35                var $db;
36        }
37        $phpgw = new phpgw;
38        $phpgw->common = CreateObject('phpgwapi.common');
39
40        $common = $phpgw->common;
41        $GLOBALS['phpgw_setup']->loaddb();
42        copyobj($GLOBALS['phpgw_setup']->db,$phpgw->db);
43
44        $tpl_root = $GLOBALS['phpgw_setup']->html->setup_tpl_dir('setup');
45        $setup_tpl = CreateObject('setup.Template',$tpl_root);
46        $setup_tpl->set_file(array(
47                'ldap'   => 'ldap.tpl',
48                'T_head' => 'head.tpl',
49                'T_footer' => 'footer.tpl',
50                'T_alert_msg' => 'msg_alert_msg.tpl'
51        ));
52
53        $GLOBALS[ 'phpgw_setup' ] -> db -> query(
54                "SELECT config_name, config_value FROM phpgw_config"
55                ." WHERE config_app = 'phpgwapi' and config_name LIKE 'ldap%' OR config_name='account_repository'",
56                __LINE__, __FILE__
57        );
58
59        while ( $GLOBALS[ 'phpgw_setup' ] -> db -> next_record( ) )
60                $config[ $GLOBALS[ 'phpgw_setup' ] -> db -> f( 'config_name' ) ] = $GLOBALS[ 'phpgw_setup' ] -> db -> f( 'config_value' );
61
62        $phpgw_info['server']['ldap_host']          = $config['ldap_host'];
63        $phpgw_info['server']['ldap_context']       = $config['ldap_context'];
64        $phpgw_info['server']['ldap_group_context'] = $config['ldap_group_context'];
65        $phpgw_info['server']['ldap_root_dn']       = $config['ldap_root_dn'];
66        $phpgw_info['server']['ldap_root_pw']       = $config['ldap_root_pw'];
67        $phpgw_info['server']['account_repository'] = $config['account_repository'];
68        $phpgw_info['server']['ldap_version3']      = $config['ldap_version3'];
69
70        $phpgw->accounts = CreateObject('phpgwapi.accounts');
71        $acct            = $phpgw->accounts;
72
73        // connect to ldap server
74        if ( ! $ldap = $common -> ldapConnect( ) )
75        {
76                Header( 'Location: config.php?error=badldapconnection' );
77                exit;
78        }
79
80        // Take the users from LDAP.
81        $sr = ldap_search( $ldap, $config[ 'ldap_context' ], '(objectClass=posixAccount)', array( 'cn', 'givenname', 'uid', 'uidnumber', 'objectClass' ) );
82        $info = ldap_get_entries( $ldap, $sr );
83        $tmp = '';
84
85        $account_info = array( );
86        for ( $i = 0; $i < $info[ 'count' ]; $i++ )
87                if ( ! array_key_exists( $info[ $i ][ 'uid' ][ 0 ], $phpgw_info[ 'server' ][ 'global_denied_users' ] ) )
88                        $account_info[ $info[ $i ][ 'dn' ] ] = $info[ $i ];
89
90        $group_info = array( );
91        if ( array_key_exists( 'ldap_group_context', $phpgw_info[ 'server' ] ) && count( $phpgw_info['server']['global_denied_groups'] ) )
92        {
93                $sr = ldap_search( $ldap, $config[ 'ldap_group_context' ], '(objectClass=posixGroup)', array( 'gidnumber', 'cn', 'memberuid' ) );
94                $info = ldap_get_entries( $ldap, $sr );
95                $tmp = '';
96
97                for ( $i = 0; $i < $info[ 'count' ]; $i++ )
98                        if ( ! array_key_exists( $info[ $i ][ 'cn' ][ 0 ], $phpgw_info[ 'server' ][ 'global_denied_groups' ] ) )
99                                $group_info[ $info[ $i ][ 'dn' ] ] = $info[ $i ];
100        }
101
102        $GLOBALS[ 'phpgw_setup' ] -> db -> query(
103                "SELECT app_name FROM phpgw_applications WHERE app_enabled!='0' AND app_enabled!='3' ORDER BY app_name",
104                __LINE__, __FILE__
105        );
106
107        while( $GLOBALS[ 'phpgw_setup' ] -> db -> next_record( ) )
108                $apps[ $GLOBALS[ 'phpgw_setup' ] -> db -> f( 'app_name' ) ] = lang( $GLOBALS[ 'phpgw_setup' ] -> db -> f( 'app_name' ) );
109
110        if ( $cancel )
111        {
112                Header( 'Location: ldap.php' );
113                exit;
114        }
115
116        $GLOBALS[ 'phpgw_setup' ] -> html -> show_header(
117                lang('LDAP Modify'),
118                false,
119                'config',
120                "{$GLOBALS[ 'phpgw_setup' ] -> ConfigDomain} ( {$phpgw_domain[ $GLOBALS[ 'phpgw_setup' ] -> ConfigDomain ][ 'db_type' ]} )"
121        );
122
123        //error_log( print_r( $config, true ), 3, '/tmp/log' );
124        //error_log( print_r( $group_info, true ), 3, '/tmp/log' );
125
126        if ( array_key_exists( 'submit', $_POST ) )
127        {
128                $acl = CreateObject('phpgwapi.acl');
129                copyobj( $GLOBALS[ 'phpgw_setup' ] -> db, $acl -> db );
130
131                if ( array_key_exists( 'ldapgroups', $_POST ) && is_array( $_POST[ 'ldapgroups' ] ) )
132                {
133                        $groups = CreateObject( 'phpgwapi.accounts' );
134                        copyobj( $GLOBALS[ 'phpgw_setup' ] -> db, $groups -> db );
135
136                        //error_log( print_r( $_POST[ 'ldapgroups' ], true ), 3, '/tmp/log' );
137
138                        foreach ( $_POST[ 'ldapgroups' ] as $groupid )
139                        {
140                                if ( ! array_key_exists( $groupid, $group_info ) )
141                                {
142                                        echo "Has occurred some problem in the group : {$groupid}<br>\n";
143                                        continue;
144                                }
145
146                                $entry = array( );
147
148                                $thisacctid    = $group_info[ $groupid ][ 'gidnumber' ][ 0 ];
149                                $thisacctlid   = $group_info[ $groupid ][ 'cn' ][ 0 ];
150                                $thisfirstname = $group_info[ $groupid ][ 'cn' ][ 0 ];
151                                $thismembers   = $group_info[ $groupid ][ 'memberuid' ];
152                                $thisdn        = $group_info[ $groupid ][ 'dn' ];
153
154                                echo "Updating GROUPID : {$thisacctlid} ({$groupid})<br>\n";
155
156                                // Do some checks before we try to import the data.
157                                if ( ! empty( $thisacctid ) && ! empty( $thisacctlid ) )
158                                {
159                                        $groups->account_id = ( int ) $thisacctid;
160
161                                        $sr = ldap_search( $ldap, $config[ 'ldap_group_context' ], 'cn=' . $thisacctlid );
162                                        $entry = ldap_get_entries( $ldap, $sr );
163
164                        //              error_log( print_r( $entry, true ), 3, '/tmp/log' );
165
166                                        reset( $entry[ 0 ][ 'objectclass' ] );
167
168                                        $add = array( );
169
170                                        if ( ! in_array( 'phpgwAccount', $entry[ 0 ][ 'objectclass' ] ) )
171                                                $add[ 'objectclass'] = array( 'phpgwAccount' );
172
173                                        if ( ! array_key_exists( 'phpgwaccountstatus', $entry[ 0 ] ) )
174                                                $add[ 'phpgwaccountstatus'] = array( 'A' );
175
176                                        if ( ! array_key_exists( 'phpgwaccounttype', $entry[ 0 ] ) )
177                                                $add[ 'phpgwaccounttype' ] = array( 'g' );
178
179                                        if ( ! array_key_exists( 'phpgwaccountexpires', $entry[ 0 ] ) )
180                                                $add[ 'phpgwaccountexpires' ] = array( -1 );
181
182                        //              error_log( print_r( $add, true ), 3, '/tmp/log' );
183
184                                        //if ( count( $add ) )
185                                        //      ldap_mod_add( $ldap, $thisdn, $add );
186
187                                        // Now make the members a member of this group in phpgw.
188                                        if ( is_array( $thismembers ) )
189                                        {
190                                                if ( array_key_exists( 'count', $thismembers ) )
191                                                        unset( $thismembers[ 'count' ] );
192
193                        //                      error_log( print_r( $thismembers, true ), 3, '/tmp/log' );
194                        //                      error_log( print_r( $account_info, true ), 3, '/tmp/log' );
195
196                                                foreach ( $thismembers as $key => $members )
197                                                {
198                                                        echo "members: {$members}<br>\n";
199
200                                                        $tmpid = NULL;
201                                                        foreach ( $account_info as $info )
202                                                                if ( $members == $info[ 'uid' ][ 0 ] )
203                                                                {
204                                                                        $tmpid = $info[ 'uidnumber' ][ 0 ];
205                                                                        break;
206                                                                }
207
208                                                        // Insert acls for this group based on memberuid field.
209                                                        // Since the group has app rights, we don't need to give users
210                                                        // these rights. Instead, we maintain group membership here.
211                                                        if ( $tmpid )
212                                                        {
213                                                                echo "inserindo user_id: {$tmpid} em {$thisacctid}<br>\n";
214
215                                                                $acl -> account_id = ( int ) $tmpid;
216                                                                $acl -> read_repository( );
217       
218                                                                $acl -> delete( 'phpgw_group', $thisacctid, 1 );
219                                                                $acl -> add( 'phpgw_group', $thisacctid, 1 );
220       
221                                                                // Now add the acl to let them change their password
222                                                                $acl -> delete( 'preferences', 'changepassword', 1 );
223                                                                $acl -> add( 'preferences', 'changepassword', 1 );
224       
225                                                                $acl -> save_repository( );
226                                                        }
227                                                }
228                                        }
229
230                                        // Now give this group some rights
231                                        $phpgw_info[ 'user' ][ 'account_id' ] = $thisacctid;
232
233                                        $acl -> account_id = ( int ) $thisacctid;
234                                        $acl -> read_repository( );
235
236                                        foreach ( $_POST[ 's_apps' ] as $app )
237                                        {
238                                                $acl -> delete( $app, 'run', 1 );
239                                                $acl -> add( $app, 'run', 1 );
240                                        }
241
242                                        $acl -> save_repository();
243                                        $defaultgroupid = $thisacctid;
244                                }
245                                echo "----------------------------------------------<br>\n";
246                        }
247                }
248
249                if ( array_key_exists( 'users', $_POST ) && is_array( $_POST[ 'users' ] ) )
250                {
251                        $accounts = CreateObject( 'phpgwapi.accounts' );
252                        copyobj( $GLOBALS[ 'phpgw_setup' ] -> db, $accounts -> db );
253
254                        $users_process = 0;
255                        $new_uidnumber = 12011;
256
257                        //error_log( print_r( $_POST[ 'users' ], true ), 3, '/tmp/log' );
258                        //error_log( print_r( $config, true ), 3, '/tmp/log' );
259
260                        foreach ( $_POST[ 'users' ] as $user_id )
261                        {
262                                $id_exist = 0;
263                                $thisacctid  = $account_info[ $user_id ][ 'uidnumber' ][ 0 ];
264                                $thisacctlid = $account_info[ $user_id ][ 'uid' ][ 0 ];
265                                $thisdn      = $account_info[ $user_id ][ 'dn'];
266
267                                echo "{$thisdn}<br>\nUpdating USERID : {$thisacctlid}<br>\n";
268                               
269                                // Do some checks before we try to import the data.
270                                if ( !empty($thisacctid) && !empty($thisacctlid))
271                                {
272                                        $users_process++;
273
274                                        //$res = ldap_search( $ldap, $config[ 'ldap_group_context' ], 'uid=' . $thisacctlid, array( 'uid', 'objectClass' ) );
275                                        //$entry = ldap_get_entries( $ldap, $res );
276
277                                        //error_log( print_r( $entry, true ), 3, '/tmp/log' );
278
279                                        //for ( $entry = ldap_first_entry( $ldap, $res ); $entry !== false, $arr = ldap_get_attributes( $ldap, $entry ); $entry = ldap_next_entry( $ldap, $entry ) )
280                                        //{
281                                        //      error_log( print_r( $arr, true ), 3, '/tmp/log' );
282                                        //}
283                                        //error_log( "---------------------------------------------------\n", 3, '/tmp/log' );
284
285                                        //error_log( print_r( $account_info[ $user_id ], true ), 3, '/tmp/log' );
286                                        //error_log( "---------------------------------------------------\n", 3, '/tmp/log' );
287
288                                        $add = array( );
289                                        $objectClass = array( );
290
291                                        if ( ! in_array( 'qmailUser', $account_info[ $user_id ][ 'objectclass' ] ) )
292                                                $objectclass[ ] = 'qmailUser';
293
294                                        if ( ! in_array( 'phpgwAccount', $account_info[ $user_id ][ 'objectclass' ] ) )
295                                        {
296                                                $objectclass[ ] = 'phpgwAccount';
297                                                $add[ 'phpgwAccountExpires' ] = array( '-1' );
298                                                $add[ 'phpgwAccountStatus' ] = array( 'A' );
299                                                $add[ 'phpgwAccountType' ] = array( 'u' );
300                                                $add[ 'phpgwLastPasswdChange' ] = array( '1290632486' );
301                                        }
302
303                                        if ( count( $objectclass ) )
304                                                $add[ 'objectclass' ] = $objectclass;
305
306                                        if ( count( $add ) )
307                                                ldap_mod_add( $ldap, $thisdn, $add );
308
309                                        $accounts -> account_id = ( int ) $thisacctid;
310                                       
311                                        // Insert default acls for this user.
312                                        $acl -> account_id = ( int ) $thisacctid;
313                                        $acl -> read_repository( );
314       
315                                        // Now add the acl to let them change their password
316                                        $acl -> delete( 'preferences', 'changepassword', 1 );
317                                        $acl -> add( 'preferences', 'changepassword', 1 );
318
319                                        // Add user to a default group, previous created
320                                        $acl -> add( 'phpgw_group', '12007', 1 );
321
322                                        echo "Adding in ACL BD: {$thisacctid}<br><br>\n";
323
324                                        // Save these new acls.
325                                        $acl -> save_repository( );
326                                               
327                                        $new_uidnumber++;
328                                }
329                        }
330                }
331
332                printf( "<br><center>%s %s<br></center>",
333                        lang( 'Modifications have been completed!' ),
334                        lang( 'Click <a href="index.php">here</a> to return to setup.' )
335                );
336
337                $GLOBALS['phpgw_setup' ] -> html -> show_footer( );
338                exit;
339        }
340
341        if ( array_key_exists( 'error', $_GET ) )
342                $GLOBALS[ 'phpgw_setup' ] -> html -> show_alert_msg( 'Error', $_GET[ 'error' ] );
343
344        $setup_tpl->set_block('ldap','header','header');
345        $setup_tpl->set_block('ldap','user_list','user_list');
346        $setup_tpl->set_block('ldap','admin_list','admin_list');
347        $setup_tpl->set_block('ldap','group_list','group_list');
348        $setup_tpl->set_block('ldap','app_list','app_list');
349        $setup_tpl->set_block('ldap','submit','submit');
350        $setup_tpl->set_block('ldap','footer','footer');
351
352        $user_list = array( );
353        while ( list( $key, $account ) = each( $account_info ) )
354                $user_list[ ] = '<option value="' . $account[ 'dn' ] . '">' . utf8_decode( $account[ 'cn' ][ 0 ] ) . " ({$account[ 'uid' ][ 0 ]})</option>";
355
356        $user_list = $admin_list = implode( '', $user_list );
357
358        $group_list = '';
359        while( list( $key, $group ) = each( $group_info ) )
360                $group_list .= '<option value="' . $group[ 'dn' ] . '">' . utf8_decode( $group[ 'cn' ][ 0 ] )  . '</option>';
361
362        $app_list = '';
363        while(list($appname,$apptitle) = each($apps))
364        {
365                if($appname == 'admin' ||
366                        $appname == 'skel' ||
367                        $appname == 'backup' ||
368                        $appname == 'netsaint' ||
369                        $appname == 'developer_tools' ||
370                        $appname == 'phpsysinfo' ||
371                        $appname == 'eldaptir' ||
372                        $appname == 'qmailldap')
373                {
374                        $app_list .= '<option value="' . $appname . '">' . $apptitle . '</option>';
375                }
376                else
377                {
378                        $app_list .= '<option value="' . $appname . '" selected>' . $apptitle . '</option>';
379                }
380        }
381
382        $setup_tpl->set_var('action_url','ldapmodify.php');
383        $setup_tpl->set_var('users',$user_list);
384        $setup_tpl->set_var('admins',$admin_list);
385        $setup_tpl->set_var('ldapgroups',$group_list);
386        $setup_tpl->set_var('s_apps',$app_list);
387
388        $setup_tpl->set_var('ldap_import',lang('LDAP Modify'));
389        $setup_tpl->set_var('description',lang("This section will help you setup your LDAP accounts for use with eGroupWare").'.');
390        $setup_tpl->set_var('select_users',lang('Select which user(s) will be modified'));
391        $setup_tpl->set_var('select_admins',lang('Select which user(s) will also have admin privileges'));
392        $setup_tpl->set_var('select_groups',lang('Select which group(s) will be modified (group membership will be maintained)'));
393        $setup_tpl->set_var('select_apps',lang('Select the default applications to which your users will have access').'.');
394        $setup_tpl->set_var('form_submit',lang('Modify'));
395        $setup_tpl->set_var('cancel',lang('Cancel'));
396
397        $setup_tpl->pfp('out','header');
398        $setup_tpl->pfp('out','user_list');
399        $setup_tpl->pfp('out','admin_list');
400        $setup_tpl->pfp('out','group_list');
401        $setup_tpl->pfp('out','app_list');
402        $setup_tpl->pfp('out','submit');
403        $setup_tpl->pfp('out','footer');
404
405        $GLOBALS['phpgw_setup']->html->show_footer();
406?>
Note: See TracBrowser for help on using the repository browser.