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 // isClient - determines whether current user is a client.
154 function isClient() {
155 return $this->is_client;
158 // isPluginEnabled checks whether a plugin is enabled for user.
159 function isPluginEnabled($plugin)
161 return in_array($plugin, explode(',', $this->plugins));
164 // getAssignedProjects - returns an array of assigned projects.
165 function getAssignedProjects()
168 $mdb2 = getConnection();
170 // Do a query with inner join to get assigned projects.
171 $sql = "select p.id, p.name, p.description, p.tasks, upb.rate from tt_projects p
172 inner join tt_user_project_binds upb on (upb.user_id = ".$this->getActiveUser()." and upb.project_id = p.id and upb.status = 1)
173 where p.group_id = $this->group_id and p.status = 1 order by p.name";
174 $res = $mdb2->query($sql);
175 if (!is_a($res, 'PEAR_Error')) {
176 while ($val = $res->fetchRow()) {
183 // getAssignedTasks - returns an array of assigned tasks.
184 function getAssignedTasks()
186 // Start with projects;
187 $projects = $this->getAssignedProjects();
188 if (!$projects) return false;
190 // Build an array of task ids.
192 foreach($projects as $project) {
193 $one_project_tasks = $project['tasks'] ? explode(',', $project['tasks']) : array();
194 $task_ids = array_unique(array_merge($task_ids, $one_project_tasks));
196 if (!$task_ids) return false;
198 // Get task descriptions.
200 $mdb2 = getConnection();
201 $tasks = implode(',', $task_ids); // This is a comma-separated list of task ids.
203 $sql = "select id, name, description from tt_tasks".
204 " where group_id = $this->group_id and status = 1 and id in ($tasks) order by name";
205 $res = $mdb2->query($sql);
206 if (!is_a($res, 'PEAR_Error')) {
207 while ($val = $res->fetchRow()) {
214 // getAssignedClients - returns an array of clients assigned to own projects.
215 function getAssignedClients()
217 // Start with projects;
218 $projects = $this->getAssignedProjects();
219 if (!$projects) return false;
220 $assigned_project_ids = array();
221 foreach($projects as $project) {
222 $assigned_project_ids[] = $project['id'];
225 $mdb2 = getConnection();
227 // Get active clients for group.
229 $sql = "select id, name, address, projects from tt_clients where group_id = $this->group_id and status = 1";
230 $res = $mdb2->query($sql);
231 if (!is_a($res, 'PEAR_Error')) {
232 while ($val = $res->fetchRow()) {
233 $client_project_ids = $val['projects'] ? explode(',', $val['projects']) : array();
234 if (array_intersect($assigned_project_ids, $client_project_ids))
235 $clients[] = $val; // Add client if one of user projects is a client project, too.
241 // isDateLocked checks whether a specifc date is locked for modifications.
242 function isDateLocked($date)
244 if (!$this->isPluginEnabled('lk'))
245 return false; // Locking feature is disabled.
247 if (!$this->lock_spec)
248 return false; // There is no lock specification.
250 if (!$this->behalf_id && $this->can('override_own_date_lock'))
251 return false; // User is working as self and can override own date lock.
253 if ($this->behalf_id && $this->can('override_date_lock'))
254 return false; // User is working on behalf of someone else and can override date lock.
256 require_once(LIBRARY_DIR.'/tdcron/class.tdcron.php');
257 require_once(LIBRARY_DIR.'/tdcron/class.tdcron.entry.php');
259 // Calculate the last occurrence of a lock.
260 $last = tdCron::getLastOccurrence($this->lock_spec, time());
261 $lockdate = new DateAndTime(DB_DATEFORMAT, strftime('%Y-%m-%d', $last));
262 if ($date->before($lockdate))
268 // canOverridePunchMode checks whether a user can override punch mode in a situation.
269 function canOverridePunchMode()
271 if (!$this->behalf_id && !$this->can('override_own_punch_mode'))
272 return false; // User is working as self and cannot override for self.
274 if ($this->behalf_id && !$this->can('override_punch_mode'))
275 return false; // User is working on behalf of someone else and cannot override.
280 // getUsers obtains users in a group, as specififed by options.
281 function getUsers($options) {
283 $mdb2 = getConnection();
285 $skipClients = !isset($options['include_clients']);
286 $includeSelf = isset($options['include_self']);
288 $select_part = 'select u.id, u.name';
289 if (isset($options['include_login'])) $select_part .= ', u.login';
290 if (!isset($options['include_clients'])) $select_part .= ', r.rights';
291 if (isset($options['include_role'])) $select_part .= ', r.name as role_name, r.rank';
293 $from_part = ' from tt_users u';
296 if (isset($options['max_rank']) || $skipClients || isset($options['include_role']))
297 $left_joins .= ' left join tt_roles r on (u.role_id = r.id)';
299 $where_part = " where u.group_id = $this->group_id";
300 if (isset($options['status']))
301 $where_part .= ' and u.status = '.(int)$options['status'];
303 $where_part .= ' and u.status is not null';
305 $where_part .= " and (u.id = $this->id || r.rank <= ".(int)$options['max_rank'].')';
307 if (isset($options['max_rank'])) $where_part .= ' and r.rank <= '.(int)$options['max_rank'];
310 $order_part = " order by upper(u.name)";
312 $sql = $select_part.$from_part.$left_joins.$where_part.$order_part;
313 $res = $mdb2->query($sql);
314 $user_list = array();
315 if (is_a($res, 'PEAR_Error'))
318 while ($val = $res->fetchRow()) {
320 $isClient = in_array('track_own_time', explode(',', $val['rights'])) ? 0 : 1; // Clients do not have track_own_time right.
322 continue; // Skip adding clients.
327 if (isset($options['self_first'])) {
328 // Put own entry at the front.
329 $cnt = count($user_list);
330 for($i = 0; $i < $cnt; $i++) {
331 if ($user_list[$i]['id'] == $this->id) {
332 $self = $user_list[$i]; // Found self.
333 array_unshift($user_list, $self); // Put own entry at the front.
334 array_splice($user_list, $i+1, 1); // Remove duplicate.
341 // getUser function is used to manage users in group and returns user details.
342 // At the moment, the function is used for user edits and deletes.
343 function getUser($user_id) {
344 if (!$this->can('manage_users')) return false;
346 $mdb2 = getConnection();
348 $sql = "select u.id, u.name, u.login, u.role_id, u.client_id, u.status, u.rate, u.email from tt_users u".
349 " left join tt_roles r on (u.role_id = r.id)".
350 " where u.id = $user_id and u.group_id = $this->group_id and u.status is not null".
351 " and (r.rank < $this->rank or (r.rank = $this->rank and u.id = $this->id))"; // Users with lesser roles or self.
352 $res = $mdb2->query($sql);
353 if (!is_a($res, 'PEAR_Error')) {
354 $val = $res->fetchRow();
360 // checkBehalfId checks whether behalf_id is appropriate.
361 // On behalf user must be active and have lower rank.
362 function checkBehalfId() {
363 $options = array('status'=>ACTIVE,'max_rank'=>$this->rank-1);
364 $users = $this->getUsers($options);
365 foreach($users as $one_user) {
366 if ($one_user['id'] == $this->behalf_id)
372 // adjustBehalfId attempts to adjust behalf_id and behalf_name to a first found
375 // Needed for situations when user does not have do_own_something right.
376 // Example: has view_charts but does not have view_own_charts.
377 // In this case we still allow access to charts, but set behalf_id to someone else.
378 function adjustBehalfId() {
379 $options = array('status'=>ACTIVE,'max_rank'=>$this->rank-1);
380 $users = $this->getUsers($options);
381 foreach($users as $one_user) {
382 // Fake loop to access first element.
383 $this->behalf_id = $one_user['id'];
384 $this->behalf_name = $one_user['name'];
385 $_SESSION['behalf_id'] = $this->behalf_id;
386 $_SESSION['behalf_name'] = $this->behalf_name;
392 // updateGroup updates group information with new data.
393 function updateGroup($fields) {
394 if (!($this->can('manage_basic_settings') ||
395 $this->can('manage_advanced_settings') ||
396 $this->can('manage_features'))) return false;
398 $mdb2 = getConnection();
400 if (isset($fields['name'])) $name_part = ', name = '.$mdb2->quote($fields['name']);
401 if (isset($fields['currency'])) $currency_part = ', currency = '.$mdb2->quote($fields['currency']);
402 if (isset($fields['lang'])) $lang_part = ', lang = '.$mdb2->quote($fields['lang']);
403 if (isset($fields['decimal_mark'])) $decimal_mark_part = ', decimal_mark = '.$mdb2->quote($fields['decimal_mark']);
404 if (isset($fields['date_format'])) $date_format_part = ', date_format = '.$mdb2->quote($fields['date_format']);
405 if (isset($fields['time_format'])) $time_format_part = ', time_format = '.$mdb2->quote($fields['time_format']);
406 if (isset($fields['week_start'])) $week_start_part = ', week_start = '.(int) $fields['week_start'];
407 if (isset($fields['tracking_mode'])) {
408 $tracking_mode_part = ', tracking_mode = '.(int) $fields['tracking_mode'];
409 $project_required_part = ' , project_required = '.(int) $fields['project_required'];
410 $task_required_part = ' , task_required = '.(int) $fields['task_required'];
412 if (isset($fields['record_type'])) $record_type_part = ', record_type = '.(int) $fields['record_type'];
413 if (isset($fields['bcc_email'])) $bcc_email_part = ', bcc_email = '.$mdb2->quote($fields['bcc_email']);
414 if (isset($fields['allow_ip'])) $allow_ip_part = ', allow_ip = '.$mdb2->quote($fields['allow_ip']);
415 if (isset($fields['plugins'])) $plugins_part = ', plugins = '.$mdb2->quote($fields['plugins']);
416 if (isset($fields['config'])) $config_part = ', config = '.$mdb2->quote($fields['config']);
417 if (isset($fields['lock_spec'])) $lock_spec_part = ', lock_spec = '.$mdb2->quote($fields['lock_spec']);
418 if (isset($fields['workday_minutes'])) $workday_minutes_part = ', workday_minutes = '.$mdb2->quote($fields['workday_minutes']);
419 $modified_part = ', modified = now(), modified_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', modified_by = '.$mdb2->quote($this->id);
421 $parts = trim($name_part.$currency_part.$lang_part.$decimal_mark_part.$date_format_part.
422 $time_format_part.$week_start_part.$tracking_mode_part.$task_required_part.$project_required_part.$record_type_part.
423 $bcc_email_part.$allow_ip_part.$plugins_part.$config_part.$lock_spec_part.$workday_minutes_part.$modified_part, ',');
425 $sql = "update tt_groups set $parts where id = $this->group_id";
426 $affected = $mdb2->exec($sql);
427 if (is_a($affected, 'PEAR_Error')) return false;
432 // markUserDeleted marks a user in group as deleted.
433 function markUserDeleted($user_id) {
434 if (!$this->can('manage_users') || $this->id == $user_id)
437 // Make sure we operate on a legit user.
438 $user_details = $this->getUser($user_id);
439 if (!$user_details) return false;
441 $mdb2 = getConnection();
443 // Mark user to project binds as deleted.
444 $sql = "update tt_user_project_binds set status = NULL where user_id = $user_id";
445 $affected = $mdb2->exec($sql);
446 if (is_a($affected, 'PEAR_Error'))
449 // Mark user favorite reports as deleted.
450 $sql = "update tt_fav_reports set status = NULL where user_id = $user_id";
451 $affected = $mdb2->exec($sql);
452 if (is_a($affected, 'PEAR_Error'))
455 // Mark user as deleted.
456 $sql = "update tt_users set status = NULL where id = $user_id and group_id = ".$this->group_id;
457 $affected = $mdb2->exec($sql);
458 if (is_a($affected, 'PEAR_Error'))
464 // enablePlugin either enables or disables a specific plugin for group.
465 function enablePlugin($plugin, $enable = true)
467 if (!$this->can('manage_advanced_settings'))
468 return false; // Note: enablePlugin is currently only used on week_view.php.
469 // So, it's not really a plugin we are enabling, but rather week view display options.
470 // Therefore, a check for manage_advanced_settings, not manage_features.
472 $plugin_array = explode(',', $this->plugins);
473 if ($enable && !in_array($plugin, $plugin_array))
474 $plugin_array[] = $plugin; // Add plugin to array.
476 if (!$enable && in_array($plugin, $plugin_array)) {
477 $key = array_search($plugin, $plugin_array);
479 unset($plugin_array[$key]); // Remove plugin from array.
482 $plugins = implode(',', $plugin_array);
483 if ($plugins != $this->plugins) {
484 if (!$this->updateGroup(array('plugins' => $plugins)))
486 $this->plugins = $plugins;