Code

Fix issues with broken dn's
[gosa.git] / gosa-plugins / samba / admin / systems / samba / class_winGeneric.inc
1 <?php
3 class wingeneric extends plugin
4 {
5   /* Generic terminal attributes */
6   var $interfaces= array();
8   /* Needed values and lists */
9   var $base= "";
10   var $cn= "";
11   var $description= "";
12   var $orig_dn= "";
13   var $shadowLastChange="";
14   var $uidNumber="";
15   var $gidNumber="";
16   var $loginShell="";
17   var $gecos="";
18   var $shadowMin="";
19   var $shadowWarning="";
20   var $shadowInactive="";
21   var $uid="";
22   var $sn="";
23   var $givenName="";
24   var $homeDirectory="";
25   var $sambaSID="";
26   var $sambaPrimaryGroupSID="";
27   var $displayName="";
28   var $sambaPwdMustChange="";
29   var $sambaNTPassword="";
30   var $sambaPwdLastSet="";
31   var $sambaAcctFlags="";
32   var $netConfigDNS;
34   var $orig_cn;
35   var $orig_base;
37   /* attribute list for save action */
38   var $ignore_account= TRUE;
39   var $attributes   = array("cn", "description","shadowLastChange",
40                             "uidNumber","gidNumber","loginShell","gecos","shadowMin","shadowWarning",
41                             "shadowInactive","uid","cn","sn","givenName","homeDirectory","sambaSID",
42                             "sambaPrimaryGroupSID","displayName", "sambaPwdMustChange",
43                             "sambaNTPassword","sambaPwdLastSet","sambaAcctFlags");
44   var $objectclasses= array("posixAccount","person","organizationalPerson","inetOrgPerson","gosaAccount","shadowAccount","sambaSamAccount","top");
46   var $view_logged = FALSE;
48   function wingeneric (&$config, $dn= NULL, $parent= NULL)
49   {
50     plugin::plugin ($config, $dn, $parent);
51     $this->netConfigDNS = new termDNS($this->config,$this,$this->objectclasses,FALSE,"uid");
52     $this->netConfigDNS->cn =  preg_replace("/\\\$\$/","",$this->uid);
53     $this->netConfigDNS->objectclasses=array("ipHost","ieee802Device");
54     $this->netConfigDNS->MACisMust = FALSE;
55     $this->netConfigDNS->IPisMust = FALSE;
57     /* Set base */
58     if ($this->dn == "new"){
59       $ui= get_userinfo();
60       $this->base= dn2base($ui->dn);
61       $this->cn= "";
62     } else {
63       $this->base= preg_replace ("/^[^,]+,".preg_quote(get_winstations_ou(), '/')."/i", "", $this->dn);
64     }
66     $this->cn= preg_replace("/\\\$\$/","",$this->cn);
68     /* Save dn for later references */
69     $this->orig_dn= $this->dn;
70     $this->orig_base= $this->base;
71     $this->orig_cn= $this->cn;
72   }
75   function set_acl_base($base)
76   {
77     plugin::set_acl_base($base);
78     $this->netConfigDNS->set_acl_base($base);
79   }
82   function set_acl_category($cat)
83   {
84     plugin::set_acl_category($cat);
85     $this->netConfigDNS->set_acl_category($cat);
86   }
89   function execute()
90   {
91     /* Call parent execute */
92     plugin::execute();
94     if($this->is_account && !$this->view_logged){
95       $this->view_logged = TRUE;
96       new log("view","winworkstation/".get_class($this),$this->dn);
97     }
100     /* Do we represent a valid phone? */
101     if (!$this->is_account && $this->parent === NULL){
102       $display= "<img alt=\"\" src=\"images/small-error.png\" align=middle>&nbsp;<b>".
103         msgPool::noValidExtension(_("network"))."</b>";
104       return($display);
105     }
107     /* Base select dialog */
108     $once = true;
109     foreach($_POST as $name => $value){
110       if(preg_match("/^chooseBase/",$name) && $once){
111         $once = false;
112         $this->dialog = new baseSelectDialog($this->config,$this);
113         $this->dialog->setCurrentBase($this->base);
114       }
115     }
117     /* Dialog handling */
118     if(is_object($this->dialog)){
119       /* Must be called before save_object */
120       $this->dialog->save_object();
122       if($this->dialog->isClosed()){
123         $this->dialog = false;
124       }elseif($this->dialog->isSelected()){
126         /* A new base was selected, check if it is a valid one */
127         $tmp = $this->get_allowed_bases();
128         if(isset($tmp[$this->dialog->isSelected()])){
129           $this->base = $this->dialog->isSelected();
130         }
132         $this->dialog= false;
133       }else{
134         return($this->dialog->execute());
135       }
136     }
138     /* Fill templating stuff */
139     $smarty= get_smarty();
140     $smarty->assign("bases"   , $this->get_allowed_bases());
141     
142     $tmp = $this->plInfo();
143     foreach($tmp['plProvidedAcls'] as $name => $translated){
144       $smarty->assign($name."ACL",$this->getacl($name));
145     }
147     /* Assign attributes */
148     foreach ($this->attributes as $attr){
149       $smarty->assign("$attr", $this->$attr);
150     }
151     
152     $smarty->assign("base_select", $this->base);
154     /* Show main page */
155     $str = $this->netConfigDNS->execute();
156     if(is_object($this->netConfigDNS->dialog)){
157       return($str);
158     }
159     $smarty->assign("netconfig", $str);
160     return($smarty->fetch (get_template_path('wingeneric.tpl', TRUE,dirname(__FILE__))));
161   }
163   function remove_from_parent()
164   {
165     $this->netConfigDNS->remove_from_parent();
166     $ldap= $this->config->get_ldap_link();
167     $ldap->rmdir($this->dn);
168     new log("remove","winworkstation/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
169     if (!$ldap->success()){
170       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
171     }
172     $this->handle_post_events("remove");
174     /* Delete references to object groups */
175     $ldap->cd ($this->config->current['BASE']);
176     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
177     while ($ldap->fetch()){
178       $og= new ogroup($this->config, $ldap->getDN());
179       unset($og->member[$this->dn]);
180       $og->save ();
181     }
182   }
185   /* Save data to object */
186   function save_object()
187   {
188     /* Create a base backup and reset the
189        base directly after calling plugin::save_object();
190        Base will be set seperatly a few lines below */
191     $base_tmp = $this->base;
192     plugin::save_object();
193     $this->base = $base_tmp;
195     $this->netConfigDNS->save_object();
197     /* Set new base if allowed */
198     $tmp = $this->get_allowed_bases();
199     if(isset($_POST['base'])){
200       if(isset($tmp[$_POST['base']])){
201         $this->base= $_POST['base'];
202       }
203     }
204   }
207   /* Check supplied data */
208   function check()
209   {
210     /* Call common method to give check the hook */
211     $message= plugin::check();
212     $message= array_merge($message, $this->netConfigDNS->check());
213     $this->dn= "cn=".$this->uid.",".get_ou('sambaMachineAccountRDN').$this->base;
215     if(!$this->acl_is_createable()){
216       $message[]= msgPool::permCreate();
217     }
219     if ($this->orig_dn != $this->dn){
220       $ldap= $this->config->get_ldap_link();
221       $ldap->cd ($this->base);
222       $ldap->search ("(cn=".$this->cn.")", array("cn"));
223       if ($ldap->count() != 0){
224         while ($attrs= $ldap->fetch()){
225           if(preg_match("/cn=dhcp,/",$attrs['dn'])){
226             continue;
227           }
228           if ($attrs['dn'] != $this->orig_dn){
229             $message[]= msgPool::duplicated(_("Name"));
230             break;
231           }
232         }
233       }
234     }
236     /* Check if we are allowed to create or move this object
237      */
238     if($this->orig_dn == "new" && !$this->acl_is_createable($this->base)){
239       $message[] = msgPool::permCreate();
240     }elseif($this->orig_dn != "new" && $this->base != $this->orig_base && !$this->acl_is_moveable($this->base)){
241       $message[] = msgPool::permMove();
242     }
244     return ($message);
245   }
248   /* Save to LDAP */
249   function save()
250   {
251     plugin::save();
253     /* Remove all empty values */
254     if ($this->orig_dn == 'new'){
255       $attrs= array();
256       foreach ($this->attrs as $key => $val){
257         if (is_array($val) && count($val) == 0){
258           continue;
259         }
260         $attrs[$key]= $val;
261       }
262       $this->attrs= $attrs;
263     }
265     if(($this->gosaUnitTag) && (!in_array_ics("gosaAdministrativeUnitTag",$this->attrs['objectClass']))){
266       $this->attrs['objectClass'][] = "gosaAdministrativeUnitTag";
267     }
269     /* Write back to ldap */
270     $ldap= $this->config->get_ldap_link();
271     if ($this->orig_dn == 'new'){
272       $ldap->cd($this->config->current['BASE']);
273       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
274       $ldap->cd($this->dn);
275       $ldap->add($this->attrs);
276       new log("create","winworkstation/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
277       $this->handle_post_events("add");
278     } else {
279       if ($this->orig_dn != $this->dn){
280         $this->move($this->orig_dn, $this->dn);
281       }
283       $ldap->cd($this->dn);
284       $this->cleanup();
285       $ldap->modify ($this->attrs); 
286       new log("modify","winworkstation/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
287       $this->handle_post_events("modify");
288     }
290     $this->netConfigDNS->cn =  preg_replace("/\\\$\$/","",$this->uid);
291     $this->netConfigDNS->save();
292     if (!$ldap->success()){
293       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
294     }
296     /* Optionally execute a command after we're done */
297     $this->postcreate();
298   }
300   /* Return plugin informations for acl handling
301   #FIXME FAIscript seams to ununsed within this class... */
302   static function plInfo()
303   {
304     return (array(
305           "plShortName"   => _("Win generic"),
306           "plDescription" => _("Windows workstation generic"),
307           "plSelfModify"  => FALSE,
308           "plDepends"     => array(),
309           "plPriority"    => 0,
310           "plSection"     => array("administration"),
311           "plCategory"    => array("winworkstation" => array("description"  => _("Win workstation"),
312                                                           "objectClass"  => "gotoWorkstation")),
313           "plProvidedAcls"=> array(
314             "cn"                  => _("Name"),
315             "base"                => _("Base"),
316             "description"         => _("Description"))
317           ));
318   }
323 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
324 ?>