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 // isAdmin - determines whether current user is admin.
155 return $this->can('administer_site');
158 // isManager - determines whether current user is group manager.
159 // This is a legacy function that we are getting rid of by replacing with rights check.
160 function isManager() {
161 return $this->can('export_data'); // By default this is assigned to managers but not co-managers.
162 // Which is sufficient for now until we refactor all calls
163 // to this function and then remove it.
166 // isCoManager - determines whether current user is group comanager.
167 // This is a legacy function that we are getting rid of by replacing with rights check.
168 function isCoManager() {
169 return ($this->can('manage_users') && !$this->can('export_data'));
172 // isClient - determines whether current user is a client.
173 function isClient() {
174 return $this->is_client;
177 // canManageTeam - determines whether current user is manager or co-manager.
178 // This is a legacy function that we are getting rid of by replacing with rights check.
179 function canManageTeam() {
180 return $this->can('manage_users'); // By default this is assigned to co-managers (an managers).
181 // Which is sufficient for now until we refactor all calls
182 // to this function and then remove it.
185 // isPluginEnabled checks whether a plugin is enabled for user.
186 function isPluginEnabled($plugin)
188 return in_array($plugin, explode(',', $this->plugins));
191 // getAssignedProjects - returns an array of assigned projects.
192 function getAssignedProjects()
195 $mdb2 = getConnection();
197 // Do a query with inner join to get assigned projects.
198 $sql = "select p.id, p.name, p.description, p.tasks, upb.rate from tt_projects p
199 inner join tt_user_project_binds upb on (upb.user_id = ".$this->getActiveUser()." and upb.project_id = p.id and upb.status = 1)
200 where p.group_id = $this->group_id and p.status = 1 order by p.name";
201 $res = $mdb2->query($sql);
202 if (!is_a($res, 'PEAR_Error')) {
203 while ($val = $res->fetchRow()) {
210 // getAssignedTasks - returns an array of assigned tasks.
211 function getAssignedTasks()
213 // Start with projects;
214 $projects = $this->getAssignedProjects();
215 if (!$projects) return false;
217 // Build an array of task ids.
219 foreach($projects as $project) {
220 $one_project_tasks = $project['tasks'] ? explode(',', $project['tasks']) : array();
221 $task_ids = array_unique(array_merge($task_ids, $one_project_tasks));
223 if (!$task_ids) return false;
225 // Get task descriptions.
227 $mdb2 = getConnection();
228 $tasks = implode(',', $task_ids); // This is a comma-separated list of task ids.
230 $sql = "select id, name, description from tt_tasks".
231 " where group_id = $this->group_id and status = 1 and id in ($tasks) order by name";
232 $res = $mdb2->query($sql);
233 if (!is_a($res, 'PEAR_Error')) {
234 while ($val = $res->fetchRow()) {
241 // getAssignedClients - returns an array of clients assigned to own projects.
242 function getAssignedClients()
244 // Start with projects;
245 $projects = $this->getAssignedProjects();
246 if (!$projects) return false;
247 $assigned_project_ids = array();
248 foreach($projects as $project) {
249 $assigned_project_ids[] = $project['id'];
252 $mdb2 = getConnection();
254 // Get active clients for group.
256 $sql = "select id, name, address, projects from tt_clients where group_id = $this->group_id and status = 1";
257 $res = $mdb2->query($sql);
258 if (!is_a($res, 'PEAR_Error')) {
259 while ($val = $res->fetchRow()) {
260 $client_project_ids = $val['projects'] ? explode(',', $val['projects']) : array();
261 if (array_intersect($assigned_project_ids, $client_project_ids))
262 $clients[] = $val; // Add client if one of user projects is a client project, too.
268 // isDateLocked checks whether a specifc date is locked for modifications.
269 function isDateLocked($date)
271 if (!$this->isPluginEnabled('lk'))
272 return false; // Locking feature is disabled.
274 if (!$this->lock_spec)
275 return false; // There is no lock specification.
277 if (!$this->behalf_id && $this->can('override_own_date_lock'))
278 return false; // User is working as self and can override own date lock.
280 if ($this->behalf_id && $this->can('override_date_lock'))
281 return false; // User is working on behalf of someone else and can override date lock.
283 require_once(LIBRARY_DIR.'/tdcron/class.tdcron.php');
284 require_once(LIBRARY_DIR.'/tdcron/class.tdcron.entry.php');
286 // Calculate the last occurrence of a lock.
287 $last = tdCron::getLastOccurrence($this->lock_spec, time());
288 $lockdate = new DateAndTime(DB_DATEFORMAT, strftime('%Y-%m-%d', $last));
289 if ($date->before($lockdate))
295 // canOverridePunchMode checks whether a user can override punch mode in a situation.
296 function canOverridePunchMode()
298 if (!$this->behalf_id && !$this->can('override_own_punch_mode'))
299 return false; // User is working as self and cannot override for self.
301 if ($this->behalf_id && !$this->can('override_punch_mode'))
302 return false; // User is working on behalf of someone else and cannot override.
307 // getUsers obtains users in a group, as specififed by options.
308 function getUsers($options) {
310 $mdb2 = getConnection();
312 $skipClients = !isset($options['include_clients']);
313 $includeSelf = isset($options['include_self']);
315 $select_part = 'select u.id, u.name';
316 if (isset($options['include_login'])) $select_part .= ', u.login';
317 if (!isset($options['include_clients'])) $select_part .= ', r.rights';
318 if (isset($options['include_role'])) $select_part .= ', r.name as role_name, r.rank';
320 $from_part = ' from tt_users u';
323 if (isset($options['max_rank']) || $skipClients || isset($options['include_role']))
324 $left_joins .= ' left join tt_roles r on (u.role_id = r.id)';
326 $where_part = " where u.group_id = $this->group_id";
327 if (isset($options['status']))
328 $where_part .= ' and u.status = '.(int)$options['status'];
330 $where_part .= ' and u.status is not null';
332 $where_part .= " and (u.id = $this->id || r.rank <= ".(int)$options['max_rank'].')';
334 if (isset($options['max_rank'])) $where_part .= ' and r.rank <= '.(int)$options['max_rank'];
337 $order_part = " order by upper(u.name)";
339 $sql = $select_part.$from_part.$left_joins.$where_part.$order_part;
340 $res = $mdb2->query($sql);
341 $user_list = array();
342 if (is_a($res, 'PEAR_Error'))
345 while ($val = $res->fetchRow()) {
347 $isClient = in_array('track_own_time', explode(',', $val['rights'])) ? 0 : 1; // Clients do not have track_own_time right.
349 continue; // Skip adding clients.
354 if (isset($options['self_first'])) {
355 // Put own entry at the front.
356 $cnt = count($user_list);
357 for($i = 0; $i < $cnt; $i++) {
358 if ($user_list[$i]['id'] == $this->id) {
359 $self = $user_list[$i]; // Found self.
360 array_unshift($user_list, $self); // Put own entry at the front.
361 array_splice($user_list, $i+1, 1); // Remove duplicate.
368 // getUser function is used to manage users in group and returns user details.
369 // At the moment, the function is used for user edits and deletes.
370 function getUser($user_id) {
371 if (!$this->can('manage_users')) return false;
373 $mdb2 = getConnection();
375 $sql = "select u.id, u.name, u.login, u.role_id, u.status, u.rate, u.email from tt_users u".
376 " left join tt_roles r on (u.role_id = r.id)".
377 " where u.id = $user_id and u.group_id = $this->group_id and u.status is not null".
378 " and (r.rank < $this->rank or (r.rank = $this->rank and u.id = $this->id))"; // Users with lesser roles or self.
379 $res = $mdb2->query($sql);
380 if (!is_a($res, 'PEAR_Error')) {
381 $val = $res->fetchRow();
387 // checkBehalfId checks whether behalf_id is appropriate.
388 // On behalf user must be active and have lower rank.
389 function checkBehalfId() {
390 $options = array('status'=>ACTIVE,'max_rank'=>$this->rank-1);
391 $users = $this->getUsers($options);
392 foreach($users as $one_user) {
393 if ($one_user['id'] == $this->behalf_id)
399 // adjustBehalfId attempts to adjust behalf_id and behalf_name to a first found
402 // Needed for situations when user does not have do_own_something right.
403 // Example: has view_charts but does not have view_own_charts.
404 // In this case we still allow access to charts, but set behalf_id to someone else.
405 function adjustBehalfId() {
406 $options = array('status'=>ACTIVE,'max_rank'=>$this->rank-1);
407 $users = $this->getUsers($options);
408 foreach($users as $one_user) {
409 // Fake loop to access first element.
410 $this->behalf_id = $one_user['id'];
411 $this->behalf_name = $one_user['name'];
412 $_SESSION['behalf_id'] = $this->behalf_id;
413 $_SESSION['behalf_name'] = $this->behalf_name;
419 // updateGroup updates group information with new data.
420 function updateGroup($fields) {
421 if (!($this->can('manage_basic_settings') ||
422 $this->can('manage_advanced_settings') ||
423 $this->can('manage_features'))) return false;
425 $mdb2 = getConnection();
427 if (isset($fields['name'])) $name_part = ', name = '.$mdb2->quote($fields['name']);
428 if (isset($fields['currency'])) $currency_part = ', currency = '.$mdb2->quote($fields['currency']);
429 if (isset($fields['lang'])) $lang_part = ', lang = '.$mdb2->quote($fields['lang']);
430 if (isset($fields['decimal_mark'])) $decimal_mark_part = ', decimal_mark = '.$mdb2->quote($fields['decimal_mark']);
431 if (isset($fields['date_format'])) $date_format_part = ', date_format = '.$mdb2->quote($fields['date_format']);
432 if (isset($fields['time_format'])) $time_format_part = ', time_format = '.$mdb2->quote($fields['time_format']);
433 if (isset($fields['week_start'])) $week_start_part = ', week_start = '.(int) $fields['week_start'];
434 if (isset($fields['tracking_mode'])) {
435 $tracking_mode_part = ', tracking_mode = '.(int) $fields['tracking_mode'];
436 $project_required_part = ' , project_required = '.(int) $fields['project_required'];
437 $task_required_part = ' , task_required = '.(int) $fields['task_required'];
439 if (isset($fields['record_type'])) $record_type_part = ', record_type = '.(int) $fields['record_type'];
440 if (isset($fields['bcc_email'])) $bcc_email_part = ', bcc_email = '.$mdb2->quote($fields['bcc_email']);
441 if (isset($fields['allow_ip'])) $allow_ip_part = ', allow_ip = '.$mdb2->quote($fields['allow_ip']);
442 if (isset($fields['plugins'])) $plugins_part = ', plugins = '.$mdb2->quote($fields['plugins']);
443 if (isset($fields['config'])) $config_part = ', config = '.$mdb2->quote($fields['config']);
444 if (isset($fields['lock_spec'])) $lock_spec_part = ', lock_spec = '.$mdb2->quote($fields['lock_spec']);
445 if (isset($fields['workday_minutes'])) $workday_minutes_part = ', workday_minutes = '.$mdb2->quote($fields['workday_minutes']);
446 $modified_part = ', modified = now(), modified_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', modified_by = '.$mdb2->quote($this->id);
448 $parts = trim($name_part.$currency_part.$lang_part.$decimal_mark_part.$date_format_part.
449 $time_format_part.$week_start_part.$tracking_mode_part.$task_required_part.$project_required_part.$record_type_part.
450 $bcc_email_part.$allow_ip_part.$plugins_part.$config_part.$lock_spec_part.$workday_minutes_part.$modified_part, ',');
452 $sql = "update tt_groups set $parts where id = $this->group_id";
453 $affected = $mdb2->exec($sql);
454 if (is_a($affected, 'PEAR_Error')) return false;
459 // enablePlugin either enables or disables a specific plugin for group.
460 function enablePlugin($plugin, $enable = true)
462 if (!$this->can('manage_advanced_settings'))
463 return false; // Note: enablePlugin is currently only used on week_view.php.
464 // So, it's not really a plugin we are enabling, but rather week view display options.
465 // Therefore, a check for manage_advanced_settings, not manage_features.
467 $plugin_array = explode(',', $this->plugins);
468 if ($enable && !in_array($plugin, $plugin_array))
469 $plugin_array[] = $plugin; // Add plugin to array.
471 if (!$enable && in_array($plugin, $plugin_array)) {
472 $key = array_search($plugin, $plugin_array);
474 unset($plugin_array[$key]); // Remove plugin from array.
477 $plugins = implode(',', $plugin_array);
478 if ($plugins != $this->plugins) {
479 if (!$this->updateGroup(array('plugins' => $plugins)))
481 $this->plugins = $plugins;