95f81672a74671d3b3f2d4ea59d811a3033aa5be
1 <?php
5 class migrateRDN implements propertyMigration
6 {
7 protected $property = NULL;
8 protected $config = NULL;
9 protected $found = array();
10 protected $filter ="";
12 // Additional suffixes or prefixes
13 // e.g. for 'faiScriptRDN' (ou=scripts,) moving to new destination lets say
14 // to 'ou=FAIscripts,' would break stuff with having 'ou=fai,ou=systems,ou=config'
15 // prepended.
16 //
17 protected $suffix = "";
18 protected $prefix = "";
20 protected $title = "";
21 protected $description = "";
24 function __construct($config,$property)
25 {
26 $this->property = &$property;
27 $this->config = &$config;
29 //Set a dummy title and description
30 if(empty($this->title)){
31 $this->title = sprintf(_("Migration of property '%s'"), $this->property->getName());
32 }
33 if(empty($this->description)){
34 $this->description = sprintf(_("GOsa has detected objects outside of the configured storage point (%s)."), $this->property->getValue(TRUE));
35 }
36 }
38 function getChanges()
39 {
40 return($this->found);
41 }
43 function checkForIssues()
44 {
45 $this->found = array('add'=>array(), 'move' => array());
46 $ldap= $this->config->get_ldap_link();
47 $ldap->cd($this->config->current['BASE']);
48 $ldap2= $this->config->get_ldap_link();
49 $ldap2->cd($this->config->current['BASE']);
51 // Search for possible release deparments/containers - this enables us to
52 // SKIP release based objects - we cannot move them right now.
53 $releases = array();
54 $ldap->search("objectClass=FAIbranch");
55 while($attrs = $ldap->fetch()){
56 $releases[$attrs['dn']] = $attrs['dn'];
57 }
59 // If the userRDN wasn't empty, then only search for users inside of the old userRDN.
60 $initialValue = $this->prefix.$this->property->getValue().$this->suffix;
61 $targetValue = $this->prefix.$this->property->getValue(TRUE).$this->suffix;
63 if(!empty($initialValue) && !preg_match("/,$/", $initialValue)) $initialValue.=",";
64 if(!empty($targetValue) && !preg_match("/,$/", $targetValue)) $targetValue.=",";
66 $dnMatch = "";
67 if(!empty($initialValue)){
68 foreach(preg_split("/,/", $initialValue) as $rdnPart){
69 if(empty($rdnPart)) continue;
70 list($namingAttrs, $container) = preg_split("/=/",$rdnPart,2);
71 $container = trim($container,', ');
72 $dnMatch.= "({$namingAttrs}:dn:={$container})";
73 }
74 }
76 // Search for users
77 $filter = sprintf($this->filter,$dnMatch);
78 $ldap->search($filter,array('dn'));
79 $found = FALSE;
80 while($attrs = $ldap->fetch()){
81 $dn = $attrs['dn'];
82 $dnTo = $dn;
84 // If there intially was no userDN given then just add the new userRDN to the user dns
85 // and create the new container objects.
86 if(empty($initialValue)){
87 list($namingAttrs, $container) = preg_split("/=/",$targetValue,2);
88 list($name, $container) = preg_split("/,/",$dn,2);
90 // Ensure that we handle a valid gosaDepartment container.
91 while(!isset($this->config->idepartments[$container])){
93 // This object is part of a FAI release - we better skip it here.
94 if(isset($releases[$container])){
95 break;
96 }
98 $container = preg_replace("/^[^,]*+,/","",$container);
99 }
101 // We haven't found a valid gosaDepartment in this dn, so skip.
102 if(isset($this->config->idepartments[$container])){
104 // Queue new containuer to be created.
105 if(!preg_match("/^".preg_quote($targetValue,'/i')."/", $container)){
106 $dnTo = $name.",".$targetValue.$container;
107 if(!$ldap->dn_exists($targetValue.$container)){
108 $this->found['add'][$targetValue.$container] = array();
109 }
110 if($dn != $dnTo){
111 $this->found['move'][] = array('from' => $dn, 'to' => $dnTo);
112 $found = TRUE;
113 }
114 }
115 }
116 }
118 // If there intially was a userDN given then replace it with the new one.
119 if(!empty($initialValue)){
121 list($name, $container) = preg_split("/,/",$dn,2);
122 if(preg_match("/^".preg_quote($initialValue,'/i')."/", $container)){
123 $container = preg_replace("/^".preg_quote($initialValue,'/')."/",$targetValue,$container);
125 // Ensure that we handle a valid gosaDepartment container.
126 while(!isset($this->config->idepartments[$container])){
128 // This object is part of a FAI release - we better skip it here.
129 if(isset($releases[$container])){
130 break;
131 }
133 $container = preg_replace("/^[^,]*+,/","",$container);
134 }
136 // We haven't found a valid gosaDepartment in this dn, so skip.
137 if(isset($this->config->idepartments[$container])){
140 $dnTo = $name.",".$targetValue.$container;
141 if(!empty($targetValue) && !$ldap->dn_exists($targetValue.$container)){
142 $this->found['add'][$targetValue.$container] = array();
143 }
144 if($dn != $dnTo){
145 $this->found['move'][] = array('from' => $dn, 'to' => $dnTo);
146 $found = TRUE;
147 }
148 }
149 }
150 }
151 }
152 return($found);
153 }
155 function execute()
156 {
157 $str = "<h3>".$this->title."</h3>";
158 $str.= $this->description;
159 $str.= "<hr>";
160 if(count($this->found['add'])) {
161 $str.= "<br>"._("Objects that will be added");
162 $str.= "<ul>";
163 foreach($this->found['add'] as $dn => $attrs){
164 $str.= "<li>".$dn."</li>";
165 }
166 $str.= "</ul>";
167 }
168 if(count($this->found['move'])) {
169 $str.= "<br>"._("Objects that will be moved")."<br>";
170 $str.= "<ul style='list-style:none; padding: 15px;'>";
171 foreach($this->found['move'] as $id => $data){
172 $checked = (!isset($_POST["migrateNow".get_class($this)])) ? 'checked':'';
173 $str.= "<li>
175 <span style='white-space:nowrap;'>
176 <input $checked type='checkbox' value='1' name='migrateEntry_{$id}'>";
177 $str.= sprintf(_("Moving object '%s' to '%s'"), $data['from'], $data['to'])."</span></li>";
178 }
179 $str.="</ul>";
180 }
181 $str.= "<button name='migrateNow".get_class($this)."'>"._("Migrate")."</button>";
182 return($str);
183 }
185 function save_object()
186 {
187 if(isset($_POST["migrateNow".get_class($this)])){
188 $ldap = $this->config->get_ldap_link();
189 $ldap->cd($this->config->current['BASE']);
191 // Try to add the new container objects
192 foreach($this->found['add'] as $dn => $data){
193 $ldap->cd($this->config->current['BASE']);
194 $ldap->create_missing_trees(ldap::convert($dn));
195 }
197 // Now move the objects to the new traget
198 $tmp = new plugin($this->config,NULL);
199 foreach($this->found['move'] as $id => $data){
200 if(isset($_POST["migrateEntry_{$id}"])){
201 if($tmp->move($data['from'], $data['to'])){
202 }elseif($ldap->dn_exists($data['to'])){
203 msg_dialog::display(_("Error"),
204 sprintf(_("Migration failed for object %s, dn already exists!"),
205 bold($data['to'])), ERROR_DIALOG);
206 }else{
207 msg_dialog::display(_("Error"),
208 sprintf(_("Migration failed for object %s, please check if it already exists!"),
209 bold($data['to'])), ERROR_DIALOG);
210 }
211 }
212 }
213 $this->checkForIssues();
214 }
215 }
217 function check()
218 {
219 return(array());
220 }
221 }
224 ?>