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