Code

Fixed saving of posix account.
[gosa.git] / plugins / personal / netatalk / class_netatalk.inc
1 <?php
2 /*
3    This code is part of GOsa (https://gosa.gonicus.de)
4    Copyright (C) 2006  Gina Haeussge <osd@foosel.net>
5    Copyright (C) 2006  Bernd Zeimetz <bernd@zeimetz.de>
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  */
21  
22 /*! \brief   netatalk plugin
23   \author  Gina Haeussge <osd@foosel.net>
24   \author  Bernd Zeimetz <bernd@zeimetz.de>
25   \version 0.1
26   \date    21.3.2006
28   This class provides the functionality to read and write all attributes
29   relevant for netatalk from/to the LDAP. It does syntax checking
30   and displays the formulars required.
31  */
33 class netatalk extends plugin {
35   /* Definitions */
36   var $plHeadline = "Netatalk";
37   var $plDescription = "Manage netatalk account";
39   /* CLI vars */
40   var $cli_summary = "Manage netatalk account";
41   var $cli_description = "Manage Account \nfor netatalk";
42   var $cli_parameters = array ("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
44   /* Plugin specific values */
45   var $apple_user_homepath_raw   = "";
46   var $apple_user_homeurl_raw     = "";
47   var $apple_user_homeurl_xml     = "";
48   var $apple_user_homeurl       = "";
49   var $apple_user_homeDirectory   = "";
50   var $apple_user_share       = "";
51   var $shares             = array();
52   var $shares_settings        = array();
53   var $selectedshare        = "";
54   var $mountDirectory         = "/Network/Servers";
55   var $shares_loaded= false;
56   
57   /* Attributes to save to LDAP */
58   /* Slip setting those attributes, we can't define class variables with - */
59   var $use_save_attributes = array ("apple-user-homeurl", "apple-user-homeDirectory");
61   /* Attributes to use in smarty template */
62   var $smarty_attributes = array ("apple_user_homepath_raw", "shares", "selectedshare");
63   
64   /* Attributes to save from $_POST */
65   var $post_attributes = array ("apple_user_share", "apple_user_homepath_raw");
67   /* Objectclasses */
68   var $objectclasses = array ("apple-user");
70   /* Checkboxes */
71   var $is_chk_box = array ();
72   
74   function get_shares()
75   {
76     $this->shares_loaded= TRUE;
78     /* Get netatalk shares */
79     $this->shares = array();
80     $ldap = $this->config->get_ldap_link();
82     if(($this->dn != "new" && $this->dn != NULL)) {
83       $ldap->cd($this->parent->by_object['user']->base);
84     } else {
85       $ldap->cd ($this->dn);
86       $ldap->cd ('..'); $ldap->cd ('..');
87     }
88     $ldap->search ("(&(objectClass=mount)(|(mountType=url)(mountType=nfs))(cn=*))");
89     
90     while ($attrs = $ldap->fetch()){
91       $tmp = split(":", $attrs["cn"][0]);
92       $host = trim($tmp[0]);
93       $dir = trim($tmp[1]);
94       $mountType = trim($attrs["mountType"][0]);
95       if ($mountType == "url") {
96         $mountTypeReal = "netatalk";
97       } else {
98         $mountTypeReal = $mountType;
99       } 
100       $share = $attrs["cn"][0]. " (" . $mountTypeReal . ")";
101       $this->shares[$share] = $share;
102       $this->shares_settings[$share]["mountType"]=$mountType;
103       $this->shares_settings[$share]["dir"]=$dir;
104       $this->shares_settings[$share]["host"]=$host;
106       $test = $host.$dir;
107       if(preg_match("#".$test."#",$this->apple_user_homeDirectory)){
108         $this->apple_user_share = $this->selectedshare = $share;
109       }
110     }
111     asort($this->shares);
112   }
114   /* The constructor just saves a copy of the config. You may add what ever you need. */
115   function netatalk($config, $dn = NULL) {
117     /* Include config object */
118     $this->config = $config;
119     plugin :: plugin($config, $dn);
121     /* Copy needed attributes */
122     foreach($this->use_save_attributes as $val) {
123       if (isset($this->attrs["$val"][0])) {
124         $name = str_replace('-', '_', $val);
125         $this->$name = $this->attrs["$val"][0];
126       }
127     }
128       
129     if (strlen($this->apple_user_homeDirectory) >0) {
130       $this->apple_user_homepath_raw = substr($this->apple_user_homeDirectory, strrpos($this->apple_user_homeDirectory, '/') + 1 );
131     }
132  
133     /* Save initial account state */
134     $this->initially_was_account = $this->is_account;
135   }
137   /* Execute the plugin, produce the output. */
138   function execute() {
139     if (!$this->shares_loaded){
140       $this->get_shares();
141     }
143     plugin::execute();
145     /* Use the smarty templating engine here... */
146     $smarty = get_smarty();
147     $display = "";
149     /* Do we need to flip is_account state? */
150     if (isset ($_POST['modify_state'])) {
151       $this->is_account = !$this->is_account;
152     }
154     /* Do we represent a valid account? */
155     if (!$this->is_account && $this->parent == NULL) {
156       $display = "<img alt=\"\"src=\"images/stop.png\" align=\"middle\">&nbsp;<b>"._("This account has no netatalk extensions.")."</b>";
158       $display .= back_to_main();
159       return ($display);
160     }
161     
162     /* Show tab dialog headers */
163     if ($this->parent != NULL) {
164       if ($this->is_account) {
165         $display = $this->show_disable_header(_("Remove netatalk account"), _("This account has netatalk features enabled. You can disable them by clicking below."));
166       } else {
167         $errmsg="";
168         $obj = $this->parent->by_object['posixAccount'];
169         if  (!($obj->is_account) ) {
170           $errmsg.="Posix features are needed for netatalk accounts, enable them first. ";
171         }
172         if (count($this->shares)== 0) {
173           $errmsg.="At least one share with netatalk or NFS mount entry needed.";
174         }
175         if($errmsg==""){
176           $display = $this->show_enable_header(_("Create netatalk account"), _("This account has netatalk features disabled. You can enable them by clicking below."));
177         } else {
178           $display = $this->show_enable_header(_("Create netatalk account"), _($errmsg), TRUE);  
179         }
180         return ($display);
181       }
182     }
183    
184     /* Assign attributes and ACL to smarty */
185     $SkipWrite = (!isset($this->parent) || !$this->parent) && !isset($_SESSION['edit']); 
186     $smarty->assign("netatalkShareACL", $this->getacl("netatalkShare",$SkipWrite));
187     $smarty->assign("netatalkUserHomepathACL", $this->getacl("netatalkUserHomepath",$SkipWrite));
188     foreach ($this->smarty_attributes as $val) {
189       $smarty->assign("$val", $this-> $val);
190       if (in_array($val, $this->is_chk_box)) {
191         if ($this-> $val == "checked") {
192           $smarty->assign($val."CHK", " checked ");
193         } else {
194           $smarty->assign($val."CHK", "");
195         }
196       }
197     }
199     /* Let smarty fetch and process the page. */
200     $display .= ($smarty->fetch(get_template_path('netatalk.tpl', TRUE, dirname(__FILE__))));
201     return ($display);
202   }
203   
204   
205   /* Check if we have correct data */
206   function check() {
207     $message = array ();
209     if (strlen($this->apple_user_share) == 0) {
210       $message[] = _("You must select a share to use.");
211     }
213     return ($message);
214   }
216   /* Save to LDAP */
217   function save() {
218     /* remove a / at the end of the homepath, we neither need it there nor
219       * do we want to check for it later.
220       */
221     if(substr($this->apple_user_homepath_raw, -1, 1) === '/') {
222       $this->apple_user_homepath_raw=substr($this->apple_user_homepath_raw, 0, -1);
223     }
225     $mountType=$this->shares_settings[$this->apple_user_share]["mountType"];
226     $dir=$this->shares_settings[$this->apple_user_share]["dir"];
227     $host=$this->shares_settings[$this->apple_user_share]["host"];
228     
229     /* Convert raw data to wished format */
230     if ($this->is_account) {
231       if($mountType=="url") {
232         $this->apple_user_homeurl_xml = '<home_dir><url>afp://'.$host.$dir . '</url><path>'.$this->apple_user_homepath_raw.'</path></home_dir>';
233         $this->apple_user_homeurl = base64_encode($this->apple_user_homeurl_xml);
234       } else {
235         $this->apple_user_homeurl = "";
236       }
237       $this->apple_user_homeDirectory = $this->mountDirectory . '/' . $host .$dir . '/' . $this->apple_user_homepath_raw;
238     } else {
239       $this->apple_user_homeurl = "";
240       $this->apple_user_homeDirectory = "";
241     }
243     $ldap = $this->config->get_ldap_link();
245     /* Call parents save to prepare $this->attrs */
246     plugin :: save();
248     /* Do attribute conversion */
249     foreach ($this->use_save_attributes as $val) {
250       $name = str_replace('-', '_', $val);
251       if ($this->$name != "") {
252         $this->attrs[$val] = $this->$name;
253       } else {
254         $this->attrs[$val] = array();
255       }
256       unset ($this->attrs[$name]);
257     }
258     /* Write back to ldap */
259     $ldap->cd($this->dn);
260     $this->cleanup();
261     $ldap->modify($this->attrs);
263     show_ldap_error($ldap->get_error(), sprintf(_("Saving of user/netatalk account with dn '%s' failed."),$this->dn));
265     /* Optionally execute a command after we're done */
266     if ($this->initially_was_account == $this->is_account) {
267       if ($this->is_modified) {
268         $this->handle_post_events("modify");
269       }
270     } else {
271       $this->handle_post_events("add");
272     }
273   }
275   /* Use Save_object for every Post handling */
276   function save_object() {
277     if (isset ($_POST['netatalkTab'])) {
278       /* Save ldap attributes */
279 //      plugin :: save_object();
280       
281       foreach($this->post_attributes as $val) {
282         if (isset ($_POST[$val])) {
283           $this->$val = $_POST[$val];
284         } else {
285           $this->$val = "";
286         }
287       }
289       /* Specialhandling for checkboxes */
290       foreach ($this->is_chk_box as $val) {
291         if (isset ($_POST[$val])) {
292           $this-> $val = "checked";
293         } else {
294           $this-> $val = "unchecked";
295         }
296       }
297       
298       $this->apple_user_homeurl_raw = 'afp://' . $this->apple_user_share;
299     }
300   }
302   function remove_from_parent() {
303     /* Cancel if there's nothing to do here */
304     if (!$this->initially_was_account) {
305       return;
306     }
308     /* include global link_info */
309     $ldap = $this->config->get_ldap_link();
311     /* Remove and write to LDAP */
312     plugin :: remove_from_parent();
314     /* Adapt attributes if needed */
315     //     $method= new $this->method($this->config);
316     //     $method->fixAttributesOnRemove($this);
318     @ DEBUG(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $this->attributes, "Save");
319     $ldap->cd($this->dn);
320     $this->cleanup();
321     $ldap->modify($this->attrs);
323     show_ldap_error($ldap->get_error(), sprintf(_("Removing of user/netatalk account with dn '%s' failed."),$this->dn));
325     /* remove the entry from LDAP */
326     unset ($this->attrs['uid']);
328     /* Optionally execute a command after we're done */
329     $this->handle_post_events('remove');
330   }
332   
333   /* Return plugin informations for acl handling*/
334   function plInfo()
335   {
336     return (array(
337           "plDescription"     => _("Netatalk"),
338           "plSelfModify"      => TRUE,
339           "plDepends"         => array("user"),
340           "plPriority"        => 1,
341           "plSection"         => "personal",
342           "plCategory"        => array("users"),
343           "plOptions"         => array(),
345           "plProvidedAcls"  => array(
346             "netatalkUserHomepath"   =>  _("User home path"),
347             "netatalkShare"          =>  _("Share"))
348           ));
349   }
352 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
353 ?>