Code

Added initial department migration step.
[gosa.git] / setup / class_setupStep_Migrate.inc
1 <?php
3 /*
4    This code is part of GOsa (https://gosa.gonicus.de)
5    Copyright (C) 2007 Fabian Hickert
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20 */
23 class Step_Migrate extends setup_step
24 {
25   var $languages      = array();
26   var $attributes     = array();
27   var $header_image   = "images/monitoring.png";
28   var $checks         = array();
30   /* Department migration attributes */
31   var $migration_dialog   = FALSE;
32   var $deps_to_migrate    = array();
34   
36   function Step_Migrate()
37   {
38     $this->update_strings(); 
39     $this->initialize_checks();
40   }
42   function update_strings()
43   {
44     $this->s_title      = _("LDAP inspection");
45     $this->s_title_long = _("LDAP inspection");
46     $this->s_info       = _("Analyze your current LDAP for GOsa compatibility");
47   }
48  
49   function initialize_checks()
50   {
51     $this->checks = array();
52     $this->checks['permissions']['TITLE']     = _("Checking permissions on ldap database");
53     $this->checks['permissions']['STATUS']    = FALSE;
54     $this->checks['permissions']['STATUS_MSG']= "";
55     $this->checks['permissions']['ERROR_MSG'] = "";
56     $this->check_ldap_permissions();
58     $this->checks['deps_visible']['TITLE']     = _("Checking for invisible deparmtments");
59     $this->checks['deps_visible']['STATUS']    = FALSE;
60     $this->checks['deps_visible']['STATUS_MSG']= "";
61     $this->checks['deps_visible']['ERROR_MSG'] = "";
62     $this->check_visible_organizationalUnits();
63   }
66   /* Check ldap accessibility 
67    * Create and remove a dummy object, 
68    *  to ensure that we have the necessary permissions
69    */
70   function check_ldap_permissions()
71   {
72     $cv = $this->parent->captured_values;
73     $ldap = new LDAP($cv['admin'],
74                      $cv['password'],
75                      $cv['connection'],
76                      FALSE,
77                      $cv['tls']);
78     $name     = "GOsa_setup_text_entry_".session_id().rand(0,999999);
79     $dn       = "ou=".$name.",".$cv['base'];
80     $testEntry= array();
81     $testEntry['objectClass'][]= "top";
82     $testEntry['objectClass'][]= "organizationalUnit";
83     $testEntry['objectClass'][]= "gosaDepartment";
84     $testEntry['description']= "Created by GOsa setup, this object can be removed.";
85     $testEntry['ou']  = $name;
86  
87     $ldap->cd ($dn);
88     $res = $ldap->add($testEntry);
89     if(!$res){
90       $this->checks['permissions']['STATUS']    = FALSE;
91       $this->checks['permissions']['STATUS_MSG']= _("Failed");
92       $this->checks['permissions']['ERROR_MSG'] = 
93         sprintf(_("The specified user '%s' does not have full access to your ldap database."),$cv['admin']);
94       $this->checks['permissions']['ERROR_MSG'].=
95         "<input type='submit' name='retry_permissions' value='"._("Retry")."'>";
96       return(false);
97     }
99     $res = $ldap->rmDir($dn);
100     if(!$res){
101       $this->checks['permissions']['STATUS']    = FALSE;
102       $this->checks['permissions']['STATUS_MSG']= _("Failed");
103       $this->checks['permissions']['ERROR_MSG'] = 
104         sprintf(_("The specified user '%s' does not have full access to your ldap database."),$cv['admin']);
105       $this->checks['permissions']['ERROR_MSG'].=
106         "<input type='submit' name='retry_permissions' value='"._("Retry")."'>";
107       return(false);
108     }
110     $this->checks['permissions']['STATUS']    = TRUE;
111     $this->checks['permissions']['STATUS_MSG']= _("Ok");
112     $this->checks['permissions']['ERROR_MSG'] = "<input type='submit' name='retry_permissions' value='"._("Retry")."'>";
113     return(true);
114   } 
117   function check_visible_organizationalUnits()
118   {
119     $this->deps_to_migrate = array();
120     $cnt_ok = 0;
122     /* Get collected configuration settings */
123     $cv = $this->parent->captured_values;
125     /* Establish ldap connection */
126     $ldap = new LDAP($cv['admin'],
127                      $cv['password'],
128                      $cv['connection'],
129                      FALSE,
130                      $cv['tls']);
132     /* Skip GOsa internal departments */
133     $skip_dns = array("/^ou=people,/","/^ou=groups,/","/(,|)ou=configs,/","/(,|)ou=systems,/",
134                       "/^ou=apps,/","/^ou=mime,/","/^ou=aclroles,/","/^ou=incoming,/",
135                       "/ou=snapshots,/","/(,|)dc=addressbook,/","/^(,|)ou=machineaccounts,/",
136                       "/(,|)ou=winstations,/");
139     /* Get all invisible departments */
140     $ldap->cd($cv['base']); 
141     $ldap->search("(&(objectClass=organizationalUnit)(!(objectClass=gosaDepartment)))",array("ou","description","dn"));
142     while($attrs = $ldap->fetch()){
143       $attrs['checked'] = FALSE;
144       $this->deps_to_migrate[] = $attrs;
145     }
146   
147     /* Filter returned list of departments */
148     foreach($this->deps_to_migrate as $key => $attrs){
149       $dn = $attrs['dn'];
150       $skip = false;
151       foreach($skip_dns as $skip_dn){
152         if(preg_match($skip_dn,$dn)){
153           $skip = true;
154         }
155       }
156       if($skip){
157         unset($this->deps_to_migrate[$key]);
158       }
159     }
160   
161     /* No invisible */
162     if(count($this->deps_to_migrate) == 0){
163       $this->checks['deps_visible']['STATUS']    = TRUE;
164       $this->checks['deps_visible']['STATUS_MSG']= _("Ok");
165       $this->checks['deps_visible']['ERROR_MSG'] = "";
166       $this->checks['deps_visible']['ERROR_MSG'] .= "<input type='submit' name='deps_visible_migrate_refresh' value='"._("Retry")."'>";
167     }else{
168       $this->checks['deps_visible']['STATUS']    = FALSE;
169       $this->checks['deps_visible']['STATUS_MSG']= sprintf(_("%s entries found"),count($this->deps_to_migrate));
170       $this->checks['deps_visible']['ERROR_MSG'] = sprintf(_("Found %s departments that will not be visible in GOsa."),count($this->deps_to_migrate));
171       $this->checks['deps_visible']['ERROR_MSG'] .= "<input type='submit' name='deps_visible_migrate' value='"._("Migrate")."'>";
172       $this->checks['deps_visible']['ERROR_MSG'] .= "<input type='submit' name='deps_visible_migrate_refresh' value='"._("Reload list")."'>";
173     }
175   }
178     
179   /* Start deparmtment migration */  
180   function migrate_organizationalUnits()
181   {
182     /* Get collected configuration settings */
183     $cv = $this->parent->captured_values;
185     /* Establish ldap connection */
186     $ldap = new LDAP($cv['admin'],
187                      $cv['password'],
188                      $cv['connection'],
189                      FALSE,
190                      $cv['tls']);
192     foreach($this->deps_to_migrate as $dep){
193       if($dep['checked']){
195         $ldap->cat($dep['dn'],array("objectClass","description"));
196         $attrs      = $ldap->fetch();
197         $new_attrs  = array();
199         for($i = 0 ; $i < $attrs['objectClass']['count']; $i ++ ){
200           $new_attrs['objectClass'][]   = $attrs['objectClass'][$i];
201         }
202         $new_attrs['objectClass'][] = "gosaDepartment";
203     
204         if(!isset($attrs['description'])){
205           $new_attrs['description'][] = "GOsa department";
206         }
207         
208         $ldap->cd($attrs['dn']);
209         if(!$ldap->modify($new_attrs)){
210           print_red(sprintf(_("Failed to migrate the department '%s' into GOsa, error message is as follows '%s'."),$attrs['dn'],$ldap->get_error()));
211           return(false);
212         }
213       }
214     }
215     return(TRUE);
216   }
220   function execute()
221   {
222     /* Permission check */
223     $this->check_ldap_permissions();
225     /* Migration options 
226      */
227     /* Refresh list of deparments */
228     if(isset($_POST['deps_visible_migrate_refresh'])){
229       $this->check_visible_organizationalUnits();
230     }
232     /* Open migration dialog */
233     if(isset($_POST['deps_visible_migrate'])){
234       $this->migration_dialog = TRUE;
235       $this->dialog =TRUE;
236     }
238     /* Close migration dialog */
239     if(isset($_POST['deps_visible_migrate_close'])){
240       $this->migration_dialog = FALSE;
241       $this->dialog =FALSE;
242     }
244     /* Start migration */
245     if(isset($_POST['deps_visible_migrate_migrate'])){
246       if($this->migrate_organizationalUnits()){
247         $this->check_visible_organizationalUnits();
248       }
249     }
251     /* Display migration dialog */
252     if($this->migration_dialog){
253       $smarty = get_smarty();
254       $smarty->assign("deps_to_migrate",$this->deps_to_migrate);
255       $smarty->assign("method","migrate");
256       return($smarty->fetch(get_template_path("setup_migrate.tpl",TRUE,dirname(__FILE__))));
257     }
259     $smarty = get_smarty();
260     $smarty->assign("checks",$this->checks);
261     $smarty->assign("method","default");
262     return($smarty->fetch(get_template_path("setup_migrate.tpl",TRUE,dirname(__FILE__))));
263   }
265   function save_object()
266   {
267     if($this->migration_dialog){
268       foreach($this->deps_to_migrate as $id => $data){
270         if(isset($_POST['migrate_'.$id])){
271           $this->deps_to_migrate[$id]['checked'] = TRUE;
272         }else{
273           $this->deps_to_migrate[$id]['checked'] = FALSE;
274         }
275       }
276     }
278   }
281 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
282 ?>