source: companies/serpro/setup/ldapmodify.php @ 903

Revision 903, 12.5 KB checked in by niltonneto, 15 years ago (diff)

Importacao inicial do Expresso do Serpro

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                                        if(!@isset($entry[0][$GLOBALS['phpgw_info']['server']['atributoexpiracao']]))
186                                                {
187                                                if(isset($GLOBALS['phpgw_info']['server']['atributoexpiracao']))
188                                                                {
189                                                                $add[$GLOBALS['phpgw_info']['server']['atributoexpiracao']][] = -1;
190                                                                }
191                                                        else
192                                                                {
193                                                                $add['phpgwaccountexpires'][] = -1;
194                                                                }
195                                                //$add[$GLOBALS['phpgw_info']['server']['atributoexpiracao']][] = -1;
196                                                //$add['phpgwaccountexpires'][] = -1;
197                                                }
198                                        if(@isset($add))
199                                        {
200                                                ldap_mod_add($ldap,$thisdn,$add);
201                                        }
202
203                                        /* Now make the members a member of this group in phpgw. */
204                                        if(is_array($thismembers))
205                                        {
206                                                foreach($thismembers as $key => $members)
207                                                {
208                                                        if($key == 'count')
209                                                        {
210                                                                continue;
211                                                        }
212                                                        echo '<br>members: ' . $members;
213                                                        $tmpid = 0;
214                                                        @reset($account_info);
215                                                        while(list($x,$y) = each($account_info))
216                                                        {
217                                                                //echo '<br>checking: '.$y['uid'][0];
218                                                                if($members == $y['uid'][0])
219                                                                {
220                                                                        $tmpid = $y['uidnumber'][0];
221                                                                }
222                                                        }
223                                                        // Insert acls for this group based on memberuid field.
224                                                        // Since the group has app rights, we don't need to give users
225                                                        //  these rights.  Instead, we maintain group membership here.
226                                                        if($tmpid)
227                                                        {
228                                                                echo '<br>inserindo user_id: ' . $tmpid . ' em ' . $thisacctid;
229                                                                $acl->account_id = (int)$tmpid;
230                                                                $acl->read_repository();
231       
232                                                                $acl->delete('phpgw_group',$thisacctid,1);
233                                                                $acl->add('phpgw_group',$thisacctid,1);
234       
235                                                                // Now add the acl to let them change their password
236                                                                $acl->delete('preferences','changepassword',1);
237                                                                $acl->add('preferences','changepassword',1);
238       
239                                                                $acl->save_repository();
240                                                        }
241                                                }
242                                        }
243                                        /* Now give this group some rights */
244                                        $phpgw_info['user']['account_id'] = $thisacctid;
245                                        $acl->account_id = (int)$thisacctid;
246                                        $acl->read_repository();
247                                        @reset($_POST['s_apps']);
248                                        while(list($key,$app) = @each($_POST['s_apps']))
249                                        {
250                                                $acl->delete($app,'run',1);
251                                                $acl->add($app,'run',1);
252                                        }
253                                        $acl->save_repository();
254                                        $defaultgroupid = $thisacctid;
255                                }
256                        }
257                }
258
259                if(isset($_POST['users']))
260                {
261                        $accounts = CreateObject('phpgwapi.accounts');
262                        copyobj($GLOBALS['phpgw_setup']->db,$accounts->db);
263                        $users_process = 0;
264                        $new_uidnumber = 12011;
265                        while(list($key,$id) = each($_POST['users']))
266                        {
267                                $id_exist = 0;
268                                $thisacctid  = $account_info[$id]['uidnumber'][0];
269                                $thisacctlid = $account_info[$id]['uid'][0];
270                                $thisdn      = $account_info[$id]['dn'];
271                                echo $thisdn . "<BR>\n";
272                                echo "Updating USERID : ".$thisacctlid."<br>\n";
273                               
274                                /* Do some checks before we try to import the data. */
275                                if(!empty($thisacctid) && !empty($thisacctlid))
276                                {
277                                        $users_process++;
278
279                                        $accounts->account_id = (int)$thisacctid;
280                                       
281                                        /* Insert default acls for this user. */
282                                        $acl->account_id = (int)$thisacctid;
283                                        $acl->read_repository();
284       
285                                        // Now add the acl to let them change their password
286                                        $acl->delete('preferences','changepassword',1);
287                                        $acl->add('preferences','changepassword',1);
288
289                                        // Add user to a default group, previous created
290                                        $acl->add('phpgw_group','12007',1);
291
292                                        echo "Adding in ACL BD: ".$thisacctid."<br><br>\n";
293                                        /* Save these new acls. */
294                                        $acl->save_repository();
295                                               
296                                        $new_uidnumber++;
297                                }
298                        }
299                }
300                $setup_complete = True;
301        }
302
303        if(isset($_GET['error']))
304        {
305                /* echo '<br><center><b>Error:</b> '.$error.'</center>'; */
306                $GLOBALS['phpgw_setup']->html->show_alert_msg('Error',$_GET['error']);
307        }
308
309        if($setup_complete)
310        {
311                echo '<br><center>'.lang('Modifications have been completed!').' '.lang('Click <a href="index.php">here</a> to return to setup.').'<br><center>';
312                $GLOBALS['phpgw_setup']->html->show_footer();
313                exit;
314        }
315
316        $setup_tpl->set_block('ldap','header','header');
317        $setup_tpl->set_block('ldap','user_list','user_list');
318        $setup_tpl->set_block('ldap','admin_list','admin_list');
319        $setup_tpl->set_block('ldap','group_list','group_list');
320        $setup_tpl->set_block('ldap','app_list','app_list');
321        $setup_tpl->set_block('ldap','submit','submit');
322        $setup_tpl->set_block('ldap','footer','footer');
323
324        $user_list = '';
325        while(list($key,$account) = @each($account_info))
326        {
327                $user_list .= '<option value="' . $account['uidnumber'][0] . '">' . utf8_decode($account['cn'][0]) . ' (' . $account['uid'][0] . ')</option>';
328        }
329
330        $admin_list = '';
331        @reset($account_info);
332        while (list($key,$account) = @each($account_info))
333        {
334                $admin_list .= '<option value="' . $account['uidnumber'][0] . '">' . utf8_decode($account['cn'][0]) . ' (' . $account['uid'][0] . ')</option>';
335        }
336
337        $group_list = '';
338        while(list($key,$group) = @each($group_info))
339        {
340                $group_list .= '<option value="' . $group['gidnumber'][0] . '">' . utf8_decode($group['cn'][0])  . '</option>';
341        }
342
343        $app_list = '';
344        while(list($appname,$apptitle) = each($apps))
345        {
346                if($appname == 'admin' ||
347                        $appname == 'skel' ||
348                        $appname == 'backup' ||
349                        $appname == 'netsaint' ||
350                        $appname == 'developer_tools' ||
351                        $appname == 'phpsysinfo' ||
352                        $appname == 'eldaptir' ||
353                        $appname == 'qmailldap')
354                {
355                        $app_list .= '<option value="' . $appname . '">' . $apptitle . '</option>';
356                }
357                else
358                {
359                        $app_list .= '<option value="' . $appname . '" selected>' . $apptitle . '</option>';
360                }
361        }
362
363        $setup_tpl->set_var('action_url','ldapmodify2.php');
364        $setup_tpl->set_var('users',$user_list);
365        $setup_tpl->set_var('admins',$admin_list);
366        $setup_tpl->set_var('ldapgroups',$group_list);
367        $setup_tpl->set_var('s_apps',$app_list);
368
369        $setup_tpl->set_var('ldap_import',lang('LDAP Modify'));
370        $setup_tpl->set_var('description',lang("This section will help you setup your LDAP accounts for use with eGroupWare").'.');
371        $setup_tpl->set_var('select_users',lang('Select which user(s) will be modified'));
372        $setup_tpl->set_var('select_admins',lang('Select which user(s) will also have admin privileges'));
373        $setup_tpl->set_var('select_groups',lang('Select which group(s) will be modified (group membership will be maintained)'));
374        $setup_tpl->set_var('select_apps',lang('Select the default applications to which your users will have access').'.');
375        $setup_tpl->set_var('form_submit',lang('Modify2'));
376        $setup_tpl->set_var('cancel',lang('Cancel'));
377
378        $setup_tpl->pfp('out','header');
379        $setup_tpl->pfp('out','user_list');
380        $setup_tpl->pfp('out','admin_list');
381        $setup_tpl->pfp('out','group_list');
382        $setup_tpl->pfp('out','app_list');
383        $setup_tpl->pfp('out','submit');
384        $setup_tpl->pfp('out','footer');
385
386        $GLOBALS['phpgw_setup']->html->show_footer();
387?>
Note: See TracBrowser for help on using the repository browser.