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 var $uid = "";
63 var $view_logged = FALSE;
65 /* constructor, if 'dn' is set, the node loads the given
66 'dn' from LDAP */
67 function nagiosAccount (&$config, $dn= NULL)
68 {
69 /* Configuration is fine, allways */
70 $this->config= $config;
72 plugin::plugin ($config, $dn);
74 /* Setting uid to default */
75 if(isset($this->attrs['uid'][0])){
76 $this->uid = $this->attrs['uid'][0];
77 }
79 /* Save initial account state */
80 $this->initially_was_account= $this->is_account;
81 }
83 function execute()
84 {
85 /* Call parent execute */
86 plugin::execute();
88 /* Log view */
89 if($this->is_account && !$this->view_logged){
90 $this->view_logged = TRUE;
91 new log("view","users/".get_class($this),$this->dn);
92 }
94 /* Load templating engine */
95 $smarty= get_smarty();
96 $display= "";
98 /* Do we need to flip is_account state? */
99 if(isset($_POST['modify_state'])){
100 if($this->is_account && $this->acl_is_removeable()){
101 $this->is_account= FALSE;
102 }elseif(!$this->is_account && $this->acl_is_createable()){
103 $this->is_account= TRUE;
104 }
105 }
107 /* Do we represent a valid account? */
108 if (!$this->is_account && $this->parent === NULL){
109 $display= "<img alt=\"\"src=\"images/stop.png\" align=\"middle\"> <b>".
110 _("This account has no nagios extensions.")."</b>";
112 $display.= back_to_main();
113 return ($display);
114 }
116 /* Show tab dialog headers */
117 if ($this->parent !== NULL){
118 if ($this->is_account){
119 $display= $this->show_disable_header(_("Remove nagios account"),
120 _("This account has nagios features enabled. You can disable them by clicking below."));
121 } else {
122 $display= $this->show_enable_header(_("Create nagios account"), _("This account has nagios features disabled. You can enable them by clicking below."));
123 return($display);
124 }
125 }
127 /* Assign acls */
128 $tmp = $this->plInfo();
129 $SkipWrite = (!isset($this->parent) || !$this->parent) && !isset($_SESSION['edit']);
130 foreach($tmp['plProvidedAcls'] as $acl => $desc){
131 $smarty->assign($acl."ACL",$this->getacl($acl,$SkipWrite));
132 }
134 /* Assign attributes an ACL to smarty */
135 foreach($this->attributes as $val) {
136 $smarty->assign("$val", $this->$val);
137 if(in_array($val,$this->is_chk_box)){
138 if($this->$val == "checked"){
139 $smarty->assign($val."CHK", " checked ");
140 }else{
141 $smarty->assign($val."CHK", "");
142 }
143 }
144 }
145 $smarty->assign('ServiceNotificationOptionsValues' ,array("d,u,r,v"=>"d,u,r,v" ,"d,u,r"=>"d,u,r" ,"d,u"=>"d,u"));
146 $smarty->assign('HostNotificationOptionsValues' ,array("w,u,c,r"=>"w,u,c,r" ,"w,u,c"=>"w,u,c" ,"c,w"=>"c,w"));
147 $smarty->assign('ServiceNotificationPeriodValues' ,array("24x7"=>"24x7" ,"24x5"=>"24x5" ,"8x5"=>"8x5"));
148 $smarty->assign('HostNotificationPeriodValues' ,array("24x7"=>"24x7" ,"24x5"=>"24x5" ,"8x5"=>"8x5"));
149 $display.= $smarty->fetch (get_template_path('nagios.tpl', TRUE, dirname(__FILE__)));
150 return ($display);
151 }
153 /* Save to LDAP */
154 function save()
155 {
156 $ldap= $this->config->get_ldap_link();
158 /* Call parents save to prepare $this->attrs */
159 plugin::save();
161 /* Adapt attributes if needed */
162 //$method= new $this->method($this->config);
163 //$id= $method->uattrib;
164 //$method->fixAttributesOnStore($this);
166 /* Write back to ldap */
167 $ldap->cd($this->dn);
168 $this->cleanup();
169 $ldap->modify ($this->attrs);
171 /* Log last action */
172 if($this->initially_was_account){
173 new log("modify","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
174 }else{
175 new log("create","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
176 }
178 show_ldap_error($ldap->get_error(), sprintf(_("Saving of user/nagios account with dn '%s' failed."),$this->dn));
180 /* Optionally execute a command after we're done */
181 if ($this->initially_was_account == $this->is_account){
182 if ($this->is_modified){
183 $this->handle_post_events("modify",array("uid" => $this->uid));
184 }
185 } else {
186 $this->handle_post_events("add", array("uid" => $this->uid));
187 }
188 }
190 function check()
191 {
192 /* Call common method to give check the hook */
193 $message= plugin::check();
195 $ldap= $this->config->get_ldap_link();
196 /* must: mail */
197 if ($this->NagiosAlias == ""){
198 $message[]= _("The required field 'NagiosAlias' is not set.");
199 }
200 if ($this->NagiosMail == ""){
201 $message[]= _("The required field 'NagiosMail' is not set.");
202 }
203 if ($this->is_template){
204 if (!is_email($this->NagiosMail, TRUE)){
205 $message[]= _("Please enter a valid email address in 'NagiosMail' field.");
206 }
207 } else {
208 if (!is_email($this->NagiosMail)){
209 $message[]= _("Please enter a valid email address in 'NagiosMail' field.");
210 }
211 }
212 return($message);
213 }
216 /* Use Save_object for every Post handling */
217 function save_object()
218 {
219 if (isset($_POST['nagiosTab'])){
220 /* Save ldap attributes */
221 plugin::save_object();
223 /* Specialhandling for checkboxes */
224 foreach($this->is_chk_box as $val){
225 if($this->acl_is_writeable($val)){
226 if(isset($_POST[$val])){
227 $this->$val = "checked";
228 }else{
229 $this->$val = "unchecked";
230 }
231 }
232 }
233 }
234 }
236 function remove_from_parent()
237 {
238 /* Cancel if there's nothing to do here */
239 if (!$this->initially_was_account){
240 return;
241 }
243 /* include global link_info */
244 $ldap= $this->config->get_ldap_link();
246 /* Remove and write to LDAP */
247 plugin::remove_from_parent();
249 /* Adapt attributes if needed */
250 // $method= new $this->method($this->config);
251 // $method->fixAttributesOnRemove($this);
253 @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,
254 $this->attributes, "Save");
255 $ldap->cd($this->dn);
256 $this->cleanup();
257 $ldap->modify ($this->attrs);
259 new log("view","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
261 show_ldap_error($ldap->get_error(), sprintf(_("Removing of user/nagios account with dn '%s' failed."),$this->dn));
263 /* remove the entry from LDAP */
264 unset ($this->attrs['uid']);
266 /* Optionally execute a command after we're done */
267 $this->handle_post_events('remove',array("uid" => $this->uid));
268 }
272 function plInfo()
273 {
274 return (array(
275 "plShortName" => _("Nagios"),
276 "plDescription" => _("Nagios account settings"),
277 "plSelfModify" => TRUE,
278 "plDepends" => array("user"),
279 "plPriority" => 8, // Position in tabs
280 "plSection" => array("personal" => _("My account")),
281 "plCategory" => array("users"),
282 "plOptions" => array(),
284 "plProvidedAcls" => array(
286 "NagiosAlias" => _("Alias"),
287 "NagiosPager" => _("Pager number"),
288 "NagiosMail" => _("Mail address"),
290 "HostNotificationCommands" => _("Host notification commands"),
291 "HostNotificationOptions" => _("Host notification options"),
292 "HostNotificationPeriod" => _("Host notification period"),
294 "ServiceNotificationCommands" => _("Service notification commands"),
295 "ServiceNotificationOptions" => _("Service notification options"),
296 "ServiceNotificationPeriod" => _("Service notification period"),
298 "AuthorizedAllHosts" => _("View all hosts"),
299 "AuthorizedAllServices" => _("View all services"),
301 "AuthorizedSystemCommands" => _("Trigger system commands"),
302 "AuthorizedAllHostCommands" => _("Trigger all host commands"),
303 "AuthorizedAllServiceCommands" => _("Trigger all service commands"),
305 "AuthorizedConfigurationInformation"=> _("View configuration information"),
306 "AuthorizedSystemInformation" => _("View system informations"))
307 ));
308 }
309 }
311 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
313 ?>