Code

Updated trustModes
[gosa.git] / gosa-core / plugins / personal / posix / trustModeDialog / class_trustModeDialog.inc
1 <?php
3 class trustModeDialog extends plugin 
4 {
5     public $attributes  = array("accessTo","trustModel");
6     public $accessTo    = array();
7     public $trustModel  = "";
8     public $trustSelect = NULL;
10     private $trustList = NULL;
12     function __construct(&$config, $dn, $parent = NULL)
13     {
14         plugin::plugin($config, $dn, $parent);
16         $this->accessTo = array();
17         $this->trustModel= "";
18         $this->is_account = FALSE; 
19         if(isset($this->attrs['trustModel'][0])){
20             $this->is_account = TRUE;
21             $this->trustModel= $this->attrs['trustModel'][0];
22             if (isset($this->attrs['accessTo'])){
23                 for ($i= 0; $i<$this->attrs['accessTo']['count']; $i++){
24                     $tmp= $this->attrs['accessTo'][$i];
25                     $this->accessTo[$tmp]= $tmp;
26                 }
27             }
29         }
31         $this->initially_was_account = $this->is_account;
33         $lData = array();
34         foreach($this->accessTo as $key => $cn){
35             $lData[$cn] = array('data' => $this->converCnToType($cn));
36         }
38         $this->trustList = new sortableListing($this->accessTo, $lData);
39         $this->trustList->setDeleteable(true);
40         $this->trustList->setInstantDelete(true);
41         $this->trustList->setEditable(false);
42         $this->trustList->setWidth("100%");
43         $this->trustList->setHeight("100px");
44         $this->trustList->setColspecs(array('20px','*'));
45         $this->trustList->setHeader(array("~",_("Name"),_("Description")));
46         $this->trustList->setDefaultSortColumn(1);
48     }
50     public function PrepareForCopyPaste($source)
51     {
52         $this->accessTo = array();
53         $this->trustModel= "";
54         $this->is_account = FALSE;
55         if(isset($source['trustModel'][0])){
56             $this->is_account = TRUE;
57             $this->trustModel= $source['trustModel'][0];
58             if (isset($source['accessTo'])){
59                 for ($i= 0; $i<$source['accessTo']['count']; $i++){
60                     $tmp= $source['accessTo'][$i];
61                     $this->accessTo[$tmp]= $tmp;
62                 }
63             }
64         }
65     }
67     public function converCnToType($cn)
68     {
69         if(isset($this->typeCache[$cn])){
70             return($this->typeCache[$cn]);
71         }
73         $ldap = $this->config->get_ldap_link();
74         $ldap->cd($this->config->current['BASE']);
75         $ldap->search("(&(|(objectClass=gotoTerminal)(objectClass=gotoWorkstation)".
76                 "(objectClass=goServer))(cn=".$cn."))", array("objectClass", "description"));
77         $this->typeCache[$cn] = array("",$cn,"");
78         if($ldap->count() != 0){
79             $attrs = $ldap->fetch();
80             $img = $desc = "";
81             if(in_array("gotoWorkstation",$attrs['objectClass'])){
82                 $img = image('plugins/systems/images/select_workstation.png'); 
83             }elseif(in_array("gotoTerminal",$attrs['objectClass'])){
84                 $img = image('plugins/systems/images/select_terminal.png'); 
85             }elseif(in_array("goServer",$attrs['objectClass'])){
86                 $img = image('plugins/systems/images/select_server.png'); 
87             }
88             if(isset($attrs['description'][0])) {
89                 $desc = $attrs['description'][0];
90             }
91             $this->typeCache[$cn] = array($img,$cn,$desc);
92         }
93         return($this->typeCache[$cn]);
94     }
96     public function setAcl($acl)
97     {
98         $this->acl = $acl;
99     }
101     public function execute()
102     {
103         // Call parent
104         plugin::execute();
106         $this->trustList->setAcl($this->getacl("accessTo"));
107         $this->trustList->save_object();
109         // Allow to select trusted machines from a list
110         if (isset($_POST["add_ws"])){
111             $this->trustSelect= new trustSelect($this->config,get_userinfo());
112             $this->dialog= TRUE;
113         }
115         // Cancel trust and group dialog
116         if (isset($_POST['add_ws_cancel'])){
117             $this->groupSelect= NULL;
118             $this->trustSelect= NULL;
119             $this->dialog= FALSE;
120         }
122         // Add selected machines to trusted ones.
123         if (isset($_POST["add_ws_finish"]) && $this->trustSelect){
124             $trusts = $this->trustSelect->detectPostActions();
125             if(isset($trusts['targets'])){
126                 $headpage = $this->trustSelect->getHeadpage();
127                 foreach($trusts['targets'] as $id){
128                     $attrs = $headpage->getEntry($id);
129                     $cn = $attrs['cn'][0];
130                     $this->accessTo[$cn]=$cn;
131                     $this->trustList->addEntry($cn,
132                             array('data'=> $this->converCnToType($cn)), 
133                             $attrs['cn'][0]);
134                 }
135                 $this->is_modified= TRUE;
136                 $this->trustList->update();
137             }
138             $this->trustSelect= NULL;
139             $this->dialog= FALSE;
140         }
142         // Remove machine from trusted ones.
143         $actionL = $this->trustList->getAction();
144         if ($actionL['action'] == "delete"){
145             $this->accessTo = $this->trustList->getMaintainedData();
146             $this->is_modified= TRUE;
147         }
150         if ($this->trustSelect){
151             session::set('filterBlacklist', array('cn' => array_values($this->accessTo)));
152             return($this->trustSelect->execute());
153         }
155         /* Work on trust modes */
156         $smarty = get_smarty();
157         $smarty->assign("trusthide", " disabled ");
158         $smarty->assign("trustmodeACL",  $this->getacl("accessTo"));
159         if ($this->trustModel == "fullaccess"){
160             $trustmode= 1;
161             // pervent double disable tag in html code, this will disturb our clean w3c html
162             $smarty->assign("trustmode",  $this->getacl("accessTo"));
164         } elseif ($this->trustModel == "byhost"){
165             $trustmode= 2;
166             $smarty->assign("trusthide", "");
167         } else {
168             // pervent double disable tag in html code, this will disturb our clean w3c html
169             $smarty->assign("trustmode",  $this->getacl("accessTo"));
170             $trustmode= 0;
171         }
172         $smarty->assign("trustmode", $trustmode);
173         $smarty->assign("trustmodes", array( 0 => _("disabled"), 1 => _("full access"),
174                     2 => _("allow access to these hosts")));
177         if((count($this->accessTo))==0)
178             $smarty->assign("emptyArrAccess",true);
179         else
180             $smarty->assign("emptyArrAccess",false);
183         $smarty->assign($smarty->assign("use_trustmode",in_array("trustmode", $this->multi_boxes)));
184         $smarty->assign("multiple_support" , $this->multiple_support_active);
185 #        $this->trustList->update();
186         $smarty->assign("trustList", $this->trustList->render());
187         return($smarty->fetch(get_template_path("generic.tpl",TRUE, dirname(__FILE__))));
188     }
190     public function save_object()
191     {
193         /* Trust mode - special handling */
194         if(preg_match("/w/", $this->getacl("accessTo"))){
195             if (isset($_POST['trustmode'])){
196                 $saved= $this->trustModel;
197                 if ($_POST['trustmode'] == "1"){
198                     $this->trustModel= "fullaccess";
199                 } elseif ($_POST['trustmode'] == "2"){
200                     $this->trustModel= "byhost";
201                 } else {
202                     $this->trustModel= "";
203                 }
204                 if ($this->trustModel != $saved){
205                     $this->is_modified= TRUE;
206                 }
207             }
208         }
210     }
212     public function save()
213     {
214         plugin::save();
215         
216         /* Trust accounts */
217         $objectclasses= array();
218         foreach ($this->attrs['objectClass'] as $key => $class){
219             if (preg_match('/trustAccount/i', $class)){
220                 continue;
221             }
222             $objectclasses[]= $this->attrs['objectClass'][$key];
223         }
224         $this->attrs['objectClass']= $objectclasses;
225         if ($this->trustModel != ""){
226             $this->attrs['objectClass'][]= "trustAccount";
227             $this->attrs['trustModel']= $this->trustModel;
228             $this->attrs['accessTo']= array();
229             if ($this->trustModel == "byhost"){
230                 foreach ($this->accessTo as $host){
231                     $this->attrs['accessTo'][]= $host;
232                 }
233             }
234         } else {
235             if ($this->initially_was_account){
236                 $this->attrs['accessTo']= array();
237                 $this->attrs['trustModel']= array();
238             }
239         }
240         $ldap = $this->config->get_ldap_link();
241         $ldap->cd($this->dn);
242         $this->cleanup();
243         $ldap->modify($this->attrs);
245         /* Log last action */
246         if($this->initially_was_account){
247             new log("modify","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
248         }else{
249             new log("create","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
250         }
252         if (!$ldap->success()){
253             msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), 
254                         $this->dn, LDAP_MOD,get_class()));
255         }
256     }
258     public function get_multi_edit_values()
259     {
260         $ret = plugin::get_multi_edit_values();
261         if(in_array("trustmode",$this->multi_boxes)){
262             $ret['trustModel'] = $this->trustModel;
263             $ret['accessTo'] = $this->accessTo;
264         }
265         return($ret);
266     }
268     public function multiple_save_object()
269     {
270         plugin::multiple_save_object();
271         if(isset($_POST["use_trustmode"])){
272             $this->multi_boxes[] = "trustmode";
273         }
275         if(preg_match("/w/", $this->getacl("accessTo"))){
276             if (isset($_POST['trustmode'])){
277                 $saved= $this->trustModel;
278                 if ($_POST['trustmode'] == "1"){
279                     $this->trustModel= "fullaccess";
280                 } elseif ($_POST['trustmode'] == "2"){
281                     $this->trustModel= "byhost";
282                 } else {
283                     $this->trustModel= "";
284                 }
285                 if ($this->trustModel != $saved){
286                     $this->is_modified= TRUE;
287                 }
288             }
289         }
290     }
292     public function init_multiple_support($attrs,$all)
293     {
294         plugin::init_multiple_support($attrs,$all);
296         if (isset($this->multi_attrs['trustModel'])){
297             $this->trustModel= $this->multi_attrs['trustModel'][0];
298             $this->initially_was_account= TRUE;
299             $this->multi_boxes[] = "trustmode";
300         } else {
301             $this->initially_was_account= FALSE;
302             $this->trustModel= "";
303         }
305         $this->accessTo = array();
306         if (isset($this->multi_attrs['accessTo'])){
307             for ($i= 0; $i<$this->multi_attrs['accessTo']['count']; $i++){
308                 $tmp= $this->multi_attrs['accessTo'][$i];
309                 $this->accessTo[$tmp]= $tmp;
310             }
311         }
312         $this->trustList->setListData($this->accessTo);
313     }
316     public function getacl($attribute,$skip_write= FALSE)
317     {
318         $ui= get_userinfo();
319         $skip_write |= $this->read_only;
320         return  $ui->get_permissions($this->acl_base,$this->acl, $attribute,$skip_write);
321     }
324
328 ?>