Code

9c0b747eb7b7cf511e3cd41832a382ce1d0480c4
[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= "Manage personal Nagios settings";
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     foreach($tmp['plProvidedAcls'] as $acl => $desc){
124       $smarty->assign($acl."ACL",$this->getacl($acl));
125     }
127     /* Assign attributes an ACL to smarty */
128     foreach($this->attributes as $val) { 
129       $smarty->assign("$val", set_post($this->$val));
130       if(in_array($val,$this->is_chk_box)){
131         if($this->$val == "checked"){
132           $smarty->assign($val."CHK", " checked ");
133         }else{
134           $smarty->assign($val."CHK", "");
135         }
136       }
137     }
138     $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"));
139     $smarty->assign('HostNotificationOptionsValues'     ,array("d,u,r,f"=>"d,u,r,f" ,"d,u,r"=>"d,u,r" ,"d,u"=>"d,u","n"=>"n"));
140     $smarty->assign('ServiceNotificationPeriodValues'   ,array("24x7"=>"24x7" ,"24x5"=>"24x5" ,"8x5"=>"8x5"));
141     $smarty->assign('HostNotificationPeriodValues'      ,array("24x7"=>"24x7" ,"24x5"=>"24x5" ,"8x5"=>"8x5"));
142     $display.= $smarty->fetch (get_template_path('nagios.tpl', TRUE, dirname(__FILE__)));
143     return ($display);
144   }
146   /* Save to LDAP */
147   function save()
148   {
149     $ldap= $this->config->get_ldap_link();
151     /* Call parents save to prepare $this->attrs */
152     plugin::save();
154     /* Adapt attributes if needed */
155     //$method= new $this->method($this->config);
156     //$id= $method->uattrib;
157     //$method->fixAttributesOnStore($this);
159     /* Write back to ldap */
160     $ldap->cd($this->dn);
161     $this->cleanup();
162     $ldap->modify ($this->attrs); 
164     /* Log last action */
165     if($this->initially_was_account){
166       new log("modify","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
167     }else{
168       new log("create","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
169     }
171     if (!$ldap->success()){
172       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
173     }
175     /* Optionally execute a command after we're done */
176     if ($this->initially_was_account == $this->is_account){
177       if ($this->is_modified){
178         $this->handle_post_events("modify",array("uid" => $this->uid));
179       }
180     } else {
181       $this->handle_post_events("add", array("uid" => $this->uid));
182     }
183   }
185   function check()
186   {
187     /* Call common method to give check the hook */
188     $message= plugin::check();
190     $ldap= $this->config->get_ldap_link();
191     /* must: mail */
192     if ($this->NagiosAlias == ""){
193       $message[]= msgPool::required(_("Alias"));
194     }
195     if ($this->NagiosMail == ""){
196       $message[]= msgPool::required(_("Mail"));
197     }
198     if ($this->is_template){
199       if (!tests::is_email($this->NagiosMail, TRUE)){
200         $message[]= msgPool::invalid(_("Mail"),"","","%givenName.%dn@your-domain.com");
201       }
202     } else {
203       if (!tests::is_email($this->NagiosMail)){
204         $message[]= msgPool::invalid(_("Mail"),"","","your-domain@your-domain.com");
205       }
206     }
207     return($message);
208   }
211   /* Use Save_object for every Post handling */
212   function save_object()
213   {
214     if (isset($_POST['nagiosTab'])){
215       /* Save ldap attributes */
216       plugin::save_object();
218       /* Specialhandling for checkboxes */
219       foreach($this->is_chk_box as $val){
220         if($this->acl_is_writeable($val)){
221           if(isset($_POST[$val])){
222             $this->$val = "checked";
223           }else{
224             $this->$val = "unchecked";
225           }
226         }
227       }
228     }
229   }
231   function remove_from_parent()
232   {
233     /* Cancel if there's nothing to do here */
234     if (!$this->initially_was_account){
235       return;
236     }
238     /* include global link_info */
239     $ldap= $this->config->get_ldap_link();
241     /* Remove and write to LDAP */
242     plugin::remove_from_parent();
244     /* Adapt attributes if needed */
245     //     $method= new $this->method($this->config);
246     //     $method->fixAttributesOnRemove($this);
248     @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,
249         $this->attributes, "Save");
250     $ldap->cd($this->dn);
251     $this->cleanup();
252     $ldap->modify ($this->attrs); 
254     new log("view","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
256     if (!$ldap->success()){
257       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
258     }
260     /* remove the entry from LDAP */
261     unset ($this->attrs['uid']);
263     /* Optionally execute a command after we're done */
264     $this->handle_post_events('remove',array("uid" => $this->uid));
265   }
269   static function plInfo()
270   {
271     return (array(
272           "plShortName"     => _("Nagios"),
273           "plDescription"   => _("Nagios account settings"),
274           "plSelfModify"    => TRUE,
275           "plDepends"       => array("user"),
276           "plPriority"      => 8,                                 // Position in tabs
277           "plSection"     => array("personal" => _("My account")),
278           "plCategory"    => array("users"),
279           "plOptions"       => array(),
280           "plRequirements"=> array(
281               'ldapSchema' => array('nagiosContact'=>'','nagiosAuth' => ''),
282               'onFailureDisablePlugin' => array(get_class())
283               ),
285           "plProvidedAcls"  => array(
287             "NagiosAlias"                       =>  _("Alias"),
288             "NagiosMail"                        =>  _("Mail address"),
289             "HostNotificationPeriod"            =>  _("Host notification period"),
290             "ServiceNotificationPeriod"         =>  _("Service notification period"),
291             "HostNotificationOptions"           =>  _("Host notification options"),
292             "ServiceNotificationOptions"        =>  _("Service notification options"),
293             "NagiosPager"                       =>  _("Pager number"),
295             "ServiceNotificationCommands"       =>  _("Service notification commands"),
296             "HostNotificationCommands"          =>  _("Host notification commands"),
297             "AuthorizedSystemInformation"       =>  _("View system informations"),
298             "AuthorizedConfigurationInformation"=>  _("View configuration information"),
299             "AuthorizedSystemCommands"          =>  _("Trigger system commands"),
300             "AuthorizedAllServices"             =>  _("View all services"),
301             "AuthorizedAllHosts"                =>  _("View all hosts"),
302             "AuthorizedAllServiceCommands"      =>  _("Trigger all service commands"),
303             "AuthorizedAllHostCommands"         =>  _("Trigger all host commands"))
305       ));
306   }
309 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
311 ?>