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;
80 }
82 function execute()
83 {
84 /* Call parent execute */
85 plugin::execute();
87 /* Load templating engine */
88 $smarty= get_smarty();
89 $display= "";
91 /* Do we need to flip is_account state? */
92 if(isset($_POST['modify_state'])){
93 if($this->is_account && $this->acl_is_removeable()){
94 $this->is_account= FALSE;
95 }elseif(!$this->is_account && $this->acl_is_createable()){
96 $this->is_account= TRUE;
97 }
98 }
100 /* Do we represent a valid account? */
101 if (!$this->is_account && $this->parent == NULL){
102 $display= "<img alt=\"\"src=\"images/stop.png\" align=\"middle\"> <b>".
103 _("This account has no nagios extensions.")."</b>";
105 $display.= back_to_main();
106 return ($display);
107 }
109 /* Show tab dialog headers */
110 if ($this->parent != NULL){
111 if ($this->is_account){
112 $display= $this->show_disable_header(_("Remove nagios account"),
113 _("This account has nagios features enabled. You can disable them by clicking below."));
114 } else {
115 $display= $this->show_enable_header(_("Create nagios account"), _("This account has nagios features disabled. You can enable them by clicking below."));
116 return($display);
117 }
118 }
120 /* Assign acls */
121 $tmp = $this->plInfo();
122 $SkipWrite = (!isset($this->parent) || !$this->parent) && !isset($_SESSION['edit']);
123 foreach($tmp['plProvidedAcls'] as $acl => $desc){
124 $smarty->assign($acl."ACL",$this->getacl($acl,$SkipWrite));
125 }
127 /* Assign attributes an ACL to smarty */
128 foreach($this->attributes as $val) {
129 $smarty->assign("$val", $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("d,u,r,v"=>"d,u,r,v" ,"d,u,r"=>"d,u,r" ,"d,u"=>"d,u"));
139 $smarty->assign('HostNotificationOptionsValues' ,array("w,u,c,r"=>"w,u,c,r" ,"w,u,c"=>"w,u,c" ,"c,w"=>"c,w"));
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 show_ldap_error($ldap->get_error(), sprintf(_("Saving of user/nagios account with dn '%s' failed."),$this->dn));
166 /* Optionally execute a command after we're done */
167 if ($this->initially_was_account == $this->is_account){
168 if ($this->is_modified){
169 $this->handle_post_events("modify",array("uid" => $this->uid));
170 }
171 } else {
172 $this->handle_post_events("add", array("uid" => $this->uid));
173 }
174 }
176 function check()
177 {
178 /* Call common method to give check the hook */
179 $message= plugin::check();
181 $ldap= $this->config->get_ldap_link();
182 /* must: mail */
183 if ($this->NagiosAlias == ""){
184 $message[]= _("The required field 'NagiosAlias' is not set.");
185 }
186 if ($this->NagiosMail == ""){
187 $message[]= _("The required field 'NagiosMail' is not set.");
188 }
189 if ($this->is_template){
190 if (!is_email($this->NagiosMail, TRUE)){
191 $message[]= _("Please enter a valid email address in 'NagiosMail' field.");
192 }
193 } else {
194 if (!is_email($this->NagiosMail)){
195 $message[]= _("Please enter a valid email address in 'NagiosMail' field.");
196 }
197 }
198 return($message);
199 }
202 /* Use Save_object for every Post handling */
203 function save_object()
204 {
205 if (isset($_POST['nagiosTab'])){
206 /* Save ldap attributes */
207 plugin::save_object();
209 /* Specialhandling for checkboxes */
210 foreach($this->is_chk_box as $val){
211 if($this->acl_is_writeable($val)){
212 if(isset($_POST[$val])){
213 $this->$val = "checked";
214 }else{
215 $this->$val = "unchecked";
216 }
217 }
218 }
219 }
220 }
222 function remove_from_parent()
223 {
224 /* Cancel if there's nothing to do here */
225 if (!$this->initially_was_account){
226 return;
227 }
229 /* include global link_info */
230 $ldap= $this->config->get_ldap_link();
232 /* Remove and write to LDAP */
233 plugin::remove_from_parent();
235 /* Adapt attributes if needed */
236 // $method= new $this->method($this->config);
237 // $method->fixAttributesOnRemove($this);
239 @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,
240 $this->attributes, "Save");
241 $ldap->cd($this->dn);
242 $this->cleanup();
243 $ldap->modify ($this->attrs);
245 show_ldap_error($ldap->get_error(), sprintf(_("Removing of user/nagios account with dn '%s' failed."),$this->dn));
247 /* remove the entry from LDAP */
248 unset ($this->attrs['uid']);
250 /* Optionally execute a command after we're done */
251 $this->handle_post_events('remove',array("uid" => $this->uid));
252 }
256 function plInfo()
257 {
258 return (array(
259 "plShortName" => _("Nagios"),
260 "plDescription" => _("Nagios account settings"),
261 "plSelfModify" => TRUE,
262 "plDepends" => array("user"),
263 "plPriority" => 8, // Position in tabs
264 "plSection" => array("personal" => _("My account")),
265 "plCategory" => array("users"),
266 "plOptions" => array(),
268 "plProvidedAcls" => array(
270 "NagiosAlias" => _("Alias"),
271 "NagiosPager" => _("Pager number"),
272 "NagiosMail" => _("Mail address"),
274 "HostNotificationCommands" => _("Host notification commands"),
275 "HostNotificationOptions" => _("Host notification options"),
276 "HostNotificationPeriod" => _("Host notification period"),
278 "ServiceNotificationCommands" => _("Service notification commands"),
279 "ServiceNotificationOptions" => _("Service notification options"),
280 "ServiceNotificationPeriod" => _("Service notification period"),
282 "AuthorizedAllHosts" => _("View all hosts"),
283 "AuthorizedAllServices" => _("View all services"),
285 "AuthorizedSystemCommands" => _("Trigger system commands"),
286 "AuthorizedAllHostCommands" => _("Trigger all host commands"),
287 "AuthorizedAllServiceCommands" => _("Trigger all service commands"),
289 "AuthorizedConfigurationInformation"=> _("View configuration information"),
290 "AuthorizedSystemInformation" => _("View system informations"))
291 ));
292 }
293 }
295 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
297 ?>