PageRenderTime 59ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/www/include/options/accessLists/groupsACL/formGroupConfig.php

https://gitlab.com/florianocomercial/centreon
PHP | 303 lines | 174 code | 34 blank | 95 comment | 37 complexity | 663f536b55046c0447393ce53a17d68a MD5 | raw file
  1. <?php
  2. /*
  3. * Copyright 2005-2015 Centreon
  4. * Centreon is developped by : Julien Mathis and Romain Le Merlus under
  5. * GPL Licence 2.0.
  6. *
  7. * This program is free software; you can redistribute it and/or modify it under
  8. * the terms of the GNU General Public License as published by the Free Software
  9. * Foundation ; either version 2 of the License.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT ANY
  12. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  13. * PARTICULAR PURPOSE. See the GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program; if not, see <http://www.gnu.org/licenses>.
  17. *
  18. * Linking this program statically or dynamically with other modules is making a
  19. * combined work based on this program. Thus, the terms and conditions of the GNU
  20. * General Public License cover the whole combination.
  21. *
  22. * As a special exception, the copyright holders of this program give Centreon
  23. * permission to link this program with independent modules to produce an executable,
  24. * regardless of the license terms of these independent modules, and to copy and
  25. * distribute the resulting executable under terms of Centreon choice, provided that
  26. * Centreon also meet, for each linked independent module, the terms and conditions
  27. * of the license of that module. An independent module is a module which is not
  28. * derived from this program. If you modify this program, you may extend this
  29. * exception to your version of the program, but you are not obliged to do so. If you
  30. * do not wish to do so, delete this exception statement from your version.
  31. *
  32. * For more information : contact@centreon.com
  33. *
  34. */
  35. if (!isset($centreon)) {
  36. exit();
  37. }
  38. require_once _CENTREON_PATH_ . 'www/class/centreonLDAP.class.php';
  39. require_once _CENTREON_PATH_ . 'www/class/centreonContactgroup.class.php';
  40. /*
  41. * Retreive information
  42. */
  43. $group = array();
  44. if (($o == "c" || $o == "w") && $acl_group_id) {
  45. $DBRESULT = $pearDB->query("SELECT * FROM acl_groups WHERE acl_group_id = '".$acl_group_id."' LIMIT 1");
  46. /*
  47. * Set base value
  48. */
  49. $group = array_map("myDecode", $DBRESULT->fetchRow());
  50. /*
  51. * Set Contact Childs
  52. */
  53. $DBRESULT = $pearDB->query("SELECT DISTINCT contact_contact_id FROM acl_group_contacts_relations WHERE acl_group_id = '".$acl_group_id."' AND contact_contact_id NOT IN (SELECT contact_id FROM contact WHERE contact_admin = '1')");
  54. for ($i = 0; $contacts = $DBRESULT->fetchRow(); $i++)
  55. $group["cg_contacts"][$i] = $contacts["contact_contact_id"];
  56. $DBRESULT->free();
  57. /*
  58. * Set ContactGroup Childs
  59. */
  60. $DBRESULT = $pearDB->query("SELECT DISTINCT cg_cg_id FROM acl_group_contactgroups_relations WHERE acl_group_id = '".$acl_group_id."'");
  61. for ($i = 0; $contactgroups = $DBRESULT->fetchRow(); $i++)
  62. $group["cg_contactGroups"][$i] = $contactgroups["cg_cg_id"];
  63. $DBRESULT->free();
  64. /*
  65. * Set Menu link List
  66. */
  67. $DBRESULT = $pearDB->query("SELECT DISTINCT acl_topology_id FROM acl_group_topology_relations WHERE acl_group_id = '".$acl_group_id."'");
  68. for ($i = 0; $data = $DBRESULT->fetchRow(); $i++)
  69. $group["menuAccess"][$i] = $data["acl_topology_id"];
  70. $DBRESULT->free();
  71. /*
  72. * Set resources List
  73. */
  74. $DBRESULT = $pearDB->query("SELECT DISTINCT acl_res_id FROM acl_res_group_relations WHERE acl_group_id = '".$acl_group_id."'");
  75. for ($i = 0; $data = $DBRESULT->fetchRow(); $i++)
  76. $group["resourceAccess"][$i] = $data["acl_res_id"];
  77. $DBRESULT->free();
  78. /*
  79. * Set Action List
  80. */
  81. $DBRESULT = $pearDB->query("SELECT DISTINCT acl_action_id FROM acl_group_actions_relations WHERE acl_group_id = '".$acl_group_id."'");
  82. for ($i = 0; $data = $DBRESULT->fetchRow(); $i++)
  83. $group["actionAccess"][$i] = $data["acl_action_id"];
  84. $DBRESULT->free();
  85. }
  86. /*
  87. * Database retrieve information for differents elements list we need on the page
  88. */
  89. # Contacts comes from DB -> Store in $contacts Array
  90. $contacts = array();
  91. $query = "SELECT contact_id, contact_name FROM contact WHERE contact_admin = '0' AND contact_register = 1 ORDER BY contact_name";
  92. $DBRESULT = $pearDB->query($query);
  93. while ($contact = $DBRESULT->fetchRow()) {
  94. $contacts[$contact["contact_id"]] = $contact["contact_name"];
  95. }
  96. unset($contact);
  97. $DBRESULT->free();
  98. $cg = new CentreonContactgroup($pearDB);
  99. $contactGroups = $cg->getListContactgroup(true);
  100. # topology comes from DB -> Store in $contacts Array
  101. $menus = array();
  102. $DBRESULT = $pearDB->query("SELECT acl_topo_id, acl_topo_name FROM acl_topology ORDER BY acl_topo_name");
  103. while ($topo = $DBRESULT->fetchRow())
  104. $menus[$topo["acl_topo_id"]] = $topo["acl_topo_name"];
  105. unset($topo);
  106. $DBRESULT->free();
  107. # Action comes from DB -> Store in $contacts Array
  108. $action = array();
  109. $DBRESULT = $pearDB->query("SELECT acl_action_id, acl_action_name FROM acl_actions ORDER BY acl_action_name");
  110. while ($data = $DBRESULT->fetchRow())
  111. $action[$data["acl_action_id"]] = $data["acl_action_name"];
  112. unset($data);
  113. $DBRESULT->free();
  114. # Resources comes from DB -> Store in $contacts Array
  115. $resources = array();
  116. $DBRESULT = $pearDB->query("SELECT acl_res_id, acl_res_name FROM acl_resources ORDER BY acl_res_name");
  117. while ($res = $DBRESULT->fetchRow())
  118. $resources[$res["acl_res_id"]] = $res["acl_res_name"];
  119. unset($res);
  120. $DBRESULT->free();
  121. ##########################################################
  122. # Var information to format the element
  123. #
  124. $attrsText = array("size"=>"30");
  125. $attrsAdvSelect = array("style" => "width: 300px; height: 130px;");
  126. $attrsTextarea = array("rows"=>"6", "cols"=>"150");
  127. $eTemplate = '<table><tr><td><div class="ams">{label_2}</div>{unselected}</td><td align="center">{add}<br /><br /><br />{remove}</td><td><div class="ams">{label_3}</div>{selected}</td></tr></table>';
  128. $form = new HTML_QuickForm('Form', 'post', "?p=".$p);
  129. if ($o == "a")
  130. $form->addElement('header', 'title', _("Add a Group"));
  131. else if ($o == "c")
  132. $form->addElement('header', 'title', _("Modify a Group"));
  133. else if ($o == "w")
  134. $form->addElement('header', 'title', _("View a Group"));
  135. /*
  136. * Contact basic information
  137. */
  138. $form->addElement('header', 'information', _("General Information"));
  139. $form->addElement('text', 'acl_group_name', _("Group Name"), $attrsText);
  140. $form->addElement('text', 'acl_group_alias', _("Alias"), $attrsText);
  141. /*
  142. * Contacts Selection
  143. */
  144. $form->addElement('header', 'notification', _("Relations"));
  145. $form->addElement('header', 'menu', _("Menu access list link"));
  146. $form->addElement('header', 'resource', _("Resources access list link"));
  147. $form->addElement('header', 'actions', _("Action access list link"));
  148. $ams1 = $form->addElement('advmultiselect', 'cg_contacts', array(_("Linked Contacts"), _("Available"), _("Selected")), $contacts, $attrsAdvSelect, SORT_ASC);
  149. $ams1->setButtonAttributes('add', array('value' => _("Add"), "class" => "btc bt_success"));
  150. $ams1->setButtonAttributes('remove', array('value' => _("Remove"), "class" => "btc bt_danger"));
  151. $ams1->setElementTemplate($eTemplate);
  152. echo $ams1->getElementJs(false);
  153. $ams1 = $form->addElement('advmultiselect', 'cg_contactGroups', array(_("Linked Contact Groups"), _("Available"), _("Selected")), $contactGroups, $attrsAdvSelect, SORT_ASC);
  154. $ams1->setButtonAttributes('add', array('value' => _("Add"), "class" => "btc bt_success"));
  155. $ams1->setButtonAttributes('remove', array('value' => _("Remove"), "class" => "btc bt_danger"));
  156. $ams1->setElementTemplate($eTemplate);
  157. echo $ams1->getElementJs(false);
  158. $ams1 = $form->addElement('advmultiselect', 'menuAccess', array(_("Menu access"), _("Available"), _("Selected")), $menus, $attrsAdvSelect, SORT_ASC);
  159. $ams1->setButtonAttributes('add', array('value' => _("Add"), "class" => "btc bt_success"));
  160. $ams1->setButtonAttributes('remove', array('value' => _("Remove"), "class" => "btc bt_danger"));
  161. $ams1->setElementTemplate($eTemplate);
  162. echo $ams1->getElementJs(false);
  163. $ams1 = $form->addElement('advmultiselect', 'actionAccess', array(_("Actions access"), _("Available"), _("Selected")), $action, $attrsAdvSelect, SORT_ASC);
  164. $ams1->setButtonAttributes('add', array('value' => _("Add"), "class" => "btc bt_success"));
  165. $ams1->setButtonAttributes('remove', array('value' => _("Remove"), "class" => "btc bt_danger"));
  166. $ams1->setElementTemplate($eTemplate);
  167. echo $ams1->getElementJs(false);
  168. $ams1 = $form->addElement('advmultiselect', 'resourceAccess', array(_("Resources access"), _("Available"), _("Selected")), $resources, $attrsAdvSelect, SORT_ASC);
  169. $ams1->setButtonAttributes('add', array('value' => _("Add"), "class" => "btc bt_success"));
  170. $ams1->setButtonAttributes('remove', array('value' => _("Remove"), "class" => "btc bt_danger"));
  171. $ams1->setElementTemplate($eTemplate);
  172. echo $ams1->getElementJs(false);
  173. /*
  174. * Further informations
  175. */
  176. $form->addElement('header', 'furtherInfos', _("Additional Information"));
  177. $groupActivation[] = HTML_QuickForm::createElement('radio', 'acl_group_activate', null, _("Enabled"), '1');
  178. $groupActivation[] = HTML_QuickForm::createElement('radio', 'acl_group_activate', null, _("Disabled"), '0');
  179. $form->addGroup($groupActivation, 'acl_group_activate', _("Status"), '&nbsp;');
  180. $form->setDefaults(array('acl_group_activate' => '1'));
  181. $tab = array();
  182. $tab[] = HTML_QuickForm::createElement('radio', 'action', null, _("List"), '1');
  183. $tab[] = HTML_QuickForm::createElement('radio', 'action', null, _("Form"), '0');
  184. $form->addGroup($tab, 'action', _("Post Validation"), '&nbsp;');
  185. $form->setDefaults(array('action' => '1'));
  186. $form->addElement('hidden', 'acl_group_id');
  187. $redirect = $form->addElement('hidden', 'o');
  188. $redirect->setValue($o);
  189. /*
  190. * Form Rules
  191. */
  192. function myReplace() {
  193. global $form;
  194. $ret = $form->getSubmitValues();
  195. return (str_replace(" ", "_", $ret["acl_group_name"]));
  196. }
  197. $form->applyFilter('__ALL__', 'myTrim');
  198. $form->applyFilter('acl_group_name', 'myReplace');
  199. $form->addRule('acl_group_name', _("Compulsory Name"), 'required');
  200. $form->addRule('acl_group_alias', _("Compulsory Alias"), 'required');
  201. $form->registerRule('exist', 'callback', 'testGroupExistence');
  202. $form->addRule('acl_group_name', _("Name is already in use"), 'exist');
  203. $form->setRequiredNote("<font style='color: red;'>*</font>&nbsp;". _("Required fields"));
  204. $form->registerRule('cg_group_exists', 'callback', 'testCg');
  205. $form->addRule('cg_contactGroups', _('Contactgroups exists. If you try to use a LDAP contactgroup, please verified if a Centreon contactgroup has the same name.'), 'cg_group_exists');
  206. /*
  207. * Smarty template Init
  208. */
  209. $tpl = new Smarty();
  210. $tpl = initSmartyTpl($path, $tpl);
  211. /*
  212. * Define tab title
  213. */
  214. $tpl->assign("sort1", _("Group Information"));
  215. $tpl->assign("sort2", _("Authorizations information"));
  216. // prepare help texts
  217. $helptext = "";
  218. include_once("help.php");
  219. foreach ($help as $key => $text) {
  220. $helptext .= '<span style="display:none" id="help:'.$key.'">'.$text.'</span>'."\n";
  221. }
  222. $tpl->assign("helptext", $helptext);
  223. /*
  224. * Just watch a Contact Group information
  225. */
  226. if ($o == "w") {
  227. $form->addElement("button", "change", _("Modify"), array("onClick"=>"javascript:window.location.href='?p=".$p."&o=c&cg_id=".$group_id."'"));
  228. $form->setDefaults($group);
  229. $form->freeze();
  230. } else if ($o == "c") {
  231. /*
  232. * Modify a Contact Group information
  233. */
  234. $subC = $form->addElement('submit', 'submitC', _("Save"), array("class" => "btc bt_success"));
  235. $res = $form->addElement('reset', 'reset', _("Reset"), array("class" => "btc bt_default"));
  236. $form->setDefaults($group);
  237. } else if ($o == "a") {
  238. /*
  239. * Add a Contact Group information
  240. */
  241. $subA = $form->addElement('submit', 'submitA', _("Save"), array("class" => "btc bt_success"));
  242. $res = $form->addElement('reset', 'reset', _("Reset"), array("class" => "btc bt_default"));
  243. }
  244. $valid = false;
  245. if ($form->validate()) {
  246. $groupObj = $form->getElement('acl_group_id');
  247. if ($form->getSubmitValue("submitA")) {
  248. $groupObj->setValue(insertGroupInDB());
  249. } else if ($form->getSubmitValue("submitC")) {
  250. updateGroupInDB($groupObj->getValue());
  251. }
  252. $o = NULL;
  253. $valid = true;
  254. }
  255. $action = $form->getSubmitValue("action");
  256. if ($valid) {
  257. require_once($path."listGroupConfig.php");
  258. } else {
  259. /*
  260. * Apply a template definition
  261. */
  262. $renderer = new HTML_QuickForm_Renderer_ArraySmarty($tpl, true);
  263. $renderer->setRequiredTemplate('{$label}&nbsp;<font color="red" size="1">*</font>');
  264. $renderer->setErrorTemplate('<font color="red">{$error}</font><br />{$html}');
  265. $form->accept($renderer);
  266. $tpl->assign('form', $renderer->toArray());
  267. $tpl->assign('o', $o);
  268. $tpl->display("formGroupConfig.ihtml");
  269. }