Standardized the diagnosis code searching in the Rules admin GUI
[openemr.git] / library / acl.inc
blobecd0ad9ef437b88ee8628af86f6b7e8dcf61e6e5
1 <?php
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
7 // below.
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":
28   //
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
41   //
42   // Section "acct" (Accounting):
43   //   bill        Billing (write optional)
44   //   disc        Allowed to discount prices (in Fee Sheet or Checkout form)
45   //   eob         EOB Data Entry
46   //   rep         Financial Reporting - my encounters
47   //   rep_a       Financial Reporting - anything
48   //
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)
57   //
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)
68   //
69   // Section "squads" applies to sports team use only:
70   //   acos in this section define the user-specified list of squads
71   //
72   // Section "sensitivities" (Sensitivities):
73   //   normal     Normal
74   //   high       High
75   //
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)
82   //
83   // Section "placeholder" (Placeholder):
84   //   filler     Placeholder (Maintains empty ACLs)
85   //
86   // Section "nationnotes" (Nation Notes):
87   //   nn_configure     Nation Notes
88   //
89   // Section "patientportal" (Patient Portal):
90   //   portal     Patient Portal
91   
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';
97   }
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
103   // be specified):
104   //
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
108   //
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);
115     }
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;
128       return 'write';
129     }
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';
134     }
135     else if ($section != 'admin') {
136       return 'write';
137     }
139     return 0;
140   }
142   // Get the ACO name/value pairs for a designated section.  Each value
143   // is an array (section_value, value, order_value, name, hidden).
144   //
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');
151       $arr = array();
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];
156         }
157       }
158       return $arr;
159     }
160     return 0;
161   }
163   // Return an array keyed on squad ACO names.
164   // This is only applicable for sports team use.
165   //
166   function acl_get_squads() {
167     return acl_get_section_acos('squads');
168   }
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).
174   //
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.
181   //
182   function acl_get_sensitivities() {
183     return acl_get_section_acos('sensitivities');
184   }
186   //
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)
191   //
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');
198     if ($aco_id) {
199      return true;
200     }
201    }
202    return false;
203   }
205   //
206   // Returns a sorted array of all available Group Titles.
207   //
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];
219       }
220       sort($arr_group_titles);
221       return $arr_group_titles;
222     }
223     return 0;
224   }
226   //
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.
230   //
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');
237       if ($user_aro_id) {
238         $arr_group_id = $gacl->get_object_groups($user_aro_id, 'ARO', 'NO_RECURSE');
239         if ($arr_group_id) {
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];
243           }
244         sort($arr_group_titles);
245         return $arr_group_titles;
246         }
247       }
248     }
249     return 0;
250   }
252   //
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)
257   //
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();
262    }
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);
267        }
268     }
269    }
270    else {
271     if (!in_array($group, $current_user_groups)) {
272      array_push($current_user_groups, $group);
273     }
274    }
275    $user_data = mysql_fetch_array(sqlStatement("select * from users where username='" .
276     $username . "'"));
277    set_user_aro($current_user_groups, $username, $user_data["fname"],
278     $user_data["mname"], $user_data["lname"]);
279    return;
280   }
282   //
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)
287   //
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);
295      }
296     }
297    }
298    else {
299     foreach ($current_user_groups as $value) {
300      if ($value != $group) {
301       array_push($new_user_groups, $value);
302      }
303     }
304    }
305    $user_data = mysql_fetch_array(sqlStatement("select * from users where username='" .
306     $username . "'"));
307    set_user_aro($new_user_groups, $username, $user_data["fname"],
308     $user_data["mname"], $user_data["lname"]);
309    return;
310   }
312   //
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
321   //
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;
336       }
337       else {
338         $gacl_protect = false;
339       }
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
347       $counter = 0;
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
358           if ($middle_name) {
359             $full_name = $first_name . " " . $middle_name . " " .  $last_name;
360           }
361           else {
362             if ($last_name) {
363               $full_name = $first_name . " " . $last_name;
364             }
365             else {
366               $full_name = $first_name;
367             }
368           }
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.
373           if ($counter == 1) {
374             //get ID of user ARO object, if it exist
375             $user_aro_id = $gacl->get_object_id($section_aro_value, $user_name, 'ARO');
376             if ($user_aro_id) {
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');
384               }
385             }
386             else {
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');
389             }
390           }
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');
395           }
397           //
398           //Below will not allow 'admin' or gacl_protected user to be removed from 'admin' group
399           //
400           if ($gacl_protect) {
401             $boolean_admin=0;
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) {
407                 $boolean_admin=1;
408               }
409             }
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');
415                 }
416               }
417             }
418           }
419         }
420         //if array of groups was empty, then we are done, and can break from loop
421         if (empty($arr_group_titles)) break;
422       }
423       return true;
424     }
425    return false;
426   }
428   //
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
433   //  used.
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)
438   //
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();
444     if (!$name) {
445      $acl = $gacl->search_acl(FALSE, FALSE, FALSE, FALSE, $title, FALSE, FALSE, FALSE, $return_value);
446     }
447     else if (!$title) {
448      $group_id = $gacl->get_group_id($name, NULL, 'ARO');
449      if ($group_id) {
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);
452      }
453      else {
454      return false;
455      }
456     }
457     else {
458      $acl = $gacl->search_acl(FALSE, FALSE, FALSE, FALSE, $title, FALSE, FALSE, FALSE, $return_value);
459     }
460     if (!empty($acl)) {
461      return true;
462     }
463     else {
464      return false;
465     }
466    }
467   }
469   //
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)
476   //
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');
483     if ($group_id) {
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);
487     }
488     else {
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);
494     }
495     return;
496    }
497    return 0;
498   }
500   //
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)
507   //
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');
521     }
522     return;
523    }
524    return 0;
525   }
527   //
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)
532   //
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)));
544     }
545     return;
546    }
547    return 0;
548   }
550   //
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)
557   //
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;
577     }
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)));
584      }
585     return;
586    }
587    return 0;
588   }
590   //
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)
595   //
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]);
603     $aco_count = 0;
604     foreach ($acl_data['aco'] as $key => $value) {
605      $aco_count = $aco_count + count($acl_data['aco'][$key]);
606     }
607     return $aco_count;
608    }
609    return 0;
610   }
612   //
613   // Function to remove an element from an array
614   //
615   function remove_element($arr, $val){
616    $arr2 = array();
617    foreach ($arr as $value){
618     if ($value != $val) {
619      array_push($arr2,$value);
620     }
621    }
622    return $arr2;
623   }