source: branches/2.2/setup/ldapimport.php @ 328

Revision 328, 16.2 KB checked in by niltonneto, 16 years ago (diff)

Alteração para carregar no setup apenas as variáveis da API,
evitando conflitos de variáveis com demais módulos.

  • 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        $phpgw->db = $GLOBALS['phpgw_setup']->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        $phpgw_info['server']['auth_type'] = 'ldap';
54
55        $phpgw->applications = CreateObject('phpgwapi.applications');
56        $applications        = $phpgw->applications;
57
58        $GLOBALS['phpgw_setup']->db->query("SELECT config_name,config_value FROM phpgw_config WHERE config_app='phpgwapi' and config_name LIKE 'ldap%' OR config_name='account_repository'",__LINE__,__FILE__);
59        while($GLOBALS['phpgw_setup']->db->next_record())
60        {
61                $config[$GLOBALS['phpgw_setup']->db->f('config_name')] = $GLOBALS['phpgw_setup']->db->f('config_value');
62        }
63        $phpgw_info['server']['ldap_host']          = $config['ldap_host'];
64        $phpgw_info['server']['ldap_context']       = $config['ldap_context'];
65        $phpgw_info['server']['ldap_group_context'] = $config['ldap_group_context'];
66        $phpgw_info['server']['ldap_root_dn']       = $config['ldap_root_dn'];
67        $phpgw_info['server']['ldap_root_pw']       = $config['ldap_root_pw'];
68        $phpgw_info['server']['account_repository'] = $config['account_repository'];
69
70        $phpgw->accounts     = CreateObject('phpgwapi.accounts');
71        $acct                = $phpgw->accounts;
72
73        // First, see if we can connect to the LDAP server, if not send `em back to config.php with an
74        // error message.
75
76        // connect to ldap server
77        if(!$ldap = $common->ldapConnect())
78        {
79                $noldapconnection = True;
80        }
81
82        if($noldapconnection)
83        {
84                Header('Location: config.php?error=badldapconnection');
85                exit;
86        }
87
88        $sr = ldap_search($ldap,$config['ldap_context'],'(|(uid=*))',array('sn','givenname','uid','uidnumber'));
89        $info = ldap_get_entries($ldap, $sr);
90        $tmp = '';
91
92        for($i=0; $i<$info['count']; $i++)
93        {
94                if(!$phpgw_info['server']['global_denied_users'][$info[$i]['uid'][0]])
95                {
96                        $tmp = $info[$i]['uidnumber'][0];
97                        $account_info[$tmp]['account_id']        = $info[$i]['uidnumber'][0];
98                        $account_info[$tmp]['account_lid']       = $info[$i]['uid'][0];
99                        $account_info[$tmp]['account_firstname'] = $info[$i]['givenname'][0];
100                        $account_info[$tmp]['account_lastname']  = $info[$i]['sn'][0];
101                        $account_info[$tmp]['account_passwd']    = $info[$i]['userpassword'][0];
102                }
103        }
104
105        if($phpgw_info['server']['ldap_group_context'])
106        {
107                $srg = ldap_search($ldap,$config['ldap_group_context'],'(|(cn=*))',array('gidnumber','cn','memberuid'));
108                $info = ldap_get_entries($ldap, $srg);
109                $tmp = '';
110
111                for($i=0; $i<$info['count']; $i++)
112                {
113                        if(!$phpgw_info['server']['global_denied_groups'][$info[$i]['cn'][0]] &&
114                                !$account_info[$i][$info[$i]['cn'][0]])
115                        {
116                                $tmp = $info[$i]['gidnumber'][0];
117                                $group_info[$tmp]['account_id']        = $info[$i]['gidnumber'][0];
118                                $group_info[$tmp]['account_lid']       = $info[$i]['cn'][0];
119                                $group_info[$tmp]['members']           = $info[$i]['memberuid'];
120                                $group_info[$tmp]['account_firstname'] = $info[$i]['cn'][0];
121                                $group_info[$tmp]['account_lastname']  = 'Group';
122                        }
123                }
124        }
125        else
126        {
127                $group_info = array();
128        }
129
130        $GLOBALS['phpgw_setup']->db->query("SELECT app_name FROM phpgw_applications WHERE app_enabled!='0' AND app_enabled!='3' ORDER BY app_name",__LINE__,__FILE__);
131        while($GLOBALS['phpgw_setup']->db->next_record())
132        {
133                $apps[$GLOBALS['phpgw_setup']->db->f('app_name')] = lang($GLOBALS['phpgw_setup']->db->f('app_name'));
134        }
135
136        $cancel = get_var('cancel','POST');
137        $submit = get_var('submit','POST');
138        $users  = get_var('users','POST');
139        $admins = get_var('admins','POST');
140        $s_apps = get_var('s_apps','POST');
141        $ldapgroups = get_var('ldapgroups','POST');
142
143        if($cancel)
144        {
145                Header('Location: ldap.php');
146                exit;
147        }
148
149        if($submit)
150        {
151                if(!count($admins))
152                {
153                        $error = '<br>You must select at least 1 admin';
154                }
155
156                if(!count($s_apps))
157                {
158                        $error .= '<br>You must select at least 1 application';
159                }
160
161                if(!$error)
162                {
163                        if($users)
164                        {
165                                while(list($key,$id) = each($users))
166                                {
167                                        $id_exist = 0;
168                                        $thisacctid    = $account_info[$id]['account_id'];
169                                        $thisacctlid   = $account_info[$id]['account_lid'];
170                                        $thisfirstname = $account_info[$id]['account_firstname'];
171                                        $thislastname  = $account_info[$id]['account_lastname'];
172                                        $thispasswd    = $account_info[$id]['account_passwd'];
173
174                                        // Do some checks before we try to import the data.
175                                        if(!empty($thisacctid) && !empty($thisacctlid))
176                                        {
177                                                $accounts = CreateObject('phpgwapi.accounts',(int)$thisacctid);
178                                                copyobj($GLOBALS['phpgw_setup']->db,$accounts->db);
179
180                                                // Check if the account is already there.
181                                                // If so, we won't try to create it again.
182                                                $acct_exist = $acct->name2id($thisacctlid);
183                                                if($acct_exist)
184                                                {
185                                                        $thisacctid = $acct_exist;
186                                                }
187                                                $id_exist = $accounts->exists($thisacctlid);
188                                                // If not, create it now.
189                                                if(!$id_exist)
190                                                {
191                                                        $thisaccount_info = array(
192                                                                'account_type'      => 'u',
193                                                                'account_lid'       => $thisacctlid,
194                                                                'account_passwd'    => 'x',
195                                                        /*      'account_passwd'    => $thispasswd, */
196                                                                'account_firstname' => $thisfirstname,
197                                                                'account_lastname'  => $thislastname,
198                                                                'account_status'    => 'A',
199                                                                'account_expires'   => -1
200                                                        );
201                                                        $thisacctid = $accounts->create($thisaccount_info);
202                                                }
203                                                if (!$thisacctid)       // if we have no account_id, we cant continue
204                                                {
205                                                        continue;
206                                                }
207                                                // Insert default acls for this user.
208                                                // Since the group has app rights, we don't need to give users
209                                                //  these rights.  Instead, we make the user a member of the Default group
210                                                //  below.
211                                                $acl = CreateObject('phpgwapi.acl',(int)$thisacctid);
212                                                $acl->db = $GLOBALS['phpgw_setup']->db;
213                                                $acl->read_repository();
214
215                                                // Only give them admin if we asked for them to have it.
216                                                // This is typically an exception to apps for run rights
217                                                //  as a group member.
218                                                for($a=0;$a<count($admins);$a++)
219                                                {
220                                                        if($admins[$a] == $thisacctlid)
221                                                        {
222                                                                $acl->delete('admin','run',1);
223                                                                $acl->add('admin','run',1);
224                                                        }
225                                                }
226
227                                                // Now make them a member of the 'Default' group.
228                                                // But, only if the current user is not the group itself.
229                                                if(!$defaultgroupid)
230                                                {
231                                                        $defaultgroupid = $accounts->name2id('Default');
232                                                }
233                                                if($defaultgroupid)
234                                                {
235                                                        $acl->delete('phpgw_group',$defaultgroupid,1);
236                                                        $acl->add('phpgw_group',$defaultgroupid,1);
237                                                }
238
239                                                // Save these new acls.
240                                                $acl->save_repository();
241                                        }
242                                }
243                        }
244
245                        if($ldapgroups)
246                        {
247                                while(list($key,$groupid) = each($ldapgroups))
248                                {
249                                        $id_exist = 0;
250                                        $thisacctid    = $group_info[$groupid]['account_id'];
251                                        $thisacctlid   = $group_info[$groupid]['account_lid'];
252                                        $thisfirstname = $group_info[$groupid]['account_firstname'];
253                                        $thislastname  = $group_info[$groupid]['account_lastname'];
254                                        $thismembers   = $group_info[$groupid]['members'];
255
256                                        // Do some checks before we try to import the data.
257                                        if(!empty($thisacctid) && !empty($thisacctlid))
258                                        {
259                                                $groups = CreateObject('phpgwapi.accounts',(int)$thisacctid);
260                                                copyobj($GLOBALS['phpgw_setup']->db,$groups->db);
261
262                                                // Check if the account is already there.
263                                                // If so, we won't try to create it again.
264                                                $acct_exist = $groups->name2id($thisacctlid);
265                                                /* echo '<br<group: ' . $acct_exist; */
266                                                if($acct_exist)
267                                                {
268                                                        $thisacctid = $acct_exist;
269                                                }
270                                                $id_exist = $groups->exists((int)$thisacctid);
271                                                // If not, create it now.
272                                                if(!$id_exist)
273                                                {
274                                                        $thisgroup_info = array(
275                                                                'account_type'      => 'g',
276                                                                'account_lid'       => $thisacctlid,
277                                                                'account_passwd'    => $passwd,
278                                                                'account_firstname' => $thisfirstname,
279                                                                'account_lastname'  => $thislastname,
280                                                                'account_status'    => 'A',
281                                                                'account_expires'   => -1
282                                                        );
283                                                        $thisacctid = $groups->create($thisgroup_info);
284                                                }
285                                                if (!$thisacctid)       // if we have no account_id, we cant continue
286                                                {
287                                                        continue;
288                                                }
289                                                // Now make them a member of this group in phpgw.
290                                                while(list($key,$members) = each($thismembers))
291                                                {
292                                                        if($key == 'count')
293                                                        {
294                                                                continue;
295                                                        }
296                                                        /* echo '<br>members: ' . $members; */
297                                                        $tmpid = 0;
298                                                        @reset($account_info);
299                                                        while(list($x,$y) = each($account_info))
300                                                        {
301                                                                /* echo '<br>checking: '.$y['account_lid']; */
302                                                                if($members == $y['account_lid'])
303                                                                {
304                                                                        $tmpid = $acct->name2id($y['account_lid']);
305                                                                }
306                                                        }
307                                                        /*
308                                                        Insert acls for this group based on memberuid field.
309                                                        Since the group has app rights, we don't need to give users
310                                                        these rights.  Instead, we maintain group membership here.
311                                                        */
312                                                        if($tmpid)
313                                                        {
314                                                                $acl = CreateObject('phpgwapi.acl',$tmpid);
315                                                                copyobj($GLOBALS['phpgw_setup']->db,$acl->db);
316                                                                $acl->account_id = (int)$tmpid;
317                                                                $acl->read_repository();
318
319                                                                $acl->delete('phpgw_group',$thisacctid,1);
320                                                                $acl->add('phpgw_group',$thisacctid,1);
321
322                                                                /* Now add the acl to let them change their password */
323                                                                $acl->delete('preferences','changepassword',1);
324                                                                $acl->add('preferences','changepassword',1);
325
326                                                                $acl->save_repository();
327
328                                                                /* Add prefs for selected apps here, since they are per-user.
329                                                                        App access is added below.
330                                                                */
331                                                                $pref = CreateObject('phpgwapi.preferences',$tmpid);
332                                                                $pref->db = $GLOBALS['phpgw_setup']->db;
333                                                                $pref->account_id = (int)$tmpid;
334                                                                $pref->read_repository();
335                                                                @reset($s_apps);
336                                                                while(list($key,$app) = each($s_apps))
337                                                                {
338                                                                        $phpgw->hooks->single('add_def_pref',$app);
339                                                                }
340                                                                $pref->save_repository();
341                                                        }
342                                                }
343                                                /* Now give this group some rights */
344                                                $phpgw_info['user']['account_id'] = $thisacctid;
345                                                $acl = CreateObject('phpgwapi.acl');
346                                                copyobj($GLOBALS['phpgw_setup']->db,$acl->db);
347                                                $acl->account_id = (int)$thisacctid;
348                                                $acl->read_repository();
349                                                @reset($s_apps);
350                                                while(list($key,$app) = each($s_apps))
351                                                {
352                                                        $acl->delete($app,'run',1);
353                                                        $acl->add($app,'run',1);
354                                                }
355                                                $acl->save_repository();
356                                                $defaultgroupid = $thisacctid;
357                                        }
358                                }
359                        }
360                        else
361                        {
362                                /* Create the 'Default' group */
363                                $groups = CreateObject('phpgwapi.accounts',$defaultgroupid);
364                                copyobj($GLOBALS['phpgw_setup']->db,$groups->db);
365
366                                // Check if the group account is already there.
367                                // If so, set our group_id to that account's id for use below.
368                                $acct_exist = $groups->name2id('Default');
369                                if($acct_exist)
370                                {
371                                        $defaultgroupid = $acct_exist;
372                                }
373                                $id_exist   = $groups->exists((int)$defaultgroupid);
374                                // if not, create it, using our original groupid.
375                                if($id_exist)
376                                {
377                                        $groups->delete($defaultgroupid);
378                                }
379                                $thisgroup_info = array(
380                                        'account_type'      => 'g',
381                                        'account_lid'       => 'Default',
382                                        'account_passwd'    => $passwd,
383                                        'account_firstname' => 'Default',
384                                        'account_lastname'  => 'Group',
385                                        'account_status'    => 'A',
386                                        'account_expires'   => -1
387                                );
388                                $acct->create($thisgroup_info);
389
390                                $defaultgroupid = $acct->name2id('Default');
391
392                                $acl = CreateObject('phpgwapi.acl',$defaultgroupid);
393                                copyobj($GLOBALS['phpgw_setup']->db,$acl->db);
394                                $acl->account_id = (int)$defaultgroupid;
395                                $acl->read_repository();
396                                @reset($s_apps);
397                                while(list($key,$app) = each($s_apps))
398                                {
399                                        $acl->delete($app,'run',1);
400                                        $acl->add($app,'run',1);
401                                }
402                                $acl->save_repository();
403                        } //end default group creation
404                }
405                $setup_complete = True;
406        }
407
408        $GLOBALS['phpgw_setup']->html->show_header(lang('LDAP Import'),False,'config',$GLOBALS['phpgw_setup']->ConfigDomain . '(' . $phpgw_domain[$GLOBALS['phpgw_setup']->ConfigDomain]['db_type'] . ')');
409
410        if($error)
411        {
412                //echo '<br><center><b>Error:</b> '.$error.'</center>';
413                $GLOBALS['phpgw_setup']->html->show_alert_msg('Error',$error);
414        }
415
416        if($setup_complete)
417        {
418                echo '<br><center>'.lang('Import has been completed!').' '.lang('Click <a href="index.php">here</a> to return to setup.').'</center>';
419                $GLOBALS['phpgw_setup']->html->show_footer();
420                exit;
421        }
422
423        $setup_tpl->set_block('ldap','header','header');
424        $setup_tpl->set_block('ldap','user_list','user_list');
425        $setup_tpl->set_block('ldap','admin_list','admin_list');
426        $setup_tpl->set_block('ldap','group_list','group_list');
427        $setup_tpl->set_block('ldap','app_list','app_list');
428        $setup_tpl->set_block('ldap','submit','submit');
429        $setup_tpl->set_block('ldap','footer','footer');
430
431        while(list($key,$account) = each($account_info))
432        {
433                $user_list .= '<option value="' . $account['account_id'] . '">'
434                        . $common->display_fullname($account['account_lid'],$account['account_firstname'],$account['account_lastname'])
435                        . '</option>';
436        }
437
438        @reset($account_info);
439        while(list($key,$account) = each($account_info))
440        {
441                $admin_list .= '<option value="' . $account['account_lid'] . '">'
442                        . $common->display_fullname($account['account_lid'],$account['account_firstname'],$account['account_lastname'])
443                        . '</option>';
444        }
445
446        while(list($key,$group) = each($group_info))
447        {
448                $group_list .= '<option value="' . $group['account_id'] . '">'
449                        . $group['account_lid']
450                        . '</option>';
451        }
452
453        while(list($appname,$apptitle) = each($apps))
454        {
455                if($appname == 'admin' ||
456                        $appname == 'skel' ||
457                        $appname == 'backup' ||
458                        $appname == 'netsaint' ||
459                        $appname == 'developer_tools' ||
460                        $appname == 'phpsysinfo' ||
461                        $appname == 'eldaptir' ||
462                        $appname == 'qmailldap')
463                {
464                        $app_list .= '<option value="' . $appname . '">' . $apptitle . '</option>';
465                }
466                else
467                {
468                        $app_list .= '<option value="' . $appname . '" selected>' . $apptitle . '</option>';
469                }
470        }
471
472        $setup_tpl->set_var('action_url','ldapimport.php');
473        $setup_tpl->set_var('users',$user_list);
474        $setup_tpl->set_var('admins',$admin_list);
475        $setup_tpl->set_var('ldapgroups',$group_list);
476        $setup_tpl->set_var('s_apps',$app_list);
477
478        $setup_tpl->set_var('ldap_import',lang('LDAP import users'));
479        $setup_tpl->set_var('description',lang("This section will help you import users and groups from your LDAP tree into eGroupWare's account tables").'.');
480        $setup_tpl->set_var('select_users',lang('Select which user(s) will be imported'));
481        $setup_tpl->set_var('select_admins',lang('Select which user(s) will have admin privileges'));
482        $setup_tpl->set_var('select_groups',lang('Select which group(s) will be imported (group membership will be maintained)'));
483        $setup_tpl->set_var('select_apps',lang('Select the default applications to which your users will have access').'.');
484        $setup_tpl->set_var('note',lang('Note: You will be able to customize this later').'.');
485        $setup_tpl->set_var('form_submit','import');
486        $setup_tpl->set_var('cancel',lang('Cancel'));
487
488        $setup_tpl->pfp('out','header');
489        $setup_tpl->pfp('out','user_list');
490        $setup_tpl->pfp('out','admin_list');
491        $setup_tpl->pfp('out','group_list');
492        $setup_tpl->pfp('out','app_list');
493        $setup_tpl->pfp('out','submit');
494        $setup_tpl->pfp('out','footer');
495
496        $GLOBALS['phpgw_setup']->html->show_footer();
497?>
Note: See TracBrowser for help on using the repository browser.