drop support for php 7.4 (#5740)
[openemr.git] / interface / modules / zend_modules / module / Acl / src / Acl / Controller / AclController.php
blobd72f01ec70876c15dda37b3b9cdbaeefd8d1cab0
1 <?php
3 /**
4 * interface/modules/zend_modules/module/Acl/src/Acl/Controller/AclController.php
6 * @package OpenEMR
7 * @link https://www.open-emr.org
8 * @author Jacob T.Paul <jacob@zhservices.com>
9 * @author Basil PT <basil@zhservices.com>
10 * @copyright Copyright (c) 2013 Z&H Consultancy Services Private Limited <sam@zhservices.com>
11 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
14 namespace Acl\Controller;
16 use Laminas\Mvc\Controller\AbstractActionController;
17 use Laminas\View\Model\ViewModel;
18 use Application\Listener\Listener;
20 class AclController extends AbstractActionController
22 /**
23 * @var \Acl\Model\AclTable
25 protected $aclTable;
27 protected $listenerObject;
28 private $htmlEscaper;
30 public function __construct(\Laminas\View\Helper\HelperInterface $htmlEscaper, \Acl\Model\AclTable $aclTable)
32 $this->htmlEscaper = $htmlEscaper;
33 // TODO: we should probably inject the Listener object as well so we can mock it in unit tests or at least make the dependency explicit.
34 $this->listenerObject = new Listener();
35 $this->aclTable = $aclTable;
38 public function indexAction()
40 $module_id = $this->params()->fromQuery('module_id');
41 $result = $this->getAclTable()->aclSections($module_id);
43 $arrayCategories = array();
44 foreach ($result as $row) {
45 $arrayCategories[$row['section_id']] = array("parent_id" => $row['parent_section'], "name" =>
46 $row['section_name'],"id" => $row['section_id']);
49 ob_start();
50 $this->createTreeView($arrayCategories, 0);
51 $sections = ob_get_clean();
53 $user_group_main = $this->createUserGroups("user_group_", "", "draggable2");
54 $user_group_allowed = $this->createUserGroups("user_group_allowed_", "display:none;", "draggable3", "class='class_li'");
55 $user_group_denied = $this->createUserGroups("user_group_denied_", "display:none;", "draggable4", "class='class_li'");
57 $result = $this->getAclTable()->getActiveModules();
58 foreach ($result as $row) {
59 $array_active_modules[$row['mod_id']] = $row['mod_name'];
62 $index = new ViewModel(array(
63 'user_group_main' => $user_group_main,
64 'user_group_allowed' => $user_group_allowed,
65 'user_group_denied' => $user_group_denied,
66 'sections' => $sections,
67 'component_id' => "0-" . $module_id,
68 'module_id' => $module_id,
69 'listenerObject' => $this->listenerObject,
70 'active_modules' => $array_active_modules,
71 ));
72 return $index;
75 public function acltabAction()
77 $module_id = $this->params()->fromQuery('module_id');
78 $this->layout('layout/layout_tabs');
79 $index = new ViewModel(array(
80 'mod_id' => $module_id,
81 ));
82 return $index;
85 public function aclAction()
87 $module_id = $this->params()->fromQuery('module_id');
88 $data = $this->getAclTable()->getGroups();
90 $user_groups = array();
91 foreach ($data as $row) {
92 $user_groups[$row['id']] = $row['name'];
95 $data = $this->getAclTable()->aclSections($module_id);
96 $module_data = array();
97 $module_data['module_components'] = array();
98 foreach ($data as $row) {
99 if ($row['parent_section'] == 0) {
100 $module_data['module_name'] = array(
101 'id' => $row['section_id'],
102 'name' => $row['section_name']
104 } else {
105 $module_data['module_components'][$row['section_id']] = $row['section_name'];
109 $data = $this->getAclTable()->getGroupAcl($module_id);
110 $saved_ACL = array();
111 foreach ($data as $row) {
112 if (!$saved_ACL[$row['section_id']]) {
113 $saved_ACL[$row['section_id']] = array();
116 array_push($saved_ACL[$row['section_id']], $row['group_id']);
119 $acl_view = new ViewModel(
120 array(
121 'user_groups' => $user_groups,
122 'listenerObject' => $this->listenerObject,
123 'module_data' => $module_data,
124 'module_id' => $module_id,
125 'acl_data' => $saved_ACL
128 return $acl_view;
131 public function ajaxAction()
133 $ajax_mode = $this->getRequest()->getPost('ajax_mode', null);
134 if ($ajax_mode == "save_acl") {
135 $selected_componet = $this->getRequest()->getPost('selected_module', null);
136 $selected_componet_arr = explode("-", $selected_componet);
137 if ($selected_componet_arr[0] == 0) {
138 $selected_componet_arr[0] = $selected_componet_arr[1];
141 $allowed_users = json_decode($this->getRequest()->getPost('allowed_users', null));
142 $denied_users = json_decode($this->getRequest()->getPost('denied_users', null));
144 $allowed_users = array_unique($allowed_users);
145 $denied_users = array_unique($denied_users);
147 // Delete Saved ACL Data
148 $data = $this->getAclTable()->deleteGroupACL($selected_componet_arr[0], $selected_componet_arr[1]);
149 $data = $this->getAclTable()->deleteUserACL($selected_componet_arr[0], $selected_componet_arr[1]);
151 // Allowed
152 foreach ($allowed_users as $allowed_user) {
153 $id = str_replace("li_user_group_allowed_", "", $allowed_user);
154 $arr_id = explode("-", $id);
156 if ($arr_id[1] == 0) {
157 $data = $this->getAclTable()->insertGroupACL($selected_componet_arr[0], $arr_id[0], $selected_componet_arr[1], 1);
158 } else {
159 $data = $this->getAclTable()->insertUserACL($selected_componet_arr[0], $arr_id[1], $selected_componet_arr[1], 1);
163 // Denied
164 foreach ($denied_users as $denied_user) {
165 $id = str_replace("li_user_group_denied_", "", $denied_user);
166 $arr_id = explode("-", $id);
168 if ($arr_id[1] == 0) {
169 $data = $this->getAclTable()->insertGroupACL($selected_componet_arr[0], $arr_id[0], $selected_componet_arr[1], 0);
170 } else {
171 $data = $this->getAclTable()->insertuserACL($selected_componet_arr[0], $arr_id[1], $selected_componet_arr[1], 0);
174 } elseif ($ajax_mode == "rebuild") {
175 $selected_componet = $_REQUEST['selected_module'];
176 $selected_componet_arr = explode("-", $selected_componet);
177 if ($selected_componet_arr[0] == 0) {
178 $selected_componet_arr[0] = $selected_componet_arr[1];
181 $array_users_allowed = array();
182 $array_users_denied = array();
183 $array_groups_allowed = array();
184 $array_groups_denied = array();
186 $res_users = $this->getAclTable()->getAclDataUsers($selected_componet_arr[1]);
187 foreach ($res_users as $row) {
188 if ($row['allowed'] == 1) {
189 if (!$array_users_allowed[$row['group_id']]) {
190 $array_users_allowed[$row['group_id']] = array();
193 array_push($array_users_allowed[$row['group_id']], $row['user_id']);
194 } else {
195 if (!$array_users_denied[$row['group_id']]) {
196 $array_users_denied[$row['group_id']] = array();
199 array_push($array_users_denied[$row['group_id']], $row['user_id']);
203 $res_group = $this->getAclTable()->getAclDataGroups($selected_componet_arr[1]);
204 foreach ($res_group as $row) {
205 if ($row['allowed'] == 1) {
206 array_push($array_groups_allowed, $row['group_id']);
207 } else {
208 array_push($array_groups_denied, $row['group_id']);
212 $arr_return = array();
213 $arr_return['group_allowed'] = $array_groups_allowed;
214 $arr_return['group_denied'] = $array_groups_denied;
215 $arr_return['user_allowed'] = $array_users_allowed;
216 $arr_return['user_denied'] = $array_users_denied;
217 echo json_encode($arr_return);
218 } elseif ($ajax_mode == "save_acl_advanced") {
219 $ACL_DATA = json_decode($this->getRequest()->getPost('acl_data', null), true);
220 $module_id = $this->getRequest()->getPost('module_id', null);
221 $this->getAclTable()->deleteModuleGroupACL($module_id);
223 foreach ($ACL_DATA['allowed'] as $section_id => $sections) {
224 foreach ($sections as $group_id) {
225 $this->getAclTable()->deleteUserACL($module_id, $section_id);
226 $this->getAclTable()->insertGroupACL($module_id, $group_id, $section_id, 1);
230 foreach ($ACL_DATA['denied'] as $section_id => $sections) {
231 foreach ($sections as $group_id) {
232 $this->getAclTable()->deleteUserACL($module_id, $section_id);
233 $this->getAclTable()->insertGroupACL($module_id, $group_id, $section_id, 0);
236 } elseif ($ajax_mode == "get_sections_by_module") {
237 $module_id = $this->getRequest()->getPost('module_id', null);
238 $result = $this->getAclTable()->getModuleSections($module_id);
240 $array_sections = array();
241 foreach ($result as $row) {
242 $array_sections[$row['section_id']] = $row['section_name'];
245 echo json_encode($array_sections);
246 } elseif ($ajax_mode == "save_sections_by_module") {
247 $module_id = $this->getRequest()->getPost('mod_id', null);
248 $parent_id = $this->getRequest()->getPost('parent_id', null);
249 $section_identifier = $this->getRequest()->getPost('section_identifier', null);
250 $section_name = $this->getRequest()->getPost('section_name', null);
252 if (!$parent_id) {
253 $parent_id = $module_id;
256 $current_section_id = $this->getAclTable()->getSectionsInsertId();
257 $this->getAclTable()->saveACLSections($module_id, $parent_id, $section_identifier, $section_name, $current_section_id);
260 exit();
266 * Function to Print Componets Tree Structure
267 * @param String $currentParent Root Node of Tree
268 * @param String $currLevel Current Depth of Tree
269 * @param String $prevLevel Prev Depth of Tree
272 private function createTreeView($array, $currentParent, $currLevel = 0, $prevLevel = -1)
274 /** Html Escape Function */
275 $escapeHtml = $this->htmlEscaper;
277 foreach ($array as $categoryId => $category) {
278 if ($category['name'] == '') {
279 continue;
282 if ($currentParent == $category['parent_id']) {
283 if ($currLevel > $prevLevel) {
284 echo " <ul> ";
287 if ($currLevel == $prevLevel) {
288 echo " </li> ";
291 $class = "";
292 echo '<li id="' . $category['parent_id'] . "-" . $category['id'] . '" value="' . $escapeHtml($category['name']) . '" ' . $escapeHtml($class) . ' ><div onclick="selectThis(\'' . $escapeHtml($category['parent_id']) . '-' . $escapeHtml($category['id']) . '\');rebuild();" class="list">' . $escapeHtml($category['name']) . "</div>";
293 if ($currLevel > $prevLevel) {
294 $prevLevel = $currLevel;
297 $currLevel++;
298 $this->createTreeView($array, $categoryId, $currLevel, $prevLevel);
299 $currLevel--;
303 if ($currLevel == $prevLevel) {
304 echo "</li></ul> ";
310 * Function to Print User group Tree Structure
311 * @param String $id String to Prepend with <li> Id
312 * @param String $visibility <li> Visibility
313 * @param String $dragabble Class to Make <li> Title Draggable
314 * @param String $li_class <li> Class Name
317 private function createUserGroups($id = "user_group_", $visibility = "", $dragabble = "draggable", $li_class = "")
319 /** Html Escape Function */
320 $escapeHtml = $this->htmlEscaper;
322 $output_string = "";
323 $res_users = $this->getAclTable()->aclUserGroupMapping();
325 $tempList = array();
326 foreach ($res_users as $row) {
327 $tempList[$row['group_id']]['group_name'] = $row['group_name'];
328 $tempList[$row['group_id']]['group_id'] = $row['group_id'];
329 $tempList[$row['group_id']]['items'][] = $row;
332 $output_string .= '<ul>';
333 foreach ($tempList as $groupID => $tempListRow) {
334 $output_string .= '<li ' . $li_class . ' id="li_' . $id . $tempListRow['group_id'] . '-0" style="' . $visibility . '"><div class="' . $escapeHtml($dragabble) . '" id="' . $id . $tempListRow['group_id'] . '-0" >' . $escapeHtml($tempListRow['group_name']) . '</div>';
335 if (!empty($tempListRow['items'])) {
336 $output_string .= '<ul>';
337 foreach ($tempListRow['items'] as $key => $itemRow) {
338 $output_string .= '<li ' . $li_class . ' id="li_' . $id . $itemRow['group_id'] . '-' . $itemRow['user_id'] . '" style="' . $visibility . '"><div class="' . $escapeHtml($dragabble) . '" id="' . $id . $itemRow['group_id'] . '-' . $itemRow['user_id'] . '">' . $escapeHtml($itemRow['display_name']) . '</div></li>';
341 $output_string .= '</ul>';
344 $output_string .= '</li>';
347 $output_string .= '</ul>';
348 return $output_string;
352 * Table Gateway
354 * @return \Acl\Model\AclTable
356 public function getAclTable()
358 return $this->aclTable;