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 // +----------------------------------------------------------------------+
30 var $login = null; // User login.
31 var $name = null; // User name.
32 var $id = null; // User id.
33 var $group_id = null; // Group id.
34 var $role_id = null; // Role id.
35 var $role_name = null; // Role name.
36 var $rank = null; // User role rank.
37 var $client_id = null; // Client id for client user role.
38 var $behalf_id = null; // User id, on behalf of whom we are working.
39 var $behalf_name = null; // User name, on behalf of whom we are working.
40 var $email = null; // User email.
41 var $lang = null; // Language.
42 var $decimal_mark = null; // Decimal separator.
43 var $date_format = null; // Date format.
44 var $time_format = null; // Time format.
45 var $week_start = 0; // Week start day.
46 var $show_holidays = 0; // Whether to show holidays in calendar.
47 var $tracking_mode = 0; // Tracking mode.
48 var $project_required = 0; // Whether project selection is required on time entires.
49 var $task_required = 0; // Whether task selection is required on time entires.
50 var $record_type = 0; // Record type (duration vs start and finish, or both).
51 var $punch_mode = 0; // Whether punch mode is enabled for user.
52 var $allow_overlap = 0; // Whether to allow overlapping time entries.
53 var $future_entries = 0; // Whether to allow creating future entries.
54 var $uncompleted_indicators = 0; // Uncompleted time entry indicators (show nowhere or on users page).
55 var $bcc_email = null; // Bcc email.
56 var $allow_ip = null; // Specification from where user is allowed access.
57 var $password_complexity = null; // Password complexity example.
58 var $currency = null; // Currency.
59 var $plugins = null; // Comma-separated list of enabled plugins.
60 var $config = null; // Comma-separated list of miscellaneous config options.
61 var $group = null; // Group name.
62 var $custom_logo = 0; // Whether to use a custom logo for group.
63 var $lock_spec = null; // Cron specification for record locking.
64 var $workday_minutes = 480; // Number of work minutes in a regular day.
65 var $rights = array(); // An array of user rights such as 'track_own_time', etc.
66 var $is_client = false; // Whether user is a client as determined by missing 'track_own_time' right.
69 function __construct($login, $id = null) {
70 if (!$login && !$id) {
71 // nothing to initialize
75 $mdb2 = getConnection();
77 $sql = "SELECT u.id, u.login, u.name, u.group_id, u.role_id, r.rank, r.name as role_name, r.rights, u.client_id, u.email, g.name as group_name,
78 g.currency, g.lang, g.decimal_mark, g.date_format, g.time_format, g.week_start,
79 g.tracking_mode, g.project_required, g.task_required, g.record_type,
80 g.bcc_email, g.allow_ip, g.password_complexity, g.plugins, g.config, g.lock_spec, g.workday_minutes, g.custom_logo
81 FROM tt_users u LEFT JOIN tt_groups g ON (u.group_id = g.id) LEFT JOIN tt_roles r on (r.id = u.role_id) WHERE ";
85 $sql .= "u.login = ".$mdb2->quote($login);
86 $sql .= " AND u.status = 1";
88 $res = $mdb2->query($sql);
89 if (is_a($res, 'PEAR_Error')) {
93 $val = $res->fetchRow();
95 $this->login = $val['login'];
96 $this->name = $val['name'];
97 $this->id = $val['id'];
98 $this->group_id = $val['group_id'];
99 $this->role_id = $val['role_id'];
100 $this->role_name = $val['role_name'];
101 $this->rights = explode(',', $val['rights']);
102 $this->is_client = !in_array('track_own_time', $this->rights);
103 $this->rank = $val['rank'];
104 $this->client_id = $val['client_id'];
105 $this->email = $val['email'];
106 $this->lang = $val['lang'];
107 $this->decimal_mark = $val['decimal_mark'];
108 $this->date_format = $val['date_format'];
109 $this->time_format = $val['time_format'];
110 $this->week_start = $val['week_start'];
111 $this->tracking_mode = $val['tracking_mode'];
112 $this->project_required = $val['project_required'];
113 $this->task_required = $val['task_required'];
114 $this->record_type = $val['record_type'];
115 $this->bcc_email = $val['bcc_email'];
116 $this->allow_ip = $val['allow_ip'];
117 $this->password_complexity = $val['password_complexity'];
118 $this->group = $val['group_name'];
119 $this->currency = $val['currency'];
120 $this->plugins = $val['plugins'];
121 $this->lock_spec = $val['lock_spec'];
122 $this->workday_minutes = $val['workday_minutes'];
123 $this->custom_logo = $val['custom_logo'];
125 $this->config = $val['config'];
126 $config_array = explode(',', $this->config);
128 // Set user config options.
129 $this->show_holidays = in_array('show_holidays', $config_array);
130 $this->punch_mode = in_array('punch_mode', $config_array);
131 $this->allow_overlap = in_array('allow_overlap', $config_array);
132 $this->future_entries = in_array('future_entries', $config_array);
133 $this->uncompleted_indicators = in_array('uncompleted_indicators', $config_array);
135 // Set "on behalf" id and name.
136 if (isset($_SESSION['behalf_id'])) {
137 $this->behalf_id = $_SESSION['behalf_id'];
138 $this->behalf_name = $_SESSION['behalf_name'];
143 // The getActiveUser returns user id on behalf of whom the current user is operating.
144 function getActiveUser() {
145 return ($this->behalf_id ? $this->behalf_id : $this->id);
148 // can - determines whether user has a right to do something.
149 function can($do_something) {
150 return in_array($do_something, $this->rights);
153 // isManager - determines whether current user is group manager.
154 // This is a legacy function that we are getting rid of by replacing with rights check.
155 function isManager() {
156 return $this->can('export_data'); // By default this is assigned to managers but not co-managers.
157 // Which is sufficient for now until we refactor all calls
158 // to this function and then remove it.
161 // isClient - determines whether current user is a client.
162 function isClient() {
163 return $this->is_client;
166 // canManageTeam - determines whether current user is manager or co-manager.
167 // This is a legacy function that we are getting rid of by replacing with rights check.
168 function canManageTeam() {
169 return $this->can('manage_users'); // By default this is assigned to co-managers (an managers).
170 // Which is sufficient for now until we refactor all calls
171 // to this function and then remove it.
174 // isPluginEnabled checks whether a plugin is enabled for user.
175 function isPluginEnabled($plugin)
177 return in_array($plugin, explode(',', $this->plugins));
180 // getAssignedProjects - returns an array of assigned projects.
181 function getAssignedProjects()
184 $mdb2 = getConnection();
186 // Do a query with inner join to get assigned projects.
187 $sql = "select p.id, p.name, p.description, p.tasks, upb.rate from tt_projects p
188 inner join tt_user_project_binds upb on (upb.user_id = ".$this->getActiveUser()." and upb.project_id = p.id and upb.status = 1)
189 where p.group_id = $this->group_id and p.status = 1 order by p.name";
190 $res = $mdb2->query($sql);
191 if (!is_a($res, 'PEAR_Error')) {
192 while ($val = $res->fetchRow()) {
199 // getAssignedTasks - returns an array of assigned tasks.
200 function getAssignedTasks()
202 // Start with projects;
203 $projects = $this->getAssignedProjects();
204 if (!$projects) return false;
206 // Build an array of task ids.
208 foreach($projects as $project) {
209 $one_project_tasks = $project['tasks'] ? explode(',', $project['tasks']) : array();
210 $task_ids = array_unique(array_merge($task_ids, $one_project_tasks));
212 if (!$task_ids) return false;
214 // Get task descriptions.
216 $mdb2 = getConnection();
217 $tasks = implode(',', $task_ids); // This is a comma-separated list of task ids.
219 $sql = "select id, name, description from tt_tasks".
220 " where group_id = $this->group_id and status = 1 and id in ($tasks) order by name";
221 $res = $mdb2->query($sql);
222 if (!is_a($res, 'PEAR_Error')) {
223 while ($val = $res->fetchRow()) {
230 // getAssignedClients - returns an array of clients assigned to own projects.
231 function getAssignedClients()
233 // Start with projects;
234 $projects = $this->getAssignedProjects();
235 if (!$projects) return false;
236 $assigned_project_ids = array();
237 foreach($projects as $project) {
238 $assigned_project_ids[] = $project['id'];
241 $mdb2 = getConnection();
243 // Get active clients for group.
245 $sql = "select id, name, address, projects from tt_clients where group_id = $this->group_id and status = 1";
246 $res = $mdb2->query($sql);
247 if (!is_a($res, 'PEAR_Error')) {
248 while ($val = $res->fetchRow()) {
249 $client_project_ids = $val['projects'] ? explode(',', $val['projects']) : array();
250 if (array_intersect($assigned_project_ids, $client_project_ids))
251 $clients[] = $val; // Add client if one of user projects is a client project, too.
257 // isDateLocked checks whether a specifc date is locked for modifications.
258 function isDateLocked($date)
260 if (!$this->isPluginEnabled('lk'))
261 return false; // Locking feature is disabled.
263 if (!$this->lock_spec)
264 return false; // There is no lock specification.
266 if (!$this->behalf_id && $this->can('override_own_date_lock'))
267 return false; // User is working as self and can override own date lock.
269 if ($this->behalf_id && $this->can('override_date_lock'))
270 return false; // User is working on behalf of someone else and can override date lock.
272 require_once(LIBRARY_DIR.'/tdcron/class.tdcron.php');
273 require_once(LIBRARY_DIR.'/tdcron/class.tdcron.entry.php');
275 // Calculate the last occurrence of a lock.
276 $last = tdCron::getLastOccurrence($this->lock_spec, time());
277 $lockdate = new DateAndTime(DB_DATEFORMAT, strftime('%Y-%m-%d', $last));
278 if ($date->before($lockdate))
284 // canOverridePunchMode checks whether a user can override punch mode in a situation.
285 function canOverridePunchMode()
287 if (!$this->behalf_id && !$this->can('override_own_punch_mode'))
288 return false; // User is working as self and cannot override for self.
290 if ($this->behalf_id && !$this->can('override_punch_mode'))
291 return false; // User is working on behalf of someone else and cannot override.
296 // getUsers obtains users in a group, as specififed by options.
297 function getUsers($options) {
299 $mdb2 = getConnection();
301 $skipClients = !isset($options['include_clients']);
302 $includeSelf = isset($options['include_self']);
304 $select_part = 'select u.id, u.name';
305 if (isset($options['include_login'])) $select_part .= ', u.login';
306 if (!isset($options['include_clients'])) $select_part .= ', r.rights';
307 if (isset($options['include_role'])) $select_part .= ', r.name as role_name, r.rank';
309 $from_part = ' from tt_users u';
312 if (isset($options['max_rank']) || $skipClients || isset($options['include_role']))
313 $left_joins .= ' left join tt_roles r on (u.role_id = r.id)';
315 $where_part = " where u.group_id = $this->group_id";
316 if (isset($options['status']))
317 $where_part .= ' and u.status = '.(int)$options['status'];
319 $where_part .= ' and u.status is not null';
321 $where_part .= " and (u.id = $this->id || r.rank <= ".(int)$options['max_rank'].')';
323 if (isset($options['max_rank'])) $where_part .= ' and r.rank <= '.(int)$options['max_rank'];
326 $order_part = " order by upper(u.name)";
328 $sql = $select_part.$from_part.$left_joins.$where_part.$order_part;
329 $res = $mdb2->query($sql);
330 $user_list = array();
331 if (is_a($res, 'PEAR_Error'))
334 while ($val = $res->fetchRow()) {
336 $isClient = in_array('track_own_time', explode(',', $val['rights'])) ? 0 : 1; // Clients do not have track_own_time right.
338 continue; // Skip adding clients.
343 if (isset($options['self_first'])) {
344 // Put own entry at the front.
345 $cnt = count($user_list);
346 for($i = 0; $i < $cnt; $i++) {
347 if ($user_list[$i]['id'] == $this->id) {
348 $self = $user_list[$i]; // Found self.
349 array_unshift($user_list, $self); // Put own entry at the front.
350 array_splice($user_list, $i+1, 1); // Remove duplicate.
357 // getUser function is used to manage users in group and returns user details.
358 // At the moment, the function is used for user edits and deletes.
359 function getUser($user_id) {
360 if (!$this->can('manage_users')) return false;
362 $mdb2 = getConnection();
364 $sql = "select u.id, u.name, u.login, u.role_id, u.status, u.rate, u.email from tt_users u".
365 " left join tt_roles r on (u.role_id = r.id)".
366 " where u.id = $user_id and u.group_id = $this->group_id and u.status is not null".
367 " and (r.rank < $this->rank or (r.rank = $this->rank and u.id = $this->id))"; // Users with lesser roles or self.
368 $res = $mdb2->query($sql);
369 if (!is_a($res, 'PEAR_Error')) {
370 $val = $res->fetchRow();
376 // checkBehalfId checks whether behalf_id is appropriate.
377 // On behalf user must be active and have lower rank.
378 function checkBehalfId() {
379 $options = array('status'=>ACTIVE,'max_rank'=>$this->rank-1);
380 $users = $this->getUsers($options);
381 foreach($users as $one_user) {
382 if ($one_user['id'] == $this->behalf_id)
388 // adjustBehalfId attempts to adjust behalf_id and behalf_name to a first found
391 // Needed for situations when user does not have do_own_something right.
392 // Example: has view_charts but does not have view_own_charts.
393 // In this case we still allow access to charts, but set behalf_id to someone else.
394 function adjustBehalfId() {
395 $options = array('status'=>ACTIVE,'max_rank'=>$this->rank-1);
396 $users = $this->getUsers($options);
397 foreach($users as $one_user) {
398 // Fake loop to access first element.
399 $this->behalf_id = $one_user['id'];
400 $this->behalf_name = $one_user['name'];
401 $_SESSION['behalf_id'] = $this->behalf_id;
402 $_SESSION['behalf_name'] = $this->behalf_name;
408 // updateGroup updates group information with new data.
409 function updateGroup($fields) {
410 if (!($this->can('manage_basic_settings') ||
411 $this->can('manage_advanced_settings') ||
412 $this->can('manage_features'))) return false;
414 $mdb2 = getConnection();
416 if (isset($fields['name'])) $name_part = ', name = '.$mdb2->quote($fields['name']);
417 if (isset($fields['currency'])) $currency_part = ', currency = '.$mdb2->quote($fields['currency']);
418 if (isset($fields['lang'])) $lang_part = ', lang = '.$mdb2->quote($fields['lang']);
419 if (isset($fields['decimal_mark'])) $decimal_mark_part = ', decimal_mark = '.$mdb2->quote($fields['decimal_mark']);
420 if (isset($fields['date_format'])) $date_format_part = ', date_format = '.$mdb2->quote($fields['date_format']);
421 if (isset($fields['time_format'])) $time_format_part = ', time_format = '.$mdb2->quote($fields['time_format']);
422 if (isset($fields['week_start'])) $week_start_part = ', week_start = '.(int) $fields['week_start'];
423 if (isset($fields['tracking_mode'])) {
424 $tracking_mode_part = ', tracking_mode = '.(int) $fields['tracking_mode'];
425 $project_required_part = ' , project_required = '.(int) $fields['project_required'];
426 $task_required_part = ' , task_required = '.(int) $fields['task_required'];
428 if (isset($fields['record_type'])) $record_type_part = ', record_type = '.(int) $fields['record_type'];
429 if (isset($fields['bcc_email'])) $bcc_email_part = ', bcc_email = '.$mdb2->quote($fields['bcc_email']);
430 if (isset($fields['allow_ip'])) $allow_ip_part = ', allow_ip = '.$mdb2->quote($fields['allow_ip']);
431 if (isset($fields['plugins'])) $plugins_part = ', plugins = '.$mdb2->quote($fields['plugins']);
432 if (isset($fields['config'])) $config_part = ', config = '.$mdb2->quote($fields['config']);
433 if (isset($fields['lock_spec'])) $lock_spec_part = ', lock_spec = '.$mdb2->quote($fields['lock_spec']);
434 if (isset($fields['workday_minutes'])) $workday_minutes_part = ', workday_minutes = '.$mdb2->quote($fields['workday_minutes']);
435 $modified_part = ', modified = now(), modified_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', modified_by = '.$mdb2->quote($this->id);
437 $parts = trim($name_part.$currency_part.$lang_part.$decimal_mark_part.$date_format_part.
438 $time_format_part.$week_start_part.$tracking_mode_part.$task_required_part.$project_required_part.$record_type_part.
439 $bcc_email_part.$allow_ip_part.$plugins_part.$config_part.$lock_spec_part.$workday_minutes_part.$modified_part, ',');
441 $sql = "update tt_groups set $parts where id = $this->group_id";
442 $affected = $mdb2->exec($sql);
443 if (is_a($affected, 'PEAR_Error')) return false;
448 // markUserDeleted marks a user in group as deleted.
449 function markUserDeleted($user_id) {
450 if (!$this->can('manage_users') || $this->id == $user_id)
453 // Make sure we operate on a legit user.
454 $user_details = $this->getUser($user_id);
455 if (!$user_details) return false;
457 $mdb2 = getConnection();
459 // Mark user to project binds as deleted.
460 $sql = "update tt_user_project_binds set status = NULL where user_id = $user_id";
461 $affected = $mdb2->exec($sql);
462 if (is_a($affected, 'PEAR_Error'))
465 // Mark user favorite reports as deleted.
466 $sql = "update tt_fav_reports set status = NULL where user_id = $user_id";
467 $affected = $mdb2->exec($sql);
468 if (is_a($affected, 'PEAR_Error'))
471 // Mark user as deleted.
472 $sql = "update tt_users set status = NULL where id = $user_id and group_id = ".$this->group_id;
473 $affected = $mdb2->exec($sql);
474 if (is_a($affected, 'PEAR_Error'))
480 // enablePlugin either enables or disables a specific plugin for group.
481 function enablePlugin($plugin, $enable = true)
483 if (!$this->can('manage_advanced_settings'))
484 return false; // Note: enablePlugin is currently only used on week_view.php.
485 // So, it's not really a plugin we are enabling, but rather week view display options.
486 // Therefore, a check for manage_advanced_settings, not manage_features.
488 $plugin_array = explode(',', $this->plugins);
489 if ($enable && !in_array($plugin, $plugin_array))
490 $plugin_array[] = $plugin; // Add plugin to array.
492 if (!$enable && in_array($plugin, $plugin_array)) {
493 $key = array_search($plugin, $plugin_array);
495 unset($plugin_array[$key]); // Remove plugin from array.
498 $plugins = implode(',', $plugin_array);
499 if ($plugins != $this->plugins) {
500 if (!$this->updateGroup(array('plugins' => $plugins)))
502 $this->plugins = $plugins;