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 // canManageTeam - determines whether current user is manager or co-manager.
159 // This is a legacy function that we are getting rid of by replacing with rights check.
160 function canManageTeam() {
161 return $this->can('manage_users'); // By default this is assigned to co-managers (an managers).
162 // Which is sufficient for now until we refactor all calls
163 // to this function and then remove it.
166 // isPluginEnabled checks whether a plugin is enabled for user.
167 function isPluginEnabled($plugin)
169 return in_array($plugin, explode(',', $this->plugins));
172 // getAssignedProjects - returns an array of assigned projects.
173 function getAssignedProjects()
176 $mdb2 = getConnection();
178 // Do a query with inner join to get assigned projects.
179 $sql = "select p.id, p.name, p.description, p.tasks, upb.rate from tt_projects p
180 inner join tt_user_project_binds upb on (upb.user_id = ".$this->getActiveUser()." and upb.project_id = p.id and upb.status = 1)
181 where p.group_id = $this->group_id and p.status = 1 order by p.name";
182 $res = $mdb2->query($sql);
183 if (!is_a($res, 'PEAR_Error')) {
184 while ($val = $res->fetchRow()) {
191 // getAssignedTasks - returns an array of assigned tasks.
192 function getAssignedTasks()
194 // Start with projects;
195 $projects = $this->getAssignedProjects();
196 if (!$projects) return false;
198 // Build an array of task ids.
200 foreach($projects as $project) {
201 $one_project_tasks = $project['tasks'] ? explode(',', $project['tasks']) : array();
202 $task_ids = array_unique(array_merge($task_ids, $one_project_tasks));
204 if (!$task_ids) return false;
206 // Get task descriptions.
208 $mdb2 = getConnection();
209 $tasks = implode(',', $task_ids); // This is a comma-separated list of task ids.
211 $sql = "select id, name, description from tt_tasks".
212 " where group_id = $this->group_id and status = 1 and id in ($tasks) order by name";
213 $res = $mdb2->query($sql);
214 if (!is_a($res, 'PEAR_Error')) {
215 while ($val = $res->fetchRow()) {
222 // getAssignedClients - returns an array of clients assigned to own projects.
223 function getAssignedClients()
225 // Start with projects;
226 $projects = $this->getAssignedProjects();
227 if (!$projects) return false;
228 $assigned_project_ids = array();
229 foreach($projects as $project) {
230 $assigned_project_ids[] = $project['id'];
233 $mdb2 = getConnection();
235 // Get active clients for group.
237 $sql = "select id, name, address, projects from tt_clients where group_id = $this->group_id and status = 1";
238 $res = $mdb2->query($sql);
239 if (!is_a($res, 'PEAR_Error')) {
240 while ($val = $res->fetchRow()) {
241 $client_project_ids = $val['projects'] ? explode(',', $val['projects']) : array();
242 if (array_intersect($assigned_project_ids, $client_project_ids))
243 $clients[] = $val; // Add client if one of user projects is a client project, too.
249 // isDateLocked checks whether a specifc date is locked for modifications.
250 function isDateLocked($date)
252 if (!$this->isPluginEnabled('lk'))
253 return false; // Locking feature is disabled.
255 if (!$this->lock_spec)
256 return false; // There is no lock specification.
258 if (!$this->behalf_id && $this->can('override_own_date_lock'))
259 return false; // User is working as self and can override own date lock.
261 if ($this->behalf_id && $this->can('override_date_lock'))
262 return false; // User is working on behalf of someone else and can override date lock.
264 require_once(LIBRARY_DIR.'/tdcron/class.tdcron.php');
265 require_once(LIBRARY_DIR.'/tdcron/class.tdcron.entry.php');
267 // Calculate the last occurrence of a lock.
268 $last = tdCron::getLastOccurrence($this->lock_spec, time());
269 $lockdate = new DateAndTime(DB_DATEFORMAT, strftime('%Y-%m-%d', $last));
270 if ($date->before($lockdate))
276 // canOverridePunchMode checks whether a user can override punch mode in a situation.
277 function canOverridePunchMode()
279 if (!$this->behalf_id && !$this->can('override_own_punch_mode'))
280 return false; // User is working as self and cannot override for self.
282 if ($this->behalf_id && !$this->can('override_punch_mode'))
283 return false; // User is working on behalf of someone else and cannot override.
288 // getUsers obtains users in a group, as specififed by options.
289 function getUsers($options) {
291 $mdb2 = getConnection();
293 $skipClients = !isset($options['include_clients']);
294 $includeSelf = isset($options['include_self']);
296 $select_part = 'select u.id, u.name';
297 if (isset($options['include_login'])) $select_part .= ', u.login';
298 if (!isset($options['include_clients'])) $select_part .= ', r.rights';
299 if (isset($options['include_role'])) $select_part .= ', r.name as role_name, r.rank';
301 $from_part = ' from tt_users u';
304 if (isset($options['max_rank']) || $skipClients || isset($options['include_role']))
305 $left_joins .= ' left join tt_roles r on (u.role_id = r.id)';
307 $where_part = " where u.group_id = $this->group_id";
308 if (isset($options['status']))
309 $where_part .= ' and u.status = '.(int)$options['status'];
311 $where_part .= ' and u.status is not null';
313 $where_part .= " and (u.id = $this->id || r.rank <= ".(int)$options['max_rank'].')';
315 if (isset($options['max_rank'])) $where_part .= ' and r.rank <= '.(int)$options['max_rank'];
318 $order_part = " order by upper(u.name)";
320 $sql = $select_part.$from_part.$left_joins.$where_part.$order_part;
321 $res = $mdb2->query($sql);
322 $user_list = array();
323 if (is_a($res, 'PEAR_Error'))
326 while ($val = $res->fetchRow()) {
328 $isClient = in_array('track_own_time', explode(',', $val['rights'])) ? 0 : 1; // Clients do not have track_own_time right.
330 continue; // Skip adding clients.
335 if (isset($options['self_first'])) {
336 // Put own entry at the front.
337 $cnt = count($user_list);
338 for($i = 0; $i < $cnt; $i++) {
339 if ($user_list[$i]['id'] == $this->id) {
340 $self = $user_list[$i]; // Found self.
341 array_unshift($user_list, $self); // Put own entry at the front.
342 array_splice($user_list, $i+1, 1); // Remove duplicate.
349 // getUser function is used to manage users in group and returns user details.
350 // At the moment, the function is used for user edits and deletes.
351 function getUser($user_id) {
352 if (!$this->can('manage_users')) return false;
354 $mdb2 = getConnection();
356 $sql = "select u.id, u.name, u.login, u.role_id, u.client_id, u.status, u.rate, u.email from tt_users u".
357 " left join tt_roles r on (u.role_id = r.id)".
358 " where u.id = $user_id and u.group_id = $this->group_id and u.status is not null".
359 " and (r.rank < $this->rank or (r.rank = $this->rank and u.id = $this->id))"; // Users with lesser roles or self.
360 $res = $mdb2->query($sql);
361 if (!is_a($res, 'PEAR_Error')) {
362 $val = $res->fetchRow();
368 // checkBehalfId checks whether behalf_id is appropriate.
369 // On behalf user must be active and have lower rank.
370 function checkBehalfId() {
371 $options = array('status'=>ACTIVE,'max_rank'=>$this->rank-1);
372 $users = $this->getUsers($options);
373 foreach($users as $one_user) {
374 if ($one_user['id'] == $this->behalf_id)
380 // adjustBehalfId attempts to adjust behalf_id and behalf_name to a first found
383 // Needed for situations when user does not have do_own_something right.
384 // Example: has view_charts but does not have view_own_charts.
385 // In this case we still allow access to charts, but set behalf_id to someone else.
386 function adjustBehalfId() {
387 $options = array('status'=>ACTIVE,'max_rank'=>$this->rank-1);
388 $users = $this->getUsers($options);
389 foreach($users as $one_user) {
390 // Fake loop to access first element.
391 $this->behalf_id = $one_user['id'];
392 $this->behalf_name = $one_user['name'];
393 $_SESSION['behalf_id'] = $this->behalf_id;
394 $_SESSION['behalf_name'] = $this->behalf_name;
400 // updateGroup updates group information with new data.
401 function updateGroup($fields) {
402 if (!($this->can('manage_basic_settings') ||
403 $this->can('manage_advanced_settings') ||
404 $this->can('manage_features'))) return false;
406 $mdb2 = getConnection();
408 if (isset($fields['name'])) $name_part = ', name = '.$mdb2->quote($fields['name']);
409 if (isset($fields['currency'])) $currency_part = ', currency = '.$mdb2->quote($fields['currency']);
410 if (isset($fields['lang'])) $lang_part = ', lang = '.$mdb2->quote($fields['lang']);
411 if (isset($fields['decimal_mark'])) $decimal_mark_part = ', decimal_mark = '.$mdb2->quote($fields['decimal_mark']);
412 if (isset($fields['date_format'])) $date_format_part = ', date_format = '.$mdb2->quote($fields['date_format']);
413 if (isset($fields['time_format'])) $time_format_part = ', time_format = '.$mdb2->quote($fields['time_format']);
414 if (isset($fields['week_start'])) $week_start_part = ', week_start = '.(int) $fields['week_start'];
415 if (isset($fields['tracking_mode'])) {
416 $tracking_mode_part = ', tracking_mode = '.(int) $fields['tracking_mode'];
417 $project_required_part = ' , project_required = '.(int) $fields['project_required'];
418 $task_required_part = ' , task_required = '.(int) $fields['task_required'];
420 if (isset($fields['record_type'])) $record_type_part = ', record_type = '.(int) $fields['record_type'];
421 if (isset($fields['bcc_email'])) $bcc_email_part = ', bcc_email = '.$mdb2->quote($fields['bcc_email']);
422 if (isset($fields['allow_ip'])) $allow_ip_part = ', allow_ip = '.$mdb2->quote($fields['allow_ip']);
423 if (isset($fields['plugins'])) $plugins_part = ', plugins = '.$mdb2->quote($fields['plugins']);
424 if (isset($fields['config'])) $config_part = ', config = '.$mdb2->quote($fields['config']);
425 if (isset($fields['lock_spec'])) $lock_spec_part = ', lock_spec = '.$mdb2->quote($fields['lock_spec']);
426 if (isset($fields['workday_minutes'])) $workday_minutes_part = ', workday_minutes = '.$mdb2->quote($fields['workday_minutes']);
427 $modified_part = ', modified = now(), modified_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', modified_by = '.$mdb2->quote($this->id);
429 $parts = trim($name_part.$currency_part.$lang_part.$decimal_mark_part.$date_format_part.
430 $time_format_part.$week_start_part.$tracking_mode_part.$task_required_part.$project_required_part.$record_type_part.
431 $bcc_email_part.$allow_ip_part.$plugins_part.$config_part.$lock_spec_part.$workday_minutes_part.$modified_part, ',');
433 $sql = "update tt_groups set $parts where id = $this->group_id";
434 $affected = $mdb2->exec($sql);
435 if (is_a($affected, 'PEAR_Error')) return false;
440 // markUserDeleted marks a user in group as deleted.
441 function markUserDeleted($user_id) {
442 if (!$this->can('manage_users') || $this->id == $user_id)
445 // Make sure we operate on a legit user.
446 $user_details = $this->getUser($user_id);
447 if (!$user_details) return false;
449 $mdb2 = getConnection();
451 // Mark user to project binds as deleted.
452 $sql = "update tt_user_project_binds set status = NULL where user_id = $user_id";
453 $affected = $mdb2->exec($sql);
454 if (is_a($affected, 'PEAR_Error'))
457 // Mark user favorite reports as deleted.
458 $sql = "update tt_fav_reports set status = NULL where user_id = $user_id";
459 $affected = $mdb2->exec($sql);
460 if (is_a($affected, 'PEAR_Error'))
463 // Mark user as deleted.
464 $sql = "update tt_users set status = NULL where id = $user_id and group_id = ".$this->group_id;
465 $affected = $mdb2->exec($sql);
466 if (is_a($affected, 'PEAR_Error'))
472 // enablePlugin either enables or disables a specific plugin for group.
473 function enablePlugin($plugin, $enable = true)
475 if (!$this->can('manage_advanced_settings'))
476 return false; // Note: enablePlugin is currently only used on week_view.php.
477 // So, it's not really a plugin we are enabling, but rather week view display options.
478 // Therefore, a check for manage_advanced_settings, not manage_features.
480 $plugin_array = explode(',', $this->plugins);
481 if ($enable && !in_array($plugin, $plugin_array))
482 $plugin_array[] = $plugin; // Add plugin to array.
484 if (!$enable && in_array($plugin, $plugin_array)) {
485 $key = array_search($plugin, $plugin_array);
487 unset($plugin_array[$key]); // Remove plugin from array.
490 $plugins = implode(',', $plugin_array);
491 if ($plugins != $this->plugins) {
492 if (!$this->updateGroup(array('plugins' => $plugins)))
494 $this->plugins = $plugins;