Fixed approve_all_reports right assignment and check.
[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->getGroup()." 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->getGroup().
60       " and name = ".$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->getGroup()." 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) {
106     global $user;
107     $mdb2 = getConnection();
108
109     $group_id = $user->getGroup();
110     $org_id = $user->org_id;
111     $rank = (int) $rank; // Cast to int just in case.
112
113     $sql = "select id from tt_roles where group_id = $group_id and org_id = $org_id and rank = $rank and (status = 1 or status = 0)";
114     $res = $mdb2->query($sql);
115
116     if (!is_a($res, 'PEAR_Error')) {
117       $val = $res->fetchRow();
118       if ($val['id'])
119         return $val['id'];
120     }
121     return false;
122   }
123
124   // update function updates a role in the database.
125   static function update($fields) {
126     global $user;
127     $mdb2 = getConnection();
128
129     $group_id = $user->getGroup();
130     $org_id = $user->org_id;
131
132     $id = (int)$fields['id'];
133     if (isset($fields['name'])) $name_part = 'name = '.$mdb2->quote($fields['name']);
134     if (isset($fields['rank'])) $rank_part = ', rank = '.(int)$fields['rank'];
135     if (isset($fields['description'])) $descr_part = ', description = '.$mdb2->quote($fields['description']);
136     if (isset($fields['status'])) $status_part = ', status = '.(int)$fields['status'];
137     if (isset($fields['rights'])) $rights_part = ', rights = '.$mdb2->quote($fields['rights']);
138     $parts = trim($name_part.$rank_part.$descr_part.$status_part.$rights_part, ',');
139     $sql = "update tt_roles set $parts where id = $id and group_id = $group_id and org_id = $org_id";
140     $affected = $mdb2->exec($sql);
141     return (!is_a($affected, 'PEAR_Error'));
142   }
143
144   // delete - marks the role as deleted.
145   static function delete($role_id) {
146     global $user;
147
148     $mdb2 = getConnection();
149     $group_id = $user->getGroup();
150     $org_id = $user->org_id;
151
152     // Mark the task as deleted.
153     $sql = "update tt_roles set status = NULL where id = $role_id and group_id = $group_id and org_id = $org_id";
154     $affected = $mdb2->exec($sql);
155     return (!is_a($affected, 'PEAR_Error'));
156   }
157
158   // insert - inserts an entry into tt_roles table.
159   static function insert($fields)
160   {
161     global $user;
162     $mdb2 = getConnection();
163
164     $group_id = $user->getGroup();
165     $org_id = $user->org_id;
166     $name = $fields['name'];
167     $rank = (int) $fields['rank'];
168     $description = $fields['description'];
169     $rights = $fields['rights'];
170     $status = $fields['status'];
171
172     $sql = "insert into tt_roles (group_id, org_id, name, rank, description, rights, status)
173       values ($group_id, $org_id, ".$mdb2->quote($name).", $rank, ".$mdb2->quote($description).", ".$mdb2->quote($rights).", ".$mdb2->quote($status).")";
174     $affected = $mdb2->exec($sql);
175     if (is_a($affected, 'PEAR_Error'))
176       return false;
177
178     $last_id = $mdb2->lastInsertID('tt_roles', 'id');
179     return $last_id;
180   }
181
182   // createPredefinedRoles - creates a set of predefined roles for a group to use.
183   static function createPredefinedRoles($group_id, $lang)
184   {
185     // We need localized role names and a new I18n object to obtain them.
186     import('I18n');
187     $i18n = new I18n();
188     $i18n->load($lang);
189
190     $mdb2 = getConnection();
191
192     $rights_client = 'view_client_reports,view_client_invoices,manage_own_settings';
193     $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';
194     $rights_supervisor = $rights_user.',track_time,track_expenses,view_reports,approve_reports,approve_timesheets,view_charts,view_own_clients,override_punch_mode,override_date_lock,override_own_date_lock,swap_roles,update_work';
195     $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,manage_work,bid_on_work';
196     $rights_manager = $rights_comanager.',manage_features,manage_advanced_settings,manage_roles,export_data,approve_all_reports,approve_own_timesheets,manage_subgroups';
197
198     // Active roles.
199     $name = $mdb2->quote($i18n->get('role.user.label'));
200     $description = $mdb2->quote($i18n->get('role.user.description'));
201     $rights = $mdb2->quote($rights_user);
202     $sql = "insert into tt_roles (group_id, org_id, name, description, rank, rights, status) values($group_id, $group_id, $name, $description, 4, $rights, 1)";
203     $affected = $mdb2->exec($sql);
204     if (is_a($affected, 'PEAR_Error'))
205       return false;
206
207     $name = $mdb2->quote($i18n->get('role.client.label'));
208     $description = $mdb2->quote($i18n->get('role.client.description'));
209     $rights = $mdb2->quote($rights_client);
210     $sql = "insert into tt_roles (group_id, org_id, name, description, rank, rights, status) values($group_id, $group_id, $name, $description, 16, $rights, 1)";
211     $affected = $mdb2->exec($sql);
212     if (is_a($affected, 'PEAR_Error'))
213       return false;
214
215     $name = $mdb2->quote($i18n->get('role.comanager.label'));
216     $description = $mdb2->quote($i18n->get('role.comanager.description'));
217     $rights = $mdb2->quote($rights_comanager);
218     $sql = "insert into tt_roles (group_id, org_id, name, description, rank, rights, status) values($group_id, $group_id, $name, $description, 68, $rights, 1)";
219     $affected = $mdb2->exec($sql);
220     if (is_a($affected, 'PEAR_Error'))
221       return false;
222
223     $name = $mdb2->quote($i18n->get('role.manager.label'));
224     $description = $mdb2->quote($i18n->get('role.manager.description'));
225     $rights = $mdb2->quote($rights_manager);
226     $sql = "insert into tt_roles (group_id, org_id, name, description, rank, rights, status) values($group_id, $group_id, $name, $description, 324, $rights, 1)";
227     $affected = $mdb2->exec($sql);
228     if (is_a($affected, 'PEAR_Error'))
229       return false;
230
231     // Inactive roles.
232     $name = $mdb2->quote($i18n->get('role.supervisor.label'));
233     $description = $mdb2->quote($i18n->get('role.supervisor.description'));
234     $rights = $mdb2->quote($rights_supervisor);
235     $sql = "insert into tt_roles (group_id, org_id, name, description, rank, rights, status) values($group_id, $group_id, $name, $description, 12, $rights, 0)";
236     $affected = $mdb2->exec($sql);
237     if (is_a($affected, 'PEAR_Error'))
238       return false;
239
240     return true;
241   }
242
243   // createPredefinedRoles_1_17_44 - used in dbinstall.php during database schema update.
244   static function createPredefinedRoles_1_17_44($group_id, $lang)
245   {
246     // We need localized role names and a new I18n object to obtain them.
247     import('I18n');
248     $i18n = new I18n();
249     $i18n->load($lang);
250
251     $mdb2 = getConnection();
252
253     $rights_client = 'view_own_reports,view_own_charts,view_own_invoices,manage_own_settings';
254     $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';
255     $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';
256     $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';
257     $rights_manager = $rights_comanager.',manage_features,manage_advanced_settings,manage_roles,export_data,manage_subgroups';
258
259     // Active roles.
260     $name = $mdb2->quote($i18n->get('role.user.label'));
261     $description = $mdb2->quote($i18n->get('role.user.description'));
262     $rights = $mdb2->quote($rights_user);
263     $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($group_id, $name, $description, 4, $rights, 1)";
264     $affected = $mdb2->exec($sql);
265     if (is_a($affected, 'PEAR_Error'))
266       return false;
267
268     $name = $mdb2->quote($i18n->get('role.client.label'));
269     $description = $mdb2->quote($i18n->get('role.client.description'));
270     $rights = $mdb2->quote($rights_client);
271     $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($group_id, $name, $description, 16, $rights, 1)";
272     $affected = $mdb2->exec($sql);
273     if (is_a($affected, 'PEAR_Error'))
274       return false;
275
276     $name = $mdb2->quote($i18n->get('role.comanager.label'));
277     $description = $mdb2->quote($i18n->get('role.comanager.description'));
278     $rights = $mdb2->quote($rights_comanager);
279     $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($group_id, $name, $description, 68, $rights, 1)";
280     $affected = $mdb2->exec($sql);
281     if (is_a($affected, 'PEAR_Error'))
282       return false;
283
284     $name = $mdb2->quote($i18n->get('role.manager.label'));
285     $description = $mdb2->quote($i18n->get('role.manager.description'));
286     $rights = $mdb2->quote($rights_manager);
287     $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($group_id, $name, $description, 324, $rights, 1)";
288     $affected = $mdb2->exec($sql);
289     if (is_a($affected, 'PEAR_Error'))
290       return false;
291
292     // Inactive roles.
293     $name = $mdb2->quote($i18n->get('role.supervisor.label'));
294     $description = $mdb2->quote($i18n->get('role.supervisor.description'));
295     $rights = $mdb2->quote($rights_supervisor);
296     $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($group_id, $name, $description, 12, $rights, 0)";
297     $affected = $mdb2->exec($sql);
298     if (is_a($affected, 'PEAR_Error'))
299       return false;
300
301     return true;
302   }
303
304   // getRoleByRank_1_17_44 is used in dbinstall.php and looks up a role by its rank.
305   static function getRoleByRank_1_17_44($rank, $group_id) {
306     global $user;
307     $mdb2 = getConnection();
308
309     $rank = (int) $rank; // Cast to int just in case for better security.
310
311     $sql = "select id from tt_roles where team_id = $group_id and rank = $rank and (status = 1 or status = 0)";
312     $res = $mdb2->query($sql);
313
314     if (!is_a($res, 'PEAR_Error')) {
315       $val = $res->fetchRow();
316       if ($val['id'])
317         return $val['id'];
318     }
319     return false;
320   }
321
322   // copyRolesToGroup copies roles from current on behalf group to another.
323   static function copyRolesToGroup($group_id) {
324     global $user;
325     $mdb2 = getConnection();
326
327     $org_id = $user->org_id;
328     $columns = '(group_id, org_id, name, description, rank, rights, status)';
329     $roles = ttGroupHelper::getRoles(); // Roles in current on behalf group.
330
331     foreach ($roles as $role) {
332       $values = "values($group_id, $org_id".
333         ', '.$mdb2->quote($role['name']).
334         ', '.$mdb2->quote($role['description']).
335         ', '.(int)$role['rank'].
336         ', '.$mdb2->quote($role['rights']).
337         ', '.$mdb2->quote($role['status']).
338         ')';
339       $sql = "insert into tt_roles $columns $values";
340       $affected = $mdb2->exec($sql);
341       if (is_a($affected, 'PEAR_Error'))
342         return false;
343     }
344     return true;
345   }
346 }