Changed access control extensions support for modules/plugins to use unique extension...
[fa-stable.git] / admin / security_roles.php
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU General Public License, GPL, 
5         as published by the Free Software Foundation, either version 3 
6         of the License, or (at your option) any later version.
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
10     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 $page_security = 'SA_SECROLES';
13 $path_to_root = "..";
14 include_once($path_to_root . "/includes/session.inc");
15
16 add_access_extensions();
17
18 page(_("Access setup"));
19
20 include_once($path_to_root . "/includes/ui.inc");
21 include_once($path_to_root . "/includes/access_levels.inc");
22 include_once($path_to_root . "/admin/db/security_db.inc");
23
24 $new_role = get_post('role')=='' || get_post('cancel') || get_post('clone'); 
25 //--------------------------------------------------------------------------------------------------
26 // Following compare function is used for sorting areas 
27 // in such a way that security areas defined by module/plugin
28 // is properly placed under related section regardless of 
29 // unique extension number
30 //
31 function comp_areas($area1, $area2) 
32 {
33         return ($area1[0]&0xffff)-($area2[0]&0xffff);
34 }
35
36 //--------------------------------------------------------------------------------------------------
37 if (list_updated('role')) {
38         $Ajax->activate('details');
39         $Ajax->activate('controls');
40 }
41
42 function clear_data()
43 {
44         unset($_POST);
45 }
46
47 if (get_post('addupdate'))
48 {
49         $input_error = 0;
50         if ($_POST['description'] == '')
51         {
52         $input_error = 1;
53         display_error( _("Role description cannot be empty."));
54                 set_focus('description');
55         }
56         elseif ($_POST['name'] == '')
57         {
58         $input_error = 1;
59         display_error( _("Role name cannot be empty."));
60                 set_focus('name');
61         }
62                 // prevent accidental editor lockup by removing SA_SECROLES
63         if (get_post('role') == $_SESSION['wa_current_user']->access) {
64                 if (!isset($_POST['Area'.$security_areas['SA_SECROLES'][0]])
65                         || !isset($_POST['Section'.SS_SETUP])) {
66                         display_error(_("Access level edition in Company setup section have to be enabled for your account."));
67                 $input_error = 1;
68                 set_focus(!isset($_POST['Section'.SS_SETUP]) 
69                         ? 'Section'.SS_SETUP : 'Area'.$security_areas['SA_SECROLES'][0]);
70                 }
71         }
72
73         if ($input_error == 0)
74         {
75                 $sections = array();
76                 $areas = array();
77                 foreach($_POST as $p =>$val) {
78                         if (substr($p,0,4) == 'Area') {
79                                 $a = substr($p, 4);
80                                 if (($a&~0xffff) && (($a&0xff00)<(99<<8))) {
81                                         $sections[] = $a&~0xff; // add extended section for plugins
82                                 }
83                                 $areas[] = $a;
84                         }
85                         if (substr($p,0,7) == 'Section')
86                                 $sections[] = substr($p, 7);
87                 }
88                 uasort($areas, 'comp_areas');
89
90                 $sections = array_values($sections);
91                 _vd($sections);
92         if ($new_role) 
93         {
94                         add_security_role($_POST['name'], $_POST['description'], $sections, $areas); 
95                         display_notification(_("New security role has been added."));
96         } else
97         {
98                         update_security_role($_POST['role'], $_POST['name'], $_POST['description'], 
99                                 $sections, $areas); 
100                         update_record_status($_POST['role'], get_post('inactive'),
101                                 'security_roles', 'id');
102
103                         display_notification(_("Security role has been updated."));
104         }
105         $new_role = true;
106         clear_data();
107         $Ajax->activate('_page_body');
108         }
109 }
110
111 //--------------------------------------------------------------------------------------------------
112
113 if (get_post('delete'))
114 {
115         if (check_role_used(get_post('role'))) {
116                 display_error(_("This role is currently assigned to some users and cannot be deleted"));
117         } else {
118                 delete_security_role(get_post('role'));
119                 display_notification(_("Security role has been sucessfully deleted."));
120                 unset($_POST['role']);
121         }
122         $Ajax->activate('_page_body');
123 }
124
125 if (get_post('cancel'))
126 {
127         unset($_POST['role']);
128         $Ajax->activate('_page_body');
129 }
130
131 if (!isset($_POST['role']) || get_post('clone') || list_updated('role')) {
132         $id = get_post('role');
133         $clone = get_post('clone');
134
135         unset($_POST);
136         if ($id) {
137                 $row = get_security_role($id);
138                 $_POST['description'] = $row['description'];
139                 $_POST['name'] = $row['role'];
140 //      if ($row['inactive']
141 //              $_POST['inactive'] = 1;
142         
143                 $_POST['inactive'] = $row['inactive'];
144                 $access = $row['areas'];
145                 $sections = $row['sections'];
146         }
147         else {
148                 $_POST['description'] = $_POST['name'] = '';
149                 unset($_POST['inactive']);
150                 $access = $sections = array();
151         }
152         foreach($access as $a) $_POST['Area'.$a] = 1;
153         foreach($sections as $s) $_POST['Section'.$s] = 1;
154
155         if($clone) {
156                 set_focus('name');
157                 $Ajax->activate('_page_body');
158         } else
159                 $_POST['role'] = $id;
160 }
161
162 //--------------------------------------------------------------------------------------------------
163
164 start_form();
165
166 start_table("class='tablestyle_noborder'");
167 start_row();
168 security_roles_list_cells(_("Role:"). "&nbsp;", 'role', null, true, true, check_value('show_inactive'));
169 $new_role = get_post('role')=='';
170 check_cells(_("Show inactive:"), 'show_inactive', null, true);
171 end_row();
172 end_table();
173 echo "<hr>";
174
175 if (get_post('_show_inactive_update')) {
176         $Ajax->activate('role');
177         set_focus('role');
178 }
179 if (find_submit('_Section')) {
180         $Ajax->activate('details');
181 }
182 //-----------------------------------------------------------------------------------------------
183 div_start('details');
184 start_table($table_style2);
185         text_row(_("Role name:"), 'name', null, 20, 22);
186         text_row(_("Role description:"), 'description', null, 50, 52);
187         record_status_list_row(_("Current status:"), 'inactive');
188 end_table(1);
189
190         start_table("$table_style width=40%");
191
192         $k = $j = 0; //row colour counter
193         $ext = $sec = $m = -1;
194         uasort($security_areas,'comp_areas');
195         foreach($security_areas as $area =>$parms ) {
196                 // system setup areas are accessable only for site admins i.e. 
197                 // admins of first registered company
198                 if (user_company() && (($parms[0]&0xff00) == SS_SADMIN)) continue;
199                 
200                 $newsec = ($parms[0]>>8)&0xff;
201                 $newext  = $parms[0]>>16;
202                 if ($newsec != $sec || (($newext != $ext) && ($newsec>99)))
203                 { // features set selection
204                         $ext = $newext; 
205                         $sec = $newsec;
206                         $m = $parms[0] & ~0xff;
207 //                      if(!isset($security_sections[$m]))
208 //                       display_error(sprintf("Bad section %X:", $m));
209                         label_row($security_sections[$m].':', 
210                                 checkbox( null, 'Section'.$m, null, true, 
211                                         _("On/off set of features")),
212                         "class='tableheader2'", "class='tableheader'");
213                 }
214                 if (check_value('Section'.$m)) {
215                                 alt_table_row_color($k);
216                                 check_cells($parms[1], 'Area'.$parms[0], null, 
217                                         false, '', "align='center'");
218                         end_row();
219                 } else {
220                         hidden('Area'.$parms[0]);
221                 }
222         }
223         end_table(1);
224 div_end();
225
226 div_start('controls');
227
228 if ($new_role) 
229 {
230         submit_center_first('Update', _("Update view"), '', null);
231         submit_center_last('addupdate', _("Insert New Role"), '', 'default');
232
233 else 
234 {
235         submit_center_first('addupdate', _("Save Role"), '', 'default');
236         submit('Update', _("Update view"), true, '', null);
237         submit('clone', _("Clone This Role"), true, '', true);
238         submit('delete', _("Delete This Role"), true, '', true);
239         submit_center_last('cancel', _("Cancel"), _("Cancel Edition"), 'cancel');
240 }
241
242 div_end();
243
244 end_form();
245 end_page();
246
247 ?>