Code

1a3e15edc4b5da1bf45adec9317b3959b436662b
[gosa.git] / gosa-plugins / nagios / 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   /* plugin specific values */
22   var $NagiosAlias  = "";
23   var $NagiosPager  = "";
24   var $NagiosMail   = "";
26   var $HostNotificationOptions      = "";
27   var $HostNotificationPeriod       = "";
28   var $ServiceNotificationPeriod    = "";
29   var $ServiceNotitificationPeriod  = "";
30   var $HostNotificationCommands     = "";
31   var $ServiceNotificationCommands  = "";
32   var $HostNotitificationOptions    = "";
33   var $ServiceNotificationOptions   = "";
35   var $AuthorizedAllHosts                 = "unchecked";
36   var $AuthorizedAllServices              = "unchecked";
37   var $AuthorizedSystemCommands           = "unchecked";
38   var $AuthorizedAllHostCommands          = "unchecked";
39   var $AuthorizedAllServiceCommands       = "unchecked";
40   var $AuthorizedConfigurationInformation = "unchecked";
41   var $AuthorizedSystemInformation        = "unchecked";
43   /* attribute list for save action */
45   var $attributes= array("NagiosAlias", "NagiosPager", "NagiosMail", "HostNotificationPeriod",
46       "ServiceNotificationPeriod", "HostNotificationCommands", "ServiceNotificationCommands",
47       "HostNotificationOptions", "ServiceNotificationOptions", "AuthorizedAllHosts", "AuthorizedAllServices",
48       "AuthorizedSystemCommands", "AuthorizedAllHostCommands", "AuthorizedAllServiceCommands",
49       "AuthorizedConfigurationInformation", "AuthorizedSystemInformation");
51   var $is_chk_box = array("AuthorizedAllHosts","AuthorizedAllServices","AuthorizedSystemCommands",
52       "AuthorizedAllHostCommands","AuthorizedAllServiceCommands",
53       "AuthorizedConfigurationInformation","AuthorizedSystemInformation");
55   var $objectclasses= array("nagiosContact","nagiosAuth");
57   var $uid = "";
58   var $view_logged = FALSE;
60   /* constructor, if 'dn' is set, the node loads the given
61      'dn' from LDAP */
62   function nagiosAccount (&$config, $dn= NULL)
63   {
64     /* Configuration is fine, allways */
65     $this->config= $config;
67     plugin::plugin ($config, $dn);
69     /* Setting uid to default */
70     if(isset($this->attrs['uid'][0])){
71       $this->uid = $this->attrs['uid'][0];
72     }
74     /* Save initial account state */  
75     $this->initially_was_account= $this->is_account;
76   }
78   function execute()
79   {
80     /* Call parent execute */
81     plugin::execute();
83     /* Log view */
84     if($this->is_account && !$this->view_logged){
85       $this->view_logged = TRUE;
86       new log("view","users/".get_class($this),$this->dn);
87     }
89     /* Load templating engine */
90     $smarty= get_smarty();
91     $display= "";
93     /* Do we need to flip is_account state? */
94     if(isset($_POST['modify_state'])){
95       if($this->is_account && $this->acl_is_removeable()){
96         $this->is_account= FALSE;
97       }elseif(!$this->is_account && $this->acl_is_createable()){
98         $this->is_account= TRUE;
99       }
100     }    
102     /* Do we represent a valid account? */
103     if (!$this->is_account && $this->parent === NULL){
104       $display= "<img alt=\"\"src=\"images/small-error.png\" align=\"middle\">&nbsp;<b>".
105         msgPool::noValidextension(_("Nagios"))."</b>";
107       $display.= back_to_main();
108       return ($display);
109     }
111     /* Show tab dialog headers */
112     if ($this->parent !== NULL){
113       if ($this->is_account){
114         $display= $this->show_disable_header(msgPool::removeFeaturesButton(_("Nagios")), msgPool::featuresEnabled(_("Nagios")));
115       } else {
116         $display= $this->show_enable_header(msgPool::addFeaturesButton(_("Nagios")), msgPool::featuresDisabled(_("Nagios")));
117         return($display);
118       }
119     }
121     /* Assign acls */
122     $tmp = $this->plInfo();
123     $SkipWrite = (!isset($this->parent) || !$this->parent) && !session::is_set('edit');
124     foreach($tmp['plProvidedAcls'] as $acl => $desc){
125       $smarty->assign($acl."ACL",$this->getacl($acl,$SkipWrite));
126     }
128     /* Assign attributes an ACL to smarty */
129     foreach($this->attributes as $val) { 
130       $smarty->assign("$val", $this->$val);
131       if(in_array($val,$this->is_chk_box)){
132         if($this->$val == "checked"){
133           $smarty->assign($val."CHK", " checked ");
134         }else{
135           $smarty->assign($val."CHK", "");
136         }
137       }
138     }
139     $smarty->assign('ServiceNotificationOptionsValues'  ,array("w,u,c,r,f"=>"w,u,c,r,f" ,"w,u,c,r"=>"w,u,c,r" ,"w,u,c"=>"w,u,c" ,"c,w"=>"c,w","n"=>"n"));
140     $smarty->assign('HostNotificationOptionsValues'     ,array("d,u,r,f"=>"d,u,r,f" ,"d,u,r"=>"d,u,r" ,"d,u"=>"d,u","n"=>"n"));
141     $smarty->assign('ServiceNotificationPeriodValues'   ,array("24x7"=>"24x7" ,"24x5"=>"24x5" ,"8x5"=>"8x5"));
142     $smarty->assign('HostNotificationPeriodValues'      ,array("24x7"=>"24x7" ,"24x5"=>"24x5" ,"8x5"=>"8x5"));
143     $display.= $smarty->fetch (get_template_path('nagios.tpl', TRUE, dirname(__FILE__)));
144     return ($display);
145   }
147   /* Save to LDAP */
148   function save()
149   {
150     $ldap= $this->config->get_ldap_link();
152     /* Call parents save to prepare $this->attrs */
153     plugin::save();
155     /* Adapt attributes if needed */
156     //$method= new $this->method($this->config);
157     //$id= $method->uattrib;
158     //$method->fixAttributesOnStore($this);
160     /* Write back to ldap */
161     $ldap->cd($this->dn);
162     $this->cleanup();
163     $ldap->modify ($this->attrs); 
165     /* Log last action */
166     if($this->initially_was_account){
167       new log("modify","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
168     }else{
169       new log("create","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
170     }
172     if (!$ldap->success()){
173       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
174     }
176     /* Optionally execute a command after we're done */
177     if ($this->initially_was_account == $this->is_account){
178       if ($this->is_modified){
179         $this->handle_post_events("modify",array("uid" => $this->uid));
180       }
181     } else {
182       $this->handle_post_events("add", array("uid" => $this->uid));
183     }
184   }
186   function check()
187   {
188     /* Call common method to give check the hook */
189     $message= plugin::check();
191     $ldap= $this->config->get_ldap_link();
192     /* must: mail */
193     if ($this->NagiosAlias == ""){
194       $message[]= msgPool::required(_("Alias"));
195     }
196     if ($this->NagiosMail == ""){
197       $message[]= msgPool::required(_("Mail"));
198     }
199     if ($this->is_template){
200       if (!tests::is_email($this->NagiosMail, TRUE)){
201         $message[]= msgPool::invalid(_("Mail"),"","","%givenName.%dn@your-domain.com");
202       }
203     } else {
204       if (!tests::is_email($this->NagiosMail)){
205         $message[]= msgPool::invalid(_("Mail"),"","","your-domain@your-domain.com");
206       }
207     }
208     return($message);
209   }
212   /* Use Save_object for every Post handling */
213   function save_object()
214   {
215     if (isset($_POST['nagiosTab'])){
216       /* Save ldap attributes */
217       plugin::save_object();
219       /* Specialhandling for checkboxes */
220       foreach($this->is_chk_box as $val){
221         if($this->acl_is_writeable($val)){
222           if(isset($_POST[$val])){
223             $this->$val = "checked";
224           }else{
225             $this->$val = "unchecked";
226           }
227         }
228       }
229     }
230   }
232   function remove_from_parent()
233   {
234     /* Cancel if there's nothing to do here */
235     if (!$this->initially_was_account){
236       return;
237     }
239     /* include global link_info */
240     $ldap= $this->config->get_ldap_link();
242     /* Remove and write to LDAP */
243     plugin::remove_from_parent();
245     /* Adapt attributes if needed */
246     //     $method= new $this->method($this->config);
247     //     $method->fixAttributesOnRemove($this);
249     @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,
250         $this->attributes, "Save");
251     $ldap->cd($this->dn);
252     $this->cleanup();
253     $ldap->modify ($this->attrs); 
255     new log("view","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
257     if (!$ldap->success()){
258       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
259     }
261     /* remove the entry from LDAP */
262     unset ($this->attrs['uid']);
264     /* Optionally execute a command after we're done */
265     $this->handle_post_events('remove',array("uid" => $this->uid));
266   }
270   static function plInfo()
271   {
272     return (array(
273           "plShortName"     => _("Nagios"),
274           "plDescription"   => _("Nagios account settings"),
275           "plSelfModify"    => TRUE,
276           "plDepends"       => array("user"),
277           "plPriority"      => 8,                                 // Position in tabs
278           "plSection"     => array("personal" => _("My account")),
279           "plCategory"    => array("users"),
280           "plOptions"       => array(),
282           "plProvidedAcls"  => array(
284             "NagiosAlias"                       =>  _("Alias"),
285             "NagiosMail"                        =>  _("Mail address"),
286             "HostNotificationPeriod"            =>  _("Host notification period"),
287             "ServiceNotificationPeriod"         =>  _("Service notification period"),
288             "HostNotificationOptions"           =>  _("Host notification options"),
289             "ServiceNotificationOptions"        =>  _("Service notification options"),
290             "NagiosPager"                       =>  _("Pager number"),
292             "ServiceNotificationCommands"       =>  _("Service notification commands"),
293             "HostNotificationCommands"          =>  _("Host notification commands"),
294             "AuthorizedSystemInformation"       =>  _("View system informations"),
295             "AuthorizedConfigurationInformation"=>  _("View configuration information"),
296             "AuthorizedSystemCommands"          =>  _("Trigger system commands"),
297             "AuthorizedAllServices"             =>  _("View all services"),
298             "AuthorizedAllHosts"                =>  _("View all hosts"),
299             "AuthorizedAllServiceCommands"      =>  _("Trigger all service commands"),
300             "AuthorizedAllHostCommands"         =>  _("Trigger all host commands"))
302       ));
303   }
306 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
308 ?>