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