Code

Improved performance, 15% faster
[gosa.git] / plugins / personal / nagios / class_nagiosAccount.inc
1 <?php
3 /*! \brief   mail plugin
4   \author  Guillame Delecourt <guillaume@opensides.be>
5   \author  Benoit Mortier <benoit.mortier@opensides.be>
6   \author  Vincent Seynhaeve <vincent@opensides.be>  
7   \version 1.00
8   \date    25.09.2005
10   This class provides the functionality to read and write all attributes
11   relevant for nagiosAccount from/to the LDAP. It does syntax checking
12   and displays the formulars required.
13  */
15 class nagiosAccount extends plugin
16 {
17   /* Definitions */
18   var $plHeadline= "Nagios";
19   var $plDescription= "This does something";
21   /* CLI vars */
22   var $cli_summary= "Manage users nagios account";
23   var $cli_description= "Manage Account and autorization\nfor Nagios";
24   var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
26   /* plugin specific values */
27   var $NagiosAlias  = "";
28   var $NagiosPager  = "";
29   var $NagiosMail   = "";
31   var $HostNotificationOptions      = "";
32   var $HostNotificationPeriod       = "";
33   var $ServiceNotificationPeriod    = "";
34   var $ServiceNotitificationPeriod  = "";
35   var $HostNotificationCommands     = "";
36   var $ServiceNotificationCommands  = "";
37   var $HostNotitificationOptions    = "";
38   var $ServiceNotificationOptions   = "";
40   var $AuthorizedAllHosts                 = "unchecked";
41   var $AuthorizedAllServices              = "unchecked";
42   var $AuthorizedSystemCommands           = "unchecked";
43   var $AuthorizedAllHostCommands          = "unchecked";
44   var $AuthorizedAllServiceCommands       = "unchecked";
45   var $AuthorizedConfigurationInformation = "unchecked";
46   var $AuthorizedSystemInformation        = "unchecked";
48   /* attribute list for save action */
50   var $attributes= array("NagiosAlias", "NagiosPager", "NagiosMail", "HostNotificationPeriod",
51       "ServiceNotificationPeriod", "HostNotificationCommands", "ServiceNotificationCommands",
52       "HostNotificationOptions", "ServiceNotificationOptions", "AuthorizedAllHosts", "AuthorizedAllServices",
53       "AuthorizedSystemCommands", "AuthorizedAllHostCommands", "AuthorizedAllServiceCommands",
54       "AuthorizedConfigurationInformation", "AuthorizedSystemInformation");
56   var $is_chk_box = array("AuthorizedAllHosts","AuthorizedAllServices","AuthorizedSystemCommands",
57       "AuthorizedAllHostCommands","AuthorizedAllServiceCommands",
58       "AuthorizedConfigurationInformation","AuthorizedSystemInformation");
60   var $objectclasses= array("nagiosContact","nagiosAuth");
62   /* constructor, if 'dn' is set, the node loads the given
63      'dn' from LDAP */
64   function nagiosAccount ($config, $dn= NULL)
65   {
66     /* Configuration is fine, allways */
67     $this->config= $config;
69     plugin::plugin ($config, $dn);
71     /* Save initial account state */  
72     $this->initially_was_account= $this->is_account;
73   }
75   function execute()
76   {
77     /* Call parent execute */
78     plugin::execute();
80     /* Load templating engine */
81     $smarty= get_smarty();
82     $display= "";
84     /* Do we need to flip is_account state? */
85     if (isset($_POST['modify_state'])){
86       $this->is_account= !$this->is_account;
87     }
89     /* Do we represent a valid account? */
90     if (!$this->is_account && $this->parent == NULL){
91       $display= "<img alt=\"\"src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
92         _("This account has no nagios extensions.")."</b>";
94       $display.= back_to_main();
95       return ($display);
96     }
98     /* Show tab dialog headers */
99     if ($this->parent != NULL){
100       if ($this->is_account){
101         $display= $this->show_header(_("Remove nagios account"),
102             _("This account has nagios features enabled. You can disable them by clicking below."));
103       } else {
104         $display= $this->show_header(_("Create nagios account"), _("This account has nagios features disabled. You can enable them by clicking below."));
105         return($display);
106       }
107     }
109     /* Assign attributes an ACL to smarty */
110     foreach($this->attributes as $val) { 
111       $smarty->assign("$val", $this->$val);
112       $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
113       if(in_array($val,$this->is_chk_box)){
114         if($this->$val == "checked"){
115           $smarty->assign($val."CHK", " checked ");
116         }else{
117           $smarty->assign($val."CHK", "");
118         }
119       }
120     }
121     $smarty->assign('ServiceNotificationOptionsValues'  ,array("d,u,r,v"=>"d,u,r,v" ,"d,u,r"=>"d,u,r" ,"d,u"=>"d,u"));
122     $smarty->assign('HostNotificationOptionsValues'     ,array("w,u,c,r"=>"w,u,c,r" ,"w,u,c"=>"w,u,c" ,"c,w"=>"c,w"));
123     $smarty->assign('ServiceNotificationPeriodValues'   ,array("24x7"=>"24x7" ,"24x5"=>"24x5" ,"8x5"=>"8x5"));
124     $smarty->assign('HostNotificationPeriodValues'      ,array("24x7"=>"24x7" ,"24x5"=>"24x5" ,"8x5"=>"8x5"));
125     $display.= $smarty->fetch (get_template_path('nagios.tpl', TRUE, dirname(__FILE__)));
126     return ($display);
127   }
129   /* Save to LDAP */
130   function save()
131   {
132     $ldap= $this->config->get_ldap_link();
134     /* Call parents save to prepare $this->attrs */
135     plugin::save();
137     /* Adapt attributes if needed */
138     //$method= new $this->method($this->config);
139     //$id= $method->uattrib;
140     //$method->fixAttributesOnStore($this);
142     /* Write back to ldap */
143     $ldap->cd($this->dn);
144     $this->cleanup();
145 $ldap->modify ($this->attrs); 
147     show_ldap_error($ldap->get_error());
149     /* Optionally execute a command after we're done */
150     if ($this->initially_was_account == $this->is_account){
151       if ($this->is_modified){
152         $this->handle_post_events("modify");
153       }
154     } else {
155       $this->handle_post_events("add");
156     }
157   }
159   function check()
160   {
161     $message= array();
162     $ldap= $this->config->get_ldap_link();
163     /* must: mail */
164     if ($this->NagiosAlias == ""){
165       $message[]= _("The required field 'NagiosAlias' is not set.");
166     }
167     if ($this->NagiosMail == ""){
168       $message[]= _("The required field 'NagiosMail' is not set.");
169     }
170     if ($this->is_template){
171       if (!is_email($this->NagiosMail, TRUE)){
172         $message[]= _("Please enter a valid email address in 'NagiosMail' field.");
173       }
174     } else {
175       if (!is_email($this->NagiosMail)){
176         $message[]= _("Please enter a valid email address in 'NagiosMail' field.");
177       }
178     }
179     return($message);
180   }
183   /* Use Save_object for every Post handling */
184   function save_object()
185   {
186     if (isset($_POST['nagiosTab'])){
187       /* Save ldap attributes */
188       plugin::save_object();
190       /* Specialhandling for checkboxes */
191       foreach($this->is_chk_box as $val){
192         if(isset($_POST[$val])){
193           $this->$val = "checked";
194         }else{
195           $this->$val = "unchecked";
196         }
197       }
198     }
199   }
201   function remove_from_parent()
202   {
203     /* Cancel if there's nothing to do here */
204     if (!$this->initially_was_account){
205       return;
206     }
208     /* include global link_info */
209     $ldap= $this->config->get_ldap_link();
211     /* Remove and write to LDAP */
212     plugin::remove_from_parent();
214     /* Adapt attributes if needed */
215     //     $method= new $this->method($this->config);
216     //     $method->fixAttributesOnRemove($this);
218     @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,
219         $this->attributes, "Save");
220     $ldap->cd($this->dn);
221     $this->cleanup();
222 $ldap->modify ($this->attrs); 
224     show_ldap_error($ldap->get_error());
226     /* remove the entry from LDAP */
227     unset ($this->attrs['uid']);
229     /* Optionally execute a command after we're done */
230     $this->handle_post_events('remove');
231   }
235 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
237 ?>