User add and edit now assign role ids to users.
[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 team_id = $user->team_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 team_id = $user->team_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 team_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   // The getLegacyRole obtains a legacy role value for a role_id.
87   // This is a temporary function to allow usage of both old and new roles
88   // while new role code is being written and deployed.
89   static function getLegacyRole($role_id) {
90     global $user;
91     $mdb2 = getConnection();
92
93     $sql = "select rank from tt_roles where team_id = $user->team_id and id = $role_id";
94     $res = $mdb2->query($sql);
95
96     if (!is_a($res, 'PEAR_Error')) {
97       $val = $res->fetchRow();
98       if ($val['rank']) {
99         $rank = $val['rank'];
100         if ($rank >= ROLE_MANAGER)
101           return ROLE_MANAGER;
102         else if ($rank >= ROLE_COMANAGER)
103           return ROLE_COMANAGER;
104         else if ($rank >= ROLE_CLIENT)
105           return ROLE_CLIENT;
106         else
107           return ROLE_USER;
108       }
109     }
110     return false;
111   }
112
113   // isClientRole determines if the role is a "client" role.
114   // This simply means the role has no "data_entry" right.
115   static function isClientRole($role_id) {
116     global $user;
117     $mdb2 = getConnection();
118
119     $sql = "select rights from tt_roles where team_id = $user->team_id and id = $role_id";
120     $res = $mdb2->query($sql);
121
122     if (!is_a($res, 'PEAR_Error')) {
123       $val = $res->fetchRow();
124       if ($val['rights']) {
125         return !in_array('data_entry', explode(',', $val['rights']));
126       }
127     }
128     return false;
129   }
130
131   // getRoleByRank looks up a role by its rank.
132   static function getRoleByRank($rank) {
133     global $user;
134     $mdb2 = getConnection();
135
136     $rank = (int) $rank; // Cast to int just in case for better security.
137
138     $sql = "select id from tt_roles where team_id = $user->team_id and rank = $rank and (status = 1 or status = 0)";
139     $res = $mdb2->query($sql);
140
141     if (!is_a($res, 'PEAR_Error')) {
142       $val = $res->fetchRow();
143       if ($val['id'])
144         return $val;
145     }
146     return false;
147   }
148
149   // update function updates a role in the database.
150   static function update($fields) {
151     global $user;
152     $mdb2 = getConnection();
153
154     $id = (int)$fields['id'];
155     if (isset($fields['name'])) $name_part = 'name = '.$mdb2->quote($fields['name']);
156     if (isset($fields['description'])) $descr_part = ', description = '.$mdb2->quote($fields['description']);
157     if (isset($fields['status'])) $status_part = ', status = '.(int)$fields['status'];
158     if (isset($fields['rights'])) $rights_part = ', rights = '.$mdb2->quote($fields['rights']);
159     $parts = trim($name_part.$descr_part.$status_part.$rights_part, ',');
160     $sql = "update tt_roles set $parts where id = $id and team_id = $user->team_id";
161     $affected = $mdb2->exec($sql);
162     return (!is_a($affected, 'PEAR_Error'));
163   }
164
165   // delete - marks the role as deleted.
166   static function delete($role_id) {
167     global $user;
168
169     $mdb2 = getConnection();
170
171     // Mark the task as deleted.
172     $sql = "update tt_roles set status = NULL where id = $role_id and team_id = $user->team_id";
173     $affected = $mdb2->exec($sql);
174     return (!is_a($affected, 'PEAR_Error'));
175   }
176
177   // insert - inserts an entry into tt_roles table.
178   static function insert($fields)
179   {
180     $mdb2 = getConnection();
181
182     $team_id = (int) $fields['team_id'];
183     $name = $fields['name'];
184     $rank = (int) $fields['rank'];
185     $description = $fields['description'];
186     $rights = $fields['rights'];
187     $status = $fields['status'];
188
189     $sql = "insert into tt_roles (team_id, name, rank, description, rights, status)
190       values ($team_id, ".$mdb2->quote($name).", $rank, ".$mdb2->quote($description).", ".$mdb2->quote($rights).", ".$mdb2->quote($status).")";
191     $affected = $mdb2->exec($sql);
192     if (is_a($affected, 'PEAR_Error'))
193       return false;
194
195     return true;
196   }
197
198   // rolesExist - checks whether roles for team already exist.
199   static function rolesExist()
200   {
201     $mdb2 = getConnection();
202     global $user;
203
204     $sql = "select count(*) as count from tt_roles where team_id = $user->team_id";
205     $res = $mdb2->query($sql);
206     if (!is_a($res, 'PEAR_Error')) {
207       $val = $res->fetchRow();
208       if ($val['count'] > 0)
209         return true; // Roles for team exist.
210     }
211     return false;
212   }
213
214   // createPredefinedRoles - creates a set of predefined roles for the team to use.
215   static function createPredefinedRoles($team_id, $lang)
216   {
217     // We need localized role names and a new I18n object to obtain them.
218     import('I18n');
219     $i18n = new I18n();
220     $i18n->load($lang);
221
222     $mdb2 = getConnection();
223
224     $rights_client = 'view_own_data,manage_own_settings';
225     $rights_user = 'data_entry,view_own_data,manage_own_settings,view_users';
226     $rights_supervisor = $rights_user.',on_behalf_data_entry,view_data,override_punch_mode,swap_roles,approve_timesheets';
227     $rights_comanager = $rights_supervisor.',manage_users,manage_projects,manage_tasks,manage_custom_fields,manage_clients,manage_invoices';
228     $rights_manager = $rights_comanager.',manage_features,manage_basic_settings,manage_advanced_settings,manage_roles,export_data,manage_subgroups';
229
230     // Active roles.
231     $name = $mdb2->quote($i18n->getKey('role.user.label'));
232     $description = $mdb2->quote($i18n->getKey('role.user.description'));
233     $rights = $mdb2->quote($rights_user);
234     $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($team_id, $name, $description, 4, $rights, 1)";
235     $affected = $mdb2->exec($sql);
236     if (is_a($affected, 'PEAR_Error'))
237       return false;
238
239     $name = $mdb2->quote($i18n->getKey('role.client.label'));
240     $description = $mdb2->quote($i18n->getKey('role.client.description'));
241     $rights = $mdb2->quote($rights_client);
242     $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($team_id, $name, $description, 16, $rights, 1)";
243     $affected = $mdb2->exec($sql);
244     if (is_a($affected, 'PEAR_Error'))
245       return false;
246
247     $name = $mdb2->quote($i18n->getKey('role.comanager.label'));
248     $description = $mdb2->quote($i18n->getKey('role.comanager.description'));
249     $rights = $mdb2->quote($rights_comanager);
250     $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($team_id, $name, $description, 68, $rights, 1)";
251     $affected = $mdb2->exec($sql);
252     if (is_a($affected, 'PEAR_Error'))
253       return false;
254
255     $name = $mdb2->quote($i18n->getKey('role.manager.label'));
256     $description = $mdb2->quote($i18n->getKey('role.manager.description'));
257     $rights = $mdb2->quote($rights_manager);
258     $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($team_id, $name, $description, 324, $rights, 1)";
259     $affected = $mdb2->exec($sql);
260     if (is_a($affected, 'PEAR_Error'))
261       return false;
262
263     // Inactive roles.
264     $name = $mdb2->quote($i18n->getKey('role.supervisor.label'));
265     $description = $mdb2->quote($i18n->getKey('role.supervisor.description'));
266     $rights = $mdb2->quote($rights_supervisor);
267     $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($team_id, $name, $description, 12, $rights, 0)";
268     $affected = $mdb2->exec($sql);
269     if (is_a($affected, 'PEAR_Error'))
270       return false;
271
272     return true;
273   }
274
275   // createDefaultRoles - creates a set of predefined roles for the team to use.
276   static function createDefaultRoles()
277   {
278     $mdb2 = getConnection();
279     global $i18n;
280     global $user;
281
282     $rights_client = 'view_own_data,manage_own_settings';
283     $rights_user = 'data_entry,view_own_data,manage_own_settings,view_users';
284     $rights_supervisor = $rights_user.',on_behalf_data_entry,view_data,override_punch_mode,swap_roles,approve_timesheets';
285     $rights_comanager = $rights_supervisor.',manage_users,manage_projects,manage_tasks,manage_custom_fields,manage_clients,manage_invoices';
286     $rights_manager = $rights_comanager.',manage_features,manage_basic_settings,manage_advanced_settings,manage_roles,export_data,manage_subgroups';
287
288     // Active roles.
289     $name = $mdb2->quote($i18n->getKey('role.user.label'));
290     $description = $mdb2->quote($i18n->getKey('role.user.description'));
291     $rights = $mdb2->quote($rights_user);
292     $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($user->team_id, $name, $description, 4, $rights, 1)";
293     $affected = $mdb2->exec($sql);
294     if (is_a($affected, 'PEAR_Error'))
295       return false;
296
297     $name = $mdb2->quote($i18n->getKey('role.client.label'));
298     $description = $mdb2->quote($i18n->getKey('role.client.description'));
299     $rights = $mdb2->quote($rights_client);
300     $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($user->team_id, $name, $description, 16, $rights, 1)";
301     $affected = $mdb2->exec($sql);
302     if (is_a($affected, 'PEAR_Error'))
303       return false;
304
305     $name = $mdb2->quote($i18n->getKey('role.comanager.label'));
306     $description = $mdb2->quote($i18n->getKey('role.comanager.description'));
307     $rights = $mdb2->quote($rights_comanager);
308     $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($user->team_id, $name, $description, 68, $rights, 1)";
309     $affected = $mdb2->exec($sql);
310     if (is_a($affected, 'PEAR_Error'))
311       return false;
312
313     $name = $mdb2->quote($i18n->getKey('role.manager.label'));
314     $description = $mdb2->quote($i18n->getKey('role.manager.description'));
315     $rights = $mdb2->quote($rights_manager);
316     $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($user->team_id, $name, $description, 324, $rights, 1)";
317     $affected = $mdb2->exec($sql);
318     if (is_a($affected, 'PEAR_Error'))
319       return false;
320
321     // Inactive roles.
322     $name = $mdb2->quote($i18n->getKey('role.supervisor.label'));
323     $description = $mdb2->quote($i18n->getKey('role.supervisor.description'));
324     $rights = $mdb2->quote($rights_supervisor);
325     $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($user->team_id, $name, $description, 12, $rights, 0)";
326     $affected = $mdb2->exec($sql);
327     if (is_a($affected, 'PEAR_Error'))
328       return false;
329
330     return true;
331   }
332 }