source: trunk/setup/ldapmodify.php @ 2

Revision 2, 12.1 KB checked in by niltonneto, 17 years ago (diff)

Removida todas as tags usadas pelo CVS ($Id, $Source).
Primeira versão no CVS externo.

  • 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("SELECT config_name,config_value FROM phpgw_config WHERE config_name LIKE 'ldap%' OR config_name='account_repository'",__LINE__,__FILE__);
54        while($GLOBALS['phpgw_setup']->db->next_record())
55        {
56                $config[$GLOBALS['phpgw_setup']->db->f('config_name')] = $GLOBALS['phpgw_setup']->db->f('config_value');
57        }
58        $phpgw_info['server']['ldap_host']          = $config['ldap_host'];
59        $phpgw_info['server']['ldap_context']       = $config['ldap_context'];
60        $phpgw_info['server']['ldap_group_context'] = $config['ldap_group_context'];
61        $phpgw_info['server']['ldap_root_dn']       = $config['ldap_root_dn'];
62        $phpgw_info['server']['ldap_root_pw']       = $config['ldap_root_pw'];
63        $phpgw_info['server']['account_repository'] = $config['account_repository'];
64        $phpgw_info['server']['ldap_version3']      = $config['ldap_version3'];
65
66        $phpgw->accounts = CreateObject('phpgwapi.accounts');
67        $acct            = $phpgw->accounts;
68
69        /* connect to ldap server */
70        if(!$ldap = $common->ldapConnect())
71        {
72                $noldapconnection = True;
73        }
74
75        if($noldapconnection)
76        {
77                Header('Location: config.php?error=badldapconnection');
78                exit;
79        }
80
81        // Take the users from LDAP.
82        $sr = ldap_search($ldap,$config['ldap_context'],'(|(uid=*))',array('cn','givenname','uid','uidnumber'));
83        $info = ldap_get_entries($ldap, $sr);
84        $tmp = '';
85
86        for($i=0; $i<$info['count']; $i++)
87        {
88                if(!$phpgw_info['server']['global_denied_users'][$info[$i]['uid'][0]])
89                {
90                        $account_info[$info[$i]['uidnumber'][0]] = $info[$i];
91                }
92        }
93
94        if($phpgw_info['server']['ldap_group_context'])
95        {
96                $srg = ldap_search($ldap,$config['ldap_group_context'],'(&(cn=*)(objectClass=posixGroup))',array('gidnumber','cn','memberuid'));
97                $info = ldap_get_entries($ldap, $srg);
98                $tmp = '';
99
100                for($i=0; $i<$info['count']; $i++)
101                {
102                        if(!$phpgw_info['server']['global_denied_groups'][$info[$i]['cn'][0]] &&
103                                !$account_info[$i][$info[$i]['cn'][0]])
104                        {
105                                $group_info[$info[$i]['gidnumber'][0]] = $info[$i];
106                        }
107                }
108        }
109        else
110        {
111                $group_info = array();
112        }
113
114        $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__);
115        while($GLOBALS['phpgw_setup']->db->next_record())
116        {
117                $apps[$GLOBALS['phpgw_setup']->db->f('app_name')] = lang($GLOBALS['phpgw_setup']->db->f('app_name'));
118        }
119
120        if($cancel)
121        {
122                Header("Location: ldap.php");
123                exit;
124        }
125
126        $GLOBALS['phpgw_setup']->html->show_header(lang('LDAP Modify'),False,'config',$GLOBALS['phpgw_setup']->ConfigDomain . '(' . $phpgw_domain[$GLOBALS['phpgw_setup']->ConfigDomain]['db_type'] . ')');
127        $setup_complete = False;
128        if(isset($_POST['submit']))
129        {
130                $acl = CreateObject('phpgwapi.acl');
131                copyobj($GLOBALS['phpgw_setup']->db,$acl->db);
132                if(isset($_POST['ldapgroups']))
133                {
134                        $groups = CreateObject('phpgwapi.accounts');
135                        copyobj($GLOBALS['phpgw_setup']->db,$groups->db);
136                        while(list($key,$groupid) = each($_POST['ldapgroups']))
137                        {
138                                $id_exist = 0;
139                                $entry = array();
140                                $thisacctid    = $group_info[$groupid]['gidnumber'][0];
141                                $thisacctlid   = $group_info[$groupid]['cn'][0];
142                                /* echo "Updating GROUPID : ".$thisacctlid."<br>\n"; */
143                                $thisfirstname = $group_info[$groupid]['cn'][0];
144                                $thismembers   = $group_info[$groupid]['memberuid'];
145                                $thisdn        = $group_info[$groupid]['dn'];
146
147                                /* Do some checks before we try to import the data. */
148                                if(!empty($thisacctid) && !empty($thisacctlid))
149                                {
150                                        $groups->account_id = (int)$thisacctid;
151
152                                        $sr = ldap_search($ldap,$config['ldap_group_context'],'cn='.$thisacctlid);
153                                        $entry = ldap_get_entries($ldap, $sr);
154
155                                        reset($entry[0]['objectclass']);
156                                        $addclass = True;
157                                        while(list($key,$value) = each($entry[0]['objectclass']))
158                                        {
159                                                if(strtolower($value) == 'phpgwaccount')
160                                                {
161                                                        $addclass = False;
162                                                }
163                                        }
164                                        if($addclass)
165                                        {
166                                                reset($entry[0]['objectclass']);
167                                                $replace['objectclass'] = $entry[0]['objectclass'];
168                                                unset($replace['objectclass']['count']);
169                                                $replace['objectclass'][]       = 'phpgwAccount';
170                                                sort($replace['objectclass']);
171                                                ldap_mod_replace($ldap,$thisdn,$replace);
172                                                unset($replace);
173                                                unset($addclass);
174                                        }
175                                        unset($add);
176                                        if(!@isset($entry[0]['phpgwaccountstatus']))
177                                        {
178                                                $add['phpgwaccountstatus'][] = 'A';
179                                        }
180                                        if(!@isset($entry[0]['phpgwaccounttype']))
181                                        {
182                                                $add['phpgwaccounttype'][] = 'g';
183                                        }
184                                        if(!@isset($entry[0]['phpgwaccountexpires']))
185                                        {
186                                                $add['phpgwaccountexpires'][] = -1;
187                                        }
188                                        if(@isset($add))
189                                        {
190                                                ldap_mod_add($ldap,$thisdn,$add);
191                                        }
192
193                                        /* Now make the members a member of this group in phpgw. */
194                                        if(is_array($thismembers))
195                                        {
196                                                foreach($thismembers as $key => $members)
197                                                {
198                                                        if($key == 'count')
199                                                        {
200                                                                continue;
201                                                        }
202                                                        echo '<br>members: ' . $members;
203                                                        $tmpid = 0;
204                                                        @reset($account_info);
205                                                        while(list($x,$y) = each($account_info))
206                                                        {
207                                                                //echo '<br>checking: '.$y['uid'][0];
208                                                                if($members == $y['uid'][0])
209                                                                {
210                                                                        $tmpid = $y['uidnumber'][0];
211                                                                }
212                                                        }
213                                                        // Insert acls for this group based on memberuid field.
214                                                        // Since the group has app rights, we don't need to give users
215                                                        //  these rights.  Instead, we maintain group membership here.
216                                                        if($tmpid)
217                                                        {
218                                                                echo '<br>inserindo user_id: ' . $tmpid . ' em ' . $thisacctid;
219                                                                $acl->account_id = (int)$tmpid;
220                                                                $acl->read_repository();
221       
222                                                                $acl->delete('phpgw_group',$thisacctid,1);
223                                                                $acl->add('phpgw_group',$thisacctid,1);
224       
225                                                                // Now add the acl to let them change their password
226                                                                $acl->delete('preferences','changepassword',1);
227                                                                $acl->add('preferences','changepassword',1);
228       
229                                                                $acl->save_repository();
230                                                        }
231                                                }
232                                        }
233                                        /* Now give this group some rights */
234                                        $phpgw_info['user']['account_id'] = $thisacctid;
235                                        $acl->account_id = (int)$thisacctid;
236                                        $acl->read_repository();
237                                        @reset($_POST['s_apps']);
238                                        while(list($key,$app) = @each($_POST['s_apps']))
239                                        {
240                                                $acl->delete($app,'run',1);
241                                                $acl->add($app,'run',1);
242                                        }
243                                        $acl->save_repository();
244                                        $defaultgroupid = $thisacctid;
245                                }
246                        }
247                }
248
249                if(isset($_POST['users']))
250                {
251                        $accounts = CreateObject('phpgwapi.accounts');
252                        copyobj($GLOBALS['phpgw_setup']->db,$accounts->db);
253                        $users_process = 0;
254                        $new_uidnumber = 12011;
255                        while(list($key,$id) = each($_POST['users']))
256                        {
257                                $id_exist = 0;
258                                $thisacctid  = $account_info[$id]['uidnumber'][0];
259                                $thisacctlid = $account_info[$id]['uid'][0];
260                                $thisdn      = $account_info[$id]['dn'];
261                                echo $thisdn . "<BR>\n";
262                                echo "Updating USERID : ".$thisacctlid."<br>\n";
263                               
264                                /* Do some checks before we try to import the data. */
265                                if(!empty($thisacctid) && !empty($thisacctlid))
266                                {
267                                        $users_process++;
268
269                                        $accounts->account_id = (int)$thisacctid;
270                                       
271                                        /* Insert default acls for this user. */
272                                        $acl->account_id = (int)$thisacctid;
273                                        $acl->read_repository();
274       
275                                        // Now add the acl to let them change their password
276                                        $acl->delete('preferences','changepassword',1);
277                                        $acl->add('preferences','changepassword',1);
278
279                                        // Add user to a default group, previous created
280                                        $acl->add('phpgw_group','12007',1);
281
282                                        echo "Adding in ACL BD: ".$thisacctid."<br><br>\n";
283                                        /* Save these new acls. */
284                                        $acl->save_repository();
285                                               
286                                        $new_uidnumber++;
287                                }
288                        }
289                }
290                $setup_complete = True;
291        }
292
293        if(isset($_GET['error']))
294        {
295                /* echo '<br><center><b>Error:</b> '.$error.'</center>'; */
296                $GLOBALS['phpgw_setup']->html->show_alert_msg('Error',$_GET['error']);
297        }
298
299        if($setup_complete)
300        {
301                echo '<br><center>'.lang('Modifications have been completed!').' '.lang('Click <a href="index.php">here</a> to return to setup.').'<br><center>';
302                $GLOBALS['phpgw_setup']->html->show_footer();
303                exit;
304        }
305
306        $setup_tpl->set_block('ldap','header','header');
307        $setup_tpl->set_block('ldap','user_list','user_list');
308        $setup_tpl->set_block('ldap','admin_list','admin_list');
309        $setup_tpl->set_block('ldap','group_list','group_list');
310        $setup_tpl->set_block('ldap','app_list','app_list');
311        $setup_tpl->set_block('ldap','submit','submit');
312        $setup_tpl->set_block('ldap','footer','footer');
313
314        $user_list = '';
315        while(list($key,$account) = @each($account_info))
316        {
317                $user_list .= '<option value="' . $account['uidnumber'][0] . '">' . utf8_decode($account['cn'][0]) . ' (' . $account['uid'][0] . ')</option>';
318        }
319
320        $admin_list = '';
321        @reset($account_info);
322        while (list($key,$account) = @each($account_info))
323        {
324                $admin_list .= '<option value="' . $account['uidnumber'][0] . '">' . utf8_decode($account['cn'][0]) . ' (' . $account['uid'][0] . ')</option>';
325        }
326
327        $group_list = '';
328        while(list($key,$group) = @each($group_info))
329        {
330                $group_list .= '<option value="' . $group['gidnumber'][0] . '">' . utf8_decode($group['cn'][0])  . '</option>';
331        }
332
333        $app_list = '';
334        while(list($appname,$apptitle) = each($apps))
335        {
336                if($appname == 'admin' ||
337                        $appname == 'skel' ||
338                        $appname == 'backup' ||
339                        $appname == 'netsaint' ||
340                        $appname == 'developer_tools' ||
341                        $appname == 'phpsysinfo' ||
342                        $appname == 'eldaptir' ||
343                        $appname == 'qmailldap')
344                {
345                        $app_list .= '<option value="' . $appname . '">' . $apptitle . '</option>';
346                }
347                else
348                {
349                        $app_list .= '<option value="' . $appname . '" selected>' . $apptitle . '</option>';
350                }
351        }
352
353        $setup_tpl->set_var('action_url','ldapmodify2.php');
354        $setup_tpl->set_var('users',$user_list);
355        $setup_tpl->set_var('admins',$admin_list);
356        $setup_tpl->set_var('ldapgroups',$group_list);
357        $setup_tpl->set_var('s_apps',$app_list);
358
359        $setup_tpl->set_var('ldap_import',lang('LDAP Modify'));
360        $setup_tpl->set_var('description',lang("This section will help you setup your LDAP accounts for use with eGroupWare").'.');
361        $setup_tpl->set_var('select_users',lang('Select which user(s) will be modified'));
362        $setup_tpl->set_var('select_admins',lang('Select which user(s) will also have admin privileges'));
363        $setup_tpl->set_var('select_groups',lang('Select which group(s) will be modified (group membership will be maintained)'));
364        $setup_tpl->set_var('select_apps',lang('Select the default applications to which your users will have access').'.');
365        $setup_tpl->set_var('form_submit',lang('Modify2'));
366        $setup_tpl->set_var('cancel',lang('Cancel'));
367
368        $setup_tpl->pfp('out','header');
369        $setup_tpl->pfp('out','user_list');
370        $setup_tpl->pfp('out','admin_list');
371        $setup_tpl->pfp('out','group_list');
372        $setup_tpl->pfp('out','app_list');
373        $setup_tpl->pfp('out','submit');
374        $setup_tpl->pfp('out','footer');
375
376        $GLOBALS['phpgw_setup']->html->show_footer();
377?>
Note: See TracBrowser for help on using the repository browser.