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.
11 // | There are only two ways to violate the license:
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).
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).
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
24 // +----------------------------------------------------------------------+
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
29 // The ttRoleHelper is a class to help with custom group roles.
32 // get - gets details of a role identified by its id.
33 static function get($id)
37 $mdb2 = getConnection();
39 $sql = "select id, name, description, rank, rights, status from tt_roles
40 where id = $id and group_id = ".$user->getActiveGroup()." and (status = 0 or status = 1)";
41 $res = $mdb2->query($sql);
43 if (!is_a($res, 'PEAR_Error')) {
44 $val = $res->fetchRow();
45 if ($val['id'] != '') {
53 // The getRoleByName looks up a role by name.
54 static function getRoleByName($role_name) {
56 $mdb2 = getConnection();
59 $sql = "select id from tt_roles where group_id = ".$user->getActiveGroup().
60 " and name = ".$mdb2->quote($role_name)." and (status = 1 or status = 0)";
61 $res = $mdb2->query($sql);
63 if (!is_a($res, 'PEAR_Error')) {
64 $val = $res->fetchRow();
71 // The getTopManagerRoleID obtains an ID for top manager role.
72 static function getTopManagerRoleID() {
73 $mdb2 = getConnection();
75 $sql = "select id from tt_roles where group_id = 0 and rank = 512";
76 $res = $mdb2->query($sql);
78 if (!is_a($res, 'PEAR_Error')) {
79 $val = $res->fetchRow();
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) {
90 $mdb2 = getConnection();
92 $sql = "select rights from tt_roles where group_id = ".$user->getActiveGroup()." and id = $role_id";
93 $res = $mdb2->query($sql);
95 if (!is_a($res, 'PEAR_Error')) {
96 $val = $res->fetchRow();
98 return !in_array('track_own_time', explode(',', $val['rights']));
104 // getRoleByRank looks up a role by its rank.
105 static function getRoleByRank($rank, $group_id) {
107 $mdb2 = getConnection();
109 $rank = (int) $rank; // Cast to int just in case for better security.
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);
114 if (!is_a($res, 'PEAR_Error')) {
115 $val = $res->fetchRow();
122 // update function updates a role in the database.
123 static function update($fields) {
125 $mdb2 = getConnection();
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'));
139 // delete - marks the role as deleted.
140 static function delete($role_id) {
143 $mdb2 = getConnection();
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'));
151 // insert - inserts an entry into tt_roles table.
152 static function insert($fields)
154 $mdb2 = getConnection();
156 $group_id = (int) $fields['group_id'];
157 $org_id = (int) $fields['org_id'];
158 $name = $fields['name'];
159 $rank = (int) $fields['rank'];
160 $description = $fields['description'];
161 $rights = $fields['rights'];
162 $status = $fields['status'];
164 $sql = "insert into tt_roles (group_id, org_id, name, rank, description, rights, status)
165 values ($group_id, $org_id, ".$mdb2->quote($name).", $rank, ".$mdb2->quote($description).", ".$mdb2->quote($rights).", ".$mdb2->quote($status).")";
166 $affected = $mdb2->exec($sql);
167 if (is_a($affected, 'PEAR_Error'))
170 $sql = "SELECT LAST_INSERT_ID() AS last_id";
171 $res = $mdb2->query($sql);
172 $val = $res->fetchRow();
173 $last_id = $val['last_id'];
177 // createPredefinedRoles - creates a set of predefined roles for a group to use.
178 static function createPredefinedRoles($group_id, $lang)
180 // We need localized role names and a new I18n object to obtain them.
185 $mdb2 = getConnection();
187 $rights_client = 'view_own_reports,view_own_charts,view_own_invoices,manage_own_settings';
188 $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';
189 $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';
190 $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';
191 $rights_manager = $rights_comanager.',manage_features,manage_advanced_settings,manage_roles,export_data,manage_subgroups';
194 $name = $mdb2->quote($i18n->get('role.user.label'));
195 $description = $mdb2->quote($i18n->get('role.user.description'));
196 $rights = $mdb2->quote($rights_user);
197 $sql = "insert into tt_roles (group_id, org_id, name, description, rank, rights, status) values($group_id, $group_id, $name, $description, 4, $rights, 1)";
198 $affected = $mdb2->exec($sql);
199 if (is_a($affected, 'PEAR_Error'))
202 $name = $mdb2->quote($i18n->get('role.client.label'));
203 $description = $mdb2->quote($i18n->get('role.client.description'));
204 $rights = $mdb2->quote($rights_client);
205 $sql = "insert into tt_roles (group_id, org_id, name, description, rank, rights, status) values($group_id, $group_id, $name, $description, 16, $rights, 1)";
206 $affected = $mdb2->exec($sql);
207 if (is_a($affected, 'PEAR_Error'))
210 $name = $mdb2->quote($i18n->get('role.comanager.label'));
211 $description = $mdb2->quote($i18n->get('role.comanager.description'));
212 $rights = $mdb2->quote($rights_comanager);
213 $sql = "insert into tt_roles (group_id, org_id, name, description, rank, rights, status) values($group_id, $group_id, $name, $description, 68, $rights, 1)";
214 $affected = $mdb2->exec($sql);
215 if (is_a($affected, 'PEAR_Error'))
218 $name = $mdb2->quote($i18n->get('role.manager.label'));
219 $description = $mdb2->quote($i18n->get('role.manager.description'));
220 $rights = $mdb2->quote($rights_manager);
221 $sql = "insert into tt_roles (group_id, org_id, name, description, rank, rights, status) values($group_id, $group_id, $name, $description, 324, $rights, 1)";
222 $affected = $mdb2->exec($sql);
223 if (is_a($affected, 'PEAR_Error'))
227 $name = $mdb2->quote($i18n->get('role.supervisor.label'));
228 $description = $mdb2->quote($i18n->get('role.supervisor.description'));
229 $rights = $mdb2->quote($rights_supervisor);
230 $sql = "insert into tt_roles (group_id, org_id, name, description, rank, rights, status) values($group_id, $group_id, $name, $description, 12, $rights, 0)";
231 $affected = $mdb2->exec($sql);
232 if (is_a($affected, 'PEAR_Error'))
238 // createPredefinedRoles_1_17_44 - used in dbinstall.php during database schema update.
239 static function createPredefinedRoles_1_17_44($group_id, $lang)
241 // We need localized role names and a new I18n object to obtain them.
246 $mdb2 = getConnection();
248 $rights_client = 'view_own_reports,view_own_charts,view_own_invoices,manage_own_settings';
249 $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';
250 $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';
251 $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';
252 $rights_manager = $rights_comanager.',manage_features,manage_advanced_settings,manage_roles,export_data,manage_subgroups';
255 $name = $mdb2->quote($i18n->get('role.user.label'));
256 $description = $mdb2->quote($i18n->get('role.user.description'));
257 $rights = $mdb2->quote($rights_user);
258 $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($group_id, $name, $description, 4, $rights, 1)";
259 $affected = $mdb2->exec($sql);
260 if (is_a($affected, 'PEAR_Error'))
263 $name = $mdb2->quote($i18n->get('role.client.label'));
264 $description = $mdb2->quote($i18n->get('role.client.description'));
265 $rights = $mdb2->quote($rights_client);
266 $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($group_id, $name, $description, 16, $rights, 1)";
267 $affected = $mdb2->exec($sql);
268 if (is_a($affected, 'PEAR_Error'))
271 $name = $mdb2->quote($i18n->get('role.comanager.label'));
272 $description = $mdb2->quote($i18n->get('role.comanager.description'));
273 $rights = $mdb2->quote($rights_comanager);
274 $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($group_id, $name, $description, 68, $rights, 1)";
275 $affected = $mdb2->exec($sql);
276 if (is_a($affected, 'PEAR_Error'))
279 $name = $mdb2->quote($i18n->get('role.manager.label'));
280 $description = $mdb2->quote($i18n->get('role.manager.description'));
281 $rights = $mdb2->quote($rights_manager);
282 $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($group_id, $name, $description, 324, $rights, 1)";
283 $affected = $mdb2->exec($sql);
284 if (is_a($affected, 'PEAR_Error'))
288 $name = $mdb2->quote($i18n->get('role.supervisor.label'));
289 $description = $mdb2->quote($i18n->get('role.supervisor.description'));
290 $rights = $mdb2->quote($rights_supervisor);
291 $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($group_id, $name, $description, 12, $rights, 0)";
292 $affected = $mdb2->exec($sql);
293 if (is_a($affected, 'PEAR_Error'))
299 // getRoleByRank_1_17_44 is used in dbinstall.php and looks up a role by its rank.
300 static function getRoleByRank_1_17_44($rank, $group_id) {
302 $mdb2 = getConnection();
304 $rank = (int) $rank; // Cast to int just in case for better security.
306 $sql = "select id from tt_roles where team_id = $group_id and rank = $rank and (status = 1 or status = 0)";
307 $res = $mdb2->query($sql);
309 if (!is_a($res, 'PEAR_Error')) {
310 $val = $res->fetchRow();
317 // copyRolesToGroup copies roles from current on behalf group to another.
318 static function copyRolesToGroup($group_id) {
320 $mdb2 = getConnection();
322 $org_id = $user->org_id;
323 $columns = '(group_id, org_id, name, description, rank, rights, status)';
324 $roles = ttGroupHelper::getRoles(); // Roles in current on behalf group.
326 foreach ($roles as $role) {
327 $values = "values($group_id, $org_id".
328 ', '.$mdb2->quote($role['name']).
329 ', '.$mdb2->quote($role['description']).
330 ', '.(int)$role['rank'].
331 ', '.$mdb2->quote($role['rights']).
332 ', '.$mdb2->quote($role['status']).
334 $sql = "insert into tt_roles $columns $values";
335 $affected = $mdb2->exec($sql);
336 if (is_a($affected, 'PEAR_Error'))