Partial changes for new access control system.
[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 = 20;
13 $path_to_root="..";
14 include_once($path_to_root . "/includes/session.inc");
15
16 page(_("Access setup"));
17
18 include_once($path_to_root . "/includes/ui.inc");
19 include_once($path_to_root . "/includes/access_levels.inc");
20 include_once($path_to_root . "/admin/db/security_db.inc");
21
22 $new_role = get_post('role')=='' || get_post('cancel') || get_post('clone'); 
23 //--------------------------------------------------------------------------------------------------
24 if (list_updated('role')) {
25         $Ajax->activate('details');
26         $Ajax->activate('controls');
27 }
28
29 function clear_data()
30 {
31         unset($_POST);
32 }
33
34 if (get_post('addupdate'))
35 {
36         $input_error = 0;
37         if ($_POST['description'] == '')
38         {
39         $input_error = 1;
40         display_error( _("Role description cannot be empty."));
41                 set_focus('description');
42         }
43         elseif ($_POST['name'] == '')
44         {
45         $input_error = 1;
46         display_error( _("Role name cannot be empty."));
47                 set_focus('name');
48         }
49         
50         if ($input_error == 0)
51         {
52                 $modules = array();
53                 $areas = array();
54                 foreach($_POST as $p =>$val) {
55                         if (substr($p,0,4) == 'Area')
56                                 $areas[] = substr($p, 4);
57                         if (substr($p,0,6) == 'Module')
58                                 $modules[] = substr($p, 6);
59                 }
60                 sort($areas);
61                 sort($modules);
62         if ($new_role) 
63         {
64                         add_security_role($_POST['name'], $_POST['description'], $modules, $areas); 
65                         display_notification(_("New security role has been added."));
66         } else
67         {
68                         update_security_role($_POST['role'], $_POST['name'], $_POST['description'], 
69                                 $modules, $areas); 
70                         update_record_status($_POST['role'], get_post('inactive'),
71                                 'security_roles', 'id');
72
73                         display_notification(_("Security role has been updated."));
74         }
75         $new_role = true;
76         clear_data();
77         $Ajax->activate('_page_body');
78         }
79 }
80
81 //--------------------------------------------------------------------------------------------------
82
83 if (get_post('delete'))
84 {
85         if (check_role_used(get_post('role'))) {
86                 display_error(_("This role is currently assigned to some users and cannot be deleted"));
87         } else {
88                 delete_security_role(get_post('role'));
89                 display_notification(_("Security role has been sucessfully deleted."));
90                 unset($_POST['role']);
91         }
92         $Ajax->activate('_page_body');
93 }
94
95 if (get_post('cancel'))
96 {
97         unset($_POST['role']);
98         $Ajax->activate('_page_body');
99 }
100
101 if (!isset($_POST['role']) || get_post('clone') || list_updated('role')) {
102         $id = get_post('role');
103         $clone = get_post('clone');
104         clear_data();
105         if ($id) {
106                 $row = get_security_role($id);
107                 $_POST['description'] = $row['description'];
108                 $_POST['name'] = $row['role'];
109 //      if ($row['inactive']
110 //              $_POST['inactive'] = 1;
111         
112                 $_POST['inactive'] = $row['inactive'];
113                 $access = $row['areas'];
114                 $modules = $row['modules'];
115         }
116         else {
117                 $_POST['description'] = $_POST['name'] = '';
118                 unset($_POST['inactive']);
119                 $access = $modules = array();
120         }
121         foreach($access as $a) $_POST['Area'.$a] = 1;
122         foreach($modules as $m) $_POST['Module'.$m] = 1;
123
124         if($clone) {
125                 set_focus('name');
126                 $Ajax->activate('_page_body');
127         } else
128                 $_POST['role'] = $id;
129 }
130
131 //--------------------------------------------------------------------------------------------------
132
133 start_form();
134
135 start_table("class='tablestyle_noborder'");
136 start_row();
137 security_roles_list_cells(_("Role:"). "&nbsp;", 'role', null, true, true, check_value('show_inactive'));
138 //$new_role = get_post('role')=='';
139 check_cells(_("Show inactive:"), 'show_inactive', null, true);
140 end_row();
141 end_table();
142 echo "<hr>";
143
144 if (get_post('_show_inactive_update')) {
145         $Ajax->activate('role');
146         set_focus('role');
147 }
148 if (find_submit('_Module')) {
149         $Ajax->activate('details');
150 //      set_focus('');
151 }
152 //-----------------------------------------------------------------------------------------------
153 div_start('details');
154 start_table($table_style2);
155         text_row(_("Role name:"), 'name', null, 20, 22);
156         text_row(_("Role description:"), 'description', null, 50, 52);
157         record_status_list_row(_("Current status:"), 'inactive');
158 end_table(1);
159
160         start_table("$table_style width=50%");
161
162         $k = $j = 0; //row colour counter
163         $m = 0;
164                 foreach($security_areas as $area =>$descr ) {
165             if (($area&~0xff) != $m)
166             { // features set selection
167                 $m = $area & ~0xff;
168                                 label_row(sprintf(_("%s features:"), $security_modules[$m]), 
169                                         checkbox( null, 'Module'.$m, null, true, 
170                                                 _("Set access to security features area")),
171                                 "class='tableheader2'", "class='tableheader'");
172             }
173                 if (check_value('Module'.$m)) {
174                                 alt_table_row_color($k);
175                                 check_cells($descr, 'Area'.$area, null, 
176                                         false, '', "align='center'");
177                         end_row();
178                 } else {
179                         hidden('Area'.$area);
180                 }
181         }
182         end_table(1);
183 div_end();
184
185 div_start('controls');
186 if ($new_role) 
187 {
188         submit_center('addupdate', _("Insert New Role"), true, '', 'default');
189
190 else 
191 {
192         submit_center_first('addupdate', _("Update Role"), '', 'default');
193         submit('clone', _("Clone This Role"), true, '', true);
194         submit('delete', _("Delete This Role"), true, '', true);
195         submit_center_last('cancel', _("Cancel"), _("Cancel Edition"), 'cancel');
196 }
197
198 div_end();
199
200 end_form();
201 end_page();
202
203 ?>