Fixed update role_id step in dbinstall.php.
[timetracker.git] / WEB-INF / lib / ttRoleHelper.class.php
1 <?php
2 // +----------------------------------------------------------------------+
3 // | Anuko Time Tracker
4 // +----------------------------------------------------------------------+
5 // | Copyright (c) Anuko International Ltd. (https://www.anuko.com)
6 // +----------------------------------------------------------------------+
7 // | LIBERAL FREEWARE LICENSE: This source code document may be used
8 // | by anyone for any purpose, and freely redistributed alone or in
9 // | combination with other software, provided that the license is obeyed.
10 // |
11 // | There are only two ways to violate the license:
12 // |
13 // | 1. To redistribute this code in source form, with the copyright
14 // |    notice or license removed or altered. (Distributing in compiled
15 // |    forms without embedded copyright notices is permitted).
16 // |
17 // | 2. To redistribute modified versions of this code in *any* form
18 // |    that bears insufficient indications that the modifications are
19 // |    not the work of the original author(s).
20 // |
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
23 // |
24 // +----------------------------------------------------------------------+
25 // | Contributors:
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
28
29 // The ttRoleHelper is a class to help with custom group roles.
30 class ttRoleHelper {
31
32   // get - gets details of a role identified by its id.
33   static function get($id)
34   {
35     global $user;
36
37     $mdb2 = getConnection();
38
39     $sql = "select id, name, description, rank, rights, status from tt_roles
40       where id = $id and group_id = $user->group_id and (status = 0 or status = 1)";
41     $res = $mdb2->query($sql);
42
43     if (!is_a($res, 'PEAR_Error')) {
44       $val = $res->fetchRow();
45           if ($val['id'] != '') {
46         return $val;
47       } else
48         return false;
49     }
50     return false;
51   }
52
53   // The getRoleByName looks up a role by name.
54   static function getRoleByName($role_name) {
55
56     $mdb2 = getConnection();
57     global $user;
58
59     $sql = "select id from tt_roles where group_id = $user->group_id and name = ".
60       $mdb2->quote($role_name)." and (status = 1 or status = 0)";
61     $res = $mdb2->query($sql);
62
63     if (!is_a($res, 'PEAR_Error')) {
64       $val = $res->fetchRow();
65       if ($val['id'])
66         return $val;
67     }
68     return false;
69   }
70
71   // The getTopManagerRoleID obtains an ID for top manager role.
72   static function getTopManagerRoleID() {
73     $mdb2 = getConnection();
74
75     $sql = "select id from tt_roles where group_id = 0 and rank = 512";
76     $res = $mdb2->query($sql);
77
78     if (!is_a($res, 'PEAR_Error')) {
79       $val = $res->fetchRow();
80       if ($val['id'])
81         return $val['id'];
82     }
83     return false;
84   }
85
86   // isClientRole determines if the role is a "client" role.
87   // This simply means the role has no "track_own_time" right.
88   static function isClientRole($role_id) {
89     global $user;
90     $mdb2 = getConnection();
91
92     $sql = "select rights from tt_roles where group_id = $user->group_id and id = $role_id";
93     $res = $mdb2->query($sql);
94
95     if (!is_a($res, 'PEAR_Error')) {
96       $val = $res->fetchRow();
97       if ($val['rights']) {
98         return !in_array('track_own_time', explode(',', $val['rights']));
99       }
100     }
101     return false;
102   }
103
104   // getRoleByRank looks up a role by its rank.
105   static function getRoleByRank($rank, $group_id) {
106     global $user;
107     $mdb2 = getConnection();
108
109     $rank = (int) $rank; // Cast to int just in case for better security.
110
111     $sql = "select id from tt_roles where group_id = $group_id and rank = $rank and (status = 1 or status = 0)";
112     $res = $mdb2->query($sql);
113
114     if (!is_a($res, 'PEAR_Error')) {
115       $val = $res->fetchRow();
116       if ($val['id'])
117         return $val['id'];
118     }
119     return false;
120   }
121
122   // update function updates a role in the database.
123   static function update($fields) {
124     global $user;
125     $mdb2 = getConnection();
126
127     $id = (int)$fields['id'];
128     if (isset($fields['name'])) $name_part = 'name = '.$mdb2->quote($fields['name']);
129     if (isset($fields['rank'])) $rank_part = ', rank = '.(int)$fields['rank'];
130     if (isset($fields['description'])) $descr_part = ', description = '.$mdb2->quote($fields['description']);
131     if (isset($fields['status'])) $status_part = ', status = '.(int)$fields['status'];
132     if (isset($fields['rights'])) $rights_part = ', rights = '.$mdb2->quote($fields['rights']);
133     $parts = trim($name_part.$rank_part.$descr_part.$status_part.$rights_part, ',');
134     $sql = "update tt_roles set $parts where id = $id and group_id = $user->group_id";
135     $affected = $mdb2->exec($sql);
136     return (!is_a($affected, 'PEAR_Error'));
137   }
138
139   // delete - marks the role as deleted.
140   static function delete($role_id) {
141     global $user;
142
143     $mdb2 = getConnection();
144
145     // Mark the task as deleted.
146     $sql = "update tt_roles set status = NULL where id = $role_id and group_id = $user->group_id";
147     $affected = $mdb2->exec($sql);
148     return (!is_a($affected, 'PEAR_Error'));
149   }
150
151   // insert - inserts an entry into tt_roles table.
152   static function insert($fields)
153   {
154     $mdb2 = getConnection();
155
156     $group_id = (int) $fields['group_id'];
157     $name = $fields['name'];
158     $rank = (int) $fields['rank'];
159     $description = $fields['description'];
160     $rights = $fields['rights'];
161     $status = $fields['status'];
162
163     $sql = "insert into tt_roles (group_id, name, rank, description, rights, status)
164       values ($group_id, ".$mdb2->quote($name).", $rank, ".$mdb2->quote($description).", ".$mdb2->quote($rights).", ".$mdb2->quote($status).")";
165     $affected = $mdb2->exec($sql);
166     if (is_a($affected, 'PEAR_Error'))
167       return false;
168
169     $sql = "SELECT LAST_INSERT_ID() AS last_id";
170     $res = $mdb2->query($sql);
171     $val = $res->fetchRow();
172     $last_id = $val['last_id'];
173     return $last_id;
174   }
175
176   // createPredefinedRoles - creates a set of predefined roles for a group to use.
177   static function createPredefinedRoles($group_id, $lang)
178   {
179     // We need localized role names and a new I18n object to obtain them.
180     import('I18n');
181     $i18n = new I18n();
182     $i18n->load($lang);
183
184     $mdb2 = getConnection();
185
186     $rights_client = 'view_own_reports,view_own_charts,view_own_invoices,manage_own_settings';
187     $rights_user = 'track_own_time,track_own_expenses,view_own_reports,view_own_charts,view_own_projects,view_own_tasks,manage_own_settings,view_users';
188     $rights_supervisor = $rights_user.',track_time,track_expenses,view_reports,view_charts,view_own_clients,override_punch_mode,override_date_lock,override_own_date_lock,swap_roles,approve_timesheets';
189     $rights_comanager = $rights_supervisor.',manage_own_account,manage_users,manage_projects,manage_tasks,manage_custom_fields,manage_clients,manage_invoices,override_allow_ip,manage_basic_settings,view_all_reports';
190     $rights_manager = $rights_comanager.',manage_features,manage_advanced_settings,manage_roles,export_data,manage_subgroups';
191
192     // Active roles.
193     $name = $mdb2->quote($i18n->get('role.user.label'));
194     $description = $mdb2->quote($i18n->get('role.user.description'));
195     $rights = $mdb2->quote($rights_user);
196     $sql = "insert into tt_roles (group_id, name, description, rank, rights, status) values($group_id, $name, $description, 4, $rights, 1)";
197     $affected = $mdb2->exec($sql);
198     if (is_a($affected, 'PEAR_Error'))
199       return false;
200
201     $name = $mdb2->quote($i18n->get('role.client.label'));
202     $description = $mdb2->quote($i18n->get('role.client.description'));
203     $rights = $mdb2->quote($rights_client);
204     $sql = "insert into tt_roles (group_id, name, description, rank, rights, status) values($group_id, $name, $description, 16, $rights, 1)";
205     $affected = $mdb2->exec($sql);
206     if (is_a($affected, 'PEAR_Error'))
207       return false;
208
209     $name = $mdb2->quote($i18n->get('role.comanager.label'));
210     $description = $mdb2->quote($i18n->get('role.comanager.description'));
211     $rights = $mdb2->quote($rights_comanager);
212     $sql = "insert into tt_roles (group_id, name, description, rank, rights, status) values($group_id, $name, $description, 68, $rights, 1)";
213     $affected = $mdb2->exec($sql);
214     if (is_a($affected, 'PEAR_Error'))
215       return false;
216
217     $name = $mdb2->quote($i18n->get('role.manager.label'));
218     $description = $mdb2->quote($i18n->get('role.manager.description'));
219     $rights = $mdb2->quote($rights_manager);
220     $sql = "insert into tt_roles (group_id, name, description, rank, rights, status) values($group_id, $name, $description, 324, $rights, 1)";
221     $affected = $mdb2->exec($sql);
222     if (is_a($affected, 'PEAR_Error'))
223       return false;
224
225     // Inactive roles.
226     $name = $mdb2->quote($i18n->get('role.supervisor.label'));
227     $description = $mdb2->quote($i18n->get('role.supervisor.description'));
228     $rights = $mdb2->quote($rights_supervisor);
229     $sql = "insert into tt_roles (group_id, name, description, rank, rights, status) values($group_id, $name, $description, 12, $rights, 0)";
230     $affected = $mdb2->exec($sql);
231     if (is_a($affected, 'PEAR_Error'))
232       return false;
233
234     return true;
235   }
236
237   // createPredefinedRoles_1_17_44 - used in dbinstall.php during database schema update.
238   static function createPredefinedRoles_1_17_44($group_id, $lang)
239   {
240     // We need localized role names and a new I18n object to obtain them.
241     import('I18n');
242     $i18n = new I18n();
243     $i18n->load($lang);
244
245     $mdb2 = getConnection();
246
247     $rights_client = 'view_own_reports,view_own_charts,view_own_invoices,manage_own_settings';
248     $rights_user = 'track_own_time,track_own_expenses,view_own_reports,view_own_charts,view_own_projects,view_own_tasks,manage_own_settings,view_users';
249     $rights_supervisor = $rights_user.',track_time,track_expenses,view_reports,view_charts,view_own_clients,override_punch_mode,override_date_lock,override_own_date_lock,swap_roles,approve_timesheets';
250     $rights_comanager = $rights_supervisor.',manage_own_account,manage_users,manage_projects,manage_tasks,manage_custom_fields,manage_clients,manage_invoices,override_allow_ip,manage_basic_settings,view_all_reports';
251     $rights_manager = $rights_comanager.',manage_features,manage_advanced_settings,manage_roles,export_data,manage_subgroups';
252
253     // Active roles.
254     $name = $mdb2->quote($i18n->get('role.user.label'));
255     $description = $mdb2->quote($i18n->get('role.user.description'));
256     $rights = $mdb2->quote($rights_user);
257     $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($group_id, $name, $description, 4, $rights, 1)";
258     $affected = $mdb2->exec($sql);
259     if (is_a($affected, 'PEAR_Error'))
260       return false;
261
262     $name = $mdb2->quote($i18n->get('role.client.label'));
263     $description = $mdb2->quote($i18n->get('role.client.description'));
264     $rights = $mdb2->quote($rights_client);
265     $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($group_id, $name, $description, 16, $rights, 1)";
266     $affected = $mdb2->exec($sql);
267     if (is_a($affected, 'PEAR_Error'))
268       return false;
269
270     $name = $mdb2->quote($i18n->get('role.comanager.label'));
271     $description = $mdb2->quote($i18n->get('role.comanager.description'));
272     $rights = $mdb2->quote($rights_comanager);
273     $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($group_id, $name, $description, 68, $rights, 1)";
274     $affected = $mdb2->exec($sql);
275     if (is_a($affected, 'PEAR_Error'))
276       return false;
277
278     $name = $mdb2->quote($i18n->get('role.manager.label'));
279     $description = $mdb2->quote($i18n->get('role.manager.description'));
280     $rights = $mdb2->quote($rights_manager);
281     $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($group_id, $name, $description, 324, $rights, 1)";
282     $affected = $mdb2->exec($sql);
283     if (is_a($affected, 'PEAR_Error'))
284       return false;
285
286     // Inactive roles.
287     $name = $mdb2->quote($i18n->get('role.supervisor.label'));
288     $description = $mdb2->quote($i18n->get('role.supervisor.description'));
289     $rights = $mdb2->quote($rights_supervisor);
290     $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($group_id, $name, $description, 12, $rights, 0)";
291     $affected = $mdb2->exec($sql);
292     if (is_a($affected, 'PEAR_Error'))
293       return false;
294
295     return true;
296   }
297
298   // getRoleByRank_1_17_44 is used in dbinstall.php and looks up a role by its rank.
299   static function getRoleByRank_1_17_44($rank, $group_id) {
300     global $user;
301     $mdb2 = getConnection();
302
303     $rank = (int) $rank; // Cast to int just in case for better security.
304
305     $sql = "select id from tt_roles where team_id = $group_id and rank = $rank and (status = 1 or status = 0)";
306     $res = $mdb2->query($sql);
307
308     if (!is_a($res, 'PEAR_Error')) {
309       $val = $res->fetchRow();
310       if ($val['id'])
311         return $val['id'];
312     }
313     return false;
314   }
315 }