2 // php-GACL access controls are included in OpenEMR. The below
3 // function will automatically create the path where gacl.class.php
4 // can be found. Note that this path can be manually set below
5 // for users who are using an external version of php-GACL.
6 // Also note that php-GACL access controls can be turned off
9 $phpgacl_location = dirname(__FILE__).'/../gacl';
12 // If using an external version of phpGACL, then uncomment the following
13 // line and manually place the path below. IN THIS CASE YOU MUST ALSO
14 // COMMENT OUT ABOVE $phpgacl_location ASSIGNMENT ABOVE, OR BACKUPS WILL
15 // NOT RESTORE PROPERLY!
17 //$phpgacl_location = "/var/www/gacl";
19 // If you want to turn off php-GACL, then uncomment the following line.
20 // IN THIS CASE YOU MUST ALSO COMMENT OUT ABOVE $phpgacl_location ASSIGNMENT(S)
21 // ABOVE, OR BACKUPS WILL NOT RESTORE PROPERLY!
23 //unset($phpgacl_location);
26 // The following Access Control Objects (ACO) are currently supported.
27 // These are the "things to be protected":
29 // Section "admin" (Administration):
30 // super Superuser - can delete patients, encounters, issues
31 // calendar Calendar Settings
32 // database Database Reporting
33 // forms Forms Administration
34 // practice Practice Settings
35 // superbill Superbill Codes Administration
36 // users Users/Groups/Logs Administration
37 // batchcom Batch Communication Tool
38 // language Language Interface Tool
39 // drugs Pharmacy Dispensary
40 // acl ACL Administration
42 // Section "acct" (Accounting):
43 // bill Billing (write optional)
44 // disc Allowed to discount prices (in Fee Sheet or Checkout form)
46 // rep Financial Reporting - my encounters
47 // rep_a Financial Reporting - anything
49 // Section "patients" (Patient Information):
50 // appt Appointments (write,wsome optional)
51 // demo Demographics (write,addonly optional)
52 // med Medical Records and History (write,addonly optional)
53 // trans Transactions, e.g. referrals (write optional)
54 // docs Documents (write,addonly optional)
55 // notes Patient Notes (write,addonly optional)
56 // sign Sign Lab Results (write,addonly optional)
58 // Section "encounters" (Encounter Information):
59 // auth Authorize - my encounters
60 // auth_a Authorize - any encounters
61 // coding Coding - my encounters (write,wsome optional)
62 // coding_a Coding - any encounters (write,wsome optional)
63 // notes Notes - my encounters (write,addonly optional)
64 // notes_a Notes - any encounters (write,addonly optional)
65 // date_a Fix encounter dates - any encounters
66 // relaxed Less-private information (write,addonly optional)
67 // (e.g. the Sports Fitness encounter form)
69 // Section "squads" applies to sports team use only:
70 // acos in this section define the user-specified list of squads
72 // Section "sensitivities" (Sensitivities):
76 // Section "lists" (Lists):
77 // default Default List (write,addonly optional)
78 // state State List (write,addonly optional)
79 // country Country List (write,addonly optional)
80 // language Language List (write,addonly optional)
81 // ethrace Ethnicity-Race List (write,addonly optional)
83 // Section "placeholder" (Placeholder):
84 // filler Placeholder (Maintains empty ACLs)
86 // Section "nationnotes" (Nation Notes):
87 // nn_configure Nation Notes
89 // Section "patientportal" (Patient Portal):
90 // portal Patient Portal
92 if (isset ($phpgacl_location)) {
93 include_once("$phpgacl_location/gacl.class.php");
94 $gacl_object = new gacl();
95 //DO NOT CHANGE BELOW VARIABLE
96 $section_aro_value = 'users';
99 // acl_check should return 0 if access is denied. Otherwise it may
100 // return anything that evaluates to true. In addition if any of the
101 // following types of access are applicable, then the corresponding value
102 // must be returned if and only if such access is granted (ony one may
105 // * write - the user may add or modify the ACO
106 // * wsome - the user has limited add/modify access to the ACO
107 // * addonly - the user may view and add but not modify entries
109 function acl_check($section, $value, $user = '') {
110 global $gacl_object, $phpgacl_location, $section_aro_value;
111 if (! $user) $user = $_SESSION['authUser'];
113 if ($phpgacl_location) {
114 return $gacl_object->acl_return_value($section, $value, $section_aro_value, $user);
117 // If no phpgacl, then apply the old static rules whereby "authorized"
118 // users (providers) can do anything, and other users can do most things.
119 // If you want custom access control but don't want to mess with phpGACL,
120 // then you could customize the code below instead.
122 if ($user == 'admin') return 'write';
123 if ($section == 'admin' && $value == 'super') return 0;
124 if ($_SESSION['userauthorized']) return 'write';
126 if ($section == 'patients') {
127 if ($value == 'med') return 1;
130 else if ($section == 'encounters') {
131 if (strpos($value, 'coding' ) === 0) return 'write';
132 if (strpos($value, 'notes' ) === 0) return 'write';
133 if ($value == 'relaxed') return 'write';
135 else if ($section != 'admin') {
142 // Get the ACO name/value pairs for a designated section. Each value
143 // is an array (section_value, value, order_value, name, hidden).
145 function acl_get_section_acos($section) {
146 global $phpgacl_location;
147 if ($phpgacl_location) {
148 include_once("$phpgacl_location/gacl_api.class.php");
149 $gacl = new gacl_api();
150 $arr1 = $gacl->get_objects($section, 1, 'ACO');
152 if (!empty($arr1[$section])) {
153 foreach ($arr1[$section] as $value) {
154 $odata = $gacl->get_object_data($gacl->get_object_id($section, $value, 'ACO'), 'ACO');
155 $arr[$value] = $odata[0];
163 // Return an array keyed on squad ACO names.
164 // This is only applicable for sports team use.
166 function acl_get_squads() {
167 return acl_get_section_acos('squads');
170 // Return an array keyed on encounter sensitivity level ACO names.
171 // Sensitivities are useful when some encounter notes are not
172 // medically sensitive (e.g. a physical fitness test), and/or if
173 // some will be "for doctor's eyes only" (e.g. STD treatment).
175 // When a non-blank sensitivity value exists in the new encounter
176 // form, it names an additional ACO required for access to all forms
177 // in the encounter. If you want some encounters to be non-sensitive,
178 // then you also need some default nonblank sensitivity for normal
179 // encounters, as well as greater encounter notes permissions for
180 // those allowed to view non-sensitive encounters.
182 function acl_get_sensitivities() {
183 return acl_get_section_acos('sensitivities');
187 // Returns true if aco exist
188 // Returns false if aco doesn't exist
189 // $section_name = name of section (string)
190 // $aco_name = name of aco (string)
192 function aco_exist($section_name, $aco_name) {
193 global $phpgacl_location;
194 if (isset ($phpgacl_location)) {
195 include_once("$phpgacl_location/gacl_api.class.php");
196 $gacl = new gacl_api();
197 $aco_id = $gacl->get_object_id($section_name, $aco_name, 'ACO');
206 // Returns a sorted array of all available Group Titles.
208 function acl_get_group_title_list() {
209 global $phpgacl_location;
210 if (isset ($phpgacl_location)) {
211 include_once("$phpgacl_location/gacl_api.class.php");
212 $gacl = new gacl_api();
213 $parent_id = $gacl->get_root_group_id();
214 $arr_group_ids = $gacl->get_group_children($parent_id, 'ARO', 'RECURSE');
215 $arr_group_titles = array();
216 foreach ($arr_group_ids as $value) {
217 $arr_group_data = $gacl->get_group_data($value, 'ARO');
218 $arr_group_titles[$value] = $arr_group_data[3];
220 sort($arr_group_titles);
221 return $arr_group_titles;
227 // Returns a sorted array of group Titles that a user belongs to.
228 // Returns 0 if does not belong to any group yet.
229 // $user_name = Username, which is login name.
231 function acl_get_group_titles($user_name) {
232 global $phpgacl_location, $section_aro_value;
233 if (isset ($phpgacl_location)) {
234 include_once("$phpgacl_location/gacl_api.class.php");
235 $gacl = new gacl_api();
236 $user_aro_id = $gacl->get_object_id($section_aro_value, $user_name, 'ARO');
238 $arr_group_id = $gacl->get_object_groups($user_aro_id, 'ARO', 'NO_RECURSE');
240 foreach ($arr_group_id as $key => $value) {
241 $arr_group_data = $gacl->get_group_data($value, 'ARO');
242 $arr_group_titles[$key] = $arr_group_data[3];
244 sort($arr_group_titles);
245 return $arr_group_titles;
253 // This will place the user aro object into selected group(s)
254 // It uses the set_user_aro() function
255 // $username = username (string)
256 // $group = title of group(s) (string or array)
258 function add_user_aros($username, $group) {
259 $current_user_groups = acl_get_group_titles($username);
260 if (!$current_user_groups) {
261 $current_user_groups = array();
263 if (is_array($group)){
264 foreach ($group as $value) {
265 if (!in_array($value, $current_user_groups)) {
266 array_push($current_user_groups, $value);
271 if (!in_array($group, $current_user_groups)) {
272 array_push($current_user_groups, $group);
275 $user_data = mysql_fetch_array(sqlStatement("select * from users where username='" .
277 set_user_aro($current_user_groups, $username, $user_data["fname"],
278 $user_data["mname"], $user_data["lname"]);
283 // This will remove the user aro object from the selected group(s)
284 // It uses the set_user_aro() function
285 // $username = username (string)
286 // $group = title of group(s) (string or array)
288 function remove_user_aros($username, $group) {
289 $current_user_groups = acl_get_group_titles($username);
290 $new_user_groups = array();
291 if (is_array($group)){
292 foreach ($current_user_groups as $value) {
293 if (!in_array($value, $group)) {
294 array_push($new_user_groups, $value);
299 foreach ($current_user_groups as $value) {
300 if ($value != $group) {
301 array_push($new_user_groups, $value);
305 $user_data = mysql_fetch_array(sqlStatement("select * from users where username='" .
307 set_user_aro($new_user_groups, $username, $user_data["fname"],
308 $user_data["mname"], $user_data["lname"]);
313 // This will either create or edit a user aro object, and then place it
314 // in the requested groups. It will not allow removal of the 'admin'
315 // user or gacl_protected users from the 'admin' group.
316 // $arr_group_titles = titles of the groups that user will be added to.
317 // $user_name = username, which is login name.
318 // $first_name = first name
319 // $middle_name = middle name
320 // $last_name = last name
322 function set_user_aro($arr_group_titles, $user_name, $first_name, $middle_name, $last_name) {
323 global $phpgacl_location, $section_aro_value;
325 if (isset ($phpgacl_location)) {
326 include_once("$phpgacl_location/gacl_api.class.php");
327 $gacl = new gacl_api();
329 //see if this user is gacl protected (ie. do not allow
330 //removal from the Administrators group)
331 require_once(dirname(__FILE__).'/user.inc');
332 require_once(dirname(__FILE__).'/calendar.inc');
333 $userNametoID = getIDfromUser($user_name);
334 if (checkUserSetting("gacl_protect","1",$userNametoID) || $user_name == "admin") {
335 $gacl_protect = true;
338 $gacl_protect = false;
341 //get array of all available group ID numbers
342 $parent_id = $gacl->get_root_group_id();
343 $arr_all_group_ids = $gacl->get_group_children($parent_id, 'ARO', 'RECURSE');
345 //Cycle through ID array to find and process each selected group
346 //Create a counter since processing of first hit is unique
348 foreach ($arr_all_group_ids as $value) {
349 $arr_group_data = $gacl->get_group_data($value, 'ARO');
350 if ((empty($arr_group_titles)) ||
351 (in_array($arr_group_data[3], $arr_group_titles))) {
352 //We have a hit, so need to add group and increment counter
353 // because processing of first hit is unique
354 //This will also deal with an empty $arr_group_titles array
355 // removing user from all groups unless 'admin'
356 $counter = $counter + 1;
357 //create user full name field
359 $full_name = $first_name . " " . $middle_name . " " . $last_name;
363 $full_name = $first_name . " " . $last_name;
366 $full_name = $first_name;
370 //If this is not the first group to be added, then will skip below
371 // and will be added. If this is the first group, then need to
372 // go thru several steps before adding the group.
374 //get ID of user ARO object, if it exist
375 $user_aro_id = $gacl->get_object_id($section_aro_value, $user_name, 'ARO');
377 //user ARO object already exist, so will edit it
378 $gacl->edit_object($user_aro_id, $section_aro_value, $full_name, $user_name, 10, 0, 'ARO');
380 //remove all current user ARO object group associations
381 $arr_remove_group_ids = $gacl->get_object_groups($user_aro_id, 'ARO', 'NO_RECURSE');
382 foreach ($arr_remove_group_ids as $value2) {
383 $gacl->del_group_object($value2, $section_aro_value, $user_name, 'ARO');
387 //user ARO object does not exist, so will create it
388 $gacl->add_object($section_aro_value, $full_name, $user_name, 10, 0, 'ARO');
392 //place the user ARO object in the selected group (if group(s) is selected)
393 if (!empty($arr_group_titles)) {
394 $gacl->add_group_object($value, $section_aro_value, $user_name, 'ARO');
398 //Below will not allow 'admin' or gacl_protected user to be removed from 'admin' group
402 $admin_id = $gacl->get_object_id($section_aro_value, $user_name, 'ARO');
403 $arr_admin = $gacl->get_object_groups($admin_id, 'ARO', 'NO_RECURSE');
404 foreach ($arr_admin as $value3) {
405 $arr_admin_data = $gacl->get_group_data($value3, 'ARO');
406 if (strcmp($arr_admin_data[2], 'admin') == 0) {
410 if (!$boolean_admin) {
411 foreach ($arr_all_group_ids as $value4) {
412 $arr_temp = $gacl->get_group_data($value4, 'ARO');
413 if ($arr_temp[2] == 'admin') {
414 $gacl->add_group_object($value4, $section_aro_value, $user_name, 'ARO');
420 //if array of groups was empty, then we are done, and can break from loop
421 if (empty($arr_group_titles)) break;
429 // Returns true if acl exist
430 // Returns false if acl doesn't exist
431 // EITHER $title or $name is required(send FALSE in variable
432 // not being used). If both are sent, then only $title will be
434 // $return_value is required
435 // $title = title of acl (string)
436 // $name = name of acl (string)
437 // $return_value = return value of acl (string)
439 function acl_exist($title, $name, $return_value) {
440 global $phpgacl_location;
441 if (isset ($phpgacl_location)) {
442 include_once("$phpgacl_location/gacl_api.class.php");
443 $gacl = new gacl_api();
445 $acl = $gacl->search_acl(FALSE, FALSE, FALSE, FALSE, $title, FALSE, FALSE, FALSE, $return_value);
448 $group_id = $gacl->get_group_id($name, NULL, 'ARO');
450 $group_data = $gacl->get_group_data($group_id, 'ARO');
451 $acl = $gacl->search_acl(FALSE, FALSE, FALSE, FALSE, $group_data[3], FALSE, FALSE, FALSE, $return_value);
458 $acl = $gacl->search_acl(FALSE, FALSE, FALSE, FALSE, $title, FALSE, FALSE, FALSE, $return_value);
470 // This will add a new acl and group(if group doesn't yet exist)
471 // with one aco in it.
472 // $acl_title = title of acl (string)
473 // $acl_name = name of acl (string)
474 // $return_value = return value of acl (string)
475 // $note = description of acl (array)
477 function acl_add($acl_title, $acl_name, $return_value, $note) {
478 global $phpgacl_location;
479 if (isset ($phpgacl_location)) {
480 include_once("$phpgacl_location/gacl_api.class.php");
481 $gacl = new gacl_api();
482 $group_id = $gacl->get_group_id($acl_name, $acl_title, 'ARO');
484 //group already exist, so just create acl
485 $gacl->add_acl(array("placeholder"=>array("filler")),
486 NULL, array($group_id), NULL, NULL, 1, 1, $return_value, $note);
489 //create group, then create acl
490 $parent_id = $gacl->get_root_group_id();
491 $aro_id = $gacl->add_group($acl_name, $acl_title, $parent_id, 'ARO');
492 $gacl->add_acl(array("placeholder"=>array("filler")),
493 NULL, array($aro_id), NULL, NULL, 1, 1, $return_value, $note);
501 // This will remove acl. It will also remove group(if the group
502 // is no longer associated with any acl's).
503 // $acl_title = title of acl (string)
504 // $acl_name = name of acl (string)
505 // $return_value = return value of acl (string)
506 // $note = description of acl (array)
508 function acl_remove($acl_title, $return_value) {
509 global $phpgacl_location;
510 if (isset ($phpgacl_location)) {
511 include_once("$phpgacl_location/gacl_api.class.php");
512 $gacl = new gacl_api();
513 //First, delete the acl
514 $acl_id=$gacl->search_acl(FALSE, FALSE, FALSE, FALSE, $acl_title, FALSE, FALSE, FALSE, $return_value);
515 $gacl->del_acl($acl_id[0]);
516 //Then, remove the group(if no more acl's are remaining)
517 $acl_search=$gacl->search_acl(FALSE, FALSE, FALSE, FALSE, $acl_title, FALSE, FALSE, FALSE, FALSE);
518 if (empty($acl_search)){
519 $group_id=$gacl-> get_group_id(NULL, $acl_title, 'ARO');
520 $gacl->del_group($group_id, TRUE, 'ARO');
528 // This will place the aco(s) into the selected acl
529 // $acl_title = title of acl (string)
530 // $return_value = return value of acl (string)
531 // $aco_id = id of aco (array)
533 function acl_add_acos($acl_title, $return_value, $aco_id) {
534 global $phpgacl_location;
535 if (isset ($phpgacl_location)) {
536 include_once("$phpgacl_location/gacl_api.class.php");
537 $gacl = new gacl_api();
538 $acl_id = $gacl->search_acl(FALSE, FALSE, FALSE, FALSE, $acl_title, FALSE, FALSE, FALSE, $return_value);
539 foreach ($aco_id as $value) {
540 $aco_data = $gacl->get_object_data($value, 'ACO');
541 $aco_section = $aco_data[0][0];
542 $aco_name = $aco_data[0][1];
543 $gacl->append_acl($acl_id[0], NULL, NULL, NULL, NULL, array($aco_section=>array($aco_name)));
551 // This will remove the aco(s) from the selected acl
552 // Note if all aco's are removed, then will place the filler-placeholder
553 // into the acl to avoid complete removal of the acl.
554 // $acl_title = title of acl (string)
555 // $return_value = return value of acl (string)
556 // $aco_id = id of aco (array)
558 function acl_remove_acos($acl_title, $return_value, $aco_id) {
559 global $phpgacl_location;
560 if (isset ($phpgacl_location)) {
561 include_once("$phpgacl_location/gacl_api.class.php");
562 $gacl = new gacl_api();
563 $acl_id = $gacl->search_acl(FALSE, FALSE, FALSE, FALSE, $acl_title, FALSE, FALSE, FALSE, $return_value);
565 // Check to see if removing all acos. If removing all acos then will
566 // ensure the filler-placeholder aco in acl to avoid complete
567 // removal of the acl.
568 if (count($aco_id) == acl_count_acos($acl_title, $return_value)) {
569 //1-get the filler-placeholder aco id
570 $filler_aco_id = $gacl->get_object_id('placeholder','filler','ACO');
571 //2-add filler-placeholder aco
572 acl_add_acos($acl_title, $return_value, array($filler_aco_id));
573 //3-ensure filler-placeholder aco is not to be deleted
574 $safeListaco = remove_element($_POST["selection"],$filler_aco_id);
575 //4-prepare to safely delete the acos
576 $aco_id = $safeListaco;
579 foreach ($aco_id as $value) {
580 $aco_data = $gacl->get_object_data($value, 'ACO');
581 $aco_section = $aco_data[0][0];
582 $aco_name = $aco_data[0][1];
583 $gacl->shift_acl($acl_id[0], NULL, NULL, NULL, NULL, array($aco_section=>array($aco_name)));
591 // This will return the number of aco objects
592 // in a specified acl.
593 // $acl_title = title of acl (string)
594 // $return_value = return value of acl (string)
596 function acl_count_acos($acl_title, $return_value) {
597 global $phpgacl_location;
598 if (isset ($phpgacl_location)) {
599 include_once("$phpgacl_location/gacl_api.class.php");
600 $gacl = new gacl_api();
601 $acl_id = $gacl->search_acl(FALSE, FALSE, FALSE, FALSE, $acl_title, FALSE, FALSE, FALSE, $return_value);
602 $acl_data = $gacl->get_acl($acl_id[0]);
604 foreach ($acl_data['aco'] as $key => $value) {
605 $aco_count = $aco_count + count($acl_data['aco'][$key]);
613 // Function to remove an element from an array
615 function remove_element($arr, $val){
617 foreach ($arr as $value){
618 if ($value != $val) {
619 array_push($arr2,$value);