Code

Updated migrate of userRDN
[gosa.git] / gosa-core / include / class_migrate_userRDN.inc
1 <?php
4 class migrate_userRDN implements propertyMigration
5 {
6     private $property = NULL;
7     private $config = NULL;
9     private $found = array();
11     function __construct($config,$property)
12     {
13         $this->property = &$property;
14         $this->config = &$config;
15     }   
17     function checkForIssues()
18     {
19         $this->found = array();
20         $ldap= $this->config->get_ldap_link();
21         $ldap->cd($this->config->current['BASE']);
22         $ldap2= $this->config->get_ldap_link();
23         $ldap2->cd($this->config->current['BASE']);
25         // If the userRDN wasn't empty, then only search for users inside of the old userRDN.
26         $initialValue = $this->property->getValue();
27         $targetValue = $this->property->getValue(TRUE);
28         $dnMatch = "";
29         if(!empty($initialValue)){
30             foreach(preg_split("/,/", $initialValue) as $rdnPart){
31                 if(empty($rdnPart)) continue;
32                 list($namingAttrs, $container) = preg_split("/=/",$rdnPart,2);
33                 $container = trim($container,', ');
34                 $dnMatch.= "({$namingAttrs}:dn:={$container})";
35             }
36         }
38         // Search for users
39         $ldap->search("(&(objectClass=gosaAccount)(!(objectClass=sambaSamAccount)){$dnMatch})",array('dn'));
40         $found = FALSE;
41         while($attrs = $ldap->fetch()){
42             $dn = $attrs['dn'];
43             $dnTo = $dn;
45             // If there intially was no userDN given then just add the new userRDN to the user dns
46             //  and create the new container objects.
47             if(empty($initialValue)){
48                 list($namingAttrs, $container) = preg_split("/=/",$targetValue,2);
49                 list($name, $container) = preg_split("/,/",$dn,2);
51                 // Ensure that we handle a valid gosaDepartment container.
52                 while(!isset($this->config->idepartments[$container])){
53                     $container = preg_replace("/^[^,]*+,/","",$container);
54                 }
56                 // Queue new containuer to be created.
57                 if(!preg_match("/^".preg_quote($targetValue,'/i')."/", $container)){
58                     $dnTo = $name.",".$targetValue.$container;
59                     if(!$ldap->dn_exists($targetValue.$container)){
60                         $this->found['add'][$targetValue.$container] = array(); 
61                     }
62                     if($dn != $dnTo){
63                         $this->found['move'][] = array('from' => $dn, 'to' => $dnTo);
64                         $found = TRUE;
65                     }
66                 }
67             }
69             // If there intially was a userDN given then replace it with the new one.
70             if(!empty($initialValue)){
72                 list($name, $container) = preg_split("/,/",$dn,2);
73                 if(preg_match("/^".preg_quote($initialValue,'/i')."/", $container)){
74                     $container = preg_replace("/^".preg_quote($initialValue,'/')."/",$targetValue,$container);
76                     // Ensure that we handle a valid gosaDepartment container.
77                     while(!isset($this->config->idepartments[$container])){
78                         $container = preg_replace("/^[^,]*+,/","",$container);
79                     }
81                     $dnTo = $name.",".$targetValue.$container;
82                     if(!empty($targetValue) && !$ldap->dn_exists($targetValue.$container)){
83                         $this->found['add'][$targetValue.$container] = array(); 
84                     }
85                     if($dn != $dnTo){
86                         $this->found['move'][] = array('from' => $dn, 'to' => $dnTo);
87                         $found = TRUE;
88                     }
89                 }
90             }    
91         }
92         return($found);
93     }
95     function execute()
96     {
97         $str = "";
98         if(count($this->found['add'])) {
99             $str.= "<br><h3>"._("Objects that will be added")."</h3>";
100             foreach($this->found['add'] as $dn => $attrs){
101                 $str.= $dn."<br>";
102             }
103         }
104         if(count($this->found['move'])) {
105             $str.= "<br><h3>"._("Objects that will be moved")."</h3>";
106             $str.="<pre>";
107             foreach($this->found['move'] as $id => $data){
108                 $checked = (!isset($_POST['migrateNow'])) ? 'checked':'';
109                 $str.= "<input $checked type='checkbox' value='1' name='migrateEntry_{$id}'>";
110                 $str.= sprintf(_("Moving object '%s' to '%s'"), $data['from'], $data['to'])."<br>";
111             }
112             $str.="</pre>";
113         }
114         $str.= "<button name='migrateNow'>"._("Migrate")."</button>";
115         return($str);
116     }
117    
118     function save_object()
119     {
120         if(isset($_POST['migrateNow'])){
121             $ldap = $this->config->get_ldap_link();
122             $ldap->cd($this->config->current['BASE']);
124             // Try to add the new container objects
125             foreach($this->found['add'] as $dn => $data){
126                 $ldap->cd($this->config->current['BASE']);
127                 $ldap->create_missing_trees(ldap::convert($dn));
128             }
130             // Now move the objects to the new traget
131             foreach($this->found['move'] as $id => $data){
132                 if(isset($_POST["migrateEntry_{$id}"])){
133                     $ldap->rename_dn($data['from'], $data['to']);
134                 }
135             }
136             $this->checkForIssues();
137         }
138     } 
140     function check()
141     {
142         return(array());
143     }
144
146 ?>